jekyll-page_extensions 0.0.2

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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +19 -0
  3. data/README.md +33 -0
  4. data/lib/jekyll-page_extensions.rb +228 -0
  5. metadata +62 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a7c866746d558839676fce8db20307e9f6dbd2a
4
+ data.tar.gz: de7d42fe3008537c499a68670c427600ea3ccc4d
5
+ SHA512:
6
+ metadata.gz: 825b90f1d6f7cb9a8f3f51098b35aa7b90c7e6573f2ad9b2704f8c4aeb2a5b7e4d1eb1f9d20a1417661000feef1a93be4ec035fcd6d76a50e3804c3a58b15905
7
+ data.tar.gz: 4a26f05232f9b1250bb3e6883c49c7b50565736c7ef7f9eac2a30e1d4c76573c476dce343bede68f500a41880eff95d68b5d91c05da48b33cf8f4ba1868134c1
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2017 Ulf Möhring
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # Jekyll Page Extensions
2
+
3
+ Adds various functionalities to pages in Jekyll:
4
+ * Automatically generates print versions of pages
5
+ * Provides table of contents tag (containing links to child pages): {% toc %}
6
+ * Provides breadcrumbs tag: {% breadcrumbs %}
7
+ * Provides Rails-style link to helper: {% link_to "My Page Title" %}
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'jekyll-page_extensions'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jekyll-page_extensions
22
+
23
+ ## TODO
24
+
25
+ * Avoid file not found warnings when generating print pages
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/paceline/jekyll-page_extensions/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
@@ -0,0 +1,228 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_support/core_ext/array'
4
+
5
+ module Jekyll
6
+
7
+ # Instantiates sitemap to automatically update sitemap file when site generation is triggered
8
+ #
9
+ #
10
+ class SitemapGenerator < Jekyll::Generator
11
+ safe true
12
+ priority :highest
13
+
14
+ def generate(site)
15
+ s = Sitemap.new(site)
16
+ s.update
17
+ end
18
+ end
19
+
20
+ # Automatically creates printable one page versions for all pages with children
21
+ #
22
+ #
23
+ class PritablePageGenerator < Jekyll::Generator
24
+ safe true
25
+ priority :high
26
+
27
+ def generate(site)
28
+ sitemap = Sitemap.new(site)
29
+ for page in site.pages
30
+ page.print(sitemap) unless page.printable?
31
+ end
32
+ end
33
+ end
34
+
35
+ # Inserts an automatically generated table of contents based on the site's directory structure
36
+ #
37
+ # Syntax {% toc %}
38
+ #
39
+ #
40
+ class TocTag < Liquid::Tag
41
+
42
+ # Render table of contents for children
43
+ def render(context)
44
+ site = context.registers[:site]
45
+ sitemap = Sitemap.new(site)
46
+ page = site.find_page(path: context['page']['path'])
47
+ page.toc(sitemap)
48
+ end
49
+
50
+ end
51
+
52
+ # Provides tag for inserting breadcumbs-sytle navigation path
53
+ #
54
+ # Syntax {% breadcrumbs %}
55
+ #
56
+ # Will render a breadcrumbs-sytle navigation path
57
+ #
58
+ #
59
+ class BreadcrumbsTag < Liquid::Tag
60
+
61
+ # Convert title to url parameter and return link tag, supplementing page path if needed
62
+ def render(context)
63
+ site = context.registers[:site]
64
+ sitemap = Sitemap.new(site)
65
+ html = ["<a href=\"/\">Home</a>"]
66
+ for path in sitemap.branch_up(context['page']['path'])
67
+ page = site.find_page(path: path)
68
+ html << "<a href=\"#{page.pretty_url}\">#{page.data['title']}</a>"
69
+ end
70
+ html.join(" > ")
71
+ end
72
+
73
+ end
74
+
75
+ # Creates link to page when given the title
76
+ #
77
+ # Syntax {% link_to "My Page Title" %}
78
+ #
79
+ # Example:
80
+ # {% link_to "Meine Wörter" %}
81
+ #
82
+ # This will render <a href="/meine-worter">Meine Wörter</a>
83
+ #
84
+ #
85
+ class LinkTag < Liquid::Tag
86
+
87
+ # Read title from tag
88
+ def initialize(tag_name, text, tokens)
89
+ super
90
+ @text = text.strip.gsub(/["']/, "")
91
+ end
92
+
93
+ # Convert title to url parameter and return link tag, supplementing page path if needed
94
+ def render(context)
95
+ site = context.registers[:site]
96
+ page = site.find_page(title: @text)
97
+ page ? "<a href=\"#{page.pretty_url}\">#{@text}</a>" : ""
98
+ end
99
+
100
+ end
101
+
102
+ # Reads and updates sitemap (as tree-style hash)
103
+ #
104
+ #
105
+ class Sitemap
106
+ # Return tree
107
+ attr_reader :tree
108
+
109
+ # Initialize with site data
110
+ def initialize(site)
111
+ @file = File.join(site.source, "_sitemap.yml")
112
+ @sources = site.pages.reject { |page| page.printable? }.collect { |page| page.path.split(File::SEPARATOR) }
113
+ @tree = File.exists?(@file) ? YAML::load_file(@file) : {}
114
+ end
115
+
116
+ # Return sub paths
117
+ def branch_down(path)
118
+ dirs = path.split(File::SEPARATOR)
119
+ keys = dirs.size <= 1 ? "" : "['#{dirs[0..dirs.size-2].join("']['")}']"
120
+ eval("@tree#{keys}")
121
+ end
122
+
123
+ # Return parent paths
124
+ def branch_up(path)
125
+ dirs = path.split(File::SEPARATOR)
126
+ ancestors = []
127
+ if dirs.size > 1
128
+ ancestors << path
129
+ (dirs.size-3).downto(0) do |i|
130
+ eval("@tree['#{dirs[0..i].join("']['")}']").each_value do |v|
131
+ ancestors << v if v.is_a?(String)
132
+ end
133
+ end
134
+ end
135
+ return ancestors.reverse
136
+ end
137
+
138
+ # Iterate through source directory and arrange folders/files in tree struture
139
+ def update(i = 0)
140
+ @new_tree = {} unless @new_tree
141
+ cur = @sources.collect { |f| f[0..i] if f.size > i }.uniq.compact.sort
142
+ for dir in cur
143
+ val = dir.last =~ /\..*$/ ? "'#{dir.join(File::SEPARATOR)}'" : {}
144
+ eval("@new_tree['#{dir.join("']['")}'] = #{val}")
145
+ end
146
+ update(i+1) unless cur.empty?
147
+ File.open(@file, 'w') { |f| f.write @new_tree.to_yaml } unless @new_tree == @tree
148
+ end
149
+ end
150
+
151
+ # Extend Jekyll Site class
152
+ #
153
+ #
154
+ class Site
155
+
156
+ # Load page by attribute
157
+ def find_page(*args)
158
+ options = args.extract_options!
159
+ attribute = options.keys.first
160
+ self.pages.detect { |page| eval("page.#{Page.method_defined?(attribute) ? attribute : "data['#{attribute}']"}") == options[attribute] }
161
+ end
162
+
163
+ end
164
+
165
+ # Extend Jekyll Page class
166
+ #
167
+ #
168
+ class Page
169
+
170
+ # Wrapper for pretty permalinks
171
+ def pretty_url
172
+ self.url.gsub(/\/index\..*$/,"")
173
+ end
174
+
175
+ # Helper to filter out auto-generated print views
176
+ def printable?
177
+ self.url =~ /print\.[a-zA-Z0-9]*$/
178
+ end
179
+
180
+ # Compile one-pager fit for printing, including all sub-pages
181
+ def print(sitemap)
182
+ branch = sitemap.branch_down(self.path)
183
+ page = Page.new(self.site, self.site.source, self.pretty_url, "print#{self.ext}")
184
+
185
+ html = "---
186
+ layout: print
187
+ title: #{self.data['title']}
188
+ ---
189
+ #{self.content}"
190
+ html = self.build_html_tree(branch, true, html)
191
+
192
+ modified = !File.exists?(page.path) || File.read(page.path) != html
193
+ if modified
194
+ open(page.path, 'w') { |f| f << html }
195
+ self.site.pages << page
196
+ end
197
+ end
198
+
199
+ # Render table of contents based on children
200
+ def toc(sitemap)
201
+ branch = sitemap.branch_down(self.path)
202
+ self.build_html_tree(branch)
203
+ end
204
+
205
+ # Generate html for table of contents
206
+ def build_html_tree(branch, content = false, html = "", level = 0)
207
+ index = branch.select { |k,v| v.is_a?(String) }
208
+ unless index.empty?
209
+ page = self.site.find_page(path: branch[index.keys.first])
210
+ if content
211
+ html += page.content.gsub(/{{\s*page.title\s*}}/, page.data['title']) unless html.include?(page.content)
212
+ else
213
+ html += "<h#{level+1}><a href=\"#{page.pretty_url}\">#{page.data['title']}</a></h#{level+1}>\n"
214
+ end
215
+ html += "\n"
216
+ end
217
+ branch.each_pair do |k,v|
218
+ html = build_html_tree(v, content, html, level+1) unless v.is_a?(String)
219
+ end
220
+ return html
221
+ end
222
+
223
+ end
224
+ end
225
+
226
+ Liquid::Template.register_tag('toc', Jekyll::TocTag)
227
+ Liquid::Template.register_tag('breadcrumbs', Jekyll::BreadcrumbsTag)
228
+ Liquid::Template.register_tag('link_to', Jekyll::LinkTag)
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-page_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ulf Möhring
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ description: A plugin for Jekyll that extends the capabilities of the page class
28
+ email: ulf@moehring.me
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ - LICENSE
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - lib/jekyll-page_extensions.rb
38
+ homepage: https://github.com/paceline/jekyll-page-extensions
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.6.10
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Page extensions for Jekyll
62
+ test_files: []