papercraft 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/papercraft/component.rb +84 -4
- data/lib/papercraft/version.rb +1 -1
- 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: 309174da790e301142b03cc8aad3e966c6b1482d514da8d3910c2e84f6981af6
|
4
|
+
data.tar.gz: 64fb5b791fce05c0347ba21ccf70160323f70ad708ece9c14222b8544efdf8e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1f56c25e4854cb6b0ffce1d149d831d51d4141159a44fd6a962c27d9e0878b33e9438d9ad621c0c30a42dfa853b96a26b20c6ebe42243844be2b42d4e2901ef
|
7
|
+
data.tar.gz: 46952658d3e237f784cfd9b608c82b001fdd188b788c931f0a1a3669d5800d8ed3ef89623f88ff9a617237fbfbbd3a0f9f5b30bfc693b2e6f0b5659f66afa41b
|
data/lib/papercraft/component.rb
CHANGED
@@ -3,13 +3,14 @@
|
|
3
3
|
require_relative './html'
|
4
4
|
|
5
5
|
module Papercraft
|
6
|
+
|
6
7
|
# Component represents a distinct, reusable HTML template. A component can
|
7
8
|
# include other components, and also be nested inside other components.
|
8
9
|
#
|
9
10
|
# Since in Papercraft HTML is expressed using blocks (or procs,) the Component
|
10
11
|
# class is simply a special kind of Proc, which has some enhanced
|
11
12
|
# capabilities, allowing it to be easily composed in a variety of ways.
|
12
|
-
|
13
|
+
#
|
13
14
|
# Components are usually created using the global methods `H` or `X`, for HTML
|
14
15
|
# or XML templates, respectively:
|
15
16
|
#
|
@@ -23,9 +24,64 @@ module Papercraft
|
|
23
24
|
#
|
24
25
|
# In the component block, HTML elements are created by simply calling
|
25
26
|
# unqualified methods:
|
27
|
+
#
|
28
|
+
# page_layout = H { html5 { head { title 'foo'
|
29
|
+
# }
|
30
|
+
# body {
|
31
|
+
# h1 "Hello, world!"
|
32
|
+
# }
|
33
|
+
# }
|
34
|
+
# }
|
35
|
+
#
|
36
|
+
# Papercraft components can take explicit parameters in order to render
|
37
|
+
# dynamic content. This can be in the form of regular or named parameters. The
|
38
|
+
# `greeter` template shown above takes a single `name` parameter. Here's how a
|
39
|
+
# anchor component could be implemented with named parameters:
|
40
|
+
#
|
41
|
+
# anchor = H { |uri: , text: | a(text, href: uri) }
|
42
|
+
#
|
43
|
+
# The above component could later be rendered by passing the needed arguments:
|
44
|
+
#
|
45
|
+
# anchor.render(uri: 'https://example.com', text: 'Example')
|
46
|
+
#
|
47
|
+
# ## Component Composition
|
48
|
+
#
|
49
|
+
# A component can be included in another component using the `emit` method:
|
50
|
+
#
|
51
|
+
# links = H {
|
52
|
+
# emit anchor, uri: '/posts', text: 'Posts'
|
53
|
+
# emit anchor, uri: '/archive', text: 'Archive'
|
54
|
+
# emit anchor, uri: '/about', text: 'About'
|
55
|
+
# }
|
56
|
+
#
|
57
|
+
# Another way of composing components is to pass the components themselves as
|
58
|
+
# parameters:
|
59
|
+
#
|
60
|
+
# links = H { |anchors|
|
61
|
+
# anchors.each { |a| emit a }
|
62
|
+
# }
|
63
|
+
# links.render([
|
64
|
+
# anchor.apply(uri: '/posts', text: 'Posts'),
|
65
|
+
# anchor.apply(uri: '/archive', text: 'Archive'),
|
66
|
+
# anchor.apply(uri: '/about', text: 'About')
|
67
|
+
# ])
|
68
|
+
#
|
69
|
+
# The `#apply` method creates a new component, applying the given parameters
|
70
|
+
# such that the component can be rendered without parameters:
|
71
|
+
#
|
72
|
+
# links_with_anchors = links.apply([
|
73
|
+
# anchor.apply(uri: '/posts', text: 'Posts'),
|
74
|
+
# anchor.apply(uri: '/archive', text: 'Archive'),
|
75
|
+
# anchor.apply(uri: '/about', text: 'About')
|
76
|
+
# ])
|
77
|
+
# links_with_anchors.render
|
26
78
|
class Component < Proc
|
27
|
-
|
28
|
-
#
|
79
|
+
|
80
|
+
# Initializes a component with the given block. The rendering mode (HTML or
|
81
|
+
# XML) can be passed in the `mode:` parameter. If `mode:` is not specified,
|
82
|
+
# the component defaults to HTML.
|
83
|
+
#
|
84
|
+
# @param mode [:html, :xml] rendering mode
|
29
85
|
# @param block [Proc] nested HTML block
|
30
86
|
def initialize(mode: :html, &block)
|
31
87
|
@mode = mode
|
@@ -34,7 +90,9 @@ module Papercraft
|
|
34
90
|
|
35
91
|
H_EMPTY = {}.freeze
|
36
92
|
|
37
|
-
# Renders the
|
93
|
+
# Renders the template with the given parameters and or block, and returns
|
94
|
+
# the string result.
|
95
|
+
#
|
38
96
|
# @param context [Hash] context
|
39
97
|
# @return [String]
|
40
98
|
def render(*a, **b, &block)
|
@@ -51,6 +109,24 @@ module Papercraft
|
|
51
109
|
raise Papercraft::Error, e.message
|
52
110
|
end
|
53
111
|
|
112
|
+
# Creates a new component, applying the given parameters and or block to the
|
113
|
+
# current one. Application is one of the principal methods of composing
|
114
|
+
# components, particularly when passing inner components as blocks:
|
115
|
+
#
|
116
|
+
# article_wrapper = H {
|
117
|
+
# article {
|
118
|
+
# emit_yield
|
119
|
+
# }
|
120
|
+
# }
|
121
|
+
# wrapped_article = article_wrapper.apply {
|
122
|
+
# h1 'Article title'
|
123
|
+
# }
|
124
|
+
# wrapped_article.render #=> "<article><h1>Article title</h1></article>"
|
125
|
+
#
|
126
|
+
# @param *a [<any>] normal parameters
|
127
|
+
# @param **b [Hash] named parameters
|
128
|
+
# @param &block [Proc] inner block
|
129
|
+
# @return [Papercraft::Component] applied component
|
54
130
|
def apply(*a, **b, &block)
|
55
131
|
template = self
|
56
132
|
if block
|
@@ -64,6 +140,10 @@ module Papercraft
|
|
64
140
|
end
|
65
141
|
end
|
66
142
|
|
143
|
+
# Returns the Renderer class used for rendering the templates, according to
|
144
|
+
# the component's mode.
|
145
|
+
#
|
146
|
+
# @return [Papercraft::Renderer] Renderer used for rendering the component
|
67
147
|
def renderer_class
|
68
148
|
case @mode
|
69
149
|
when :html
|
data/lib/papercraft/version.rb
CHANGED
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.8.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sharon Rosner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-12-
|
11
|
+
date: 2021-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: escape_utils
|