forked from bitcoin-dot-org/Bitcoin.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubify.rb
73 lines (55 loc) · 1.75 KB
/
githubify.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# This file is licensed under the MIT License (MIT) available on
# http://opensource.org/licenses/MIT.
## githubify.rb automatically adds links to pull requests, issues, and
## commits using pattern matches
## Example (URL is the repository to link to):
## {% githubify https://github.com/bitcoin/bitcoin %}
## ...content...
## {% endgithubify %}
module Jekyll
require 'yaml'
class GitHubifyBlock < Liquid::Block
def initialize(tag_name, text, tokens)
super
@repository_url = text.strip()
end
def render(context)
output = super
## Convert #1234 into URL for the pull request
## If #1234 links to an issue, GitHub automatically redirects
#
## Require at least two digits to reduce false positive matches
output.gsub!(/#([0-9][0-9][0-9]*)/){ |s|
'<a href="' + @repository_url + '/pull/' + $1 + '">' + s + '</a>'
}
## Convert `123abcd` into URL for the commit
#
## Only operate on 7 to 10 chars to reduce false positive matches
output.gsub!(/`([0-9abcdef]{7,10})`/){ |s|
'<a href="' + @repository_url + '/commit/' + $1 + '">' + s + '</a>'
}
output
end
end
end
## Code to run if plugin is disabled
module Jekyll
require 'yaml'
class GitHubifyBlockDisabled < Liquid::Block
def initialize(tag_name, text, tokens)
super
end
def render(context)
output = super
output
end
end
end
#Do nothing if plugin is disabled
plugin_name = "githubify"
if !ENV['ENABLED_PLUGINS'].nil? and ENV['ENABLED_PLUGINS'].index(plugin_name).nil?
print plugin_name + ' disabled' + "\n"
Liquid::Template.register_tag(plugin_name, Jekyll::GitHubifyBlockDisabled)
else
Liquid::Template.register_tag(plugin_name, Jekyll::GitHubifyBlock)
end