jekyll-template 0.20.0 → 0.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jekyll/template.rb +176 -95
- data/lib/jekyll/template/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e8f60f5367a96ac7522046816cd83bd07c83e22
|
4
|
+
data.tar.gz: 380118afd1b0c1df088fc31dd6127197c062dc34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 199a1af8eae5f9fd5fcecaf7cb69a0ee9f587c839a0fc4ff1e08bd718fd369f258e18e3bb8e3d97bfc8b3a45e4b1aa6b5a17e657239b6a56e030adfd50453366
|
7
|
+
data.tar.gz: d9c1287559431faaf3572706daf907879a4dc7253a9f1d20a7e09c78aa5a46807a2ec99b9de49535f2b59eabf7993ed0359315274ba9dca1940ccae0ee7931b4
|
data/lib/jekyll/template.rb
CHANGED
@@ -6,9 +6,20 @@ module Jekyll
|
|
6
6
|
module Tags
|
7
7
|
class TemplateBlock < Liquid::Block
|
8
8
|
include Liquid::StandardFilters
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
|
10
|
+
CONTEXT_NAME = "template"
|
11
|
+
CONTEXT_CACHE_NAME = :cached_templates
|
12
|
+
CONTEXT_DATA_NAME = :template_data
|
13
|
+
CONTEXT_SCOPE_NAME = :template_data_scope
|
14
|
+
CONTEXT_STORE_NAME = :template_data_store
|
15
|
+
|
16
|
+
PROPS_NAME = "props"
|
17
|
+
TEMPLATE_DIR = "_templates"
|
18
|
+
|
19
|
+
LIQUID_SYNTAX_REGEXP = /(#{Liquid::QuotedFragment}+)?/
|
20
|
+
PROPS_REGEXP = /#{PROPS_NAME}\./
|
21
|
+
WHITESPACE_REGEXP = %r!^\s*!m
|
22
|
+
# Source
|
12
23
|
# https://github.com/jekyll/jekyll/blob/35c5e073625100b0f8f8eab6f7da6cb6d5734930/lib/jekyll/document.rb
|
13
24
|
YAML_FRONT_MATTER_REGEXP = %r!(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m
|
14
25
|
|
@@ -16,30 +27,34 @@ module Jekyll
|
|
16
27
|
# Description: Extends Liquid's default initialize method.
|
17
28
|
def initialize(tag_name, markup, tokens)
|
18
29
|
super
|
19
|
-
@site = false
|
20
|
-
|
21
|
-
# @template_name = markup
|
22
|
-
if markup =~ Syntax
|
23
30
|
|
24
|
-
|
31
|
+
if markup =~ LIQUID_SYNTAX_REGEXP
|
25
32
|
@attributes = {}
|
33
|
+
@context = false
|
34
|
+
@id = rand(36**8).to_s(36).freeze
|
35
|
+
@props = {}
|
26
36
|
@sanitize = false
|
27
|
-
@
|
37
|
+
@site = false
|
38
|
+
@template_name = $1.freeze
|
39
|
+
|
40
|
+
@compressor = HtmlCompressor::Compressor.new({
|
41
|
+
:remove_comments => true
|
42
|
+
}).freeze
|
28
43
|
|
29
44
|
# Parse parameters
|
30
45
|
# Source: https://gist.github.com/jgatjens/8925165
|
31
46
|
markup.scan(Liquid::TagAttributes) do |key, value|
|
32
|
-
|
47
|
+
if (value =~ PROPS_REGEXP) != nil
|
48
|
+
@attributes[key] = value
|
49
|
+
else
|
50
|
+
@attributes[key] = Liquid::Expression.parse(value)
|
51
|
+
end
|
33
52
|
end
|
34
53
|
else
|
35
54
|
raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
|
36
55
|
end
|
37
56
|
end
|
38
57
|
|
39
|
-
def id
|
40
|
-
@id
|
41
|
-
end
|
42
|
-
|
43
58
|
# blank?
|
44
59
|
# Description: Override's Liquid's default blank checker. This allows
|
45
60
|
# for templates to be used without passing inner content.
|
@@ -47,42 +62,118 @@ module Jekyll
|
|
47
62
|
false
|
48
63
|
end
|
49
64
|
|
65
|
+
# template_store_data(data = Array)
|
66
|
+
# Description: Stores/updates the template data in cache
|
67
|
+
# Returns: Hash of the template store data
|
68
|
+
def template_store_data(data = {})
|
69
|
+
@context.registers[CONTEXT_STORE_NAME] ||= {}
|
70
|
+
unless @context.registers[CONTEXT_STORE_NAME].key?(@id)
|
71
|
+
@context.registers[CONTEXT_STORE_NAME][@id] = {
|
72
|
+
"id": @id,
|
73
|
+
"index": @context.registers[CONTEXT_STORE_NAME].length,
|
74
|
+
"template_name": @template_name
|
75
|
+
}
|
76
|
+
end
|
77
|
+
@context.registers[CONTEXT_STORE_NAME][@id] = @context.registers[CONTEXT_STORE_NAME][@id].merge(data)
|
78
|
+
end
|
79
|
+
|
80
|
+
# prop?
|
81
|
+
# Description: Determines if the variable is a template.props key
|
82
|
+
# Return: Boolean
|
83
|
+
def prop?(variable = "")
|
84
|
+
(variable =~ PROPS_REGEXP) != nil
|
85
|
+
end
|
86
|
+
|
87
|
+
# prop(data = Hash, value = String)
|
88
|
+
# Description: Returns the props value
|
89
|
+
def prop(data, value = "")
|
90
|
+
index = data[:index]
|
91
|
+
value = data[value.gsub(PROPS_REGEXP, "")]
|
92
|
+
if value and prop?(value) and index > 0
|
93
|
+
store = @context.registers[CONTEXT_STORE_NAME]
|
94
|
+
previous_scope = store[store.keys[index - 1]]
|
95
|
+
prop(previous_scope, value)
|
96
|
+
else
|
97
|
+
value
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# evaluate_props
|
102
|
+
# Description: Evaluates props that are being passed into the template.
|
103
|
+
def evaluate_props()
|
104
|
+
store = @context.registers[CONTEXT_STORE_NAME]
|
105
|
+
data = store[@id]
|
106
|
+
index = data[:index]
|
107
|
+
|
108
|
+
if (index > 0)
|
109
|
+
parent = store[store.keys[index - 1]]
|
110
|
+
# Update the data scope
|
111
|
+
@context[CONTEXT_SCOPE_NAME] = parent
|
112
|
+
data.each do |key, value|
|
113
|
+
if prop?(value)
|
114
|
+
value = prop(parent, value)
|
115
|
+
if value
|
116
|
+
@props[key] = value
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
50
123
|
# render
|
51
124
|
# Description: Extends Liquid's default render method. This method also
|
52
125
|
# adds additional features:
|
53
126
|
# - YAML front-matter parsing and handling
|
54
|
-
# - properly handles indentation and whitespace (resolves
|
127
|
+
# - properly handles indentation and whitespace (resolves rendering issues)
|
55
128
|
# - ability to parse content as markdown vs. html
|
56
129
|
# - supports custom attributes to be used in template
|
57
130
|
def render(context)
|
58
|
-
|
59
|
-
@site = context.registers[:site]
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
131
|
+
@context = context
|
132
|
+
@site = @context.registers[:site]
|
133
|
+
|
134
|
+
template_store_data(@attributes)
|
135
|
+
|
136
|
+
# This allows for Jekyll intelligently re-render markup during
|
137
|
+
# incremental builds.
|
138
|
+
add_template_to_dependency(@template_name)
|
139
|
+
# Loading the template from cache/template directory
|
140
|
+
template = load_cached_template(@template_name)
|
65
141
|
|
66
|
-
|
67
|
-
|
142
|
+
# Props must be evaluated before super is initialized.
|
143
|
+
# This allows for props to be evaluated before they're parsed by Liquid.
|
144
|
+
evaluate_props()
|
68
145
|
|
146
|
+
content = super
|
147
|
+
|
148
|
+
# Return the parsed/normalized content
|
149
|
+
render_template(template, content)
|
150
|
+
end
|
151
|
+
|
152
|
+
# render_template(template = Liquid::Template, content = String)
|
153
|
+
# Description: Serializes the context to be rendered by Liquid. Also
|
154
|
+
# resets the context to ensure template data doesn't leak from
|
155
|
+
# the scope.
|
156
|
+
# Returns: String
|
157
|
+
def render_template(template, content)
|
69
158
|
# Define the default template attributes
|
70
159
|
# Source:
|
71
160
|
# https://github.com/Shopify/liquid/blob/9a7778e52c37965f7b47673da09cfb82856a6791/lib/liquid/tags/include.rb
|
72
|
-
context[
|
73
|
-
context["partial"] = true
|
74
|
-
context["template"] = Hash.new
|
161
|
+
@context[CONTEXT_NAME] = Hash.new
|
75
162
|
|
76
163
|
# Parse and extend template's front-matter with content front-matter
|
77
164
|
update_attributes(get_front_matter(content))
|
165
|
+
# Add props
|
166
|
+
update_attributes(@props)
|
167
|
+
# Update the template's store data
|
168
|
+
template_store_data(@attributes)
|
78
169
|
|
79
|
-
# Setting template attributes from @attributes
|
170
|
+
# Setting context's template attributes from @attributes
|
80
171
|
# This allows for @attributes to be used within the template as
|
81
172
|
# {{ template.atttribute_name }}
|
82
|
-
if @attributes
|
173
|
+
if @attributes.length
|
83
174
|
@attributes.each do |key, value|
|
84
|
-
val = context.evaluate(value)
|
85
|
-
context[
|
175
|
+
val = @context.evaluate(value)
|
176
|
+
@context[CONTEXT_NAME][key] = val
|
86
177
|
|
87
178
|
# Adjust sanitize if parse: html
|
88
179
|
if (key == "parse") && (val == "html")
|
@@ -90,69 +181,70 @@ module Jekyll
|
|
90
181
|
end
|
91
182
|
end
|
92
183
|
end
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
store_template_data(
|
97
|
-
content = compressor.compress(template.render(context))
|
98
|
-
reset_template_data(
|
184
|
+
|
185
|
+
# puts @attributes
|
186
|
+
@context[CONTEXT_NAME]["content"] = sanitize(strip_front_matter(content))
|
187
|
+
store_template_data()
|
188
|
+
content = @compressor.compress(template.render(@context))
|
189
|
+
reset_template_data()
|
99
190
|
|
100
191
|
content
|
101
192
|
end
|
102
193
|
|
103
|
-
# update_attributes(data)
|
194
|
+
# update_attributes(data = Hash)
|
104
195
|
# Description: Merges data with @attributes.
|
105
|
-
# @param data { hash }
|
106
196
|
def update_attributes(data)
|
107
197
|
if data
|
108
198
|
@attributes.merge!(data)
|
109
199
|
end
|
110
200
|
end
|
111
201
|
|
112
|
-
# store_template_data(
|
202
|
+
# store_template_data()
|
113
203
|
# Description: Works with reset_template_data. This is a work-around
|
114
204
|
# to ensure data stays in scope and isn't leaked from child->parent
|
115
205
|
# template.
|
116
|
-
def store_template_data(
|
117
|
-
context.registers[
|
118
|
-
|
119
|
-
|
120
|
-
store[@id] = context["template"]
|
206
|
+
def store_template_data()
|
207
|
+
@context.registers[CONTEXT_DATA_NAME] ||= {}
|
208
|
+
unless @context.registers[CONTEXT_DATA_NAME].key?(@id)
|
209
|
+
@context.registers[CONTEXT_DATA_NAME][@id] = @context[CONTEXT_NAME]
|
121
210
|
end
|
122
211
|
end
|
123
212
|
|
124
|
-
# reset_template_data(
|
213
|
+
# reset_template_data()
|
125
214
|
# Description: Works with store_template_data. This is a work-around
|
126
215
|
# to ensure data stays in scope and isn't leaked from child->parent
|
127
216
|
# template.
|
128
|
-
def reset_template_data(
|
129
|
-
context.registers[
|
130
|
-
store = context.registers[
|
131
|
-
if store.keys.
|
217
|
+
def reset_template_data()
|
218
|
+
@context.registers[CONTEXT_DATA_NAME] ||= {}
|
219
|
+
store = @context.registers[CONTEXT_DATA_NAME]
|
220
|
+
if store.keys.length
|
132
221
|
if store.keys[0] == @id
|
133
|
-
|
222
|
+
# Resets template data
|
223
|
+
@context.registers[CONTEXT_DATA_NAME] = false
|
224
|
+
@context.registers[CONTEXT_SCOPE_NAME] = false
|
134
225
|
else
|
135
|
-
context[
|
226
|
+
@context[CONTEXT_NAME] = store[store.keys[0]]
|
136
227
|
end
|
137
228
|
end
|
138
229
|
end
|
139
230
|
|
140
|
-
# add_template_to_dependency(path
|
231
|
+
# add_template_to_dependency(path = String)
|
141
232
|
# source: https://github.com/jekyll/jekyll/blob/e509cf2139d1a7ee11090b09721344608ecf48f6/lib/jekyll/tags/include.rb
|
142
|
-
def add_template_to_dependency(path
|
143
|
-
if context.registers[:page] && context.registers[:page].key?("path")
|
233
|
+
def add_template_to_dependency(path)
|
234
|
+
if @context.registers[:page] && @context.registers[:page].key?("path")
|
144
235
|
@site.regenerator.add_dependency(
|
145
|
-
@site.in_source_dir(context.registers[:page]["path"]),
|
146
|
-
|
236
|
+
@site.in_source_dir(@context.registers[:page]["path"]),
|
237
|
+
template_path(path)
|
147
238
|
)
|
148
239
|
end
|
149
240
|
end
|
150
241
|
|
151
|
-
# load_cached_template(path
|
242
|
+
# load_cached_template(path = String)
|
152
243
|
# source: https://github.com/jekyll/jekyll/blob/e509cf2139d1a7ee11090b09721344608ecf48f6/lib/jekyll/tags/include.rb
|
153
|
-
|
154
|
-
|
155
|
-
|
244
|
+
# Returns: Liquid template from Jekyll's cache.
|
245
|
+
def load_cached_template(path)
|
246
|
+
@context.registers[CONTEXT_CACHE_NAME] ||= {}
|
247
|
+
cached_templates = @context.registers[CONTEXT_CACHE_NAME]
|
156
248
|
|
157
249
|
unless cached_templates.key?(path)
|
158
250
|
cached_templates[path] = load_template()
|
@@ -163,19 +255,17 @@ module Jekyll
|
|
163
255
|
template["template"]
|
164
256
|
end
|
165
257
|
|
166
|
-
#
|
258
|
+
# template_path(path = String)
|
167
259
|
# Returns: A full file path of the template
|
168
|
-
|
169
|
-
|
170
|
-
File.join(@site.source.to_s, "_templates", path.to_s)
|
260
|
+
def template_path(path)
|
261
|
+
File.join(@site.source.to_s, TEMPLATE_DIR, path.to_s)
|
171
262
|
end
|
172
263
|
|
173
|
-
#
|
264
|
+
# template_content(template_name = String)
|
174
265
|
# Description: Opens, reads, and returns template content as string.
|
175
266
|
# Returns: Template content
|
176
|
-
|
177
|
-
|
178
|
-
File.read(get_template_path(template).strip)
|
267
|
+
def template_content(template_name)
|
268
|
+
File.read(template_path(template_name).strip)
|
179
269
|
end
|
180
270
|
|
181
271
|
# load_template()
|
@@ -186,28 +276,27 @@ module Jekyll
|
|
186
276
|
def load_template()
|
187
277
|
file = @site
|
188
278
|
.liquid_renderer
|
189
|
-
.file(
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
data = get_front_matter(
|
195
|
-
markup = strip_front_matter(
|
196
|
-
|
197
|
-
if
|
198
|
-
|
199
|
-
|
200
|
-
|
279
|
+
.file(template_path(@template_name))
|
280
|
+
|
281
|
+
content = template_content(@template_name)
|
282
|
+
|
283
|
+
template = Hash.new
|
284
|
+
data = get_front_matter(content)
|
285
|
+
markup = strip_front_matter(content)
|
286
|
+
|
287
|
+
if content
|
288
|
+
template["data"] = data
|
289
|
+
template["template"] = file.parse(markup)
|
290
|
+
template
|
201
291
|
else
|
202
292
|
raise Liquid::SyntaxError, "Could not find #{file_path} in your templates"
|
203
293
|
end
|
204
294
|
end
|
205
295
|
|
206
|
-
# sanitize(content)
|
296
|
+
# sanitize(content = String)
|
207
297
|
# Description: Renders the content as markdown or HTML based on the
|
208
298
|
# "parse" attribute.
|
209
299
|
# Returns: Content (string).
|
210
|
-
# @param content { string }
|
211
300
|
def sanitize(content)
|
212
301
|
unless @sanitize
|
213
302
|
converter = @site.find_converter_instance(::Jekyll::Converters::Markdown)
|
@@ -217,27 +306,22 @@ module Jekyll
|
|
217
306
|
end
|
218
307
|
end
|
219
308
|
|
220
|
-
# unindent(content)
|
309
|
+
# unindent(content = String)
|
221
310
|
# Description: Removes initial indentation.
|
222
311
|
# Returns: Content (string).
|
223
|
-
# @param content { string }
|
224
312
|
def unindent(content)
|
225
313
|
# Remove initial whitespace
|
226
314
|
content.gsub!(/\A^\s*\n/, "")
|
227
|
-
|
228
315
|
# Remove indentations
|
229
|
-
|
230
|
-
if content =~ whitespace_regex
|
316
|
+
if content =~ WHITESPACE_REGEXP
|
231
317
|
indentation = Regexp.last_match(0).length
|
232
318
|
content.gsub!(/^\ {#{indentation}}/, "")
|
233
319
|
end
|
234
|
-
|
235
320
|
content
|
236
321
|
end
|
237
322
|
|
238
|
-
# get_front_matter(content)
|
323
|
+
# get_front_matter(content = String)
|
239
324
|
# Returns: A hash of data parsed from the content's YAML
|
240
|
-
# @param content { string }
|
241
325
|
def get_front_matter(content)
|
242
326
|
# Strip leading white-spaces
|
243
327
|
content = unindent(content)
|
@@ -249,20 +333,17 @@ module Jekyll
|
|
249
333
|
end
|
250
334
|
end
|
251
335
|
|
252
|
-
# strip_front_matter(content)
|
336
|
+
# strip_front_matter(content = String)
|
253
337
|
# Description: Removes the YAML front-matter content.
|
254
338
|
# Returns: Template content, with front-matter removed.
|
255
|
-
# @param content { string }
|
256
339
|
def strip_front_matter(content)
|
257
340
|
# Strip leading white-spaces
|
258
341
|
content = unindent(content)
|
259
|
-
|
260
342
|
if content =~ YAML_FRONT_MATTER_REGEXP
|
261
343
|
front_matter = Regexp.last_match(0)
|
262
344
|
# Returns content with stripped front-matter
|
263
345
|
content.gsub!(front_matter, "")
|
264
346
|
end
|
265
|
-
|
266
347
|
content
|
267
348
|
end
|
268
349
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-template
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.21.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ItsJonQ
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|