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
SVG_ELEMENTS =
Phlex::SVG::StandardElements.registered_elements.values.to_h { |element| [element.downcase, element] }

Instance Method Summary collapse

Instance Method Details

#arg(string) ⇒ Object



26
27
28
# File 'lib/phlexing/helpers.rb', line 26

def arg(string)
  "#{string}: "
end

#block(params = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/phlexing/helpers.rb', line 75

def block(params = nil)
  out << " {"

  if params
    out << " "
    out << "|"
    out << params
    out << "|"
  end

  yield
  out << " }"
end

#blocklistObject



96
97
98
99
100
# File 'lib/phlexing/helpers.rb', line 96

def blocklist
  [
    "render"
  ]
end

#braces(string) ⇒ Object



38
39
40
# File 'lib/phlexing/helpers.rb', line 38

def braces(string)
  "{ #{string} }"
end

#children?(node) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/phlexing/helpers.rb', line 132

def children?(node)
  node.children.length >= 1
end

#interpolate(string) ⇒ Object



42
43
44
# File 'lib/phlexing/helpers.rb', line 42

def interpolate(string)
  "\#\{#{string}\}"
end

#known_rails_helpersObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/phlexing/helpers.rb', line 109

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

Returns:

  • (Boolean)


136
137
138
# File 'lib/phlexing/helpers.rb', line 136

def multiple_children?(node)
  node.children.length > 1
end

#newlineObject



18
19
20
# File 'lib/phlexing/helpers.rb', line 18

def newline
  "\n"
end

#output(name, string) ⇒ Object



89
90
91
92
93
94
# File 'lib/phlexing/helpers.rb', line 89

def output(name, string)
  out << name
  out << " "
  out << string.strip
  out << newline
end

#parens(string) ⇒ Object



34
35
36
# File 'lib/phlexing/helpers.rb', line 34

def parens(string)
  "(#{string})"
end

#quote(string) ⇒ Object



30
31
32
# File 'lib/phlexing/helpers.rb', line 30

def quote(string)
  "%(#{string})"
end

#routes_helpersObject



102
103
104
105
106
107
# File 'lib/phlexing/helpers.rb', line 102

def routes_helpers
  [
    /\w+_url/,
    /\w+_path/
  ]
end

#siblings?(node) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/phlexing/helpers.rb', line 140

def siblings?(node)
  multiple_children?(node.parent)
end

#string_output?(node) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
# File 'lib/phlexing/helpers.rb', line 121

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



22
23
24
# File 'lib/phlexing/helpers.rb', line 22

def symbol(string)
  ":#{string}"
end

#tag_name(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/phlexing/helpers.rb', line 63

def tag_name(node)
  name = node.name.tr("-", "_")

  return name if name == "template_tag"
  return name if name.start_with?("s.")
  return name if KNOWN_ELEMENTS.include?(name)

  @converter.custom_elements << name

  name
end

#unescape(source) ⇒ Object



46
47
48
# File 'lib/phlexing/helpers.rb', line 46

def unescape(source)
  CGI.unescapeHTML(source)
end

#unwrap_erb(source) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/phlexing/helpers.rb', line 50

def unwrap_erb(source)
  source
    .delete_prefix("<%==")
    .delete_prefix("<%=")
    .delete_prefix("<%-")
    .delete_prefix("<%#")
    .delete_prefix("<% #")
    .delete_prefix("<%")
    .delete_suffix("-%>")
    .delete_suffix("%>")
    .strip
end

#whitespaceObject



14
15
16
# File 'lib/phlexing/helpers.rb', line 14

def whitespace
  options.whitespace? ? "whitespace\n" : ""
end