papercraft 2.19 → 2.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/papercraft/compiler/tag_translator.rb +2 -0
- data/lib/papercraft/compiler.rb +4 -0
- data/lib/papercraft/proc_ext.rb +109 -99
- data/lib/papercraft/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c19544c09d7b8276b00634c3c3f018db6334d3e04ed630c8b5c6aa5b9e1fa881
|
4
|
+
data.tar.gz: f3ae23754232af2f87b66bebcb90aa319cb153171703e0367110c04c3e6ced26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2883f9ebdfa04b818419903c2c5e37ae9cde85da0dc559b78952fa7827851d6b54da7261fc04409cbd408a8afe5dd49bf94923bd68f1e9998afe9fd3ace959c2
|
7
|
+
data.tar.gz: 32e2512bc5feaf58619e568d5f0c5e0a4af131ad750eb57bc6846aaa253460c652f9915a51185366c95200697bd4625e7a1cd188e73fbd68ed687f4b9d03bd06
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
# 2.21 2025-10-08
|
2
|
+
|
3
|
+
- Fix `Proc#apply` parameter handling
|
4
|
+
- Put Proc extensions in separate module, included into Proc
|
5
|
+
|
6
|
+
# 2.20 2025-10-08
|
7
|
+
|
8
|
+
- Raise error on void element with child nodes or inner text
|
9
|
+
- Fix compilation of empty template
|
10
|
+
|
1
11
|
# 2.19 2025-10-08
|
2
12
|
|
3
13
|
- Use gem.coop in Gemfile
|
data/lib/papercraft/compiler.rb
CHANGED
@@ -165,6 +165,10 @@ module Papercraft
|
|
165
165
|
is_void = is_void_element?(tag)
|
166
166
|
is_raw_inner_text = is_raw_inner_text_element?(tag)
|
167
167
|
|
168
|
+
if is_void && (node.block || node.inner_text)
|
169
|
+
raise Papercraft::Error, "Void element #{tag} cannot contain child nodes or inner text"
|
170
|
+
end
|
171
|
+
|
168
172
|
emit_html(node.tag_location, format_html_tag_open(node.tag_location, tag, node.attributes))
|
169
173
|
return if is_void
|
170
174
|
|
data/lib/papercraft/proc_ext.rb
CHANGED
@@ -2,117 +2,127 @@
|
|
2
2
|
|
3
3
|
require_relative './compiler'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
module Papercraft
|
6
|
+
# Extensions to the Proc class.
|
7
|
+
module ProcExtensions
|
8
|
+
# Returns the compiled form code for the proc.
|
9
|
+
#
|
10
|
+
# @return [String] compiled proc code
|
11
|
+
def compiled_code
|
12
|
+
Papercraft::Compiler.compile_to_code(self).last
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
# Returns the source map for the compiled proc.
|
16
|
+
#
|
17
|
+
# @return [Array<String>] source map
|
18
|
+
def source_map
|
19
|
+
loc = source_location
|
20
|
+
fn = compiled? ? loc.first : Papercraft::Compiler.source_location_to_fn(loc)
|
21
|
+
Papercraft::Compiler.source_map_store[fn]
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
# Returns the AST for the proc.
|
25
|
+
#
|
26
|
+
# @return [Prism::Node] AST root
|
27
|
+
def ast
|
28
|
+
Sirop.to_ast(self)
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
# Returns true if proc is marked as compiled.
|
32
|
+
#
|
33
|
+
# @return [bool] is the proc marked as compiled
|
34
|
+
def compiled?
|
35
|
+
@is_compiled
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
# Marks the proc as compiled, i.e. can render directly and takes a string
|
39
|
+
# buffer as first argument.
|
40
|
+
#
|
41
|
+
# @return [self]
|
42
|
+
def compiled!
|
43
|
+
@is_compiled = true
|
44
|
+
self
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
# Returns the compiled proc for the given proc. If marked as compiled, returns
|
48
|
+
# self.
|
49
|
+
#
|
50
|
+
# @param mode [Symbol] compilation mode (:html, :xml)
|
51
|
+
# @return [Proc] compiled proc or self
|
52
|
+
def compiled_proc(mode: :html)
|
53
|
+
@compiled_proc ||= @is_compiled ? self : compile(mode:)
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
# Compiles the proc into the compiled form.
|
57
|
+
#
|
58
|
+
# @param mode [Symbol] compilation mode (:html, :xml)
|
59
|
+
# @return [Proc] compiled proc
|
60
|
+
def compile(mode: :html)
|
61
|
+
Papercraft::Compiler.compile(self, mode:).compiled!
|
62
|
+
rescue Sirop::Error
|
63
|
+
raise Papercraft::Error, "Dynamically defined procs cannot be compiled"
|
64
|
+
end
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
66
|
+
# Renders the proc to HTML with the given arguments.
|
67
|
+
#
|
68
|
+
# @return [String] HTML string
|
69
|
+
def render(*a, **b, &c)
|
70
|
+
compiled_proc.(+'', *a, **b, &c)
|
71
|
+
rescue Exception => e
|
72
|
+
e.is_a?(Papercraft::Error) ? raise : raise(Papercraft.translate_backtrace(e))
|
73
|
+
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
75
|
+
# Renders the proc to XML with the given arguments.
|
76
|
+
#
|
77
|
+
# @return [String] XML string
|
78
|
+
def render_xml(*a, **b, &c)
|
79
|
+
compiled_proc(mode: :xml).(+'', *a, **b, &c)
|
80
|
+
rescue Exception => e
|
81
|
+
e.is_a?(Papercraft::Error) ? raise : raise(Papercraft.translate_backtrace(e))
|
82
|
+
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
84
|
+
# Renders the proc to HTML with the given arguments into the given buffer.
|
85
|
+
#
|
86
|
+
# @param buf [String] buffer
|
87
|
+
# @return [String] HTML string
|
88
|
+
def render_to_buffer(buf, *a, **b, &c)
|
89
|
+
compiled_proc.(buf, *a, **b, &c)
|
90
|
+
rescue Exception => e
|
91
|
+
raise Papercraft.translate_backtrace(e)
|
92
|
+
end
|
92
93
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
94
|
+
# Returns a proc that applies the given arguments to the original proc. The
|
95
|
+
# returned proc calls the *compiled* form of the proc, merging the
|
96
|
+
# positional and keywords parameters passed to `#apply` with parameters
|
97
|
+
# passed to the applied proc. If a block is given, it is wrapped in a proc
|
98
|
+
# that passed merged parameters to the block.
|
99
|
+
#
|
100
|
+
# @param *pos1 [Array<any>] applied positional parameters
|
101
|
+
# @param **kw1 [Hash<any, any] applied keyword parameters
|
102
|
+
# @return [Proc] applied proc
|
103
|
+
def apply(*pos1, **kw1, &block)
|
104
|
+
compiled = compiled_proc
|
105
|
+
c_compiled = block&.compiled_proc
|
99
106
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
107
|
+
->(__buffer__, *pos2, **kw2, &block2) {
|
108
|
+
c_proc = c_compiled && ->(__buffer__, *pos3, **kw3) {
|
109
|
+
c_compiled.(__buffer__, *pos3, **kw3, &block2)
|
110
|
+
}.compiled!
|
104
111
|
|
105
|
-
|
106
|
-
|
107
|
-
|
112
|
+
compiled.(__buffer__, *pos1, *pos2, **kw1, **kw2, &c_proc)
|
113
|
+
}.compiled!
|
114
|
+
end
|
108
115
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
116
|
+
# Caches and returns the rendered HTML for the template with the given
|
117
|
+
# arguments.
|
118
|
+
#
|
119
|
+
# @return [String] HTML string
|
120
|
+
def render_cached(*args, **kargs, &block)
|
121
|
+
@render_cache ||= {}
|
122
|
+
key = args.empty? && kargs.empty? && !block ? nil : [args, kargs, block&.source_location]
|
123
|
+
@render_cache[key] ||= render(*args, **kargs, &block)
|
124
|
+
end
|
117
125
|
end
|
118
126
|
end
|
127
|
+
|
128
|
+
::Proc.include(Papercraft::ProcExtensions)
|
data/lib/papercraft/version.rb
CHANGED