jekyll-categorize-pages 0.1.1 → 0.1.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.
- checksums.yaml +4 -4
- data/lib/generator.rb +81 -34
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76f301d8bfd1a555042e66e8be6d3296b4031eb7
|
4
|
+
data.tar.gz: 624a2fe951c103566c9eb9c1b28306c822156706
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5836409dc0ea7275477bbb8b91486d1b9360ea14cf16ef377d6d9ac800cf2494785ee1b89ad397285b8a052d11356071c8d89d48fcf6b8d25c7436311c152d8
|
7
|
+
data.tar.gz: ca023fa10dc6def534d9b51691142fe0f327dba83a0336579358811ee8211ee0bf3522ec439c8c3a4fb72c323a6f2c605908896e480458e47dbff86e7171e37f
|
data/lib/generator.rb
CHANGED
@@ -2,60 +2,107 @@
|
|
2
2
|
require "jekyll"
|
3
3
|
|
4
4
|
module Jekyll
|
5
|
-
|
5
|
+
# You can create a generator when you need Jekyll
|
6
|
+
# to create additional content based on your own rules.
|
7
|
+
|
8
|
+
# A generator is a subclass of Jekyll::Generator that defines a generate method,
|
9
|
+
# which receives an instance of Jekyll::Site. The return value of generate is ignored.
|
10
|
+
|
11
|
+
|
6
12
|
# Injects front matter defaults to set default category/sub-category values
|
7
|
-
class
|
13
|
+
class InjectFm < Jekyll::Generator
|
8
14
|
# priority :high
|
9
15
|
safe true
|
10
16
|
|
11
|
-
def log(*args)
|
12
|
-
puts args
|
13
|
-
end
|
14
|
-
|
15
17
|
def get_collection(name)
|
16
18
|
@site.collections[name] ? @site.collections[name].docs : []
|
17
19
|
end
|
18
20
|
|
19
|
-
def add_to_frontmatter(item)
|
20
|
-
|
21
|
-
|
21
|
+
def add_to_frontmatter(item, values)
|
22
|
+
# puts "adding: #{item.relative_path}"
|
23
|
+
# puts "values: #{values}"
|
24
|
+
|
25
|
+
item.data["category"] = values[:category] # i.data is the front matter header
|
26
|
+
item.data["sub_category"] = values[:sub_category]
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_category_only(item, values)
|
30
|
+
# puts "adding: #{item.relative_path}"
|
31
|
+
# puts "values: #{values}"
|
32
|
+
item.data["category"] = values[:category]
|
33
|
+
# item.data["sub_category"] = values[:sub_category].slice(0, values[:sub_category].length - 3)
|
22
34
|
end
|
23
35
|
|
24
36
|
|
25
37
|
def isValid?(item)
|
26
|
-
|
27
|
-
puts folders_to_categorize
|
28
|
-
# all_categories.include?(page[:n])
|
38
|
+
all_categories = @site.data["all_categories"] || [] # sub folders inside collection
|
29
39
|
path = item.relative_path.split('/')
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
40
|
+
|
41
|
+
category = path[1] if path[1] != "index.md"
|
42
|
+
|
43
|
+
# ignore index.md pages
|
44
|
+
if path.include?("index.md")
|
45
|
+
return false
|
46
|
+
end
|
47
|
+
|
48
|
+
# if category folder has no sub categories
|
49
|
+
if path.length == 3
|
50
|
+
# puts "path: #{path}"
|
51
|
+
# puts "length: #{path.length}"
|
52
|
+
header_values = get_header_values_to_add(item)
|
53
|
+
add_category_only(item, header_values)
|
54
|
+
return false
|
55
|
+
end
|
56
|
+
|
57
|
+
return all_categories.include?(category)
|
58
|
+
|
59
|
+
# if path.include?("index.md")
|
60
|
+
# return false
|
61
|
+
# else
|
62
|
+
# return all_categories.include?(category)
|
63
|
+
# end
|
39
64
|
end
|
40
65
|
|
41
66
|
|
67
|
+
def get_header_values_to_add(item)
|
68
|
+
all_categories = @site.data["all_categories"] || [] # sub folders inside collection
|
69
|
+
# puts "-----------------------"
|
70
|
+
# puts "all_categories: #{all_categories}"
|
71
|
+
# puts "item.relative_path: #{item.relative_path}"
|
72
|
+
|
73
|
+
path = item.relative_path.split('/')
|
74
|
+
category = path[1] if path[1] != "index.md"
|
75
|
+
# puts "@category: #{@category}"
|
76
|
+
sub_category = path[2] if path[2] != "index.md"
|
77
|
+
# puts "@sub_category: #{@sub_category}"
|
78
|
+
file = path[3] if path[3] != "index.md"
|
79
|
+
# puts "file: #{file}"
|
80
|
+
|
81
|
+
return {
|
82
|
+
:category => category,
|
83
|
+
:sub_category => sub_category,
|
84
|
+
:file => file
|
85
|
+
}
|
86
|
+
end
|
42
87
|
|
43
88
|
def generate(site)
|
44
|
-
|
45
|
-
#
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
89
|
+
@site = site
|
90
|
+
# The collections to add category/sub category front matter values
|
91
|
+
collections = ["pages"] # TODO: Move this variable to _config/_data
|
92
|
+
|
93
|
+
for c in collections
|
94
|
+
for item in get_collection(c)
|
95
|
+
valid_category = isValid?(item)
|
96
|
+
if valid_category
|
97
|
+
header_values = get_header_values_to_add(item)
|
98
|
+
# puts "header_values: #{header_values}"
|
99
|
+
add_to_frontmatter(item, header_values)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
57
103
|
|
58
104
|
end
|
59
105
|
|
60
106
|
end
|
107
|
+
|
61
108
|
end
|