jekyll-site-tree 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f2ccb902abceef0ee4301c9294bdcf6b5f74d3a4dc2007ff5540aa56bd0702bf
4
+ data.tar.gz: cd6548e901ca669f9b0a476033c8bef354702bb9d017e9e80f3c89ad882fe612
5
+ SHA512:
6
+ metadata.gz: ecb13b35782ce2aa96a91e9afcef6c7ece0118811eeac6f79d27fee3815616588ac34ecc9ff0c17d890e264c67b83525490e10e4006671db30378356f148926f
7
+ data.tar.gz: 0f2dba3b28b7fbb5fc01bee25a26465a7e8bce1d67a2aad6fbec854ca9455707e5cbe10aae0824b3909e0312fc199588e9f02533f0351ab45839d946b8026af2
@@ -0,0 +1,153 @@
1
+ require 'jekyll'
2
+ require 'natural_sort'
3
+
4
+ module Jekyll
5
+ class SiteTreeGenerator < Jekyll::Generator
6
+ def generate(site)
7
+ @site = site
8
+
9
+ unless site_tree_file
10
+ Jekyll.logger.warn("SiteTree:", "skipped because no 'site-tree' file specified in config")
11
+ return
12
+ end
13
+
14
+ page = @site.pages.find { |page| page.dir.slice(1, page.dir.length) + page.name == site_tree_file }
15
+
16
+ if page.nil?
17
+ Jekyll.logger.error("SiteTree:", "unable to find 'site-tree' file: " + site_tree_file)
18
+ return
19
+ end
20
+
21
+ Jekyll.logger.info("SiteTree:", "building site tree")
22
+ # page.data['site_tree_permalinks'] = permalinks
23
+ page.data['site_tree'] = site_tree
24
+ end
25
+
26
+ def permalinks
27
+ pages = @site.pages
28
+ pages += @site.static_files
29
+ pages += @site.collections.map do |tuple|
30
+ name, collection = tuple
31
+ (collection.write?) ? collection.docs : []
32
+ end.flatten
33
+
34
+ pages.map { |pg| pg.url }.select do |page|
35
+ !excludes.any? { |rx| rx.match? page }
36
+ end.uniq.sort(&NaturalSort)
37
+ end
38
+
39
+ private
40
+
41
+ def config
42
+ @site.config["site_tree"] || Hash.new
43
+ end
44
+
45
+ def site_tree_file
46
+ config["file"]
47
+ end
48
+
49
+ def include_extension?
50
+ !!config["extension"]
51
+ end
52
+
53
+ def excludes
54
+ if @excludes.nil?
55
+ @excludes = (config["exclude"] || []).map do |exclude|
56
+ # if exclude begins with a slash, the expression references
57
+ # an absolute path, otherwise any match is considered valid
58
+ Regexp.new(exclude[0] == '/' ? '^' + exclude : exclude)
59
+ end
60
+ end
61
+ @excludes
62
+ end
63
+
64
+ def substitutions
65
+ if @substitutions.nil?
66
+ @substitutions = (config["substitute"] || []).each do |subs|
67
+ subs["expr"] = Regexp.new(subs["expr"])
68
+ end
69
+ end
70
+ @substitutions
71
+ end
72
+
73
+ def permalink_tuples
74
+ subs = substitutions
75
+
76
+ permalinks.map do |link|
77
+ sub = subs.find { |sub| sub["expr"].match?(link) }
78
+ new = (sub.nil?) ? link : link.gsub(sub["expr"], sub["name"])
79
+
80
+ unless include_extension?
81
+ new = File.join(File.dirname(new), File.basename(new, '.*'))
82
+ end
83
+
84
+ { :path => new, :link => link }
85
+ end
86
+ end
87
+
88
+ # kind of an ugly solution, nests each path in the sites permalinks
89
+ # into a series of hashes (each hash connects to the next hash) which
90
+ # constructs a sort of tree. The path & link for the current entry is
91
+ # found through the :path & :link fields.
92
+ def permalink_tree
93
+ tree = Hash.new
94
+
95
+ permalink_tuples.each do |tuple|
96
+ branch = tree
97
+ branch_path = tuple[:path].split('/')
98
+
99
+ branch_path.slice(1, branch_path.length-2).each do |path|
100
+ branch = (branch[path] ||= Hash.new)
101
+ end
102
+
103
+ branch[branch_path.last] = tuple
104
+ end
105
+
106
+ tree
107
+ end
108
+
109
+ # converts a permalink_tree into an XML structure
110
+ def site_tree
111
+ def recursive_construct_tree(name, tree)
112
+ result = '<li>'
113
+
114
+ current_path = tree.delete(:path)
115
+ current_link = tree.delete(:link)
116
+
117
+ if current_path
118
+ result += "<a href=\"%s\">%s</a>" % [
119
+ current_link, name].map { |s| html_coder.encode(s) }
120
+ end
121
+
122
+ unless tree.empty?
123
+ if tree.length == 1 && !current_path then
124
+ child_name = tree.keys[0]
125
+
126
+ return recursive_construct_tree(
127
+ name + '/' + child_name, tree[child_name])
128
+ else
129
+ result += html_coder.encode(name) if !current_path
130
+
131
+ result += '<ul>'
132
+ tree.each do |name, subtree|
133
+ result += recursive_construct_tree(name, subtree)
134
+ end
135
+ result += '</ul>'
136
+ end
137
+ end
138
+
139
+ result += '</li>'
140
+ end
141
+
142
+ result = '<ul>'
143
+ permalink_tree.each do |name, tree|
144
+ result += recursive_construct_tree(name, tree)
145
+ end
146
+ result += '</ul>'
147
+ end
148
+
149
+ def html_coder
150
+ @@html_coder ||= HTMLEntities.new
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module SiteTree
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-site-tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mohkale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-13 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: natural_sort
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0
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.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |
56
+ A jekyll generator which creates a site-tree consisting of all the files in the output (built) path of a jekyll website.
57
+
58
+ For a site with a structure like:
59
+ - /foo.md
60
+ - /bar.html
61
+ - /posts/2019/15/10/hello-world.md
62
+ - /posts/2019/15/10/lo-and-behold.md
63
+ - /assets/styles/main.scss
64
+
65
+ You'll recieve an XML unordered list like:
66
+ - foo
67
+ - bar
68
+ - posts/2019/15/10
69
+ - hello-world
70
+ - lo-and-behold
71
+ - assets/styles
72
+ - main
73
+ email: mohkalsin@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - lib/jekyll-site-tree.rb
79
+ - lib/utils/version.rb
80
+ homepage:
81
+ licenses:
82
+ - GPL-3.0
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.0.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: create a site tree from a jekyll page
103
+ test_files: []