papercraft 0.25 → 0.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +37 -3
- data/lib/papercraft/tags.rb +50 -0
- 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: 2b64cff374a2d8fdf50de978d3a4b0ceb6fb939741c00ed7cf63154af7cf6732
|
4
|
+
data.tar.gz: 67c0fc2aa65a1056f04c378ebe50a0a433bb1cabba75a1fbd522d60604ddcd1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 690eaec575b4ad635fc913609197d166841536811b1e1d4314d763c6c37fb520b9fc8f2f19d50f4a0cfd242a98b69d59aebf16eaa16f4298bfc74ac0040b516a
|
7
|
+
data.tar.gz: 66a468eeee29e5c2b9b9e7a2f3adc605fa2f9648ade7a0aa444a798d896de3eb8c5aa02c2e79bf27780769f86d2b1960b8a111be1d8ed6604af739cab5931adb
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -86,6 +86,7 @@ hello.render('world')
|
|
86
86
|
- [XML Templates](#xml-templates)
|
87
87
|
- [JSON Templates](#json-templates)
|
88
88
|
- [Papercraft Extensions](#papercraft-extensions)
|
89
|
+
- [Extending Specific Templates](#extending-specific-templates)
|
89
90
|
- [Inline Helper Methods](#inline-helper-methods)
|
90
91
|
- [Bundled Extensions](#bundled-extensions)
|
91
92
|
- [API Reference](#api-reference)
|
@@ -682,7 +683,40 @@ Papercraft.html {
|
|
682
683
|
}
|
683
684
|
```
|
684
685
|
|
685
|
-
|
686
|
+
### Extending Specific Templates
|
687
|
+
|
688
|
+
Sometimes you wish to extend a specific template locally, without the extension
|
689
|
+
API being available to other templates. To do this you can use `#extend`:
|
690
|
+
|
691
|
+
```ruby
|
692
|
+
module CustomTags
|
693
|
+
def label(text)
|
694
|
+
span text, class: 'label'
|
695
|
+
end
|
696
|
+
end
|
697
|
+
|
698
|
+
Papercraft.html {
|
699
|
+
extend CustomTags
|
700
|
+
|
701
|
+
label 'foo'
|
702
|
+
}
|
703
|
+
```
|
704
|
+
|
705
|
+
The extension is in effect as long as the template is processing, so it is also
|
706
|
+
accessible to any sub templates that are emitted.
|
707
|
+
|
708
|
+
Local extensions can also be namespaced by passing `#extend` a hash mapping
|
709
|
+
namespaces to modules:
|
710
|
+
|
711
|
+
```ruby
|
712
|
+
Papercraft.html {
|
713
|
+
extend custom: CustomTags
|
714
|
+
|
715
|
+
custom.label 'foo'
|
716
|
+
}
|
717
|
+
```
|
718
|
+
|
719
|
+
### Inline Helper Methods
|
686
720
|
|
687
721
|
In addition to proper extensions defined in modules, you can also define
|
688
722
|
individual extension methods inline in your Papercraft templates. You can do
|
@@ -733,7 +767,7 @@ def_tag(:section) do |title, &inner|
|
|
733
767
|
end
|
734
768
|
```
|
735
769
|
|
736
|
-
|
770
|
+
### Bundled Extensions
|
737
771
|
|
738
772
|
Papercraft comes bundled with a few extensions that address common use cases.
|
739
773
|
All bundled extensions are namespaced under `Papercraft::Extensions`, and must
|
@@ -742,7 +776,7 @@ be specifically required in order to be available to templates.
|
|
742
776
|
For all bundled Papercraft extensions, there's no need to call
|
743
777
|
`Papercraft.extension`, requiring the extension is sufficient.
|
744
778
|
|
745
|
-
|
779
|
+
#### SOAP Extension
|
746
780
|
|
747
781
|
> The SOAP extension was contributed by [@aemadrid](https://github.com/aemadrid).
|
748
782
|
|
data/lib/papercraft/tags.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative './extension_proxy'
|
4
|
+
|
3
5
|
module Papercraft
|
4
6
|
# Markup (HTML/XML) extensions
|
5
7
|
module Tags
|
@@ -203,6 +205,43 @@ module Papercraft
|
|
203
205
|
self.class.define_method(sym, &block)
|
204
206
|
end
|
205
207
|
|
208
|
+
alias_method :orig_extend, :extend
|
209
|
+
|
210
|
+
# Extends the template with the provided module or map of modules. When
|
211
|
+
# given a module, the template body will be extended with the module,
|
212
|
+
# and will have access to all the module's methods:
|
213
|
+
#
|
214
|
+
# module CustomTags
|
215
|
+
# def label(text)
|
216
|
+
# span text, class: 'label'
|
217
|
+
# end
|
218
|
+
# end
|
219
|
+
#
|
220
|
+
# Papercraft.html {
|
221
|
+
# extend CustomTags
|
222
|
+
# label('foo')
|
223
|
+
# }
|
224
|
+
#
|
225
|
+
# When given a hash, each module in the hash is namespaced, and can be
|
226
|
+
# accessed using its key:
|
227
|
+
#
|
228
|
+
# Papercraft.html {
|
229
|
+
# extend custom: CustomTags
|
230
|
+
# custom.label('foo')
|
231
|
+
# }
|
232
|
+
#
|
233
|
+
# @param ext [Module, Hash] extension module or hash mapping symbols to modules
|
234
|
+
# @return [Object] self
|
235
|
+
def extend(ext)
|
236
|
+
if ext.is_a?(Module)
|
237
|
+
orig_extend(ext)
|
238
|
+
else
|
239
|
+
ext.each do |sym, mod|
|
240
|
+
define_extension_method(sym, mod)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
206
245
|
private
|
207
246
|
|
208
247
|
# Defines a method that emits the given tag based on a constant. The
|
@@ -232,6 +271,17 @@ module Papercraft
|
|
232
271
|
self.class.class_eval(code, __FILE__, S_TAG_METHOD_LINE)
|
233
272
|
end
|
234
273
|
|
274
|
+
# Defines a namespace referring to the given module.
|
275
|
+
#
|
276
|
+
# @param sym [Symbol] namespace
|
277
|
+
# @param mod [Module] module
|
278
|
+
# @return [void]
|
279
|
+
def define_extension_method(sym, mod)
|
280
|
+
self.singleton_class.define_method(sym) do
|
281
|
+
(@extension_proxies ||= {})[mod] ||= ExtensionProxy.new(self, mod)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
235
285
|
# Emits an arbitrary object by converting it to string, then adding it to
|
236
286
|
# the internal buffer. This method is called internally by `Renderer#emit`.
|
237
287
|
#
|
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.
|
4
|
+
version: '0.26'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sharon Rosner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: escape_utils
|