Class: Jekyll::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-github.rb

Constant Summary collapse

InvalidJekyllGithubConfig =
Class.new(Jekyll::Errors::FatalException)
GH_COMMAND =
'gh@'
REGEX_S =
{
  'mention'     => /^[a-zA-Z0-9_.\-]+$/.freeze,
  'repo'        => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+$/.freeze,
  'repo_branch' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+:[a-zA-Z0-9_.\-]+$/.freeze,
  'issue_pr'    => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+#[1-9][0-9]*$/.freeze,
  'file'        => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-][a-zA-Z0-9_.\-\/]*$/.freeze,
  'file_branch' => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+:[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-][a-zA-Z0-9_.\-\/]*$/.freeze,
  'tag'         => /^[a-zA-Z0-9_.\-]+\/[a-zA-Z0-9_.\-]+=[a-zA-Z0-9_.\-]+$/.freeze
}.freeze
FORMAT_VARIABLES =
{
  'user'      => '<user>',
  'repo'      => '<repo>',
  'branch'    => '<branch>',
  'issue_pr'  => '<issue_pr>',
  'file'      => '<file>',
  'tag'       => '<tag>',
  'link'      => '<link>'
}
DEFAULT_CONFIG =
{
  'mention'     => '@<user>',
  'repo'        => '<user>/<repo>',
  'repo_branch' => '<user>/<repo>:<branch>',
  'issue_pr'    => '#<issue_pr>',
  'file'        => '<repo>/<file>',
  'file_branch' => '<repo>:<branch>/<file>',
  'tag'         => '<repo>=<tag>'
}

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Github

Returns a new instance of Github.



39
40
41
42
43
# File 'lib/jekyll-github.rb', line 39

def initialize(document)
  @content = document.content
  @config = DEFAULT_CONFIG
  configure(document.site.config, document.data)
end

Instance Method Details

#configure(site_data, doc_data) ⇒ Object



45
46
47
48
# File 'lib/jekyll-github.rb', line 45

def configure(site_data, doc_data)
  parse_data(site_data['jekyll-github'])
  parse_data(doc_data['jekyll-github'])
end

#parse_data(data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/jekyll-github.rb', line 50

def parse_data(data)
  case data
  when nil, NilClass
    return
  when Hash
    write_config(data)
  else raise InvalidJekyllGithubConfig,
             'Only Hash data type accepted as \'jekyll-github\' config'
  end
end

#process_file(word) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jekyll-github.rb', line 157

def process_file(word)
  items = word.split('/')
  user  = items[0]
  repo  = items[1]
  file  = items[2...(items.length)].join('/')
  text  = @config['file']
  text.gsub!(FORMAT_VARIABLES['user'], user)
  text.gsub!(FORMAT_VARIABLES['repo'], repo)
  text.gsub!(FORMAT_VARIABLES['file'], file)
  link = "https://github.com/#{user}/#{repo}/tree/master/#{file}"
  text.gsub!(FORMAT_VARIABLES['link'], link)
  return "[#{text}](#{link})"
end

#process_file_branch(word) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/jekyll-github.rb', line 171

def process_file_branch(word)
  items  = word.split(':')
  tempi  = items[0].split('/')
  user   = tempi[0]
  repo   = tempi[1]
  items  = items[1].split('/')
  branch = items[0]
  file   = items[1...(items.length)].join('/')
  text   = @config['file_branch']
  text.gsub!(FORMAT_VARIABLES['user'], user)
  text.gsub!(FORMAT_VARIABLES['repo'], repo)
  text.gsub!(FORMAT_VARIABLES['branch'], branch)
  text.gsub!(FORMAT_VARIABLES['file'], file)
  link = "https://github.com/#{user}/#{repo}/tree/#{branch}/#{file}"
  text.gsub!(FORMAT_VARIABLES['link'], link)
  return "[#{text}](#{link})"
end

#process_issue_pr(word) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/jekyll-github.rb', line 142

def process_issue_pr(word)
  items    = word.split('/')
  user     = items[0]
  items    = items[1].split('#')
  repo     = items[0]
  issue_pr = items[1]
  text     = @config['issue_pr']
  text.gsub!(FORMAT_VARIABLES['user'], user)
  text.gsub!(FORMAT_VARIABLES['repo'], repo)
  text.gsub!(FORMAT_VARIABLES['issue_pr'], issue_pr)
  link = "https://github.com/#{user}/#{repo}/pull/#{issue_pr}"
  text.gsub!(FORMAT_VARIABLES['link'], link)
  return "[#{text}](#{link})"
end

#process_mention(word) ⇒ Object



108
109
110
111
112
113
# File 'lib/jekyll-github.rb', line 108

def process_mention(word)
  text = @config['mention'].gsub(FORMAT_VARIABLES['user'], word)
  link = "https://github.com/#{word}"
  text.gsub!(FORMAT_VARIABLES['link'], link)
  return "[#{text}](#{link})"
end

#process_repo(word) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jekyll-github.rb', line 115

def process_repo(word)
  items = word.split('/')
  user  = items[0]
  repo  = items[1]
  text  = @config['repo']
  text.gsub!(FORMAT_VARIABLES['user'], user)
  text.gsub!(FORMAT_VARIABLES['repo'], repo)
  link = "https://github.com/#{user}/#{repo}"
  text.gsub!(FORMAT_VARIABLES['link'], link)
  return "[#{text}](#{link})"
end

#process_repo_branch(word) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jekyll-github.rb', line 127

def process_repo_branch(word)
  items  = word.split('/')
  user   = items[0]
  items  = items[1].split(':')
  repo   = items[0]
  branch = items[1]
  text   = @config['repo_branch']
  text.gsub!(FORMAT_VARIABLES['user'], user)
  text.gsub!(FORMAT_VARIABLES['repo'], repo)
  text.gsub!(FORMAT_VARIABLES['branch'], branch)
  link = "https://github.com/#{user}/#{repo}/tree/#{branch}"
  text.gsub!(FORMAT_VARIABLES['link'], link)
  return "[#{text}](#{link})"
end

#process_tag(word) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/jekyll-github.rb', line 189

def process_tag(word)
  items = word.split('/')
  user  = items[0]
  items = items[1].split('=')
  repo  = items[0]
  tag   = items[1]
  text  = @config['tag']
  text.gsub!(FORMAT_VARIABLES['user'], user)
  text.gsub!(FORMAT_VARIABLES['repo'], repo)
  text.gsub!(FORMAT_VARIABLES['tag'], tag)
  link = "https://github.com/#{user}/#{repo}/releases/tag/#{tag}"
  text.gsub!(FORMAT_VARIABLES['link'], link)
  return "[#{text}](#{link})"
end

#process_word(word) ⇒ Object



83
84
85
86
87
# File 'lib/jekyll-github.rb', line 83

def process_word(word)
  content = word[(GH_COMMAND.length)...(word.length)]
  REGEX_S.each { |key, val| content.match(val) { return segregate_word(key, content) } }
  return word
end

#render_contentObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jekyll-github.rb', line 65

def render_content
  return @content unless @content.include?(GH_COMMAND)
  i = -1 # as it increments first
  while i < @content.length do
    i += 1
    next unless @content[i, GH_COMMAND.length] == GH_COMMAND
    s = i
    while i < @content.length do
      i += 1
      break if @content[i].match(/\s/)
    end
    e = i
    word = process_word(@content[s...e])
    @content = @content[0...s] + word + @content[e...(@content.length)]
  end
  return @content
end

#segregate_word(pattern, word) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jekyll-github.rb', line 89

def segregate_word(pattern, word)
  case pattern
  when 'mention'
    return process_mention(word)
  when 'repo'
    return process_repo(word)
  when 'repo_branch'
    return process_repo_branch(word)
  when 'issue_pr'
    return process_issue_pr(word)
  when 'file'
    return process_file(word)
  when 'file_branch'
    return process_file_branch(word)
  when 'tag'
    return process_tag(word)
  end
end

#write_config(data) ⇒ Object



61
62
63
# File 'lib/jekyll-github.rb', line 61

def write_config(data)
  data.each { |key, val| @config[key] = val if DEFAULT_CONFIG.key?(key) }
end