pakyow-presenter 0.11.3 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/{pakyow-presenter/CHANGELOG.md → CHANGELOG.md} +0 -5
- data/LICENSE +4 -0
- data/{pakyow-presenter/README.md → README.md} +1 -2
- data/lib/pakyow/generators/presenter/presenter_generator.rb +28 -0
- data/lib/pakyow/generators/presenter/templates/presenter.rb.tt +2 -0
- data/lib/pakyow/plugin/helpers/rendering.rb +19 -0
- data/lib/pakyow/presenter/actions/auto_render.rb +15 -0
- data/lib/pakyow/presenter/attributes/attribute.rb +22 -0
- data/lib/pakyow/presenter/attributes/boolean.rb +24 -0
- data/lib/pakyow/presenter/attributes/hash.rb +89 -0
- data/lib/pakyow/presenter/attributes/set.rb +61 -0
- data/lib/pakyow/presenter/attributes/string.rb +38 -0
- data/lib/pakyow/presenter/attributes.rb +126 -0
- data/lib/pakyow/presenter/behavior/config.rb +29 -0
- data/lib/pakyow/presenter/behavior/error_rendering.rb +124 -0
- data/lib/pakyow/presenter/behavior/exposures.rb +29 -0
- data/lib/pakyow/presenter/behavior/implicit_rendering.rb +21 -0
- data/lib/pakyow/presenter/behavior/initializing.rb +37 -0
- data/lib/pakyow/presenter/behavior/modes.rb +117 -0
- data/lib/pakyow/presenter/behavior/versions.rb +71 -0
- data/lib/pakyow/presenter/behavior/watching.rb +21 -0
- data/lib/pakyow/presenter/binder.rb +115 -0
- data/lib/pakyow/presenter/binding_parts.rb +64 -0
- data/lib/pakyow/presenter/component.rb +74 -0
- data/lib/pakyow/presenter/composers/component.rb +78 -0
- data/lib/pakyow/presenter/composers/view.rb +73 -0
- data/lib/pakyow/presenter/errors.rb +63 -0
- data/lib/pakyow/presenter/framework.rb +203 -0
- data/lib/pakyow/presenter/front_matter_parser.rb +42 -0
- data/lib/pakyow/presenter/helpers/rendering.rb +18 -0
- data/lib/pakyow/presenter/presentable.rb +28 -0
- data/lib/pakyow/presenter/presentable_error.rb +19 -0
- data/lib/pakyow/presenter/presenter.rb +766 -0
- data/lib/pakyow/presenter/presenters/endpoint.rb +158 -0
- data/lib/pakyow/presenter/presenters/form.rb +522 -0
- data/lib/pakyow/presenter/processor.rb +61 -0
- data/lib/pakyow/presenter/renderable.rb +28 -0
- data/lib/pakyow/presenter/renderer.rb +269 -0
- data/lib/pakyow/presenter/rendering/actions/cleanup_prototype_nodes.rb +21 -0
- data/lib/pakyow/presenter/rendering/actions/cleanup_unbound_bindings.rb +35 -0
- data/lib/pakyow/presenter/rendering/actions/create_template_nodes.rb +27 -0
- data/lib/pakyow/presenter/rendering/actions/insert_prototype_bar.rb +101 -0
- data/lib/pakyow/presenter/rendering/actions/install_authenticity.rb +42 -0
- data/lib/pakyow/presenter/rendering/actions/place_in_mode.rb +56 -0
- data/lib/pakyow/presenter/rendering/actions/render_components.rb +279 -0
- data/lib/pakyow/presenter/rendering/actions/set_page_title.rb +35 -0
- data/lib/pakyow/presenter/rendering/actions/setup_endpoints.rb +62 -0
- data/lib/pakyow/presenter/rendering/actions/setup_forms.rb +174 -0
- data/lib/pakyow/presenter/significant_nodes.rb +309 -0
- data/lib/pakyow/presenter/templates.rb +229 -0
- data/lib/pakyow/presenter/versioned_view.rb +209 -0
- data/lib/pakyow/presenter/view.rb +586 -0
- data/lib/pakyow/presenter/views/form.rb +58 -0
- data/lib/pakyow/presenter/views/layout.rb +32 -0
- data/lib/pakyow/presenter/views/page.rb +72 -0
- data/lib/pakyow/presenter/views/partial.rb +28 -0
- data/lib/pakyow/presenter.rb +33 -0
- data/lib/pakyow/views/errors/layouts/development_error.html +102 -0
- data/lib/pakyow/views/errors/layouts/production_error.html +27 -0
- data/lib/pakyow/views/errors/pages/404.html +12 -0
- data/lib/pakyow/views/errors/pages/500.html +12 -0
- data/lib/pakyow/views/errors/pages/development/500.html +39 -0
- data/lib/string_doc/attributes.rb +109 -0
- data/lib/string_doc/meta_attributes.rb +48 -0
- data/lib/string_doc/meta_node.rb +328 -0
- data/lib/string_doc/node.rb +377 -0
- data/lib/string_doc.rb +642 -0
- metadata +95 -75
- data/pakyow-presenter/LICENSE +0 -20
- data/pakyow-presenter/lib/pakyow/presenter/attributes.rb +0 -228
- data/pakyow-presenter/lib/pakyow/presenter/base.rb +0 -38
- data/pakyow-presenter/lib/pakyow/presenter/binder.rb +0 -116
- data/pakyow-presenter/lib/pakyow/presenter/binder_set.rb +0 -94
- data/pakyow-presenter/lib/pakyow/presenter/binding_eval.rb +0 -37
- data/pakyow-presenter/lib/pakyow/presenter/config/presenter.rb +0 -42
- data/pakyow-presenter/lib/pakyow/presenter/container.rb +0 -6
- data/pakyow-presenter/lib/pakyow/presenter/doc_helpers.rb +0 -17
- data/pakyow-presenter/lib/pakyow/presenter/exceptions.rb +0 -11
- data/pakyow-presenter/lib/pakyow/presenter/ext/app.rb +0 -33
- data/pakyow-presenter/lib/pakyow/presenter/ext/call_context.rb +0 -28
- data/pakyow-presenter/lib/pakyow/presenter/helpers.rb +0 -46
- data/pakyow-presenter/lib/pakyow/presenter/page.rb +0 -110
- data/pakyow-presenter/lib/pakyow/presenter/partial.rb +0 -6
- data/pakyow-presenter/lib/pakyow/presenter/presenter.rb +0 -232
- data/pakyow-presenter/lib/pakyow/presenter/string_doc.rb +0 -380
- data/pakyow-presenter/lib/pakyow/presenter/string_doc_parser.rb +0 -144
- data/pakyow-presenter/lib/pakyow/presenter/string_doc_renderer.rb +0 -18
- data/pakyow-presenter/lib/pakyow/presenter/template.rb +0 -51
- data/pakyow-presenter/lib/pakyow/presenter/view.rb +0 -541
- data/pakyow-presenter/lib/pakyow/presenter/view_collection.rb +0 -330
- data/pakyow-presenter/lib/pakyow/presenter/view_composer.rb +0 -237
- data/pakyow-presenter/lib/pakyow/presenter/view_context.rb +0 -111
- data/pakyow-presenter/lib/pakyow/presenter/view_store.rb +0 -262
- data/pakyow-presenter/lib/pakyow/presenter/view_store_loader.rb +0 -43
- data/pakyow-presenter/lib/pakyow/presenter/view_version.rb +0 -113
- data/pakyow-presenter/lib/pakyow/presenter.rb +0 -8
- data/pakyow-presenter/lib/pakyow/views/errors/404.erb +0 -26
- data/pakyow-presenter/lib/pakyow/views/errors/500.erb +0 -23
- data/pakyow-presenter/lib/pakyow-presenter.rb +0 -1
data/lib/string_doc.rb
ADDED
@@ -0,0 +1,642 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "cgi"
|
4
|
+
|
5
|
+
require "oga"
|
6
|
+
|
7
|
+
require "pakyow/support/inflector"
|
8
|
+
require "pakyow/support/silenceable"
|
9
|
+
|
10
|
+
# String-based XML document optimized for fast manipulation and rendering.
|
11
|
+
#
|
12
|
+
# In Pakyow, we rarely care about every node in a document. Instead, only significant nodes and
|
13
|
+
# immediate children are available for manipulation. StringDoc provides "just enough" for our
|
14
|
+
# purposes. A StringDoc is represented as a multi- dimensional array of strings, making
|
15
|
+
# rendering essentially a +flatten.join+.
|
16
|
+
#
|
17
|
+
# Because less work is performed during render, StringDoc is consistently faster than rendering
|
18
|
+
# a document using Nokigiri or Oga. One obvious tradeoff is that parsing is much slower (we use
|
19
|
+
# Oga to parse the XML, then convert it into a StringDoc). This is an acceptable tradeoff
|
20
|
+
# because we only pay the parsing cost once (when the Pakyow application boots).
|
21
|
+
#
|
22
|
+
# All that to say, StringDoc is a tool that is very specialized to Pakyow's use-case. Use it
|
23
|
+
# only when a longer parse time is acceptable and you only care about a handful of identifiable
|
24
|
+
# nodes in a document.
|
25
|
+
#
|
26
|
+
class StringDoc
|
27
|
+
require "string_doc/attributes"
|
28
|
+
require "string_doc/node"
|
29
|
+
require "string_doc/meta_node"
|
30
|
+
|
31
|
+
class << self
|
32
|
+
# Creates an empty doc.
|
33
|
+
#
|
34
|
+
def empty
|
35
|
+
allocate.tap do |doc|
|
36
|
+
doc.instance_variable_set(:@nodes, [])
|
37
|
+
doc.instance_variable_set(:@collapsed, nil)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Registers a significant node with a name and an object to handle parsing.
|
42
|
+
#
|
43
|
+
def significant(name, object, descend: true)
|
44
|
+
significant_types[name] = { object: object, descend: descend }
|
45
|
+
end
|
46
|
+
|
47
|
+
# Creates a +StringDoc+ from an array of +Node+ objects.
|
48
|
+
#
|
49
|
+
def from_nodes(nodes)
|
50
|
+
allocate.tap do |instance|
|
51
|
+
instance.instance_variable_set(:@nodes, nodes)
|
52
|
+
instance.instance_variable_set(:@collapsed, nil)
|
53
|
+
|
54
|
+
nodes.each do |node|
|
55
|
+
node.parent = instance
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Yields nodes from an oga document, breadth-first.
|
61
|
+
#
|
62
|
+
def breadth_first(doc)
|
63
|
+
queue = [doc]
|
64
|
+
|
65
|
+
until queue.empty?
|
66
|
+
element = queue.shift
|
67
|
+
|
68
|
+
if element == doc
|
69
|
+
queue.concat(element.children.to_a); next
|
70
|
+
end
|
71
|
+
|
72
|
+
yield element
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns attributes for an oga element.
|
77
|
+
#
|
78
|
+
def attributes(element)
|
79
|
+
if element.is_a?(Oga::XML::Element)
|
80
|
+
element.attributes
|
81
|
+
else
|
82
|
+
[]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Builds a string-based representation of attributes for an oga element.
|
87
|
+
#
|
88
|
+
def attributes_string(element)
|
89
|
+
attributes(element).each_with_object(String.new) do |attribute, string|
|
90
|
+
string << " #{attribute.name}=\"#{attribute.value}\""
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Determines the significance of +element+.
|
95
|
+
#
|
96
|
+
def find_significance(element)
|
97
|
+
significant_types.each_with_object([]) do |(key, info), significance|
|
98
|
+
if info[:object].significant?(element)
|
99
|
+
significance << key
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns true if the given Oga element contains a child node that is significant.
|
105
|
+
#
|
106
|
+
def contains_significant_child?(element)
|
107
|
+
element.children.each do |child|
|
108
|
+
return true if find_significance(child).any?
|
109
|
+
return true if contains_significant_child?(child)
|
110
|
+
end
|
111
|
+
|
112
|
+
false
|
113
|
+
end
|
114
|
+
|
115
|
+
# @api private
|
116
|
+
def significant_types
|
117
|
+
@significant_types ||= {}
|
118
|
+
end
|
119
|
+
|
120
|
+
# @api private
|
121
|
+
def nodes_from_doc_or_string(doc_node_or_string)
|
122
|
+
case doc_node_or_string
|
123
|
+
when StringDoc
|
124
|
+
doc_node_or_string.nodes
|
125
|
+
when Node, MetaNode
|
126
|
+
[doc_node_or_string]
|
127
|
+
else
|
128
|
+
StringDoc.new(doc_node_or_string.to_s).nodes
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
include Pakyow::Support::Silenceable
|
134
|
+
|
135
|
+
# Array of +Node+ objects.
|
136
|
+
#
|
137
|
+
attr_reader :nodes, :collapsed
|
138
|
+
|
139
|
+
# Creates a +StringDoc+ from an html string.
|
140
|
+
#
|
141
|
+
def initialize(html)
|
142
|
+
@nodes = parse(Oga.parse_html(html))
|
143
|
+
@collapsed = nil
|
144
|
+
end
|
145
|
+
|
146
|
+
# @api private
|
147
|
+
def initialize_copy(_)
|
148
|
+
super
|
149
|
+
|
150
|
+
@nodes = @nodes.map { |node|
|
151
|
+
node.dup.tap do |duped_node|
|
152
|
+
duped_node.parent = self
|
153
|
+
end
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
# @api private
|
158
|
+
def soft_copy
|
159
|
+
instance = self.class.allocate
|
160
|
+
|
161
|
+
instance.instance_variable_set(:@nodes, @nodes.map { |node|
|
162
|
+
duped_node = node.soft_copy
|
163
|
+
duped_node.parent = instance
|
164
|
+
duped_node
|
165
|
+
})
|
166
|
+
|
167
|
+
instance.instance_variable_set(:@collapsed, @collapsed)
|
168
|
+
|
169
|
+
instance
|
170
|
+
end
|
171
|
+
|
172
|
+
include Enumerable
|
173
|
+
|
174
|
+
def each(descend: false, &block)
|
175
|
+
return enum_for(:each, descend: descend) unless block_given?
|
176
|
+
|
177
|
+
@nodes.each do |node|
|
178
|
+
yield node
|
179
|
+
|
180
|
+
if descend || node.label(:descend) != false
|
181
|
+
if node.children.is_a?(StringDoc)
|
182
|
+
node.children.each(descend: descend, &block)
|
183
|
+
else
|
184
|
+
yield node.children
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# Yields each node matching the significant type.
|
191
|
+
#
|
192
|
+
def each_significant_node(type, descend: false)
|
193
|
+
return enum_for(:each_significant_node, type, descend: descend) unless block_given?
|
194
|
+
|
195
|
+
each(descend: descend) do |node|
|
196
|
+
yield node if (node.is_a?(Node) || node.is_a?(MetaNode)) && node.significant?(type)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
# Yields each node matching the significant type, without descending into nodes that are of that type.
|
201
|
+
#
|
202
|
+
def each_significant_node_without_descending_into_type(type, descend: false, &block)
|
203
|
+
return enum_for(:each_significant_node_without_descending_into_type, type, descend: descend) unless block_given?
|
204
|
+
|
205
|
+
@nodes.each do |node|
|
206
|
+
if node.is_a?(Node) || node.is_a?(MetaNode)
|
207
|
+
if node.significant?(type)
|
208
|
+
yield node
|
209
|
+
else
|
210
|
+
if descend || node.label(:descend) != false
|
211
|
+
if node.children.is_a?(StringDoc)
|
212
|
+
node.children.each_significant_node_without_descending_into_type(type, descend: descend, &block)
|
213
|
+
else
|
214
|
+
yield node.children
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# Yields each node matching the significant type and name.
|
223
|
+
#
|
224
|
+
# @see find_significant_nodes
|
225
|
+
#
|
226
|
+
def each_significant_node_with_name(type, name, descend: false)
|
227
|
+
return enum_for(:each_significant_node_with_name, type, name, descend: descend) unless block_given?
|
228
|
+
|
229
|
+
each_significant_node(type, descend: descend) do |node|
|
230
|
+
yield node if node.label(type) == name
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
# Returns the first node matching the significant type.
|
235
|
+
#
|
236
|
+
def find_first_significant_node(type, descend: false)
|
237
|
+
each(descend: descend).find { |node|
|
238
|
+
node.significant?(type)
|
239
|
+
}
|
240
|
+
end
|
241
|
+
|
242
|
+
# Returns nodes matching the significant type.
|
243
|
+
#
|
244
|
+
def find_significant_nodes(type, descend: false)
|
245
|
+
[].tap do |nodes|
|
246
|
+
each_significant_node(type, descend: descend) do |node|
|
247
|
+
nodes << node
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
# Returns nodes matching the significant type and name.
|
253
|
+
#
|
254
|
+
# @see find_significant_nodes
|
255
|
+
#
|
256
|
+
def find_significant_nodes_with_name(type, name, descend: false)
|
257
|
+
[].tap do |nodes|
|
258
|
+
each_significant_node_with_name(type, name, descend: descend) do |node|
|
259
|
+
nodes << node
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
# Clears all nodes.
|
265
|
+
#
|
266
|
+
def clear
|
267
|
+
tap do
|
268
|
+
@nodes.clear
|
269
|
+
end
|
270
|
+
end
|
271
|
+
alias remove clear
|
272
|
+
|
273
|
+
# Replaces the current document.
|
274
|
+
#
|
275
|
+
# Accepts a +StringDoc+ or XML +String+.
|
276
|
+
#
|
277
|
+
def replace(doc_or_string)
|
278
|
+
tap do
|
279
|
+
nodes = self.class.nodes_from_doc_or_string(doc_or_string)
|
280
|
+
|
281
|
+
nodes.each do |node|
|
282
|
+
node.parent = self
|
283
|
+
end
|
284
|
+
|
285
|
+
@nodes = nodes
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
# Appends to this document.
|
290
|
+
#
|
291
|
+
# Accepts a +StringDoc+ or XML +String+.
|
292
|
+
#
|
293
|
+
def append(doc_or_string)
|
294
|
+
tap do
|
295
|
+
nodes = self.class.nodes_from_doc_or_string(doc_or_string)
|
296
|
+
|
297
|
+
nodes.each do |node|
|
298
|
+
node.parent = self
|
299
|
+
end
|
300
|
+
|
301
|
+
@nodes.concat(nodes)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
# Appends raw html to this document, without parsing.
|
306
|
+
#
|
307
|
+
def append_html(html)
|
308
|
+
tap do
|
309
|
+
node = Node.new(html.to_s)
|
310
|
+
node.parent = self
|
311
|
+
@nodes << node
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
# Prepends to this document.
|
316
|
+
#
|
317
|
+
# Accepts a +StringDoc+ or XML +String+.
|
318
|
+
#
|
319
|
+
def prepend(doc_or_string)
|
320
|
+
tap do
|
321
|
+
nodes = self.class.nodes_from_doc_or_string(doc_or_string)
|
322
|
+
|
323
|
+
nodes.each do |node|
|
324
|
+
node.parent = self
|
325
|
+
end
|
326
|
+
|
327
|
+
@nodes.unshift(*nodes)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
# Inserts a node after another node contained in this document.
|
332
|
+
#
|
333
|
+
def insert_after(node_to_insert, after_node)
|
334
|
+
tap do
|
335
|
+
if after_node_index = @nodes.index(after_node)
|
336
|
+
nodes = self.class.nodes_from_doc_or_string(node_to_insert)
|
337
|
+
|
338
|
+
nodes.each do |node|
|
339
|
+
node.parent = self
|
340
|
+
end
|
341
|
+
|
342
|
+
@nodes.insert(after_node_index + 1, *nodes)
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
# Inserts a node before another node contained in this document.
|
348
|
+
#
|
349
|
+
def insert_before(node_to_insert, before_node)
|
350
|
+
tap do
|
351
|
+
if before_node_index = @nodes.index(before_node)
|
352
|
+
nodes = self.class.nodes_from_doc_or_string(node_to_insert)
|
353
|
+
|
354
|
+
nodes.each do |node|
|
355
|
+
node.parent = self
|
356
|
+
end
|
357
|
+
|
358
|
+
@nodes.insert(before_node_index, *nodes)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
# Removes a node from the document.
|
364
|
+
#
|
365
|
+
def remove_node(node_to_delete)
|
366
|
+
tap do
|
367
|
+
@nodes.delete_if { |node|
|
368
|
+
node.object_id == node_to_delete.object_id
|
369
|
+
}
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
# Replaces a node from the document.
|
374
|
+
#
|
375
|
+
def replace_node(node_to_replace, replacement_node)
|
376
|
+
tap do
|
377
|
+
if replace_node_index = @nodes.index(node_to_replace)
|
378
|
+
nodes_to_insert = self.class.nodes_from_doc_or_string(replacement_node)
|
379
|
+
|
380
|
+
nodes_to_insert.each do |node|
|
381
|
+
node.parent = self
|
382
|
+
end
|
383
|
+
|
384
|
+
@nodes.insert(replace_node_index + 1, *nodes_to_insert)
|
385
|
+
@nodes.delete_at(replace_node_index)
|
386
|
+
end
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
def render(output = String.new, context: nil)
|
391
|
+
if collapsed && empty?
|
392
|
+
output << collapsed
|
393
|
+
else
|
394
|
+
nodes.each do |node|
|
395
|
+
case node
|
396
|
+
when MetaNode
|
397
|
+
node.render(output, context: context)
|
398
|
+
when Node
|
399
|
+
node.render(output, context: context)
|
400
|
+
else
|
401
|
+
output << node.to_s
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
output
|
406
|
+
end
|
407
|
+
end
|
408
|
+
alias :to_html :render
|
409
|
+
alias :to_xml :render
|
410
|
+
|
411
|
+
# Returns the node as an xml string, without transforming.
|
412
|
+
#
|
413
|
+
def to_s
|
414
|
+
@nodes.each_with_object(String.new) do |node, string|
|
415
|
+
string << node.to_s
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
def ==(other)
|
420
|
+
other.is_a?(StringDoc) && @nodes == other.nodes
|
421
|
+
end
|
422
|
+
|
423
|
+
def collapse(*significance)
|
424
|
+
if significance?(*significance)
|
425
|
+
@nodes.each do |node|
|
426
|
+
node.children.collapse(*significance)
|
427
|
+
end
|
428
|
+
else
|
429
|
+
@collapsed = to_xml
|
430
|
+
@nodes = []
|
431
|
+
end
|
432
|
+
|
433
|
+
@collapsed
|
434
|
+
end
|
435
|
+
|
436
|
+
def significance?(*significance)
|
437
|
+
@nodes.any? { |node|
|
438
|
+
node.significance?(*significance) || node.children.significance?(*significance)
|
439
|
+
}
|
440
|
+
end
|
441
|
+
|
442
|
+
def remove_empty_nodes
|
443
|
+
@nodes.each do |node|
|
444
|
+
node.children.remove_empty_nodes
|
445
|
+
end
|
446
|
+
|
447
|
+
unless empty?
|
448
|
+
@nodes.delete_if(&:empty?)
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
def empty?
|
453
|
+
@nodes.empty?
|
454
|
+
end
|
455
|
+
|
456
|
+
def transforms?
|
457
|
+
@nodes.any?(&:transforms?)
|
458
|
+
end
|
459
|
+
|
460
|
+
private
|
461
|
+
|
462
|
+
# Parses an Oga document into an array of +Node+ objects.
|
463
|
+
#
|
464
|
+
def parse(doc)
|
465
|
+
nodes = []
|
466
|
+
|
467
|
+
unless doc.is_a?(Oga::XML::Element) || !doc.respond_to?(:doctype) || doc.doctype.nil?
|
468
|
+
nodes << Node.new("<!DOCTYPE html>")
|
469
|
+
end
|
470
|
+
|
471
|
+
self.class.breadth_first(doc) do |element|
|
472
|
+
significance = self.class.find_significance(element)
|
473
|
+
|
474
|
+
unless significance.any? || self.class.contains_significant_child?(element)
|
475
|
+
# Nothing inside of the node is significant, so collapse it to a single node.
|
476
|
+
nodes << Node.new(element.to_xml); next
|
477
|
+
end
|
478
|
+
|
479
|
+
node = if significance.any?
|
480
|
+
build_significant_node(element, significance)
|
481
|
+
elsif element.is_a?(Oga::XML::Text) || element.is_a?(Oga::XML::Comment)
|
482
|
+
Node.new(element.to_xml)
|
483
|
+
else
|
484
|
+
Node.new("<#{element.name}#{self.class.attributes_string(element)}")
|
485
|
+
end
|
486
|
+
|
487
|
+
if element.is_a?(Oga::XML::Element)
|
488
|
+
node.close(element.name, parse(element))
|
489
|
+
end
|
490
|
+
|
491
|
+
nodes << node
|
492
|
+
end
|
493
|
+
|
494
|
+
nodes
|
495
|
+
end
|
496
|
+
|
497
|
+
# Attributes that should be prefixed with +data-+
|
498
|
+
#
|
499
|
+
DATA_ATTRS = %i(ui binding endpoint endpoint-action version).freeze
|
500
|
+
|
501
|
+
# Attributes that will be turned into +StringDoc+ labels
|
502
|
+
#
|
503
|
+
LABEL_ATTRS = %i(ui mode version include exclude endpoint endpoint-action prototype binding dataset).freeze
|
504
|
+
|
505
|
+
LABEL_MAPPING = {}.freeze
|
506
|
+
|
507
|
+
# Attributes that should be deleted from the view
|
508
|
+
#
|
509
|
+
DELETED_ATTRS = %i(include exclude prototype mode dataset).freeze
|
510
|
+
|
511
|
+
ATTR_MAPPING = {
|
512
|
+
binding: :b,
|
513
|
+
endpoint: :e,
|
514
|
+
"endpoint-action": :"e-a",
|
515
|
+
version: :v
|
516
|
+
}.freeze
|
517
|
+
|
518
|
+
def attributes_hash(element)
|
519
|
+
StringDoc.attributes(element).each_with_object({}) { |attribute, elements|
|
520
|
+
elements[attribute.name.to_sym] = CGI.escape_html(attribute.value.to_s)
|
521
|
+
}
|
522
|
+
end
|
523
|
+
|
524
|
+
def labels_hash(element)
|
525
|
+
StringDoc.attributes(element).dup.each_with_object({}) { |attribute, labels|
|
526
|
+
attribute_name = attribute.name.to_sym
|
527
|
+
|
528
|
+
if LABEL_ATTRS.include?(attribute_name)
|
529
|
+
labels[LABEL_MAPPING.fetch(attribute_name, attribute_name)] = attribute.value.to_s.to_sym
|
530
|
+
end
|
531
|
+
}
|
532
|
+
end
|
533
|
+
|
534
|
+
def build_significant_node(element, significance)
|
535
|
+
node = if element.is_a?(Oga::XML::Element)
|
536
|
+
attributes = attributes_hash(element).each_with_object({}) { |(key, value), remapped_attributes|
|
537
|
+
unless DELETED_ATTRS.include?(key)
|
538
|
+
remapped_key = ATTR_MAPPING.fetch(key, key)
|
539
|
+
|
540
|
+
if DATA_ATTRS.include?(key)
|
541
|
+
remapped_key = :"data-#{remapped_key}"
|
542
|
+
end
|
543
|
+
|
544
|
+
remapped_attributes[remapped_key] = value || ""
|
545
|
+
end
|
546
|
+
}
|
547
|
+
|
548
|
+
labels = labels_hash(element)
|
549
|
+
|
550
|
+
if labels.include?(:binding)
|
551
|
+
post_process_binding!(element, attributes, labels)
|
552
|
+
end
|
553
|
+
|
554
|
+
significance_options = significance.map { |significant_type|
|
555
|
+
self.class.significant_types[significant_type]
|
556
|
+
}
|
557
|
+
|
558
|
+
labels[:descend] = significance_options.all? { |options| options[:descend] == true }
|
559
|
+
|
560
|
+
Node.new("<#{element.name}", Attributes.new(attributes), significance: significance, labels: labels, parent: self)
|
561
|
+
else
|
562
|
+
name = element.text.strip.match(/@[^\s]*\s*([a-zA-Z0-9\-_]*)/)[1]
|
563
|
+
labels = significance.each_with_object({}) { |significant_type, labels_hash|
|
564
|
+
# FIXME: remove this special case logic
|
565
|
+
labels_hash[significant_type] = if name.empty? && significant_type == :container
|
566
|
+
Pakyow::Presenter::Page::DEFAULT_CONTAINER
|
567
|
+
else
|
568
|
+
name.to_sym
|
569
|
+
end
|
570
|
+
}
|
571
|
+
|
572
|
+
Node.new(element.to_xml, significance: significance, parent: self, labels: labels)
|
573
|
+
end
|
574
|
+
|
575
|
+
significance.each do |significant_type|
|
576
|
+
object = StringDoc.significant_types.dig(significant_type, :object)
|
577
|
+
if object && object.respond_to?(:decorate)
|
578
|
+
object.decorate(node)
|
579
|
+
end
|
580
|
+
end
|
581
|
+
|
582
|
+
node
|
583
|
+
end
|
584
|
+
|
585
|
+
def post_process_binding!(element, attributes, labels)
|
586
|
+
channel = semantic_channel_for_element(element)
|
587
|
+
binding = labels[:binding].to_s
|
588
|
+
|
589
|
+
if binding.start_with?("@")
|
590
|
+
plug, binding = binding.split(".", 2)
|
591
|
+
plug_name, plug_instance = plug.split("(", 2)
|
592
|
+
|
593
|
+
if plug_instance
|
594
|
+
plug_instance = plug_instance[0..-2]
|
595
|
+
else
|
596
|
+
plug_instance = :default
|
597
|
+
end
|
598
|
+
|
599
|
+
labels[:plug] = {
|
600
|
+
name: plug_name[1..-1].to_sym,
|
601
|
+
instance: plug_instance.to_sym,
|
602
|
+
}
|
603
|
+
|
604
|
+
labels[:plug][:key] = if labels[:plug][:instance] == :default
|
605
|
+
"@#{labels[:plug][:name]}"
|
606
|
+
else
|
607
|
+
"@#{labels[:plug][:name]}.#{labels[:plug][:instance]}"
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
611
|
+
binding_parts = binding.split(":").map(&:to_sym)
|
612
|
+
binding_name, binding_prop = binding_parts[0].to_s.split(".", 2).map(&:to_sym)
|
613
|
+
plural_binding_name = Pakyow::Support.inflector.pluralize(binding_name).to_sym
|
614
|
+
singular_binding_name = Pakyow::Support.inflector.singularize(binding_name).to_sym
|
615
|
+
|
616
|
+
labels[:binding] = binding_name
|
617
|
+
labels[:plural_binding] = plural_binding_name
|
618
|
+
labels[:singular_binding] = singular_binding_name
|
619
|
+
|
620
|
+
if binding_prop
|
621
|
+
labels[:binding_prop] = binding_prop
|
622
|
+
end
|
623
|
+
|
624
|
+
channel.concat(binding_parts[1..-1])
|
625
|
+
labels[:channeled_binding] = [binding_name].concat(channel).join(":").to_sym
|
626
|
+
labels[:plural_channeled_binding] = [plural_binding_name].concat(channel).join(":").to_sym
|
627
|
+
labels[:singular_channeled_binding] = [singular_binding_name].concat(channel).join(":").to_sym
|
628
|
+
attributes[:"data-b"] = [binding_parts[0]].concat(channel).join(":")
|
629
|
+
end
|
630
|
+
|
631
|
+
SEMANTIC_TAGS = %w(
|
632
|
+
form
|
633
|
+
).freeze
|
634
|
+
|
635
|
+
def semantic_channel_for_element(element, channel = [])
|
636
|
+
if SEMANTIC_TAGS.include?(element.name)
|
637
|
+
channel << element.name.to_sym
|
638
|
+
end
|
639
|
+
|
640
|
+
channel
|
641
|
+
end
|
642
|
+
end
|