phlex 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of phlex might be problematic. Click here for more details.

Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +5 -0
  3. data/.rubocop.yml +6 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/Rakefile +2 -2
  6. data/bench.rb +1 -1
  7. data/docs/assets/application.css +2 -0
  8. data/docs/build.rb +4 -4
  9. data/docs/components/callout.rb +5 -5
  10. data/docs/components/code_block.rb +18 -18
  11. data/docs/components/example.rb +23 -23
  12. data/docs/components/heading.rb +5 -5
  13. data/docs/components/layout.rb +42 -41
  14. data/docs/components/markdown.rb +19 -19
  15. data/docs/components/tabs/tab.rb +18 -18
  16. data/docs/components/tabs.rb +21 -21
  17. data/docs/components/title.rb +5 -5
  18. data/docs/page_builder.rb +32 -32
  19. data/docs/pages/application_page.rb +3 -3
  20. data/docs/pages/index.rb +23 -25
  21. data/docs/pages/rails_integration.rb +58 -0
  22. data/docs/pages/templates.rb +238 -238
  23. data/docs/pages/views.rb +175 -0
  24. data/fixtures/compilation/vcall.rb +38 -0
  25. data/fixtures/dummy/app/views/articles/form.rb +9 -9
  26. data/fixtures/dummy/app/views/card.rb +8 -8
  27. data/fixtures/dummy/app/views/heading.rb +5 -5
  28. data/fixtures/dummy/config/routes.rb +1 -1
  29. data/fixtures/dummy/db/schema.rb +2 -2
  30. data/fixtures/layout.rb +24 -24
  31. data/fixtures/page.rb +34 -34
  32. data/fixtures/test_helper.rb +2 -2
  33. data/fixtures/view_helper.rb +16 -0
  34. data/lib/generators/phlex/component/USAGE +1 -1
  35. data/lib/generators/phlex/component/component_generator.rb +8 -8
  36. data/lib/generators/phlex/component/templates/{component.rb.erb → view.rb.erb} +1 -1
  37. data/lib/install/phlex.rb +18 -0
  38. data/lib/overrides/symbol/name.rb +1 -1
  39. data/lib/phlex/block.rb +12 -12
  40. data/lib/phlex/buffered.rb +13 -13
  41. data/lib/phlex/compiler/formatter.rb +91 -0
  42. data/lib/phlex/compiler/generators/standard_element.rb +30 -0
  43. data/lib/phlex/compiler/generators/void_element.rb +29 -0
  44. data/lib/phlex/compiler/optimizers/base_optimizer.rb +34 -0
  45. data/lib/phlex/compiler/optimizers/vcall.rb +29 -0
  46. data/lib/phlex/compiler/visitors/base_visitor.rb +19 -0
  47. data/lib/phlex/compiler/visitors/component.rb +28 -0
  48. data/lib/phlex/compiler/visitors/component_method.rb +28 -0
  49. data/lib/phlex/compiler/visitors/file.rb +17 -0
  50. data/lib/phlex/compiler.rb +50 -0
  51. data/lib/phlex/configuration.rb +3 -3
  52. data/lib/phlex/engine.rb +11 -0
  53. data/lib/phlex/html.rb +128 -17
  54. data/lib/phlex/rails/tag_helpers.rb +23 -23
  55. data/lib/phlex/renderable.rb +32 -32
  56. data/lib/phlex/version.rb +1 -1
  57. data/lib/phlex/view.rb +223 -0
  58. data/lib/phlex.rb +27 -12
  59. data/lib/tasks/phlex_tasks.rake +10 -0
  60. metadata +39 -10
  61. data/CHANGELOG.md +0 -5
  62. data/docs/pages/components.rb +0 -175
  63. data/fixtures/component_helper.rb +0 -16
  64. data/lib/phlex/component.rb +0 -196
  65. data/lib/phlex/rails.rb +0 -8
@@ -1,196 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0")
4
- using Overrides::Symbol::Name
5
- end
6
-
7
- module Phlex
8
- class Component
9
- extend HTML
10
- include Renderable
11
-
12
- class << self
13
- attr_accessor :rendered_at_least_once
14
- end
15
-
16
- def call(buffer = +"", view_context: nil, parent: nil, &block)
17
- raise "The same component instance shouldn't be rendered twice" if rendered?
18
-
19
- @_rendered = true
20
- @_target = buffer
21
- @_view_context = view_context
22
- @_parent = parent
23
- @output_buffer = self
24
-
25
- template(&block)
26
-
27
- self.class.rendered_at_least_once ||= true
28
-
29
- buffer
30
- end
31
-
32
- def rendered?
33
- @_rendered ||= false
34
- end
35
-
36
- HTML::STANDARD_ELEMENTS.each do |element|
37
- register_element(element)
38
- end
39
-
40
- HTML::VOID_ELEMENTS.each do |element|
41
- register_void_element(element)
42
- end
43
-
44
- register_element :template_tag, tag: "template"
45
-
46
- def content(&block)
47
- return unless block_given?
48
-
49
- original_length = @_target.length
50
- output = yield(self)
51
- unchanged = (original_length == @_target.length)
52
-
53
- text(output) if unchanged
54
- nil
55
- end
56
-
57
- def text(content)
58
- @_target << case content
59
- when String then CGI.escape_html(content)
60
- when Symbol then CGI.escape_html(content.name)
61
- else CGI.escape_html(content.to_s)
62
- end
63
-
64
- nil
65
- end
66
-
67
- def whitespace
68
- @_target << " "
69
- nil
70
- end
71
-
72
- def doctype
73
- @_target << HTML::DOCTYPE
74
- nil
75
- end
76
-
77
- def raw(content)
78
- @_target << content
79
- nil
80
- end
81
-
82
- def html_safe?
83
- true
84
- end
85
-
86
- def safe_append=(value)
87
- return unless value
88
-
89
- @_target << case value
90
- when String then value
91
- when Symbol then value.name
92
- else value.to_s
93
- end
94
- end
95
-
96
- def append=(value)
97
- return unless value
98
-
99
- if value.html_safe?
100
- self.safe_append = value
101
- else
102
- @_target << case value
103
- when String then CGI.escape_html(value)
104
- when Symbol then CGI.escape_html(value.name)
105
- else CGI.escape_html(value.to_s)
106
- end
107
- end
108
- end
109
-
110
- def capture(&block)
111
- return unless block_given?
112
-
113
- original_buffer = @_target
114
- new_buffer = +""
115
- @_target = new_buffer
116
-
117
- yield
118
-
119
- @_target = original_buffer
120
- new_buffer.html_safe
121
- end
122
-
123
- def classes(*tokens, **conditional_tokens)
124
- { class: self.tokens(*tokens, **conditional_tokens) }
125
- end
126
-
127
- def tokens(*tokens, **conditional_tokens)
128
- conditional_tokens.each do |condition, token|
129
- case condition
130
- when Symbol then next unless send(condition)
131
- when Proc then next unless condition.call
132
- else raise ArgumentError,
133
- "The class condition must be a Symbol or a Proc."
134
- end
135
-
136
- case token
137
- when Symbol then tokens << token.name
138
- when String then tokens << token
139
- when Array then tokens.concat(t)
140
- else raise ArgumentError,
141
- "Conditional classes must be Symbols, Strings, or Arrays of Symbols or Strings."
142
- end
143
- end
144
-
145
- tokens.compact.join(" ")
146
- end
147
-
148
- def _attributes(attributes, buffer: +"")
149
- if attributes[:href]&.start_with?(/\s*javascript/)
150
- attributes[:href] = attributes[:href].sub(/^\s*(javascript:)+/, "")
151
- end
152
-
153
- _build_attributes(attributes, buffer: buffer)
154
-
155
- unless self.class.rendered_at_least_once
156
- Phlex::ATTRIBUTE_CACHE[attributes.hash] = buffer.freeze
157
- end
158
-
159
- buffer
160
- end
161
-
162
- def _build_attributes(attributes, buffer:)
163
- attributes.each do |k, v|
164
- next unless v
165
-
166
- name = case k
167
- when String
168
- k
169
- when Symbol
170
- k.name.tr("_", "-")
171
- else
172
- k.to_s
173
- end
174
-
175
- if HTML::EVENT_ATTRIBUTES[name] || name.match?(/[<>&"']/)
176
- raise ArgumentError, "Unsafe attribute name detected: #{k}."
177
- end
178
-
179
- case v
180
- when true
181
- buffer << " " << name
182
- when String
183
- buffer << " " << name << '="' << CGI.escape_html(v) << '"'
184
- when Symbol
185
- buffer << " " << name << '="' << CGI.escape_html(v.name) << '"'
186
- when Hash
187
- _build_attributes(v.transform_keys { "#{k}-#{_1.name.tr('_', '-')}" }, buffer: buffer)
188
- else
189
- buffer << " " << name << '="' << CGI.escape_html(v.to_s) << '"'
190
- end
191
- end
192
-
193
- buffer
194
- end
195
- end
196
- end
data/lib/phlex/rails.rb DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Phlex
4
- module Rails
5
- end
6
- end
7
-
8
- Phlex::Component.include(Phlex::Rails::TagHelpers)