Module: Phlexing::Helpers
- Included in:
- Converter
- Defined in:
- lib/phlexing/helpers.rb
Constant Summary collapse
- KNOWN_ELEMENTS =
Phlex::HTML::VOID_ELEMENTS.values + Phlex::HTML::STANDARD_ELEMENTS.values
Instance Method Summary collapse
- #do_block_end(level = 0) ⇒ Object
- #do_block_start ⇒ Object
- #double_quote(string) ⇒ Object
- #element_node?(node) ⇒ Boolean
- #erb_comment?(node) ⇒ Boolean
- #erb_interpolation?(node) ⇒ Boolean
- #erb_node?(node) ⇒ Boolean
- #erb_safe_output?(node) ⇒ Boolean
- #indent(level) ⇒ Object
- #multi_line_block(level) ⇒ Object
- #multiple_children?(node) ⇒ Boolean
- #node_name(node) ⇒ Object
- #percent_literal_string(string) ⇒ Object
- #quote(string) ⇒ Object
- #siblings?(node) ⇒ Boolean
- #single_line_block ⇒ Object
- #single_quote(string) ⇒ Object
- #text_node?(node) ⇒ Boolean
- #whitespace(options) ⇒ Object
Instance Method Details
#do_block_end(level = 0) ⇒ Object
52 53 54 |
# File 'lib/phlexing/helpers.rb', line 52 def do_block_end(level = 0) "#{indent(level)}end\n" end |
#do_block_start ⇒ Object
48 49 50 |
# File 'lib/phlexing/helpers.rb', line 48 def do_block_start " do\n" end |
#double_quote(string) ⇒ Object
19 20 21 |
# File 'lib/phlexing/helpers.rb', line 19 def double_quote(string) "\"#{string}\"" end |
#element_node?(node) ⇒ Boolean
72 73 74 |
# File 'lib/phlexing/helpers.rb', line 72 def element_node?(node) node.is_a?(Nokogiri::XML::Element) end |
#erb_comment?(node) ⇒ Boolean
102 103 104 |
# File 'lib/phlexing/helpers.rb', line 102 def erb_comment?(node) node.attributes["comment"] end |
#erb_interpolation?(node) ⇒ Boolean
88 89 90 91 92 93 94 95 96 |
# File 'lib/phlexing/helpers.rb', line 88 def erb_interpolation?(node) first = node.children.first erb_node?(node) && node.children.one? && first.children.none? && text_node?(first) && node.attributes["interpolated"] end |
#erb_node?(node) ⇒ Boolean
68 69 70 |
# File 'lib/phlexing/helpers.rb', line 68 def erb_node?(node) node.is_a?(Nokogiri::XML::Element) && node.name == "erb" end |
#erb_safe_output?(node) ⇒ Boolean
98 99 100 |
# File 'lib/phlexing/helpers.rb', line 98 def erb_safe_output?(node) erb_interpolation?(node) && node.text.start_with?("=") end |
#indent(level) ⇒ Object
9 10 11 12 13 |
# File 'lib/phlexing/helpers.rb', line 9 def indent(level) return "" if level == 1 " " * level end |
#multi_line_block(level) ⇒ Object
56 57 58 59 60 |
# File 'lib/phlexing/helpers.rb', line 56 def multi_line_block(level) @buffer << " do\n" yield @buffer << ("#{indent(level)}end\n") end |
#multiple_children?(node) ⇒ Boolean
80 81 82 |
# File 'lib/phlexing/helpers.rb', line 80 def multiple_children?(node) node.children.length > 1 end |
#node_name(node) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/phlexing/helpers.rb', line 38 def node_name(node) return "template_tag" if node.name == "template" name = node.name.gsub("-", "_") @custom_elements << name unless KNOWN_ELEMENTS.include?(name) name end |
#percent_literal_string(string) ⇒ Object
27 28 29 |
# File 'lib/phlexing/helpers.rb', line 27 def percent_literal_string(string) "%(#{string})" end |
#quote(string) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/phlexing/helpers.rb', line 31 def quote(string) return double_quote(string) unless string.include?('"') return single_quote(string) unless string.include?("'") percent_literal_string(string) end |
#siblings?(node) ⇒ Boolean
84 85 86 |
# File 'lib/phlexing/helpers.rb', line 84 def siblings?(node) multiple_children?(node.parent) end |
#single_line_block ⇒ Object
62 63 64 65 66 |
# File 'lib/phlexing/helpers.rb', line 62 def single_line_block @buffer << " { " yield @buffer << " }\n" end |
#single_quote(string) ⇒ Object
23 24 25 |
# File 'lib/phlexing/helpers.rb', line 23 def single_quote(string) "'#{string}'" end |
#text_node?(node) ⇒ Boolean
76 77 78 |
# File 'lib/phlexing/helpers.rb', line 76 def text_node?(node) node.is_a?(Nokogiri::XML::Text) end |
#whitespace(options) ⇒ Object
15 16 17 |
# File 'lib/phlexing/helpers.rb', line 15 def whitespace() .fetch(:whitespace, true) ? "whitespace\n" : "" end |