papercraft 0.25 → 0.26

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f23902f7431dcbd6c1180e26f612ae5099d4e7f2ba468dd0e0e5e9a76e324ef4
4
- data.tar.gz: 5bf38e4faea0ab6c6145d769b24b416e467836b29c6d8856bb96442f86b43bfe
3
+ metadata.gz: 2b64cff374a2d8fdf50de978d3a4b0ceb6fb939741c00ed7cf63154af7cf6732
4
+ data.tar.gz: 67c0fc2aa65a1056f04c378ebe50a0a433bb1cabba75a1fbd522d60604ddcd1d
5
5
  SHA512:
6
- metadata.gz: 69ed90f079f1db073bbc18b1b631c13cbcd2db5dae9e512781046fb7fa5ab0a164653137b28c201f48671d77e4d08ff1da3818381aca5d3686d61eb185f8a586
7
- data.tar.gz: 85681c583e3e145ca27a52638e0d3370fa94462d5baaae8cccb477a1a2938ad56f3040ee4f6cceb2757b49019890bcae505bcdc008e2ff0931987f09ab9eb91a
6
+ metadata.gz: 690eaec575b4ad635fc913609197d166841536811b1e1d4314d763c6c37fb520b9fc8f2f19d50f4a0cfd242a98b69d59aebf16eaa16f4298bfc74ac0040b516a
7
+ data.tar.gz: 66a468eeee29e5c2b9b9e7a2f3adc605fa2f9648ade7a0aa444a798d896de3eb8c5aa02c2e79bf27780769f86d2b1960b8a111be1d8ed6604af739cab5931adb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.26 2023-01-13
2
+
3
+ - Add support for namespaced local extensions using `#extend`
4
+
1
5
  ## 0.25 2023-01-12
2
6
 
3
7
  - Implement `#def_tag` for defining custom tags inline
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
- ## Inline Helper Methods
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
- ## Bundled Extensions
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
- ### SOAP Extension
779
+ #### SOAP Extension
746
780
 
747
781
  > The SOAP extension was contributed by [@aemadrid](https://github.com/aemadrid).
748
782
 
@@ -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
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Papercraft
4
- VERSION = '0.25'
4
+ VERSION = '0.26'
5
5
  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.25'
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-12 00:00:00.000000000 Z
11
+ date: 2023-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils