Sha256: d98a11ba8e3b19e10e0e4d42ea3f6f65d84970dc654401eb256095690c436b5a

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'nokogiri'
require 'erubis'

module Deface
  class Parser
    # converts erb to markup
    #
    def self.erb_markup!(source)
      replacements = [ {"<%=" => "<code erb-loud>"},
                       {"<%"  => "<code erb-silent>"},
                       {"%>"  => "</code>"} ]

      replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } }
      source
    end

    # undoes ERB markup generated by Deface::Parser::ERB
    #
    def self.undo_erb_markup!(source)
      replacements = [ {"<code erb-silent>" => '<%'},
                       {"<code erb-loud>"   => '<%='},
                       {"</code>"           => '%>'},
                       {/(<|&lt;)code erb-silent(&gt;|>)/ => '<%'},
                       {/(<|&lt;)code erb-loud(&gt;|>)/   => '<%='},
                       {/(<|&lt;)\/code(&gt;|>)/          => '%>'},
                       {/(?!=<%=?)&gt;(?=.*%>)/           => '>'},
                       {/(?!=<%=?)&lt;(?=.*%>)/           => '<'}]

      replacements.each{ |h| h.each { |replace, with| source.gsub! replace, with } }
      source
    end


    def self.convert_fragment(source)
      erb_markup!(source)
      Nokogiri::HTML::DocumentFragment.parse(source)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
deface-0.2.0 lib/deface/parser.rb