jekyll-github 1.0.0

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-github.rb +209 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5fa8ba69d5a59df8a37ae2605b0529c1582e1705976a872c2b598cbd1ef284e8
4
+ data.tar.gz: 2c500758db9053f1a13bc632ed2fdb5a8f6ea8c4998808771b0039319edf61d7
5
+ SHA512:
6
+ metadata.gz: 5a21ccd72463421afc2955c3413427446302a05e7d47eb7dd79216db8ba7bf81111060d2c9930a9ec1452ea1a57c54a7383af0f35f972afb55bca5897e9e9117
7
+ data.tar.gz: 0f0e08201ed29a474908512cb49274fc9bd54713350f022db35bcc4633c34a332164b2d87904efadd21374598ef9ea30ea01e5f315aeb0f3223f71772ff260c8
@@ -0,0 +1,209 @@
1
+ require 'jekyll'
2
+
3
+ module Jekyll
4
+ class Github
5
+ InvalidJekyllGithubConfig = Class.new(Jekyll::Errors::FatalException)
6
+
7
+ GH_COMMAND = 'gh@'
8
+
9
+ REGEX_S = {
10
+ 'mention' => /^[a-zA-Z0-9_.\-]+$/.freeze,
11
+ 'repo' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+$/.freeze,
12
+ 'repo_branch' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+:[a-zA-Z0-9_.\-]+$/.freeze,
13
+ 'issue_pr' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+#[1-9][0-9]*$/.freeze,
14
+ 'file' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-][a-zA-Z0-9_.\-\/]*$/.freeze,
15
+ 'file_branch' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+:[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-][a-zA-Z0-9_.\-\/]*$/.freeze,
16
+ 'tag' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+=[a-zA-Z0-9_.\-]+$/.freeze
17
+ }.freeze
18
+
19
+ FORMAT_VARIABLES = {
20
+ 'user' => '<user>',
21
+ 'repo' => '<repo>',
22
+ 'branch' => '<branch>',
23
+ 'issue_pr' => '<issue_pr>',
24
+ 'file' => '<file>',
25
+ 'tag' => '<tag>',
26
+ 'link' => '<link>'
27
+ }
28
+
29
+ DEFAULT_CONFIG = {
30
+ 'mention' => '@<user>',
31
+ 'repo' => '<user>/<repo>',
32
+ 'repo_branch' => '<user>/<repo>:<branch>',
33
+ 'issue_pr' => '#<issue_pr>',
34
+ 'file' => '<repo>/<file>',
35
+ 'file_branch' => '<repo>:<branch>/<file>',
36
+ 'tag' => '<repo>=<tag>'
37
+ }
38
+
39
+ def initialize(document)
40
+ @content = document.content
41
+ @config = DEFAULT_CONFIG
42
+ configure(document.site.config, document.data)
43
+ end
44
+
45
+ def configure(site_data, doc_data)
46
+ parse_data(site_data['jekyll-github'])
47
+ parse_data(doc_data['jekyll-github'])
48
+ end
49
+
50
+ def parse_data(data)
51
+ case data
52
+ when nil, NilClass
53
+ return
54
+ when Hash
55
+ write_config(data)
56
+ else raise InvalidJekyllGithubConfig,
57
+ 'Only Hash data type accepted as \'jekyll-github\' config'
58
+ end
59
+ end
60
+
61
+ def write_config(data)
62
+ data.each { |key, val| @config[key] = val if DEFAULT_CONFIG.key?(key) }
63
+ end
64
+
65
+ def render_content
66
+ return @content unless @content.include?(GH_COMMAND)
67
+ i = -1 # as it increments first
68
+ while i < @content.length do
69
+ i += 1
70
+ next unless @content[i, GH_COMMAND.length] == GH_COMMAND
71
+ s = i
72
+ while i < @content.length do
73
+ i += 1
74
+ break if @content[i].match(/\s/)
75
+ end
76
+ e = i
77
+ word = process_word(@content[s...e])
78
+ @content = @content[0...s] + word + @content[e...(@content.length)]
79
+ end
80
+ return @content
81
+ end
82
+
83
+ def process_word(word)
84
+ content = word[(GH_COMMAND.length)...(word.length)]
85
+ REGEX_S.each { |key, val| content.match(val) { return segregate_word(key, content) } }
86
+ return word
87
+ end
88
+
89
+ def segregate_word(pattern, word)
90
+ case pattern
91
+ when 'mention'
92
+ return process_mention(word)
93
+ when 'repo'
94
+ return process_repo(word)
95
+ when 'repo_branch'
96
+ return process_repo_branch(word)
97
+ when 'issue_pr'
98
+ return process_issue_pr(word)
99
+ when 'file'
100
+ return process_file(word)
101
+ when 'file_branch'
102
+ return process_file_branch(word)
103
+ when 'tag'
104
+ return process_tag(word)
105
+ end
106
+ end
107
+
108
+ def process_mention(word)
109
+ text = @config['mention'].gsub(FORMAT_VARIABLES['user'], word)
110
+ link = "https://github.com/#{word}"
111
+ text.gsub!(FORMAT_VARIABLES['link'], link)
112
+ return "[#{text}](#{link})"
113
+ end
114
+
115
+ def process_repo(word)
116
+ items = word.split('/')
117
+ user = items[0]
118
+ repo = items[1]
119
+ text = @config['repo']
120
+ text.gsub!(FORMAT_VARIABLES['user'], user)
121
+ text.gsub!(FORMAT_VARIABLES['repo'], repo)
122
+ link = "https://github.com/#{user}/#{repo}"
123
+ text.gsub!(FORMAT_VARIABLES['link'], link)
124
+ return "[#{text}](#{link})"
125
+ end
126
+
127
+ def process_repo_branch(word)
128
+ items = word.split('/')
129
+ user = items[0]
130
+ items = items[1].split(':')
131
+ repo = items[0]
132
+ branch = items[1]
133
+ text = @config['repo_branch']
134
+ text.gsub!(FORMAT_VARIABLES['user'], user)
135
+ text.gsub!(FORMAT_VARIABLES['repo'], repo)
136
+ text.gsub!(FORMAT_VARIABLES['branch'], branch)
137
+ link = "https://github.com/#{user}/#{repo}/tree/#{branch}"
138
+ text.gsub!(FORMAT_VARIABLES['link'], link)
139
+ return "[#{text}](#{link})"
140
+ end
141
+
142
+ def process_issue_pr(word)
143
+ items = word.split('/')
144
+ user = items[0]
145
+ items = items[1].split('#')
146
+ repo = items[0]
147
+ issue_pr = items[1]
148
+ text = @config['issue_pr']
149
+ text.gsub!(FORMAT_VARIABLES['user'], user)
150
+ text.gsub!(FORMAT_VARIABLES['repo'], repo)
151
+ text.gsub!(FORMAT_VARIABLES['issue_pr'], issue_pr)
152
+ link = "https://github.com/#{user}/#{repo}/pull/#{issue_pr}"
153
+ text.gsub!(FORMAT_VARIABLES['link'], link)
154
+ return "[#{text}](#{link})"
155
+ end
156
+
157
+ def process_file(word)
158
+ items = word.split('/')
159
+ user = items[0]
160
+ repo = items[1]
161
+ file = items[2...(items.length)].join('/')
162
+ text = @config['file']
163
+ text.gsub!(FORMAT_VARIABLES['user'], user)
164
+ text.gsub!(FORMAT_VARIABLES['repo'], repo)
165
+ text.gsub!(FORMAT_VARIABLES['file'], file)
166
+ link = "https://github.com/#{user}/#{repo}/tree/master/#{file}"
167
+ text.gsub!(FORMAT_VARIABLES['link'], link)
168
+ return "[#{text}](#{link})"
169
+ end
170
+
171
+ def process_file_branch(word)
172
+ items = word.split(':')
173
+ tempi = items[0].split('/')
174
+ user = tempi[0]
175
+ repo = tempi[1]
176
+ items = items[1].split('/')
177
+ branch = items[0]
178
+ file = items[1...(items.length)].join('/')
179
+ text = @config['file_branch']
180
+ text.gsub!(FORMAT_VARIABLES['user'], user)
181
+ text.gsub!(FORMAT_VARIABLES['repo'], repo)
182
+ text.gsub!(FORMAT_VARIABLES['branch'], branch)
183
+ text.gsub!(FORMAT_VARIABLES['file'], file)
184
+ link = "https://github.com/#{user}/#{repo}/tree/#{branch}/#{file}"
185
+ text.gsub!(FORMAT_VARIABLES['link'], link)
186
+ return "[#{text}](#{link})"
187
+ end
188
+
189
+ def process_tag(word)
190
+ items = word.split('/')
191
+ user = items[0]
192
+ items = items[1].split('=')
193
+ repo = items[0]
194
+ tag = items[1]
195
+ text = @config['tag']
196
+ text.gsub!(FORMAT_VARIABLES['user'], user)
197
+ text.gsub!(FORMAT_VARIABLES['repo'], repo)
198
+ text.gsub!(FORMAT_VARIABLES['tag'], tag)
199
+ link = "https://github.com/#{user}/#{repo}/releases/tag/#{tag}"
200
+ text.gsub!(FORMAT_VARIABLES['link'], link)
201
+ return "[#{text}](#{link})"
202
+ end
203
+ end
204
+ end
205
+
206
+ Jekyll::Hooks.register [:pages, :documents], :pre_render do |doc|
207
+ github_doc = Jekyll::Github.new(doc)
208
+ doc.content = github_doc.render_content
209
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-github
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - vrongmeal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-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: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/jekyll-github.rb
34
+ homepage: https://github.com/vrongmeal/jekyll-github
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.3.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.0.1
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Jekyll plugin for Github
57
+ test_files: []