Sha256: 5ab261dc56ee1f6360679e08455a37253274a9dc5d496381710bb67f563fb816

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

module MasterView

  # Helper services to support a preprocessing filter to run a template
  # document through HTML Tidy to automatically clean up the markup 
  # to ensure valid xhtml.  Optionally applied to all incoming templates
  # prior to MasterView processing if the MasterView <code>:tidy</code> parser 
  # option is enabled.
  # 
  # Unlike web browsers, which for historical and cultural reasons typically
  # accept all sorts of mangled and invalid html and attempt to always 
  # produce some form of rendering, MasterView's template parsing is 
  # based on XML parsing technology and requires a syntactically valid
  # xhtml document.  If you're not sure your templates are well-formed,
  # you can turn on the MasterView <code>:tidy</code> parser option
  # to have tidy automatically applied as a preprocessing filter
  # on your templates.  (Requires that you have tidy installed on your system;
  # use the MasterView <code>:tidy_path</code> option to specify how
  # MasterView should invoke tidy)
  # 
  module TidyHelper

    # Run HTML tidy on an html document and return the tidy'd output.
    # 
    def self.tidy(html)
      Tidy.path = ::MasterView::TidyPath unless Tidy.path
      xml = Tidy.open do |tidy|
        tidy.options.output_xml = true
        tidy.options.indent = true
        tidy.options.wrap = 0
        xml = tidy.clean(html)
      end
      xml = ::MasterView::EscapeErbHelper.escape_erb(xml)
      ::MasterView::Log.debug { 'tidy corrected xml='+xml }
      xml
    end


  end

  # Helper service to support a preprocessing filter to escape Erb markup
  # into inline-erb notation within a template document.
  # Optionally applied to all incoming templates prior to MasterView processing
  # if the MasterView <code>:escape_erb</code> parser option is enabled.
  #
  module EscapeErbHelper
    def self.escape_erb(html)
      html = html.gsub(/<%/, InlineErbStart)
      html.gsub!(/%>/, InlineErbEnd)
      html
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
masterview-0.3.2 lib/masterview/filter_helpers.rb
masterview-0.3.0 lib/masterview/filter_helpers.rb
masterview-0.3.1 lib/masterview/filter_helpers.rb
masterview-0.3.3 lib/masterview/filter_helpers.rb
masterview-0.3.4 lib/masterview/filter_helpers.rb