Module: Phlexing::Helpers
- Included in:
- ComponentGenerator, TemplateGenerator, Visitor
- Defined in:
- lib/phlexing/helpers.rb
Constant Summary collapse
- KNOWN_ELEMENTS =
Phlex::HTML::VoidElements.registered_elements.values + Phlex::HTML::StandardElements.registered_elements.values
Instance Method Summary collapse
- #arg(string) ⇒ Object
- #block ⇒ Object
- #blocklist ⇒ Object
- #braces(string) ⇒ Object
- #children?(node) ⇒ Boolean
- #interpolate(string) ⇒ Object
- #known_rails_helpers ⇒ Object
- #multiple_children?(node) ⇒ Boolean
- #newline ⇒ Object
- #output(name, string) ⇒ Object
- #parens(string) ⇒ Object
- #quote(string) ⇒ Object
- #routes_helpers ⇒ Object
- #siblings?(node) ⇒ Boolean
- #string_output?(node) ⇒ Boolean
- #symbol(string) ⇒ Object
- #tag_name(node) ⇒ Object
- #unescape(source) ⇒ Object
- #unwrap_erb(source) ⇒ Object
- #whitespace ⇒ Object
Instance Method Details
#arg(string) ⇒ Object
24 25 26 |
# File 'lib/phlexing/helpers.rb', line 24 def arg(string) "#{string}: " end |
#block ⇒ Object
71 72 73 74 75 |
# File 'lib/phlexing/helpers.rb', line 71 def block out << " {" yield out << " }" end |
#blocklist ⇒ Object
84 85 86 87 88 |
# File 'lib/phlexing/helpers.rb', line 84 def blocklist [ "render" ] end |
#braces(string) ⇒ Object
36 37 38 |
# File 'lib/phlexing/helpers.rb', line 36 def braces(string) "{ #{string} }" end |
#children?(node) ⇒ Boolean
120 121 122 |
# File 'lib/phlexing/helpers.rb', line 120 def children?(node) node.children.length >= 1 end |
#interpolate(string) ⇒ Object
40 41 42 |
# File 'lib/phlexing/helpers.rb', line 40 def interpolate(string) "\#\{#{string}\}" end |
#known_rails_helpers ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/phlexing/helpers.rb', line 97 def known_rails_helpers Phlex::Rails::Helpers .constants .reject { |m| m == :Routes } .map { |m| Module.const_get("::Phlex::Rails::Helpers::#{m}") } .each_with_object({}) { |m, sum| (m.instance_methods - Module.instance_methods).each do |method| sum[method.to_s] = m.name end } end |
#multiple_children?(node) ⇒ Boolean
124 125 126 |
# File 'lib/phlexing/helpers.rb', line 124 def multiple_children?(node) node.children.length > 1 end |
#newline ⇒ Object
16 17 18 |
# File 'lib/phlexing/helpers.rb', line 16 def newline "\n" end |
#output(name, string) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/phlexing/helpers.rb', line 77 def output(name, string) out << name out << " " out << string.strip out << newline end |
#parens(string) ⇒ Object
32 33 34 |
# File 'lib/phlexing/helpers.rb', line 32 def parens(string) "(#{string})" end |
#quote(string) ⇒ Object
28 29 30 |
# File 'lib/phlexing/helpers.rb', line 28 def quote(string) "%(#{string})" end |
#routes_helpers ⇒ Object
90 91 92 93 94 95 |
# File 'lib/phlexing/helpers.rb', line 90 def routes_helpers [ /\w+_url/, /\w+_path/ ] end |
#siblings?(node) ⇒ Boolean
128 129 130 |
# File 'lib/phlexing/helpers.rb', line 128 def siblings?(node) multiple_children?(node.parent) end |
#string_output?(node) ⇒ Boolean
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/phlexing/helpers.rb', line 109 def string_output?(node) word = node.text.strip.scan(/^\w+/)[0] return true if word.nil? blocklist_matched = known_rails_helpers.keys.include?(word) || blocklist.include?(word) route_matched = routes_helpers.map { |regex| word.scan(regex).any? }.reduce(:|) !(blocklist_matched || route_matched) end |
#symbol(string) ⇒ Object
20 21 22 |
# File 'lib/phlexing/helpers.rb', line 20 def symbol(string) ":#{string}" end |
#tag_name(node) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/phlexing/helpers.rb', line 61 def tag_name(node) return "template_tag" if node.name == "template-tag" name = node.name.tr("-", "_") @converter.custom_elements << name unless KNOWN_ELEMENTS.include?(name) name end |
#unescape(source) ⇒ Object
44 45 46 |
# File 'lib/phlexing/helpers.rb', line 44 def unescape(source) CGI.unescapeHTML(source) end |
#unwrap_erb(source) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/phlexing/helpers.rb', line 48 def unwrap_erb(source) source .delete_prefix("<%==") .delete_prefix("<%=") .delete_prefix("<%-") .delete_prefix("<%#") .delete_prefix("<% #") .delete_prefix("<%") .delete_suffix("-%>") .delete_suffix("%>") .strip end |
#whitespace ⇒ Object
12 13 14 |
# File 'lib/phlexing/helpers.rb', line 12 def whitespace .whitespace? ? "whitespace\n" : "" end |