papercraft 0.27 → 0.29

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: bd2f4f4d1ebc2cfc5718079d68c9393b3d95def9c02b057107425ddd5b45eaf3
4
- data.tar.gz: f3f82f29227e22e5e31b0d685bcab211ebbef0ecf8a4433d251cb1f204875cf7
3
+ metadata.gz: 1ca707074311c909d6e2b4aa86748f1ffbd5b614d73fb78e85985d174b69e263
4
+ data.tar.gz: f53dc4fa5088472ccc805f50938f25539950446fe14e97facb2cc346722d13fe
5
5
  SHA512:
6
- metadata.gz: 7389861bea04a92e06ce1a53a682ef3b4db1bf975bb7f5e63cef0ed6c1a4ebb4b92920a0bb4ea04eba24dc72b41c70ba8fac0d8aa605a2b5a720d7860198b7e0
7
- data.tar.gz: 2a617260deef291c56cd0c3fc1b275d47dd1dc95bdeeafc69289d630260480e49126c29f0ec77c177fd6c1032c633ba2568be1ffb767e13f91e51b5a9c298831
6
+ metadata.gz: 0d216f2c8db4beed8c7fe184d3b8e0a13ba8af4684d4e46f5506f0b1eb6c3bfaa0ad76b7a5b1b6e28a8ba37a730545ca00139fd0bf35b74628d117c504fc1e9c
7
+ data.tar.gz: 27ac04cf8b75b5d6abda511513618fb6ef20417365ac7b45855d909323db3c536204f85d719a44e2374b095d955f4eb0c25901290990dab861c908fa4d73142e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.29 2023-03-11
2
+
3
+ - Add Tilt integration (#15)
4
+
5
+ ## 0.28 2023-03-11
6
+
7
+ - Add `HTML#import_map`, `HTML#js_module` methods
8
+
1
9
  ## 0.27 2023-01-19
2
10
 
3
11
  - Fix rendering of HTML void element tags
data/README.md CHANGED
@@ -66,6 +66,7 @@ hello.render('world')
66
66
  ## Table of Content
67
67
 
68
68
  - [Installing Papercraft](#installing-papercraft)
69
+ - [Using with Tilt](#using-with-tilt)
69
70
  - [Basic Usage](#basic-usage)
70
71
  - [Adding Tags](#adding-tags)
71
72
  - [Tag and Attribute Formatting](#tag-and-attribute-formatting)
@@ -126,6 +127,45 @@ Rendering a template is done using `#render`:
126
127
  html.render #=> "<div id="greeter"><p>Hello!</p></div>"
127
128
  ```
128
129
 
130
+ ## Using with Tilt
131
+
132
+ Papercraft templates can also be rendered using Tilt:
133
+
134
+ ```ruby
135
+ require 'tilt/papercraft'
136
+
137
+ # loading from a file (with a .papercraft extension)
138
+ template = Tilt.new('mytemplate.papercraft')
139
+ template.render
140
+
141
+ # defining a template inline:
142
+ template = Tilt['papercraft'].new { <<~RUBY
143
+ h1 'Hello'
144
+ RUBY
145
+ }
146
+ template.render
147
+ ```
148
+
149
+ When rendering using Tilt, the following local variables are available to the
150
+ template: `scope`, `locals` and `block`:
151
+
152
+ ```ruby
153
+ template = Tilt['papercraft'].new { <<~RUBY
154
+ title scope.title
155
+
156
+ h1 locals[:message]
157
+
158
+ emit block if block
159
+ RUBY
160
+ }
161
+
162
+ def title
163
+ 'foo'
164
+ end
165
+
166
+ template.render(self, message: 'bar') { p 'this is a paragraph' }
167
+ ```
168
+
129
169
  ## Adding Tags
130
170
 
131
171
  Tags are added using unqualified method calls, and can be nested using blocks:
@@ -71,7 +71,47 @@ module Papercraft
71
71
  @buffer << '></script>'
72
72
  end
73
73
  end
74
+
75
+ # Returns a versioned URL for the given file spec.
76
+ #
77
+ # @param href [String] relative file path
78
+ # @param root_path [String] root path for file
79
+ # @param root_url [String] root URL
80
+ # @return [String] versioned URL
81
+ def versioned_file_href(href, root_path, root_url = '')
82
+ fn = File.join(root_path, href)
83
+ version = File.stat(fn).mtime.to_i rescue 0
84
+ "#{root_url}/#{href}?v=#{version}"
85
+ end
74
86
 
87
+ # Emits an import map scrit tag. If a hash is given, emits the hash as is.
88
+ # If a string is given, searches for all *.js files under the given path,
89
+ # and emits an import map including all found files, with versioned URLs.
90
+ #
91
+ # @param root_path [String, Hash] root path or hash
92
+ # @param root_url [String] root URL to construct URLs against
93
+ # @return [void]
94
+ def import_map(root_path, root_url = '')
95
+ if root_path.is_a?(Hash)
96
+ script(root_path.to_json, type: 'importmap')
97
+ else
98
+ map = Dir["#{root_path}/*.js"].sort.each_with_object({}) do |fn, h|
99
+ name = File.basename(fn)
100
+ m = fn.match(/\/([^\/]+)\.js$/)
101
+ h[m[1]] = versioned_file_href(name, root_path, root_url)
102
+ end
103
+ script(map.to_json, type: 'importmap')
104
+ end
105
+ end
106
+
107
+ # Emits a script tag with type attribute set to module.
108
+ #
109
+ # @param code [String] JS code
110
+ # @return [void]
111
+ def js_module(code)
112
+ script code, type: 'module'
113
+ end
114
+
75
115
  # Converts and emits the given markdown. Papercraft uses
76
116
  # [Kramdown](https://github.com/gettalong/kramdown/) to do the Markdown to
77
117
  # HTML conversion. Optional Kramdown settings can be provided in order to
@@ -198,7 +198,6 @@ module Papercraft
198
198
  tag = sym.to_s
199
199
  if tag =~ /^[A-Z]/ && (Object.const_defined?(tag))
200
200
  define_const_tag_method(tag)
201
- # return send(tag, *args, **opts)
202
201
  else
203
202
  define_tag_method(tag)
204
203
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Papercraft
4
- VERSION = '0.27'
4
+ VERSION = '0.29'
5
5
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'papercraft'
4
+ require 'tilt'
5
+
6
+ # Tilt.
7
+ module Tilt
8
+ # Papercraft templating engine for Tilt
9
+ class PapercraftTemplate < Template
10
+ metadata[:mime_type] = 'text/html'
11
+
12
+ protected
13
+
14
+ def prepare
15
+ inner = eval("proc { |scope:, locals:, block:|\n#{data}\n}")
16
+ @template = Papercraft.html(&inner)
17
+ end
18
+
19
+ def evaluate(scope, locals, &block)
20
+ @template.render(scope: scope, locals: locals, block: block)
21
+ end
22
+ end
23
+
24
+ register(PapercraftTemplate, 'papercraft')
25
+ 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.27'
4
+ version: '0.29'
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-19 00:00:00.000000000 Z
11
+ date: 2023-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 2.0.9
117
+ version: 2.1.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 2.0.9
124
+ version: 2.1.0
125
125
  description:
126
126
  email: sharon@noteflakes.com
127
127
  executables: []
@@ -143,6 +143,7 @@ files:
143
143
  - lib/papercraft/template.rb
144
144
  - lib/papercraft/version.rb
145
145
  - lib/papercraft/xml.rb
146
+ - lib/tilt/papercraft.rb
146
147
  - papercraft.png
147
148
  homepage: http://github.com/digital-fabric/papercraft
148
149
  licenses: