Class: Fabulator::Template::ParseResult

Inherits:
Object
  • Object
show all
Defined in:
lib/fabulator/template/parse_result.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ ParseResult

Returns a new instance of ParseResult.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/fabulator/template/parse_result.rb', line 6

def initialize(text)
  ## we want to build up the XPath expression for structural and
  ## interactive elements for use in adding default info and
  ## building names for those elements -- saves the XSLT from having
  ## to do this

  structurals = { }
  interactives = { }

  Fabulator::TagLib.namespaces.each_pair do |ns, ob|
    x = ob.presentation.structurals || []
    structurals[ns] = x unless x.nil? || x.empty?
    x = ob.presentation.interactives || []
    interactives[ns] = x unless x.nil? || x.empty?
  end

  @namespaces = { }
  i = 1
  (structurals.keys + interactives.keys).uniq.sort.each do |ns|
    @namespaces["fab_ns_#{i.to_s}"] = ns
    i += 1
  end

  structural_xpaths = []
  interactive_xpaths = []
  interesting_xpaths = [ ]
  @fab_prefix = ''

  @namespaces.keys.each do |p|
    @fab_prefix = p if @namespaces[p] == Fabulator::FAB_NS
    structural_xpaths += structurals[@namespaces[p]].collect{ |e| "ancestor::#{p}:#{e}" }
    interactive_xpaths += interactives[@namespaces[p]].collect{ |e| "//#{p}:#{e}" }
    interesting_xpaths += structurals[@namespaces[p]].collect{ |e| "//#{p}:#{e}" }
  end

  @structural_xpath = structural_xpaths.join("[@id != ''] | ") + "[@id != '']"
  @interactive_xpath = interactive_xpaths.join(" | ")

  @interesting_xpath = (interactive_xpaths + interesting_xpaths).join(" | ")

  @namespaces = @namespaces.collect{ |k,v| "#{k}:#{v}" }

  ## We also may do our dependency tree -- namespaces in the
  ## root element of the stylesheet will be run after the current
  ## stylesheet

  @doc = LibXML::XML::Document.string text

  @fab_ns = nil
  @doc.root.namespaces.each do |ns|
    if ns.href == Fabulator::FAB_NS
      @fab_ns = ns
    end
  end

  if @fab_ns.nil?
    @fab_ns = XML::Namespace.new(@doc.root, @fab_prefix, Fabulator::FAB_NS)
  end
end

Instance Method Details

#add_captions(captions = { }) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fabulator/template/parse_result.rb', line 116

def add_captions(captions = { })
  each_element do |el|
    id = el_id(el)
    next if id == ''
    caption = nil
    if captions.is_a?(Hash)
      caption = captions[id]
    else
      caption = captions.traverse_path(id.split('.')).first.to_s
    end

    next if caption.nil?

    is_grid = false
    if el.name == 'grid'
    else
      cap = el.find_first("./#{@fab_prefix}:caption", @namespaces)
      if cap.nil?
        el << text_node('caption', caption)
      else
        cap.content = caption
        cap.parent << text_node('caption', caption)
        cap.remove!
      end
    end
  end
end

#add_default_values(context) ⇒ Object

This function walks through all of the elements in the provided markup and adds f:default child elements. TagLibs declare data elements that should receive default values.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fabulator/template/parse_result.rb', line 69

def add_default_values(context)
  return if context.nil?
  each_form_element do |el|
    own_id = el.attributes['id']
    next if own_id.nil? || own_id.to_s == ''

    default = nil
    default = el.find("./#{@fab_prefix}:default", @namespaces).to_a

    id = el_id(el)
    ids = id.split('/')
    l = context.traverse_path(ids)
    if !l.nil? && !l.empty?
      if !default.nil? && !default.empty?
        default.each { |d| d.remove! }
      end
      l.collect{|ll| ll.value}.each do |v|
        el << text_node('default', v)
      end
    end
  end
end

#add_errors(errors = { }) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fabulator/template/parse_result.rb', line 101

def add_errors(errors = { })
  each_form_element do |el|
    id = el_id(el)
    next if id == ''
    next unless errors.has_key?(id)
    if errors[id].is_a?(Array)
      errors[id].each do |e|
        el << text_node('error', e) 
      end
    else
      el << text_node('error', errors[id])
    end
  end
end

#add_missing_values(missing = [ ]) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/fabulator/template/parse_result.rb', line 92

def add_missing_values(missing = [ ])
  each_form_element do |el|
    id = el_id(el)
    next if id == ''
    next unless missing.include?(id)
    el.attributes["missing"] = "1"
  end
end

#to_html(popts = { }) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/fabulator/template/parse_result.rb', line 148

def to_html(popts = { })
  opts = { :form => true, :theme => 'coal' }.update(popts)

  deps = { }
  Fabulator::TagLib.namespaces.each_pair do |ns, ob|
    deps[ns] = ob.presentation.get_root_namespaces(:html) & (Fabulator::TagLib.namespaces.keys) - [ ns ]
  end

  ordered_ns = [ ]

  next_round = ([ Fabulator::FAB_NS ] + deps.keys.select { |k| deps[k].empty? }).uniq

  while !next_round.empty? do
    next_round.each { |k| deps.delete(k) }

    ordered_ns += next_round

    deps.keys.each do |k|
      deps[k] -= ordered_ns
    end

    next_round = deps.keys.select{ |k| deps[k].empty? }
  end

  ordered_ns.reverse!

  res = @doc
  ordered_ns.each do |ns|
    ob = Fabulator::TagLib.namespaces[ns]
    next if ob.nil?
    res = ob.presentation.transform(:html, res, opts)
  end

  ret = ''
  if opts[:form]
    ret = res.to_s.gsub(/^\s*<\?xml\s+.*?\?>\s*/, '').gsub(/xmlns(:\S+)?=['"][^'"]*['"]/, '').gsub(/\s+/, ' ').gsub(/\s+>/, '>')
  else
    ret = res.find('//form/*').collect{ |e| e.to_s}.join('').gsub(/^\s*<\?xml\s+.*?\?>\s*/, '').gsub(/xmlns(:\S+)?=['"][^'"]*['"]/, '').gsub(/\s+/, ' ').gsub(/\s+>/, '>')

  end

  ret.gsub!(/<(div\s*[^<]*?)\/>/, "<\\1></div>")
  ret.gsub!(/<(span\s*[^<]*?)\/>/, "<\\1></span>")
  ret
end

#to_sObject



144
145
146
# File 'lib/fabulator/template/parse_result.rb', line 144

def to_s
  @doc.to_s.gsub(/^\s*<\?xml\s+.*?\?>\s*/, '')
end