papercraft 0.11 → 0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +3 -5
- data/lib/papercraft/html.rb +57 -4
- data/lib/papercraft/renderer.rb +75 -5
- data/lib/papercraft/version.rb +1 -1
- data/lib/papercraft.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be072094c35a6f89327b9c88b90264ad3db61cb43311d0e3baca865f403fdbc5
|
4
|
+
data.tar.gz: 7e565933dd83c48c05bac8c648cbe9b0007bc43f8fc5bd862af46dcd00dfb328
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b8da36fb3c7298e8121b9c612cb81752609e1e3d1f01a94e4bf939864b29de718884989203077c5c1392926a54205c921adbcaa1bf3aa75d84694b7b4db4dcc
|
7
|
+
data.tar.gz: 778163311cb86ee425c4617ff54b10e8853d9b92404ef29abac848a4252818386b2e23058c355678c286a5bae3db9d4a7bfb182acb0a4aacf00a042431cc196a
|
data/CHANGELOG.md
CHANGED
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,7 +366,7 @@ 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
|
@@ -430,7 +428,7 @@ page = default_layout.apply {
|
|
430
428
|
|
431
429
|
Papercraft extensions are modules that contain one or more methods that can be
|
432
430
|
used to render complex HTML components. Extension modules can be used by
|
433
|
-
installing them as a namespaced extension using `Papercraft
|
431
|
+
installing them as a namespaced extension using `Papercraft::extension`.
|
434
432
|
Extensions are particularly useful when you work with CSS frameworks such as
|
435
433
|
[Bootstrap](https://getbootstrap.com/), [Tailwind](https://tailwindui.com/) or
|
436
434
|
[Primer](https://primer.style/).
|
@@ -476,7 +474,7 @@ end
|
|
476
474
|
Papercraft.extension(bootstrap: BootstrapComponents)
|
477
475
|
```
|
478
476
|
|
479
|
-
The call to `Papercraft
|
477
|
+
The call to `Papercraft::extension` lets us access the different methods of
|
480
478
|
`BootstrapComponents` by calling `#bootstrap` inside a template. With this,
|
481
479
|
we'll be able to express the above markup as follows:
|
482
480
|
|
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)
|
@@ -89,10 +110,12 @@ module Papercraft
|
|
89
110
|
@buffer
|
90
111
|
end
|
91
112
|
|
113
|
+
# The tag method template below is optimized for performance. Do not touch!
|
114
|
+
|
92
115
|
S_TAG_METHOD_LINE = __LINE__ + 1
|
93
116
|
S_TAG_METHOD = <<~EOF
|
94
|
-
S_TAG_%<TAG>s_PRE =
|
95
|
-
S_TAG_%<TAG>s_CLOSE =
|
117
|
+
S_TAG_%<TAG>s_PRE = %<tag_pre>s
|
118
|
+
S_TAG_%<TAG>s_CLOSE = %<tag_close>s
|
96
119
|
|
97
120
|
def %<tag>s(text = nil, **props, &block)
|
98
121
|
if text.is_a?(Hash) && props.empty?
|
@@ -119,6 +142,48 @@ module Papercraft
|
|
119
142
|
end
|
120
143
|
EOF
|
121
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
|
+
|
122
187
|
# Catches undefined tag method call and handles it by defining the method.
|
123
188
|
#
|
124
189
|
# @param sym [Symbol] HTML tag or component identifier
|
@@ -128,7 +193,12 @@ module Papercraft
|
|
128
193
|
# @return [void]
|
129
194
|
def method_missing(sym, *args, **opts, &block)
|
130
195
|
tag = sym.to_s
|
131
|
-
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
|
+
}
|
132
202
|
self.class.class_eval(code, __FILE__, S_TAG_METHOD_LINE)
|
133
203
|
send(sym, *args, **opts, &block)
|
134
204
|
end
|
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.12'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sharon Rosner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: escape_utils
|