papercraft 2.20 → 2.22

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2aaeb902deb28df3d773705b69f22c34d8496009254f6bd8106419651e13e46d
4
- data.tar.gz: 7d496c4b973b9bc0f96d608956cd8f6941345e751a37f15244f155e9698ac9ab
3
+ metadata.gz: c79e7a01733a8d28a981d2d499d660eae12c3c0e26981447769da667de559d46
4
+ data.tar.gz: 2e7d88cfb86953b0029ec8a93c686265f70970223980462997a9b92e1e97f754
5
5
  SHA512:
6
- metadata.gz: 8f1c40175e5ec13cac0573ad58c491a935e7cd0c7198b79b1a37d4b662a304f8468b55b2b6a59ef59816d49d047772a06d3bb8440d2bdc24ba8a8c0e1f0e04f9
7
- data.tar.gz: 5bad88609d161535014bcbf7cd6dd26775c3ec1f0505a927bcdc65eaea58a623387407e6d5920e3eb8b61982ce592732bf1d14f062ef90e09caa960e8bf35927
6
+ metadata.gz: 4d8c2fe6c0a663ff5fc6d2a6c10267108b9db138a65c049eebe18042e94217da6b3d217ead201fc7e919f93f355783b04e81931988f11c9508a62ed0e7e812cd
7
+ data.tar.gz: 283b0225f7febd9f1559e0c181a090af6a41d6ed65f7fd1a7e32a0568b1ef5a9b35502b669d1ec796b34c679861e172d806d1acf709f86dd79c85936952f6368
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 2.22 2025-10-08
2
+
3
+ - Use `prepend` instead of `include` to extend the `Proc` class
4
+
5
+ # 2.21 2025-10-08
6
+
7
+ - Fix `Proc#apply` parameter handling
8
+ - Put Proc extensions in separate module, included into Proc
9
+
1
10
  # 2.20 2025-10-08
2
11
 
3
12
  - Raise error on void element with child nodes or inner text
@@ -2,117 +2,127 @@
2
2
 
3
3
  require_relative './compiler'
4
4
 
5
- # Extensions to the Proc class.
6
- class ::Proc
7
- # Returns the compiled form code for the proc.
8
- #
9
- # @return [String] compiled proc code
10
- def compiled_code
11
- Papercraft::Compiler.compile_to_code(self).last
12
- end
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
- # Returns the source map for the compiled proc.
15
- #
16
- # @return [Array<String>] source map
17
- def source_map
18
- loc = source_location
19
- fn = compiled? ? loc.first : Papercraft::Compiler.source_location_to_fn(loc)
20
- Papercraft::Compiler.source_map_store[fn]
21
- end
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
- # Returns the AST for the proc.
24
- #
25
- # @return [Prism::Node] AST root
26
- def ast
27
- Sirop.to_ast(self)
28
- end
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
- # Returns true if proc is marked as compiled.
31
- #
32
- # @return [bool] is the proc marked as compiled
33
- def compiled?
34
- @is_compiled
35
- end
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
- # Marks the proc as compiled, i.e. can render directly and takes a string
38
- # buffer as first argument.
39
- #
40
- # @return [self]
41
- def compiled!
42
- @is_compiled = true
43
- self
44
- end
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
- # Returns the compiled proc for the given proc. If marked as compiled, returns
47
- # self.
48
- #
49
- # @param mode [Symbol] compilation mode (:html, :xml)
50
- # @return [Proc] compiled proc or self
51
- def compiled_proc(mode: :html)
52
- @compiled_proc ||= @is_compiled ? self : compile(mode:)
53
- end
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
- # Compiles the proc into the compiled form.
56
- #
57
- # @param mode [Symbol] compilation mode (:html, :xml)
58
- # @return [Proc] compiled proc
59
- def compile(mode: :html)
60
- Papercraft::Compiler.compile(self, mode:).compiled!
61
- rescue Sirop::Error
62
- raise Papercraft::Error, "Dynamically defined procs cannot be compiled"
63
- end
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
- # Renders the proc to HTML with the given arguments.
66
- #
67
- # @return [String] HTML string
68
- def render(*a, **b, &c)
69
- compiled_proc.(+'', *a, **b, &c)
70
- rescue Exception => e
71
- e.is_a?(Papercraft::Error) ? raise : raise(Papercraft.translate_backtrace(e))
72
- end
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
- # Renders the proc to XML with the given arguments.
75
- #
76
- # @return [String] XML string
77
- def render_xml(*a, **b, &c)
78
- compiled_proc(mode: :xml).(+'', *a, **b, &c)
79
- rescue Exception => e
80
- e.is_a?(Papercraft::Error) ? raise : raise(Papercraft.translate_backtrace(e))
81
- end
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
- # Renders the proc to HTML with the given arguments into the given buffer.
84
- #
85
- # @param buf [String] buffer
86
- # @return [String] HTML string
87
- def render_to_buffer(buf, *a, **b, &c)
88
- compiled_proc.(buf, *a, **b, &c)
89
- rescue Exception => e
90
- raise Papercraft.translate_backtrace(e)
91
- end
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
- # Returns a proc that applies the given arguments to the original proc.
94
- #
95
- # @return [Proc] applied proc
96
- def apply(*a, **b, &c)
97
- compiled = compiled_proc
98
- c_compiled = c&.compiled_proc
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
- ->(__buffer__, *x, **y, &z) {
101
- c_proc = c_compiled && ->(__buffer__, *d, **e) {
102
- c_compiled.(__buffer__, *a, *d, **b, **e, &z)
103
- }.compiled!
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
- compiled.(__buffer__, *a, *x, **b, **y, &c_proc)
106
- }.compiled!
107
- end
112
+ compiled.(__buffer__, *pos1, *pos2, **kw1, **kw2, &c_proc)
113
+ }.compiled!
114
+ end
108
115
 
109
- # Caches and returns the rendered HTML for the template with the given
110
- # arguments.
111
- #
112
- # @return [String] HTML string
113
- def render_cached(*args, **kargs, &block)
114
- @render_cache ||= {}
115
- key = args.empty? && kargs.empty? && !block ? nil : [args, kargs, block&.source_location]
116
- @render_cache[key] ||= render(*args, **kargs, &block)
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.prepend(Papercraft::ProcExtensions)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Papercraft
4
- VERSION = '2.20'
4
+ VERSION = '2.22'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papercraft
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.20'
4
+ version: '2.22'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner