jekyll-id 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6d612e8b6adffc3bf1942aba34c9deb41af024c15690203a47d3aec02f57406
4
- data.tar.gz: 137843c64bf07337eb0bd41c529983fa6d8a2dcae34d8697b1e33d56d967ff0e
3
+ metadata.gz: a752baec6b8cd1ed0166b772d833d8c0193705a991a22a1b1396aa350b9bc11c
4
+ data.tar.gz: 348a752ef9abb6ff465239f76b1b9111f540a511b4d851383e574dcd4067290a
5
5
  SHA512:
6
- metadata.gz: b250239213feeda31ce106751b73ca4ed104212686b30288eea9a1f5a062bba9c18361d393cdb5518976cc340b93fb4912d24363f1edc47f8a2f9d90964ab8de
7
- data.tar.gz: 33e13ee948dc4adfe49fee2c3b67f3a9b22b775b6ed261107db823bc6c42ac445c3cd0eb7640d35cb4a9f5d6c16890ab3e96c53d9f51cb7d7007cd77aa81a96d
6
+ metadata.gz: 9fd79dfbb882d4f5ae8841d0f835a591a92999e89198feed4491faec884863122100a29288bedfd283fb4c2b7d38a5788130b5abb06f9f85c30022750e4cdbcb
7
+ data.tar.gz: 5a606567d7217078b25eed300d2436b84dfd9f3590a6fc73cc8c680b3f7981599203b5aa180b164760a412602639fcf483f55814e923e8afe027ea992ab6a4f7
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+
5
+ module Jekyll
6
+ module Drops
7
+
8
+ class UrlDrop
9
+
10
+ def id
11
+ # 'to_s' is necessary for scenarios where all ID chars are numbers
12
+ @obj.data['id'].to_s
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Jekyll
4
4
  module ID
5
- VERSION = "0.0.1"
5
+
6
+ VERSION = "0.0.2"
7
+
6
8
  end
7
9
  end
data/lib/jekyll-id.rb CHANGED
@@ -1,10 +1,171 @@
1
1
  # frozen_string_literal: true
2
+ require 'jekyll'
3
+ require 'nanoid'
2
4
 
5
+ require_relative "jekyll-id/patch/url_drop"
3
6
  require_relative "jekyll-id/version"
4
7
 
5
8
  module Jekyll
6
9
  module ID
7
- class Error < StandardError; end
8
- # Your code goes here...
10
+
11
+ class Generator < Jekyll::Generator
12
+ priority :highest
13
+
14
+ # for testing
15
+ attr_reader :config
16
+
17
+ CONVERTER_CLASS = Jekyll::Converters::Markdown
18
+ # config
19
+ CONFIG_KEY = "ids"
20
+ ENABLED_KEY = "enabled"
21
+ EXCLUDE_KEY = "exclude"
22
+ FORMAT_KEY = "format"
23
+ ALPHA_KEY = "alpha"
24
+ SIZE_KEY = "size"
25
+
26
+ def initialize(config)
27
+ @config ||= config
28
+ @testing ||= config['testing'].nil? ? false : config['testing']
29
+ end
30
+
31
+ def generate(site)
32
+ return if disabled?
33
+
34
+ @site = site
35
+ @yesall = false
36
+ @noall = false
37
+
38
+ markdown_converter = site.find_converter_instance(CONVERTER_CLASS)
39
+ # filter docs based on configs
40
+ docs = []
41
+ docs += site.docs_to_write.filter { |d| !exclude?(d.type) }
42
+ @md_docs = docs.filter { |doc| markdown_converter.matches(doc.extname) }
43
+ if @md_docs.nil? || @md_docs.empty?
44
+ Jekyll.logger.warn("Jekyll-ID: No documents to process.")
45
+ end
46
+
47
+ @md_docs.each do |doc|
48
+ prep_id(doc)
49
+ end
50
+ end
51
+
52
+ # helpers
53
+
54
+ def prep_id(doc)
55
+ return if @noall
56
+ has_id = doc.data.keys.include?('id')
57
+ # 0 == is strict
58
+ # nil == isn't strict
59
+ is_strict_id = (alpha_formatted?(doc.data['id'].to_s) && size_formatted?(doc.data['id'].to_s))
60
+ # cases where we would want to generate a new id
61
+ case_1 = !has_id # no id exists
62
+ case_2 = strict? && !is_strict_id # id isn't formatted properly
63
+ if (case_1 || case_2)
64
+ new_id = generate_id.to_s
65
+ # populate missing id
66
+ if case_1
67
+ Jekyll.logger.info("\n> Generate frontmatter ID for \n> #{doc.inspect}\n>with new ID: '#{new_id}'")
68
+ # replace invalid format id
69
+ elsif case_2
70
+ Jekyll.logger.info("\n> Replace frontmatter ID for:\n> #{doc.inspect}\n> from:'#{doc.data['id']}'\n> to:'#{new_id}'")
71
+ # um...
72
+ else
73
+ Jekyll.logger.warn("Jekyll-ID: Invalid ID case")
74
+ end
75
+ resp = request if !@testing
76
+ if @testing || ((@yesall || resp == "yes") && !@noall)
77
+ write_id(doc, new_id)
78
+ doc.data['id'] = new_id
79
+ end
80
+ end
81
+ end
82
+
83
+ def generate_id
84
+ has_size = (size.size != 0)
85
+ has_alpha = (alpha.size != 0)
86
+ return Nanoid.generate if !strict?
87
+ return Nanoid.generate(size: size, alphabet: alpha) if has_size && has_alpha
88
+ return Nanoid.generate(size: size) if has_size && !has_alpha
89
+ return Nanoid.generate(alphabet: alpha) if !has_size && has_alpha
90
+ end
91
+
92
+ def request
93
+ if !@yesall && !@noall
94
+ Jekyll.logger.info("\n> Is that ok?")
95
+ Jekyll.logger.info("> (yes, no, yesall, or noall)")
96
+ cont = gets
97
+ if cont.strip == "yesall"
98
+ @yesall = true
99
+ Jekyll.logger.info("> Handle all IDs...")
100
+ elsif cont.strip == "noall"
101
+ @noall = true
102
+ Jekyll.logger.info("> Leaving all IDs alone...")
103
+ elsif cont.strip == "yes"
104
+ Jekyll.logger.info("> Handling ID...")
105
+ elsif cont.strip == "no"
106
+ Jekyll.logger.info("> Leaving ID alone...")
107
+ else
108
+ Jekyll.logger.error("Jekyll-ID: Invalid response. Skipping...")
109
+ end
110
+ return cont.strip
111
+ end
112
+ end
113
+
114
+ def write_id(doc, id)
115
+ lines = IO.readlines(doc.path)
116
+ lines.delete_if { |l| l.include?("id: #{doc.data['id']}") }
117
+ lines.insert(1, "id: #{id}\n")
118
+ File.open(doc.path, 'w') do |file|
119
+ file.puts lines
120
+ end
121
+ end
122
+
123
+ # descriptor methods
124
+
125
+ def alpha_formatted?(id)
126
+ return true if !option_format(ALPHA_KEY)
127
+ return id.chars.all? { |char| alpha.include?(char) }
128
+ end
129
+
130
+ def size_formatted?(id)
131
+ return true if !option_format(SIZE_KEY)
132
+ return id.size == size
133
+ end
134
+
135
+ def strict?
136
+ return option(FORMAT_KEY) && (option_format(SIZE_KEY) || option_format(ALPHA_KEY))
137
+ end
138
+
139
+ # 'getters'
140
+
141
+ def alpha
142
+ return option_format(ALPHA_KEY).nil? ? '' : option_format(ALPHA_KEY)
143
+ end
144
+
145
+ def size
146
+ return option_format(SIZE_KEY).nil? ? '' : option_format(SIZE_KEY)
147
+ end
148
+
149
+ # config helpers
150
+
151
+ def disabled?
152
+ option(ENABLED_KEY) == false
153
+ end
154
+
155
+ def exclude?(type)
156
+ return false unless option(EXCLUDE_KEY)
157
+ return option(EXCLUDE_KEY).include?(type.to_s)
158
+ end
159
+
160
+ def option(key)
161
+ @config[CONFIG_KEY] && @config[CONFIG_KEY][key]
162
+ end
163
+
164
+ def option_format(key)
165
+ @config[CONFIG_KEY] && @config[CONFIG_KEY][FORMAT_KEY] && @config[CONFIG_KEY][FORMAT_KEY][key]
166
+ end
167
+
168
+ end
169
+
9
170
  end
10
171
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - manunamz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-24 00:00:00.000000000 Z
11
+ date: 2021-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: nanoid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.0
27
41
  description:
28
42
  email:
29
43
  - manunamz@pm.me
@@ -32,10 +46,11 @@ extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
34
48
  - lib/jekyll-id.rb
49
+ - lib/jekyll-id/patch/url_drop.rb
35
50
  - lib/jekyll-id/version.rb
36
51
  homepage: https://github.com/manunamz/jekyll-id
37
52
  licenses:
38
- - MIT
53
+ - GPL3
39
54
  metadata:
40
55
  homepage_uri: https://github.com/manunamz/jekyll-id
41
56
  source_code_uri: https://github.com/manunamz/jekyll-id
@@ -55,8 +70,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
70
  - !ruby/object:Gem::Version
56
71
  version: '0'
57
72
  requirements: []
58
- rubygems_version: 3.2.17
73
+ rubygems_version: 3.2.27
59
74
  signing_key:
60
75
  specification_version: 4
61
- summary: "You thought there was something in here, didn't you? \U0001F609"
76
+ summary: Add unique ID support for jekyll (markdown) documents.
62
77
  test_files: []