Class: Dryml::Parser::Document

Inherits:
REXML::Document
  • Object
show all
Defined in:
lib/dryml/parser/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, path) ⇒ Document

Returns a new instance of Document.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dryml/parser/document.rb', line 7

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 path=\"#{ERB::Util.html_escape path}\">" + 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

Instance Method Details

#default_attribute_valueObject



38
39
40
# File 'lib/dryml/parser/document.rb', line 38

def default_attribute_value
  "&true"
end

#element_line_num(el) ⇒ Object



32
33
34
35
# File 'lib/dryml/parser/document.rb', line 32

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

#restore_erb_scriptlets(src) ⇒ Object



43
44
45
# File 'lib/dryml/parser/document.rb', line 43

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