Sha256: 15bb699ed9d014689a468a714a2684bdcaded3d22a619f04f44bda24aa126afd

Contents?: true

Size: 1.25 KB

Versions: 44

Compression:

Stored size: 1.25 KB

Contents

module Dryml

  module Parser

    class Document < REXML::Document

      def initialize(source, path)
        super(nil)

        # Replace <%...%> scriptlets with xml-safe references into a hash of scriptlets
        @scriptlets = {}
        source = source.gsub(/<%(.*?)%>/m) do
          _, scriptlet = *Regexp.last_match
          id = @scriptlets.size + 1
          @scriptlets[id] = scriptlet
          newlines = "\n" * scriptlet.count("\n")
          "[![DRYML-ERB#{id}#{newlines}]!]"
        end


        @reference_src = "<dryml_page>" + source + "</dryml_page>"
        rex_src = Dryml::Parser::Source.new(@reference_src)

        @elements = Dryml::Parser::Elements.new(self)
        build(rex_src)

      rescue REXML::ParseException => e
        raise Dryml::DrymlSyntaxError, "File: #{path}\n#{e}"
      end


      def element_line_num(el)
        offset = el.source_offset
        @reference_src[0..offset].count("\n") + 1
      end


      def default_attribute_value
        "&true"
      end


      def restore_erb_scriptlets(src)
        src.gsub(/\[!\[DRYML-ERB(\d+)\s*\]!\]/m) {|s| "<%#{@scriptlets[$1.to_i]}%>" }
      end


      private
      def build( source )
        Dryml::Parser::TreeParser.new( source, self ).parse
      end

    end

  end

end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
dryml-1.3.0.pre13 lib/dryml/parser/document.rb
dryml-1.3.0.pre12 lib/dryml/parser/document.rb
dryml-1.3.0.pre11 lib/dryml/parser/document.rb
dryml-1.3.0.pre10 lib/dryml/parser/document.rb