papercraft 0.24 → 0.26

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: f90c4ea6ac9d3090ae45d79f7f4a58668ccf7f8ae5d4dfa2a727b44a82699eb9
4
- data.tar.gz: 10ff5bc3df10c10b0a968921517d2e0c18a61973dd97b88e95eef38670b9e451
3
+ metadata.gz: 2b64cff374a2d8fdf50de978d3a4b0ceb6fb939741c00ed7cf63154af7cf6732
4
+ data.tar.gz: 67c0fc2aa65a1056f04c378ebe50a0a433bb1cabba75a1fbd522d60604ddcd1d
5
5
  SHA512:
6
- metadata.gz: '02461962ba174678a5b4a6ec550a8faa2f643fe87234e41513f7b49d798e3f3b2ade02adcfb0fa4961124a74484ddddea9437cb02eec98e9e897721729c0ef63'
7
- data.tar.gz: f99728bb218382a040f67cbf2fd9652c33b3e0648efefbc695ceee708006758b6ec249748f3e3d41b065bf3b2fb28014b1ae61e02f7d3b01231e928f028e6ebd
6
+ metadata.gz: 690eaec575b4ad635fc913609197d166841536811b1e1d4314d763c6c37fb520b9fc8f2f19d50f4a0cfd242a98b69d59aebf16eaa16f4298bfc74ac0040b516a
7
+ data.tar.gz: 66a468eeee29e5c2b9b9e7a2f3adc605fa2f9648ade7a0aa444a798d896de3eb8c5aa02c2e79bf27780769f86d2b1960b8a111be1d8ed6604af739cab5931adb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.26 2023-01-13
2
+
3
+ - Add support for namespaced local extensions using `#extend`
4
+
5
+ ## 0.25 2023-01-12
6
+
7
+ - Implement `#def_tag` for defining custom tags inline
8
+
1
9
  ## 0.24 2022-03-19
2
10
 
3
11
  - Fix usage of const components (#13)
data/README.md CHANGED
@@ -86,6 +86,8 @@ 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)
90
+ - [Inline Helper Methods](#inline-helper-methods)
89
91
  - [Bundled Extensions](#bundled-extensions)
90
92
  - [API Reference](#api-reference)
91
93
 
@@ -681,7 +683,91 @@ Papercraft.html {
681
683
  }
682
684
  ```
683
685
 
684
- ## Bundled Extensions
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
720
+
721
+ In addition to proper extensions defined in modules, you can also define
722
+ individual extension methods inline in your Papercraft templates. You can do
723
+ this using any of the following techniques:
724
+
725
+ 1. Define a method in the template body:
726
+
727
+ ```ruby
728
+ Papercraft.html {
729
+ def label(text)
730
+ span text, class: 'label'
731
+ end
732
+
733
+ label 'foo'
734
+ label 'bar'
735
+ }
736
+ ```
737
+
738
+ 2. Use `def_tag` to define a custom tag:
739
+
740
+ ```ruby
741
+ Papercraft.html {
742
+ def_tag(:label) { |text| span text, class: 'label' }
743
+
744
+ label 'foo'
745
+ label 'bar'
746
+ }
747
+ ```
748
+
749
+ Note that using any of the above methods you can also create custom components
750
+ that take a block with inner HTML:
751
+
752
+ ```ruby
753
+ # using def
754
+ def section(title, &inner)
755
+ div {
756
+ h1 title
757
+ emit inner
758
+ }
759
+ end
760
+
761
+ # using def_tag
762
+ def_tag(:section) do |title, &inner|
763
+ div {
764
+ h1 title
765
+ emit inner
766
+ }
767
+ end
768
+ ```
769
+
770
+ ### Bundled Extensions
685
771
 
686
772
  Papercraft comes bundled with a few extensions that address common use cases.
687
773
  All bundled extensions are namespaced under `Papercraft::Extensions`, and must
@@ -690,7 +776,7 @@ be specifically required in order to be available to templates.
690
776
  For all bundled Papercraft extensions, there's no need to call
691
777
  `Papercraft.extension`, requiring the extension is sufficient.
692
778
 
693
- ### SOAP Extension
779
+ #### SOAP Extension
694
780
 
695
781
  > The SOAP extension was contributed by [@aemadrid](https://github.com/aemadrid).
696
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
@@ -180,6 +182,66 @@ module Papercraft
180
182
  @buffer << escape_text(data)
181
183
  end
182
184
 
185
+ # Defines a custom tag. This is handy for defining helper or extension
186
+ # methods inside the template body.
187
+ #
188
+ # Papercraft.html {
189
+ # def_tag(:section) { |title, &inner|
190
+ # div {
191
+ # h1 title
192
+ # emit inner
193
+ # }
194
+ # }
195
+ #
196
+ # section('Foo') {
197
+ # p 'Bar'
198
+ # }
199
+ # }
200
+ #
201
+ # @param tag [Symbol, String] tag/method name
202
+ # @param block [Proc] method body
203
+ # @return [void]
204
+ def def_tag(sym, &block)
205
+ self.class.define_method(sym, &block)
206
+ end
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
+
183
245
  private
184
246
 
185
247
  # Defines a method that emits the given tag based on a constant. The
@@ -209,6 +271,17 @@ module Papercraft
209
271
  self.class.class_eval(code, __FILE__, S_TAG_METHOD_LINE)
210
272
  end
211
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
+
212
285
  # Emits an arbitrary object by converting it to string, then adding it to
213
286
  # the internal buffer. This method is called internally by `Renderer#emit`.
214
287
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Papercraft
4
- VERSION = '0.24'
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.24'
4
+ version: '0.26'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-19 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
@@ -122,7 +122,7 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: 2.0.9
125
- description:
125
+ description:
126
126
  email: sharon@noteflakes.com
127
127
  executables: []
128
128
  extensions: []
@@ -152,7 +152,7 @@ metadata:
152
152
  documentation_uri: https://www.rubydoc.info/gems/papercraft
153
153
  homepage_uri: https://github.com/digital-fabric/papercraft
154
154
  changelog_uri: https://github.com/digital-fabric/papercraft/blob/master/CHANGELOG.md
155
- post_install_message:
155
+ post_install_message:
156
156
  rdoc_options:
157
157
  - "--title"
158
158
  - Papercraft
@@ -171,8 +171,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  - !ruby/object:Gem::Version
172
172
  version: '0'
173
173
  requirements: []
174
- rubygems_version: 3.3.3
175
- signing_key:
174
+ rubygems_version: 3.4.1
175
+ signing_key:
176
176
  specification_version: 4
177
177
  summary: 'Papercraft: component-based HTML templating for Ruby'
178
178
  test_files: []