Class: Fabulator::Lib::Template

Inherits:
Structural
  • Object
show all
Defined in:
lib/fabulator/lib/function.rb

Instance Method Summary collapse

Methods inherited from Structural

#accepts_structural?, accepts_structural?, contained_in, contains, element, #initialize, structurals

Constructor Details

This class inherits a constructor from Fabulator::Structural

Instance Method Details

#compile_xml(xml, context) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fabulator/lib/function.rb', line 40

def compile_xml(xml, context)
  super

  @actions = [ ]
  @wrapper = [ ]

  if !xml.nil?
    ctx = nil
    if xml.name == 'template' && xml.namespaces.namespace.href == FAB_LIB_NS
      ctx = @context.merge(xml)
      @wrapper = [ '', '' ]
    else
      ctx = @context.merge
      # we need to set @wrapper to [ begin, end ]
      s = ""
      if (xml.namespaces.namespace.prefix rescue nil)
        s += xml.namespaces.namespace.prefix + ":"
      end
      s += xml.name
      e = "</" + s + ">"
      s = "<" + s
      xml.each_attr do |attr|
        s += " "
        if attr.ns?
          s += attr.ns.prefix + ":"
        end
        s += attr.name + "="
        if attr.value =~ /"/
          s += "'" + attr.value.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/'/, '&quot;') + "'"
        else
          s += '"' + attr.value.gsub(/&/, '&amp;').gsub(/</, '&lt;') + '"'
        end
      end
      s += ">"
      @wrapper = [ s, e ]
    end
    xml.each_child do |node|
      if node.element?
        if ctx.action_exists?((node.namespaces.namespace.href rescue nil), node.name)
          @actions << ctx.compile_action(node)
        else 
          a = self.class.new
          a.compile_xml(node, ctx)
          @actions << a
        end
      elsif node.text? || node.cdata?
        @actions << node.content
      end
    end
  end

  self
end

#run(context, autovivify = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fabulator/lib/function.rb', line 94

def run(context, autovivify = false)
  s = ''
  @context.with(context) do |ctx|
    @actions.each do |action|
      if action.is_a?(String)
        s += action
      else
        r = action.run(ctx, autovivify)
        s += r.collect { |v| v.to([FAB_NS, 'string'], ctx).value }.join('')
      end
    end
  end
  s.gsub!(/^\s+/, '')
  s.gsub!(/\s+$/, '')
# we want to see if we need to remove a whitespace prefix
  indent = s.
           split(/[\x0a\0x0d]/).
           map{|l| (v=l[/^([\s]+)/].to_s.length; v==0)? nil : v }.
           compact.min
  s.gsub!(/^#{' '*indent.to_i}/, '')
  s = @wrapper.first + s + @wrapper.last
  return [ context.root.anon_node(s, [ FAB_NS, 'string' ]) ]
end