jekyll-template 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cb4983307a8165900476e04a6bd8d5fb17769664
4
+ data.tar.gz: 86dfecc8cafe326dce8ea6196743d85d59795509
5
+ SHA512:
6
+ metadata.gz: 3198b7b9334c33fbefdb5f81a3234a8625aa1d36c54d7e6dc88fee0d6ce6a365a09a9bd44163c4f6456ad96a62a942c579b7f1d2d28c32ab31c39094a1b036b3
7
+ data.tar.gz: 74d56f7273f2bdc8b3146dc60564f0126f632f804b0a8161e64aef088bc17fa24497178a29d007c90fe9835fff620d07748c16f1724c619f47c9eb77cc40773e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem "jekyll", "3.1.2"
5
+ gem "htmlcompressor", "~> 0.3.1"
6
+ gem "unindent", "~> 1.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Help Scout
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # Jekyll::Template
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'jekyll-template'
9
+ ```
10
+
11
+ And then execute:
12
+ ```
13
+ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```
18
+ gem install jekyll-template
19
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jekyll/template"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jekyll/template/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-template"
8
+ spec.version = Jekyll::Template::VERSION
9
+ spec.authors = ["ItsJonQ"]
10
+ spec.email = ["itsjonq@gmail.com"]
11
+
12
+ spec.summary = "Custom template blocks with YAML support"
13
+ spec.homepage = "https://github.com/helpscout/jekyll-template"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency("jekyll", "3.1.2")
24
+ spec.add_runtime_dependency("htmlcompressor", "~> 0.3.1")
25
+ spec.add_runtime_dependency("unindent", "~> 1.0")
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Template
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,199 @@
1
+ require "htmlcompressor"
2
+ require "jekyll"
3
+ require "jekyll/template/version"
4
+ require "unindent"
5
+
6
+ module Jekyll
7
+ module Tags
8
+ class TemplateBlock < Liquid::Block
9
+ include Liquid::StandardFilters
10
+ Syntax = /(#{Liquid::QuotedFragment}+)?/
11
+
12
+ # YAML REGEXP
13
+ # https://github.com/jekyll/jekyll/blob/35c5e073625100b0f8f8eab6f7da6cb6d5734930/lib/jekyll/document.rb
14
+ YAML_FRONT_MATTER_REGEXP = %r!(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m
15
+
16
+ # initialize
17
+ # Description: Extends Liquid's default initialize method.
18
+ def initialize(tag_name, markup, tokens)
19
+ super
20
+ @root_path = false
21
+ @template_content = false
22
+
23
+ # @template_name = markup
24
+ if markup =~ Syntax
25
+
26
+ @template_name = $1
27
+ @attributes = {}
28
+ @sanitize = false
29
+
30
+ # Parse parameters
31
+ # Source: https://gist.github.com/jgatjens/8925165
32
+ markup.scan(Liquid::TagAttributes) do |key, value|
33
+ @attributes[key] = Liquid::Expression.parse(value)
34
+ end
35
+ else
36
+ raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
37
+ end
38
+ end
39
+
40
+ # parse_content
41
+ # Description: Extends Liquid's default parse_content method.
42
+ def parse_content(context, content)
43
+ content
44
+ end
45
+
46
+ # render
47
+ # Description: Extends Liquid's default render method. This method also
48
+ # adds additional features:
49
+ # - YAML front-matter parsing and handling
50
+ # - properly handles indentation and whitespace (resolves renderin issues)
51
+ # - ability to parse content as markdown vs. html
52
+ # - supports custom attributes to be used in template
53
+ def render(context)
54
+ content = super
55
+ # Remove leading whitespace
56
+ # content = content.lstrip
57
+ compressor = HtmlCompressor::Compressor.new
58
+ site = context.registers[:site]
59
+ template = load_template(context)
60
+
61
+ # Define the default template attributes
62
+ # Source:
63
+ # https://github.com/Shopify/liquid/blob/9a7778e52c37965f7b47673da09cfb82856a6791/lib/liquid/tags/include.rb
64
+ context["template_name"] = context.evaluate(@template_name)
65
+ context["partial"] = true
66
+ context["template"] = Hash.new
67
+
68
+ # Parse and extend template's front-matter with content front-matter
69
+ content = parse_front_matter(content)
70
+
71
+ # Setting template attributes from @attributes
72
+ # This allows for @attributes to be used within the template as
73
+ # {{ template.atttribute_name }}
74
+ if @attributes
75
+ @attributes.each do |key, value|
76
+ # Render the attribute(s) with context
77
+ if value.instance_of? Liquid::VariableLookup
78
+ # val = value.name
79
+ val = context.evaluate(value)
80
+ else
81
+ val = context.evaluate(value)
82
+ end
83
+ context["template"][key] = val
84
+
85
+ # Adjust sanitize if parse: html
86
+ if (key == "parse") && (val == "html")
87
+ @sanitize = true
88
+ end
89
+ end
90
+ end
91
+
92
+ content = parse_content(context, content)
93
+
94
+ # sanitize
95
+ # Determines whether to parse as HTML or markdown
96
+ unless @sanitize
97
+ converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
98
+ content = content.to_s.unindent
99
+ @content = converter.convert(content)
100
+ else
101
+ @content = content.to_s.unindent
102
+ end
103
+
104
+ # handling empty content
105
+ if @content.empty?
106
+ @content = "<!-- Template: #{ @template_name } -->"
107
+ end
108
+
109
+ # setting the template content
110
+ context["template"]["content"] = @content
111
+
112
+ # rendering the template with the content
113
+ @output = template.render( context )
114
+ # normalizes whitespace and indentation
115
+ @output = compressor.compress(@output)
116
+ end
117
+
118
+ # get_template_content(template)
119
+ # Description: Opens, reads, and returns template content as string.
120
+ # Returns: Template content
121
+ # @param template { string }
122
+ def get_template_content(template)
123
+ # default template path
124
+ view = "_templates/" + template
125
+ file_path = File.join(@root_path, view)
126
+ path = File.read(file_path.strip)
127
+ # returns template content
128
+ path
129
+ end
130
+
131
+ # load_template(template)
132
+ # Description: Extends Liquid's default load_template method. Also provides
133
+ # extra enhancements:
134
+ # - parses and sets template front-matter content
135
+ # Returns: Template class
136
+ def load_template(context)
137
+ # Set the root_path
138
+ @root_path = context.registers[:site].source
139
+ # Set the template_content
140
+ @template_content = get_template_content(@template_name)
141
+ # Parse front matter
142
+ @template_content = parse_front_matter(@template_content)
143
+
144
+ if @template_content
145
+ Liquid::Template.parse(@template_content)
146
+ else
147
+ raise Liquid::SyntaxError, "Could not find #{view} in your templates"
148
+ end
149
+ end
150
+
151
+ # unindent(content)
152
+ # Description: Removes initial indentation.
153
+ # Returns: Content (string).
154
+ # @param content { string }
155
+ def unindent(content)
156
+ # Remove initial whitespace
157
+ content = content.gsub(/^\s*\n/, "")
158
+
159
+ # Remove indentations
160
+ whitespace_regex = %r!^\s*!m
161
+ if content =~ whitespace_regex
162
+ indentation = Regexp.last_match(0)
163
+ content = content.gsub(indentation, "")
164
+ end
165
+
166
+ content
167
+ end
168
+
169
+ # parse_front_matter(content)
170
+ # Description: Parses and sets YAML front-matter content.
171
+ # Returns: Template content, with front-matter removed.
172
+ # @param content { string }
173
+ def parse_front_matter(content)
174
+ # Strip leading white-spaces
175
+ compressor = HtmlCompressor::Compressor.new({
176
+ :preserve_line_breaks => true
177
+ })
178
+ content = unindent(content)
179
+
180
+ if content =~ YAML_FRONT_MATTER_REGEXP
181
+ front_matter = Regexp.last_match(0)
182
+ # Push YAML data to the template's attributes
183
+ values = SafeYAML.load(front_matter)
184
+ # Set YAML data to @attributes
185
+ values.each do |key, value|
186
+ @attributes[key] = value
187
+ end
188
+ # Returns content with stripped front-matter
189
+ content = content.gsub(front_matter, "")
190
+ end
191
+
192
+ compressor.compress(content)
193
+ end
194
+
195
+ end
196
+ end
197
+ end
198
+
199
+ Liquid::Template.register_tag("template", Jekyll::Tags::TemplateBlock)
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ItsJonQ
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: htmlcompressor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: unindent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description:
98
+ email:
99
+ - itsjonq@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - jekyll-template.gemspec
114
+ - lib/jekyll/template.rb
115
+ - lib/jekyll/template/version.rb
116
+ homepage: https://github.com/helpscout/jekyll-template
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.5.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Custom template blocks with YAML support
140
+ test_files: []