Class: ViewInspect::Handlers::HTMLTemplate
- Inherits:
-
Object
- Object
- ViewInspect::Handlers::HTMLTemplate
- Defined in:
- lib/view_inspect/handlers/html_template.rb
Direct Known Subclasses
Constant Summary collapse
- STUB_PREFIX =
"__template_expression_stub__"
Class Method Summary collapse
Instance Method Summary collapse
- #add_file_line(source, filepath) ⇒ Object
- #add_file_line_to_html_tags(source, filepath) ⇒ Object
-
#initialize ⇒ HTMLTemplate
constructor
A new instance of HTMLTemplate.
- #replace_expression_with_stub(source) ⇒ Object
- #replace_stub_with_expression(source) ⇒ Object
Constructor Details
#initialize ⇒ HTMLTemplate
Returns a new instance of HTMLTemplate.
9 10 11 |
# File 'lib/view_inspect/handlers/html_template.rb', line 9 def initialize @expression_stub_map = {} end |
Class Method Details
.expression_regex ⇒ Object
13 14 15 |
# File 'lib/view_inspect/handlers/html_template.rb', line 13 def self.expression_regex raise "must be implemented by subclass" end |
Instance Method Details
#add_file_line(source, filepath) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/view_inspect/handlers/html_template.rb', line 27 def add_file_line(source, filepath) doc = ::Nokogiri::HTML.fragment(source) doc.traverse do |node| if node.is_a?(::Nokogiri::XML::Element) file_line = [filepath, node.line].join(":") node.set_attribute "data-orig-file-line", file_line end end CGI.unescapeHTML(doc.inner_html) end |
#add_file_line_to_html_tags(source, filepath) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/view_inspect/handlers/html_template.rb', line 17 def (source, filepath) return source if html_layout?(source) # currently dont support html layout templates source = replace_expression_with_stub(source) source = add_file_line(source, filepath) source = replace_stub_with_expression(source) source end |
#replace_expression_with_stub(source) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/view_inspect/handlers/html_template.rb', line 40 def replace_expression_with_stub(source) source.gsub(self.class.expression_regex).with_index do |match, index| stub = "#{STUB_PREFIX}#{index}" stub = preserve_linecount(stub, match) @expression_stub_map[stub] = match stub end end |
#replace_stub_with_expression(source) ⇒ Object
49 50 51 52 53 |
# File 'lib/view_inspect/handlers/html_template.rb', line 49 def replace_stub_with_expression(source) @expression_stub_map.inject(source) do |result, (stub, expression)| result = result.sub(stub, expression) end end |