Class: Hpreserve::Parser
- Inherits:
-
Object
- Object
- Hpreserve::Parser
- Defined in:
- lib/hpreserve/parser.rb
Instance Attribute Summary collapse
-
#cacher ⇒ Object
Returns the value of attribute cacher.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#filter_sandbox ⇒ Object
Returns the value of attribute filter_sandbox.
-
#include_base ⇒ Object
Returns the value of attribute include_base.
-
#variables ⇒ Object
Returns the value of attribute variables.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(doc = '') ⇒ Parser
constructor
A new instance of Parser.
- #render(vars = nil) ⇒ Object
- #render_collection(node, values = []) ⇒ Object
- #render_includes ⇒ Object
- #render_node_content(node) ⇒ Object
- #render_node_filters(node) ⇒ Object
- #render_nodes(base = doc) ⇒ Object
Constructor Details
Instance Attribute Details
#cacher ⇒ Object
Returns the value of attribute cacher.
9 10 11 |
# File 'lib/hpreserve/parser.rb', line 9 def cacher @cacher end |
#doc ⇒ Object
Returns the value of attribute doc.
9 10 11 |
# File 'lib/hpreserve/parser.rb', line 9 def doc @doc end |
#filter_sandbox ⇒ Object
Returns the value of attribute filter_sandbox.
9 10 11 |
# File 'lib/hpreserve/parser.rb', line 9 def filter_sandbox @filter_sandbox end |
#include_base ⇒ Object
Returns the value of attribute include_base.
9 10 11 |
# File 'lib/hpreserve/parser.rb', line 9 def include_base @include_base end |
#variables ⇒ Object
Returns the value of attribute variables.
9 10 11 |
# File 'lib/hpreserve/parser.rb', line 9 def variables @variables end |
Class Method Details
.render(doc = '', variables = {}) ⇒ Object
4 5 6 7 |
# File 'lib/hpreserve/parser.rb', line 4 def self.render(doc='', variables={}) doc = new(doc) doc.render(variables) end |
Instance Method Details
#render(vars = nil) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/hpreserve/parser.rb', line 20 def render(vars=nil) self.variables = vars unless vars.nil? render_includes render_nodes doc.to_s end |
#render_collection(node, values = []) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hpreserve/parser.rb', line 71 def render_collection(node, values=[]) variable_name = node.remove_attribute('local') || 'item' base = node.children.detect {|n| !n.is_a?(Hpricot::Text) } base.following.remove base.preceding.remove template = base.to_s values.each_with_index do |value, index| variables.storage[variable_name] = value ele = (Hpricot(template)/'*').first node.insert_after(ele, node.children.last) render_nodes(ele) end base.swap('') end |
#render_includes ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/hpreserve/parser.rb', line 27 def render_includes (doc/"[@include]").each do |node| var, default = node['include'].split('|').collect {|s| s.strip } incl = variables.substitute(var) incl = [include_base, incl.split('.')].flatten.compact node.inner_html = variables[incl] || variables[default] node.remove_attribute('include') end end |
#render_node_content(node) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/hpreserve/parser.rb', line 43 def render_node_content(node) variable = node.remove_attribute('content').strip cache = cacher.nil? ? nil : cacher.match?(variable) if cache and stored = cacher.retrieve(cache) node.swap(stored) else value = variables[variable.split('.')] value = value['default'] if value.respond_to?(:has_key?) if value.is_a?(Array) render_collection(node, value) else node.inner_html = value end render_node_filters(node) if node['filter'] cacher.store(cache, node.to_s) if cache end end |
#render_node_filters(node) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/hpreserve/parser.rb', line 61 def render_node_filters(node) filters = Hpreserve::Filters.parse(node.remove_attribute('filter')) filters.each do |filterset| filter = filterset.shift next unless filter_sandbox.respond_to?(filter) args = filterset.collect {|a| variables.substitute(a) } filter_sandbox.run(filter, node, *args) end end |
#render_nodes(base = doc) ⇒ Object
37 38 39 40 41 |
# File 'lib/hpreserve/parser.rb', line 37 def render_nodes(base=doc) (base/'meta[@content]').each {|node| node.set_attribute('content', variables.substitute(node['content']))} (base/'[@content]:not(meta)').each {|node| render_node_content(node) } (base/'[@filter]').each {|node| render_node_filters(node) } end |