jekyll-template 0.12.0 → 0.12.1

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
  SHA1:
3
- metadata.gz: c33d85226ebc6880ffe3bba8ae7364d3c097709a
4
- data.tar.gz: a04ad9bf291e404be4e21aa2ffc3fddf7d857780
3
+ metadata.gz: 8e7036c6985df3bba0f3906dd8f67cf904b367ea
4
+ data.tar.gz: 9b7177cbde9aaa97865393ddf9bbfa142f55fec4
5
5
  SHA512:
6
- metadata.gz: 7e1a36ee14d0faf296a00615969775fbd31e8129618fadb02746ccd76cf8bd0882735662f20bfde2c41a1943eb1d267d288564be0f92e26a2d14e394e34a9acf
7
- data.tar.gz: 10a4e98343cfb8d5fa934c134d72638ce32368bcd44627ab308a1642f679208ae10dc82e09059e0b6a05df0239e556b0b6d8ee69ae057a0dc41098101f0f8d0a
6
+ metadata.gz: 472ef5e24287a64670d487977968092a37d7d40eed1e9c4539331b5ee76374412d61090dc86f0dc3c322181dc7d8927508180852f738f7bdff9976aeb96fc2a3
7
+ data.tar.gz: de8b2f6cda23fcc9655011d2c9ab7b232065dbb481300b0185c7cd18549e2d59f279ee33e6383ddcb63a5c0c83fe3a8367589d89bfdb2263e5e96438f42bed47
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Template
3
- VERSION = "0.12.0"
3
+ VERSION = "0.12.1"
4
4
  end
5
5
  end
@@ -19,7 +19,6 @@ module Jekyll
19
19
  def initialize(tag_name, markup, tokens)
20
20
  super
21
21
  @root_path = false
22
- @template_content = false
23
22
 
24
23
  # @template_name = markup
25
24
  if markup =~ Syntax
@@ -53,6 +52,8 @@ module Jekyll
53
52
  # - supports custom attributes to be used in template
54
53
  def render(context)
55
54
  content = super
55
+ # Set the root_path
56
+ @root_path = context.registers[:site].source
56
57
  # Remove leading whitespace
57
58
  # content = content.lstrip
58
59
  compressor = HtmlCompressor::Compressor.new({
@@ -63,7 +64,13 @@ module Jekyll
63
64
  :remove_comments => true
64
65
  })
65
66
  site = context.registers[:site]
66
- template = load_template(context)
67
+
68
+ add_template_to_dependency(site, @template_name, context)
69
+ template_obj = load_cached_template(@template_name, context)
70
+ template_data = template_obj["data"]
71
+ update_attributes(template_data)
72
+
73
+ template = template_obj["template"]
67
74
 
68
75
  # Define the default template attributes
69
76
  # Source:
@@ -73,20 +80,18 @@ module Jekyll
73
80
  context["template"] = Hash.new
74
81
 
75
82
  # Parse and extend template's front-matter with content front-matter
76
- content = parse_front_matter(content)
83
+ content_data = get_front_matter(content)
84
+ update_attributes(content_data)
85
+
86
+ # Remove front matter
87
+ content = strip_front_matter(content)
77
88
 
78
89
  # Setting template attributes from @attributes
79
90
  # This allows for @attributes to be used within the template as
80
91
  # {{ template.atttribute_name }}
81
92
  if @attributes
82
93
  @attributes.each do |key, value|
83
- # Render the attribute(s) with context
84
- if value.instance_of? Liquid::VariableLookup
85
- # val = value.name
86
- val = context.evaluate(value)
87
- else
88
- val = context.evaluate(value)
89
- end
94
+ val = context.evaluate(value)
90
95
  context["template"][key] = val
91
96
 
92
97
  # Adjust sanitize if parse: html
@@ -103,18 +108,13 @@ module Jekyll
103
108
  unless @sanitize
104
109
  converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
105
110
  content = content.to_s.unindent
106
- @content = converter.convert(content)
111
+ content = converter.convert(content)
107
112
  else
108
- @content = content.to_s.unindent
109
- end
110
-
111
- # handling empty content
112
- if @content.empty?
113
- @content = "<!-- Template: #{ @template_name } -->"
113
+ content = content.to_s.unindent
114
114
  end
115
115
 
116
116
  # setting the template content
117
- context["template"]["content"] = @content
117
+ context["template"]["content"] = content
118
118
 
119
119
  # rendering the template with the content
120
120
  @output = template.render( context )
@@ -122,20 +122,61 @@ module Jekyll
122
122
  @output = compressor.compress(@output)
123
123
  end
124
124
 
125
+ def update_attributes(data)
126
+ if data
127
+ @attributes = @attributes.merge(data)
128
+ end
129
+ end
130
+
131
+ # add_template_to_dependency(site, path, context)
132
+ # source: https://github.com/jekyll/jekyll/blob/e509cf2139d1a7ee11090b09721344608ecf48f6/lib/jekyll/tags/include.rb
133
+ def add_template_to_dependency(site, path, context)
134
+ if context.registers[:page] && context.registers[:page].key?("path")
135
+ site.regenerator.add_dependency(
136
+ site.in_source_dir(context.registers[:page]["path"]),
137
+ path
138
+ )
139
+ end
140
+ end
141
+
142
+ # load_cached_template(path, context)
143
+ # source: https://github.com/jekyll/jekyll/blob/e509cf2139d1a7ee11090b09721344608ecf48f6/lib/jekyll/tags/include.rb
144
+ def load_cached_template(path, context)
145
+ context.registers[:cached_templates] ||= {}
146
+ cached_templates = context.registers[:cached_templates]
147
+
148
+ if cached_templates.key?(path)
149
+ cached_templates[path]
150
+ else
151
+ begin
152
+ template = load_template(context)
153
+ cached_templates[path] = template
154
+ end
155
+ end
156
+ end
157
+
158
+ # get_template_path(path)
159
+ # Returns: A full file path of the template
160
+ # @param path { string }
161
+ def get_template_path(path)
162
+ # default template path
163
+ dir = @root_path.to_s
164
+ view = "_templates/" + path.to_s
165
+ File.join(dir, view).to_s
166
+ end
167
+
125
168
  # get_template_content(template)
126
169
  # Description: Opens, reads, and returns template content as string.
127
170
  # Returns: Template content
128
171
  # @param template { string }
129
172
  def get_template_content(template)
130
- # default template path
131
- view = "_templates/" + template
132
- file_path = File.join(@root_path, view)
173
+ file_path = get_template_path(template)
133
174
  path = File.read(file_path.strip)
134
175
  # returns template content
135
176
  path
136
177
  end
137
178
 
138
- # load_template(template)
179
+ # load_template(context)
139
180
  # Description: Extends Liquid's default load_template method. Also provides
140
181
  # extra enhancements:
141
182
  # - parses and sets template front-matter content
@@ -143,15 +184,23 @@ module Jekyll
143
184
  def load_template(context)
144
185
  # Set the root_path
145
186
  @root_path = context.registers[:site].source
187
+ file_path = get_template_path(@template_name)
188
+ file = context.registers[:site]
189
+ .liquid_renderer
190
+ .file(file_path)
146
191
  # Set the template_content
147
- @template_content = get_template_content(@template_name)
148
- # Parse front matter
149
- @template_content = parse_front_matter(@template_content)
192
+ template_content = get_template_content(@template_name)
150
193
 
151
- if @template_content
152
- Liquid::Template.parse(@template_content)
194
+ template_obj = Hash.new
195
+ data = get_front_matter(template_content)
196
+ content = strip_front_matter(template_content)
197
+
198
+ if template_content
199
+ template_obj["data"] = data
200
+ template_obj["template"] = file.parse(content)
201
+ template_obj
153
202
  else
154
- raise Liquid::SyntaxError, "Could not find #{view} in your templates"
203
+ raise Liquid::SyntaxError, "Could not find #{file_path} in your templates"
155
204
  end
156
205
  end
157
206
 
@@ -173,22 +222,36 @@ module Jekyll
173
222
  content
174
223
  end
175
224
 
176
- # parse_front_matter(content)
177
- # Description: Parses and sets YAML front-matter content.
178
- # Returns: Template content, with front-matter removed.
225
+ # get_front_matter(content)
226
+ # Returns: A hash of data parsed from the content's YAML
179
227
  # @param content { string }
180
- def parse_front_matter(content)
228
+ def get_front_matter(content)
181
229
  # Strip leading white-spaces
182
230
  content = unindent(content)
183
-
231
+ data = Hash.new
184
232
  if content =~ YAML_FRONT_MATTER_REGEXP
185
233
  front_matter = Regexp.last_match(0)
186
234
  # Push YAML data to the template's attributes
187
235
  values = SafeYAML.load(front_matter)
188
236
  # Set YAML data to @attributes
189
237
  values.each do |key, value|
190
- @attributes[key] = value
238
+ data[key] = value
191
239
  end
240
+ # Returns data
241
+ data
242
+ end
243
+ end
244
+
245
+ # strip_front_matter(content)
246
+ # Description: Removes the YAML front-matter content.
247
+ # Returns: Template content, with front-matter removed.
248
+ # @param content { string }
249
+ def strip_front_matter(content)
250
+ # Strip leading white-spaces
251
+ content = unindent(content)
252
+
253
+ if content =~ YAML_FRONT_MATTER_REGEXP
254
+ front_matter = Regexp.last_match(0)
192
255
  # Returns content with stripped front-matter
193
256
  content = content.gsub(front_matter, "")
194
257
  end
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.12.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ItsJonQ
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-08 00:00:00.000000000 Z
11
+ date: 2017-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll