Class: Dominate::Dom
Constant Summary collapse
- YIELD_REGEX =
/<!--(?:\s*|)@yield(?:\s*|)-->/- PARTIAL_REGEX =
/<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/- PARTIAL_REGEX_WITHIN =
/<!--(?:\s*|)@partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->(.*?)<!--(?:\s*|)\/partial(?:\s*|)([a-zA-Z0-9\-_]*)(?:\s*|)-->/m
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#options ⇒ Object
Returns the value of attribute options.
-
#raw_html ⇒ Object
Returns the value of attribute raw_html.
Instance Method Summary collapse
- #apply(data, &block) ⇒ Object
- #apply_instance ⇒ Object
- #html ⇒ Object
-
#initialize(raw_html, instance = false, config = {}) ⇒ Dom
constructor
A new instance of Dom.
- #load_html ⇒ Object
- #load_layout ⇒ Object
- #reset_html ⇒ Object
- #scope(name, &block) ⇒ Object
Constructor Details
#initialize(raw_html, instance = false, config = {}) ⇒ Dom
9 10 11 12 13 14 15 16 |
# File 'lib/dominate/dom.rb', line 9 def initialize raw_html, instance = false, config = {} @raw_html = raw_html @instance = instance @config = (Dominate.config.to_h.merge config).to_deep_ostruct set_doc raw_html load_html if Dominate.config.view_path end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
3 4 5 |
# File 'lib/dominate/dom.rb', line 3 def config @config end |
#doc ⇒ Object
Returns the value of attribute doc.
3 4 5 |
# File 'lib/dominate/dom.rb', line 3 def doc @doc end |
#instance ⇒ Object
Returns the value of attribute instance.
3 4 5 |
# File 'lib/dominate/dom.rb', line 3 def instance @instance end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/dominate/dom.rb', line 3 def @options end |
#raw_html ⇒ Object
Returns the value of attribute raw_html.
3 4 5 |
# File 'lib/dominate/dom.rb', line 3 def raw_html @raw_html end |
Instance Method Details
#apply(data, &block) ⇒ Object
64 65 66 67 68 |
# File 'lib/dominate/dom.rb', line 64 def apply data, &block @scope.apply data, &block self end |
#apply_instance ⇒ Object
70 71 72 73 |
# File 'lib/dominate/dom.rb', line 70 def apply_instance reset_html Scope.new(instance, config, doc).apply_instance end |
#html ⇒ Object
57 58 59 60 61 62 |
# File 'lib/dominate/dom.rb', line 57 def html @html ||= begin apply_instance if instance "#{doc}" end end |
#load_html ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dominate/dom.rb', line 18 def load_html load_layout if config.layout inner_html = doc.inner_html updated_html = inner_html.gsub(PARTIAL_REGEX_WITHIN) do |m| match = m.strip.match(PARTIAL_REGEX) partial = match[1] HTML.load_file "#{view_path}/#{partial}", config, instance end set_doc updated_html if updated_html doc.traverse do |e| if match = e.to_html.strip.match(PARTIAL_REGEX) partial = match[1] e.swap Nokogiri::HTML.fragment( HTML.load_file "#{view_path}/#{partial}", config, instance ) end end end |
#load_layout ⇒ Object
41 42 43 44 45 46 |
# File 'lib/dominate/dom.rb', line 41 def load_layout path = "#{config.view_path}/#{config.layout}" html = HTML.load_file path, config, instance inner_html = doc.inner_html set_doc html.gsub YIELD_REGEX, inner_html end |
#reset_html ⇒ Object
75 76 77 |
# File 'lib/dominate/dom.rb', line 75 def reset_html @html = false end |