papercraft 0.10 → 0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +55 -5
- data/lib/papercraft/component.rb +2 -2
- data/lib/papercraft/html.rb +57 -4
- data/lib/papercraft/renderer.rb +150 -6
- data/lib/papercraft/version.rb +1 -1
- data/lib/papercraft.rb +6 -0
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5250720b438ab385dca9f3b91487fc2e5c887ba7e709015f8e26a1a2e78fa8d6
|
4
|
+
data.tar.gz: '049f2399c403f55fdd0c0ee9de3b9bac5d314d0c57520184ddc59f361f7ec87d'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 176f86dbf9e69deaf1fd60edbcba23401c1288a96d9eb5210175815e1ea1b297178daf2ba3533ae8020d18b8bc1b7003bcf39b2175c4ad95439982d622c01fad
|
7
|
+
data.tar.gz: 9f623e968a7462225e4f504c880a911557d024b45393658bec01426ebff934bfa9c94094ca52feae8c2a23b642054e9cd3b88df25644019a61f62c5fda4350ad
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
## 0.13 2022-01-19
|
2
|
+
|
3
|
+
- Add support for partial parameter application (#3)
|
4
|
+
|
5
|
+
## 0.12 2022-01-06
|
6
|
+
|
7
|
+
- Improve documentation
|
8
|
+
- Add `Renderer#tag` method
|
9
|
+
- Add `HTML#style`, `HTML#script` methods
|
10
|
+
|
11
|
+
## 0.11 2022-01-04
|
12
|
+
|
13
|
+
- Add deferred evaluation
|
14
|
+
|
15
|
+
## 0.10.1 2021-12-25
|
16
|
+
|
17
|
+
- Fix tag rendering with empty text in Ruby 3.0
|
18
|
+
|
1
19
|
## 0.10 2021-12-25
|
2
20
|
|
3
21
|
- Add support for extensions
|
data/README.md
CHANGED
@@ -20,8 +20,6 @@
|
|
20
20
|
<a href="https://www.rubydoc.info/gems/papercraft">API reference</a>
|
21
21
|
</p>
|
22
22
|
|
23
|
-
# Papercraft - Composable HTML templating for Ruby
|
24
|
-
|
25
23
|
## What is Papercraft?
|
26
24
|
|
27
25
|
```ruby
|
@@ -368,17 +366,69 @@ The default Kramdown options are:
|
|
368
366
|
```
|
369
367
|
|
370
368
|
The deafult options can be configured by accessing
|
371
|
-
`Papercraft::HTML.kramdown_options
|
369
|
+
`Papercraft::HTML.kramdown_options`, e.g.:
|
372
370
|
|
373
371
|
```ruby
|
374
372
|
Papercraft::HTML.kramdown_options[:auto_ids] = false
|
375
373
|
```
|
376
374
|
|
375
|
+
## Deferred evaluation
|
376
|
+
|
377
|
+
Deferred evaluation allows deferring the rendering of parts of a template until
|
378
|
+
the last moment, thus allowing an inner component to manipulate the state of the
|
379
|
+
outer component. To in order to defer a part of a template, use `#defer`, and
|
380
|
+
include any markup in the provided block. This technique, in in conjunction with
|
381
|
+
holding state in instance variables, is an alternative to passing parameters,
|
382
|
+
which can be limiting in some situations.
|
383
|
+
|
384
|
+
A few use cases for deferred evaulation come to mind:
|
385
|
+
|
386
|
+
- Setting the page title.
|
387
|
+
- Adding a flash message to a page.
|
388
|
+
- Using components that dynamically add static dependencies (JS and CSS) to the
|
389
|
+
page.
|
390
|
+
|
391
|
+
The last use case is particularly interesting. Imagine a `DependencyMananger`
|
392
|
+
class that can collect JS and CSS dependencies from the different components
|
393
|
+
integrated into the page, and adds them to the page's `<head>` element:
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
default_layout = H { |**args|
|
397
|
+
@dependencies = DependencyMananger.new
|
398
|
+
head {
|
399
|
+
defer { emit @dependencies.head_markup }
|
400
|
+
}
|
401
|
+
body { emit_yield **args }
|
402
|
+
}
|
403
|
+
|
404
|
+
button = proc { |text, onclick|
|
405
|
+
@dependencies.js '/static/js/button.js'
|
406
|
+
@dependencies.css '/static/css/button.css'
|
407
|
+
|
408
|
+
button text, onclick: onclick
|
409
|
+
}
|
410
|
+
|
411
|
+
heading = proc { |text|
|
412
|
+
@dependencies.js '/static/js/heading.js'
|
413
|
+
@dependencies.css '/static/css/heading.css'
|
414
|
+
|
415
|
+
h1 text
|
416
|
+
}
|
417
|
+
|
418
|
+
page = default_layout.apply {
|
419
|
+
emit heading, "What's your favorite cheese?"
|
420
|
+
|
421
|
+
emit button, 'Beaufort', 'eat_beaufort()'
|
422
|
+
emit button, 'Mont d''or', 'eat_montdor()'
|
423
|
+
emit button, 'Époisses', 'eat_epoisses()'
|
424
|
+
}
|
425
|
+
```
|
426
|
+
|
377
427
|
## Papercraft extensions
|
378
428
|
|
379
429
|
Papercraft extensions are modules that contain one or more methods that can be
|
380
430
|
used to render complex HTML components. Extension modules can be used by
|
381
|
-
installing them as a namespaced extension using `Papercraft
|
431
|
+
installing them as a namespaced extension using `Papercraft::extension`.
|
382
432
|
Extensions are particularly useful when you work with CSS frameworks such as
|
383
433
|
[Bootstrap](https://getbootstrap.com/), [Tailwind](https://tailwindui.com/) or
|
384
434
|
[Primer](https://primer.style/).
|
@@ -424,7 +474,7 @@ end
|
|
424
474
|
Papercraft.extension(bootstrap: BootstrapComponents)
|
425
475
|
```
|
426
476
|
|
427
|
-
The call to `Papercraft
|
477
|
+
The call to `Papercraft::extension` lets us access the different methods of
|
428
478
|
`BootstrapComponents` by calling `#bootstrap` inside a template. With this,
|
429
479
|
we'll be able to express the above markup as follows:
|
430
480
|
|
data/lib/papercraft/component.rb
CHANGED
@@ -138,11 +138,11 @@ module Papercraft
|
|
138
138
|
template = self
|
139
139
|
if block
|
140
140
|
Component.new(&proc do |*x, **y|
|
141
|
-
with_block(block) { instance_exec(*x, **y, &template) }
|
141
|
+
with_block(block) { instance_exec(*a, *x, **b, **y, &template) }
|
142
142
|
end)
|
143
143
|
else
|
144
144
|
Component.new(&proc do |*x, **y|
|
145
|
-
instance_exec(*a, **b, &template)
|
145
|
+
instance_exec(*a, *x, **b, **y, &template)
|
146
146
|
end)
|
147
147
|
end
|
148
148
|
end
|
data/lib/papercraft/html.rb
CHANGED
@@ -44,15 +44,53 @@ module Papercraft
|
|
44
44
|
link(**attributes)
|
45
45
|
end
|
46
46
|
|
47
|
-
|
48
|
-
|
47
|
+
# Emits an inline CSS style element.
|
48
|
+
#
|
49
|
+
# @param css [String] CSS code
|
50
|
+
# @param **props [Hash] optional element attributes
|
51
|
+
# @return [void]
|
52
|
+
def style(css, **props, &block)
|
53
|
+
@buffer << '<style'
|
54
|
+
emit_props(props) unless props.empty?
|
55
|
+
|
56
|
+
@buffer << '>' << css << '</style>'
|
49
57
|
end
|
58
|
+
|
59
|
+
# Emits an inline JS script element.
|
60
|
+
#
|
61
|
+
# @param js [String, nil] Javascript code
|
62
|
+
# @param **props [Hash] optional element attributes
|
63
|
+
# @return [void]
|
64
|
+
def script(js = nil, **props, &block)
|
65
|
+
@buffer << '<script'
|
66
|
+
emit_props(props) unless props.empty?
|
50
67
|
|
51
|
-
|
52
|
-
|
68
|
+
if js
|
69
|
+
@buffer << '>' << js << '</script>'
|
70
|
+
else
|
71
|
+
@buffer << '></script>'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Converts and emits the given markdown. Papercraft uses
|
76
|
+
# [Kramdown](https://github.com/gettalong/kramdown/) to do the Markdown to
|
77
|
+
# HTML conversion. Optional Kramdown settings can be provided in order to
|
78
|
+
# control the conversion. Those are merged with the default Kramdown
|
79
|
+
# settings, which can be controlled using
|
80
|
+
# `Papercraft::HTML.kramdown_options`.
|
81
|
+
#
|
82
|
+
# @param markdown [String] Markdown content
|
83
|
+
# @param **opts [Hash] Kramdown options
|
84
|
+
# @return [void]
|
85
|
+
def emit_markdown(markdown, **opts)
|
86
|
+
emit Kramdown::Document.new(markdown, **kramdown_options(opts)).to_html
|
53
87
|
end
|
54
88
|
|
55
89
|
class << self
|
90
|
+
# Returns the default Kramdown options used for converting Markdown to
|
91
|
+
# HTML.
|
92
|
+
#
|
93
|
+
# @return [Hash] Default Kramdown options
|
56
94
|
def kramdown_options
|
57
95
|
@kramdown_options ||= {
|
58
96
|
entity_output: :numeric,
|
@@ -62,9 +100,24 @@ module Papercraft
|
|
62
100
|
}
|
63
101
|
end
|
64
102
|
|
103
|
+
# Sets the default Kramdown options used for converting Markdown to
|
104
|
+
# HTML.
|
105
|
+
#
|
106
|
+
# @param opts [Hash] New deafult Kramdown options
|
107
|
+
# @return [Hash] New default Kramdown options
|
65
108
|
def kramdown_options=(opts)
|
66
109
|
@kramdown_options = opts
|
67
110
|
end
|
68
111
|
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
# Returns the default Kramdown options, merged with the given overrides.
|
116
|
+
#
|
117
|
+
# @param opts [Hash] Kramdown option overrides
|
118
|
+
# @return [Hash] Merged Kramdown options
|
119
|
+
def kramdown_options(opts)
|
120
|
+
HTML.kramdown_options.merge(**opts)
|
121
|
+
end
|
69
122
|
end
|
70
123
|
end
|
data/lib/papercraft/renderer.rb
CHANGED
@@ -34,8 +34,29 @@ module Papercraft
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
#
|
38
|
-
#
|
37
|
+
# call_seq:
|
38
|
+
# Papercraft::Renderer.extension(name => mod, ...)
|
39
|
+
# Papercraft.extension(name => mod, ...)
|
40
|
+
#
|
41
|
+
# Installs the given extensions, passed in the form of a Ruby hash mapping
|
42
|
+
# methods to extension modules. The methods will be available to all
|
43
|
+
# Papercraft components. Extension methods are executed in the context of
|
44
|
+
# the the renderer instance, so they can look just like normal proc
|
45
|
+
# components. In cases where method names in the module clash with HTML
|
46
|
+
# tag names, you can use the `#tag` method to emit the relevant tag.
|
47
|
+
#
|
48
|
+
# module ComponentLibrary
|
49
|
+
# def card(title, content)
|
50
|
+
# div(class: 'card') {
|
51
|
+
# h3 title
|
52
|
+
# div(class: 'card-content') { emit_markdown content }
|
53
|
+
# }
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# Papercraft.extension(components: ComponentLibrary)
|
58
|
+
# H { components.card('Foo', '**Bar**') }
|
59
|
+
#
|
39
60
|
# @param map [Hash] hash mapping methods to extension modules
|
40
61
|
# @return [void]
|
41
62
|
def extension(map)
|
@@ -57,12 +78,14 @@ module Papercraft
|
|
57
78
|
end
|
58
79
|
end
|
59
80
|
|
81
|
+
INITIAL_BUFFER_CAPACITY = 8192
|
82
|
+
|
60
83
|
# Initializes the renderer and evaulates the given template in the
|
61
84
|
# renderer's scope.
|
62
85
|
#
|
63
86
|
# @param &template [Proc] template block
|
64
87
|
def initialize(&template)
|
65
|
-
@buffer =
|
88
|
+
@buffer = String.new(capacity: INITIAL_BUFFER_CAPACITY)
|
66
89
|
instance_eval(&template)
|
67
90
|
end
|
68
91
|
|
@@ -70,15 +93,36 @@ module Papercraft
|
|
70
93
|
#
|
71
94
|
# @return [String]
|
72
95
|
def to_s
|
96
|
+
if @parts
|
97
|
+
last = @buffer
|
98
|
+
@buffer = String.new(capacity: INITIAL_BUFFER_CAPACITY)
|
99
|
+
parts = @parts
|
100
|
+
@parts = nil
|
101
|
+
parts.each do |p|
|
102
|
+
if Proc === p
|
103
|
+
render_deferred_proc(&p)
|
104
|
+
else
|
105
|
+
@buffer << p
|
106
|
+
end
|
107
|
+
end
|
108
|
+
@buffer << last unless last.empty?
|
109
|
+
end
|
73
110
|
@buffer
|
74
111
|
end
|
75
112
|
|
113
|
+
# The tag method template below is optimized for performance. Do not touch!
|
114
|
+
|
76
115
|
S_TAG_METHOD_LINE = __LINE__ + 1
|
77
116
|
S_TAG_METHOD = <<~EOF
|
78
|
-
S_TAG_%<TAG>s_PRE =
|
79
|
-
S_TAG_%<TAG>s_CLOSE =
|
117
|
+
S_TAG_%<TAG>s_PRE = %<tag_pre>s
|
118
|
+
S_TAG_%<TAG>s_CLOSE = %<tag_close>s
|
80
119
|
|
81
120
|
def %<tag>s(text = nil, **props, &block)
|
121
|
+
if text.is_a?(Hash) && props.empty?
|
122
|
+
props = text
|
123
|
+
text = nil
|
124
|
+
end
|
125
|
+
|
82
126
|
@buffer << S_TAG_%<TAG>s_PRE
|
83
127
|
emit_props(props) unless props.empty?
|
84
128
|
|
@@ -98,6 +142,48 @@ module Papercraft
|
|
98
142
|
end
|
99
143
|
EOF
|
100
144
|
|
145
|
+
# Emits an HTML tag with the given content, properties and optional block.
|
146
|
+
# This method is an alternative to emitting HTML tags using dynamically
|
147
|
+
# created methods. This is particularly useful when using extensions that
|
148
|
+
# have method names that clash with HTML tags, such as `button` or `a`, or
|
149
|
+
# when you need to override the behaviour of a particular HTML tag.
|
150
|
+
#
|
151
|
+
# The following two method calls have the same effect:
|
152
|
+
#
|
153
|
+
# button 'text', id: 'button1'
|
154
|
+
# tag :button, 'text', id: 'button1'
|
155
|
+
#
|
156
|
+
# @param sym [Symbol, String] HTML tag
|
157
|
+
# @param text [String, nil] tag content
|
158
|
+
# @param **props [Hash] tag attributes
|
159
|
+
# @param &block [Proc] optional inner HTML
|
160
|
+
# @return [void]
|
161
|
+
def tag(sym, text = nil, **props, &block)
|
162
|
+
if text.is_a?(Hash) && props.empty?
|
163
|
+
props = text
|
164
|
+
text = nil
|
165
|
+
end
|
166
|
+
|
167
|
+
tag = sym.to_s.tr('_', '-')
|
168
|
+
|
169
|
+
@buffer << S_LT << tag
|
170
|
+
emit_props(props) unless props.empty?
|
171
|
+
|
172
|
+
if block
|
173
|
+
@buffer << S_GT
|
174
|
+
instance_eval(&block)
|
175
|
+
@buffer << S_LT_SLASH << tag << S_GT
|
176
|
+
elsif Proc === text
|
177
|
+
@buffer << S_GT
|
178
|
+
emit(text)
|
179
|
+
@buffer << S_LT_SLASH << tag << S_GT
|
180
|
+
elsif text
|
181
|
+
@buffer << S_GT << escape_text(text.to_s) << S_LT_SLASH << tag << S_GT
|
182
|
+
else
|
183
|
+
@buffer << S_SLASH_GT
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
101
187
|
# Catches undefined tag method call and handles it by defining the method.
|
102
188
|
#
|
103
189
|
# @param sym [Symbol] HTML tag or component identifier
|
@@ -107,7 +193,12 @@ module Papercraft
|
|
107
193
|
# @return [void]
|
108
194
|
def method_missing(sym, *args, **opts, &block)
|
109
195
|
tag = sym.to_s
|
110
|
-
code = S_TAG_METHOD % {
|
196
|
+
code = S_TAG_METHOD % {
|
197
|
+
tag: tag,
|
198
|
+
TAG: tag.upcase,
|
199
|
+
tag_pre: "<#{tag.tr('_', '-')}".inspect,
|
200
|
+
tag_close: "</#{tag.tr('_', '-')}>".inspect
|
201
|
+
}
|
111
202
|
self.class.class_eval(code, __FILE__, S_TAG_METHOD_LINE)
|
112
203
|
send(sym, *args, **opts, &block)
|
113
204
|
end
|
@@ -155,6 +246,42 @@ module Papercraft
|
|
155
246
|
|
156
247
|
instance_exec(*a, **b, &@inner_block)
|
157
248
|
end
|
249
|
+
|
250
|
+
# Defers the given block to be evaluated later. Deferred evaluation allows
|
251
|
+
# Papercraft components to inject state into sibling components, regardless
|
252
|
+
# of the component's order in the container component. For example, a nested
|
253
|
+
# component may set an instance variable used by another component. This is
|
254
|
+
# an elegant solution to the problem of setting the HTML page's title, or
|
255
|
+
# adding elements to the `<head>` section. Here's how a title can be
|
256
|
+
# controlled from a nested component:
|
257
|
+
#
|
258
|
+
# layout = H {
|
259
|
+
# html {
|
260
|
+
# head {
|
261
|
+
# defer { title @title }
|
262
|
+
# }
|
263
|
+
# body {
|
264
|
+
# emit_yield
|
265
|
+
# }
|
266
|
+
# }
|
267
|
+
# }
|
268
|
+
#
|
269
|
+
# html.render {
|
270
|
+
# @title = 'My super page'
|
271
|
+
# h1 'content'
|
272
|
+
# }
|
273
|
+
#
|
274
|
+
# @param &block [Proc] Deferred block to be emitted
|
275
|
+
# @return [void]
|
276
|
+
def defer(&block)
|
277
|
+
if !@parts
|
278
|
+
@parts = [@buffer, block]
|
279
|
+
else
|
280
|
+
@parts << @buffer unless @buffer.empty?
|
281
|
+
@parts << block
|
282
|
+
end
|
283
|
+
@buffer = String.new(capacity: INITIAL_BUFFER_CAPACITY)
|
284
|
+
end
|
158
285
|
|
159
286
|
S_LT = '<'
|
160
287
|
S_GT = '>'
|
@@ -212,6 +339,23 @@ module Papercraft
|
|
212
339
|
end
|
213
340
|
}
|
214
341
|
end
|
342
|
+
|
343
|
+
# Renders a deferred proc by evaluating it, then adding the rendered result
|
344
|
+
# to the buffer.
|
345
|
+
#
|
346
|
+
# @param &block [Proc] deferred proc
|
347
|
+
# @return [void]
|
348
|
+
def render_deferred_proc(&block)
|
349
|
+
old_buffer = @buffer
|
350
|
+
|
351
|
+
@buffer = String.new(capacity: INITIAL_BUFFER_CAPACITY)
|
352
|
+
@parts = nil
|
353
|
+
|
354
|
+
instance_eval(&block)
|
355
|
+
|
356
|
+
old_buffer << to_s
|
357
|
+
@buffer = old_buffer
|
358
|
+
end
|
215
359
|
end
|
216
360
|
|
217
361
|
# Implements an HTML renderer
|
data/lib/papercraft/version.rb
CHANGED
data/lib/papercraft.rb
CHANGED
@@ -16,6 +16,12 @@ module Papercraft
|
|
16
16
|
# by adding namespaced methods to emplates. An extension is implemented as a
|
17
17
|
# Ruby module containing one or more methods. Each method in the extension
|
18
18
|
# module can be used to render a specific HTML element or a set of elements.
|
19
|
+
#
|
20
|
+
# This is a convenience method. For more information on using Papercraft
|
21
|
+
# extensions, see `Papercraft::Renderer::extension`
|
22
|
+
#
|
23
|
+
# @param map [Hash] hash mapping methods to extension modules
|
24
|
+
# @return [void]
|
19
25
|
def self.extension(map)
|
20
26
|
Renderer.extension(map)
|
21
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: papercraft
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.13'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sharon Rosner
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: escape_utils
|
@@ -122,7 +122,7 @@ dependencies:
|
|
122
122
|
- - '='
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: 2.0.9
|
125
|
-
description:
|
125
|
+
description:
|
126
126
|
email: sharon@noteflakes.com
|
127
127
|
executables: []
|
128
128
|
extensions: []
|
@@ -147,7 +147,7 @@ metadata:
|
|
147
147
|
documentation_uri: https://www.rubydoc.info/gems/papercraft
|
148
148
|
homepage_uri: https://github.com/digital-fabric/papercraft
|
149
149
|
changelog_uri: https://github.com/digital-fabric/papercraft/blob/master/CHANGELOG.md
|
150
|
-
post_install_message:
|
150
|
+
post_install_message:
|
151
151
|
rdoc_options:
|
152
152
|
- "--title"
|
153
153
|
- Papercraft
|
@@ -166,8 +166,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
166
|
- !ruby/object:Gem::Version
|
167
167
|
version: '0'
|
168
168
|
requirements: []
|
169
|
-
rubygems_version: 3.
|
170
|
-
signing_key:
|
169
|
+
rubygems_version: 3.3.3
|
170
|
+
signing_key:
|
171
171
|
specification_version: 4
|
172
172
|
summary: 'Papercraft: component-based HTML templating for Ruby'
|
173
173
|
test_files: []
|