Skip to content

Commit 4ef4c6f

Browse files
committed
Plugins: add GitHubify plugin
Originally from Bitcoin.org by the same author as this commit.
1 parent ce4d143 commit 4ef4c6f

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

_plugins/githubify.rb

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

0 commit comments

Comments
 (0)