jekyll-categorize-pages 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generator.rb +94 -0
  3. metadata +5 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76b0b989a699dd749dab1f178b2798adb6a84bb6
4
- data.tar.gz: adf810741c1a6326e89d53a26716ab4ca5b0b301
3
+ metadata.gz: 4149d175b2055a691f1d12047a443528ce319bf3
4
+ data.tar.gz: 518fdd17a74e0dc6f48a5bef5a32291048691291
5
5
  SHA512:
6
- metadata.gz: d1d7afd376a5d85452be160217e2b86724ef7bc889d0b086acb35a8febf5e029d10966dbfcc124aa37cc214cf13ee1b9de42bdbb0521aca416d74758ac49afaf
7
- data.tar.gz: a6c45a9d44257c913ba1794c4a1ab1963acb95f98994b30976cc035658c62dc79cf0f535d9d3acd82cb7a1ea4a70d6b912dfc550e47c7078659ec2885aa8d5a6
6
+ metadata.gz: d75566b154b1a85bb5f3b553e4147177c7cd3e3b71126fc756d6955e597ec94e0f7b02995e34f77bd6eaab98c848b098e86b1ace5ee42f67920d0c11b171b358
7
+ data.tar.gz: 986c1476de278f1d4fb5976fdb5f4c6065e5b7800d880ba7cd63d053be78dea7b9c9d817b7c99e66816fa49d06b8103f57ee035d679670fc51a578370857d281
data/lib/generator.rb ADDED
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+ require "jekyll"
3
+
4
+ module Jekyll
5
+
6
+ # Injects front matter defaults to set default category/sub-category values
7
+ class CategorizePageGenerator < Jekyll::Generator
8
+ # priority :high
9
+ safe true
10
+
11
+ def get_collection(name)
12
+ @site.collections[name] ? @site.collections[name].docs : []
13
+ end
14
+
15
+ def add_category_and_sub_category(item, values)
16
+ # puts "adding: #{item.relative_path}"
17
+ # puts "values: #{values}"
18
+
19
+ item.data["category"] = values[:category] # i.data is the front matter header
20
+ item.data["sub_category"] = values[:sub_category]
21
+ end
22
+
23
+ def add_category_only(item, values)
24
+ # puts "adding: #{item.relative_path}"
25
+ # puts "values: #{values}"
26
+ item.data["category"] = values[:category]
27
+ # item.data["sub_category"] = values[:sub_category].slice(0, values[:sub_category].length - 3)
28
+ end
29
+
30
+
31
+ def isValid?(item)
32
+ all_categories = @site.data["all_categories"] || [] # sub folders inside collection
33
+ path = item.relative_path.split('/')
34
+
35
+ category = path[1] if path[1] != "index.md"
36
+
37
+ # ignore index.md pages
38
+ if path.include?("index.md")
39
+ return false
40
+ end
41
+
42
+ # if category folder has no sub categories
43
+ if path.length == 3
44
+ # puts "path: #{path}"
45
+ # puts "length: #{path.length}"
46
+ header_values = get_header_values_to_add(item)
47
+ add_category_only(item, header_values)
48
+ return false
49
+ end
50
+
51
+ return all_categories.include?(category)
52
+ end
53
+
54
+
55
+ def get_header_values_to_add(item)
56
+ all_categories = @site.data["all_categories"] || [] # sub folders inside collection
57
+ # puts "-----------------------"
58
+ # puts "all_categories: #{all_categories}"
59
+ # puts "item.relative_path: #{item.relative_path}"
60
+
61
+ path = item.relative_path.split('/')
62
+ category = path[1] if path[1] != "index.md"
63
+ # puts "@category: #{@category}"
64
+ sub_category = path[2] if path[2] != "index.md"
65
+ # puts "@sub_category: #{@sub_category}"
66
+ file = path[3] if path[3] != "index.md"
67
+ # puts "file: #{file}"
68
+
69
+ return {
70
+ :category => category,
71
+ :sub_category => sub_category,
72
+ :file => file
73
+ }
74
+ end
75
+
76
+ def generate(site)
77
+ @site = site
78
+ # The collections to add category/sub category front matter values
79
+ collections = ["pages"] # TODO: Move this variable to _config/_data
80
+
81
+ for c in collections
82
+ for item in get_collection(c)
83
+ valid_category = isValid?(item)
84
+ if valid_category
85
+ header_values = get_header_values_to_add(item)
86
+ # puts "header_values: #{header_values}"
87
+ add_category_and_sub_category(item, header_values)
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ end
94
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-categorize-pages
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
@@ -16,21 +16,22 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.6'
19
+ version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.6'
26
+ version: '3.0'
27
27
  description:
28
28
  email:
29
29
  - ''
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
- files: []
33
+ files:
34
+ - lib/generator.rb
34
35
  homepage: ''
35
36
  licenses:
36
37
  - MIT