Skip to content

Commit a6868b4

Browse files
committed
Converting to Ruby gem
To make this plugin easier to include in Jekyll projects it has been converted into a gem. This also allows for some testing of more complex code, which has now been included.
1 parent 8f04358 commit a6868b4

15 files changed

Lines changed: 313 additions & 114 deletions

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
.repl_history
1515
build/
1616

17+
## Specific to Rubymine
18+
.idea/*
19+
1720
## Documentation cache and generated files:
1821
/.yardoc/
1922
/_yardoc/
@@ -27,8 +30,8 @@ build/
2730
# for a library or gem, you might want to ignore these files since the code is
2831
# intended to run in multiple environments; otherwise, check them in:
2932
# Gemfile.lock
30-
# .ruby-version
31-
# .ruby-gemset
33+
.ruby-version
34+
.ruby-gemset
3235

3336
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
3437
.rvmrc

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
PATH
2+
remote: .
3+
specs:
4+
jekyll_github_sample (0.1.0)
5+
activesupport (~> 4.0)
6+
jekyll (~> 1.0)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
activesupport (4.1.1)
12+
i18n (~> 0.6, >= 0.6.9)
13+
json (~> 1.7, >= 1.7.7)
14+
minitest (~> 5.1)
15+
thread_safe (~> 0.1)
16+
tzinfo (~> 1.1)
17+
blankslate (2.1.2.4)
18+
classifier (1.3.4)
19+
fast-stemmer (>= 1.0.0)
20+
colorator (0.1)
21+
commander (4.1.6)
22+
highline (~> 1.6.11)
23+
diff-lcs (1.2.5)
24+
fast-stemmer (1.0.2)
25+
ffi (1.9.3)
26+
highline (1.6.21)
27+
i18n (0.6.9)
28+
jekyll (1.5.1)
29+
classifier (~> 1.3)
30+
colorator (~> 0.1)
31+
commander (~> 4.1.3)
32+
liquid (~> 2.5.5)
33+
listen (~> 1.3)
34+
maruku (= 0.7.0)
35+
pygments.rb (~> 0.5.0)
36+
redcarpet (~> 2.3.0)
37+
safe_yaml (~> 1.0)
38+
toml (~> 0.1.0)
39+
json (1.8.1)
40+
liquid (2.5.5)
41+
listen (1.3.1)
42+
rb-fsevent (>= 0.9.3)
43+
rb-inotify (>= 0.9)
44+
rb-kqueue (>= 0.2)
45+
maruku (0.7.0)
46+
minitest (5.3.4)
47+
parslet (1.5.0)
48+
blankslate (~> 2.0)
49+
posix-spawn (0.3.8)
50+
pygments.rb (0.5.4)
51+
posix-spawn (~> 0.3.6)
52+
yajl-ruby (~> 1.1.0)
53+
rake (10.3.2)
54+
rb-fsevent (0.9.4)
55+
rb-inotify (0.9.4)
56+
ffi (>= 0.5.0)
57+
rb-kqueue (0.2.2)
58+
ffi (>= 0.5.0)
59+
redcarpet (2.3.0)
60+
rspec (2.14.1)
61+
rspec-core (~> 2.14.0)
62+
rspec-expectations (~> 2.14.0)
63+
rspec-mocks (~> 2.14.0)
64+
rspec-core (2.14.8)
65+
rspec-expectations (2.14.5)
66+
diff-lcs (>= 1.1.3, < 2.0)
67+
rspec-mocks (2.14.6)
68+
safe_yaml (1.0.3)
69+
thread_safe (0.3.4)
70+
toml (0.1.1)
71+
parslet (~> 1.5.0)
72+
tzinfo (1.2.0)
73+
thread_safe (~> 0.1)
74+
yajl-ruby (1.1.0)
75+
76+
PLATFORMS
77+
ruby
78+
79+
DEPENDENCIES
80+
bundler
81+
jekyll_github_sample!
82+
rake
83+
rspec

Rakefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'rspec/core/rake_task'
2+
3+
RSpec::Core::RakeTask.new(:spec)
4+
5+
task :default => :spec

github_sample_tag.rb

Lines changed: 0 additions & 112 deletions
This file was deleted.

jekyll_github_sample.gemspec

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path('../lib', __FILE__)
3+
require 'jekyll_github_sample/version'
4+
5+
Gem::Specification.new do |s|
6+
s.name = 'jekyll_github_sample'
7+
s.version = JekyllGithubSample::VERSION
8+
s.license = 'MIT'
9+
s.authors = ['Ben Willis']
10+
s.email = ['benjamin.willis@gmail.com']
11+
s.homepage = 'http://bwillis.github.io/jekyll-github-sample'
12+
s.summary = %q{Include a sample of a Github repo file.}
13+
s.description = %q{Easily reference and include a Github repo file in your post.}
14+
15+
s.files = `git ls-files -z`.split("\x0")
16+
s.test_files = `git ls-files -- {spec}/*`.split('\n')
17+
s.require_paths = ['lib']
18+
19+
s.add_dependency 'activesupport', '~> 4.0'
20+
s.add_dependency 'jekyll', '~> 1.0'
21+
22+
s.add_development_dependency 'rspec'
23+
s.add_development_dependency 'bundler'
24+
s.add_development_dependency 'rake'
25+
26+
end

lib/jekyll_github_sample.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'jekyll_github_sample/text_utils'
2+
require 'jekyll_github_sample/file_helper'
3+
require 'jekyll_github_sample/code_tag'
4+
require 'jekyll_github_sample/reference_tag'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'cgi'
2+
require 'open-uri'
3+
require 'active_support'
4+
require 'liquid'
5+
6+
module JekyllGithubSample
7+
class CodeTag < ::Liquid::Tag
8+
9+
include TextUtils
10+
11+
def initialize(tag_name, params, tokens)
12+
github_file_path, @line_start, @line_end = params.split
13+
@github_file = FileHelper.new(github_file_path)
14+
@line_start, @line_end = determine_line_numbers(@line_start, @line_end)
15+
super
16+
end
17+
18+
def render(context)
19+
all_lines = cache.fetch(@github_file.raw_uri) do
20+
open(@github_file.raw_uri).readlines
21+
end
22+
lines = all_lines[@line_start..@line_end]
23+
lines = remove_common_indentation(lines)
24+
lines.join
25+
end
26+
27+
private
28+
29+
def cache
30+
@@cache ||= ActiveSupport::Cache::MemoryStore.new
31+
end
32+
33+
def determine_line_numbers(first, last)
34+
if first.nil? && last.nil?
35+
first = 0
36+
last = -1
37+
elsif last.nil?
38+
last = first
39+
end
40+
41+
[first.to_i, last.to_i]
42+
end
43+
end
44+
end
45+
46+
Liquid::Template.register_tag('github_sample', JekyllGithubSample::CodeTag)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module JekyllGithubSample
2+
class FileHelper
3+
4+
GITHUB_RAW_URI = 'https://raw.githubusercontent.com/'
5+
GITHUB_URI = 'https://github.com/'
6+
WEB_URI_PART = 'blob'
7+
8+
attr_reader :filename, :user, :project_name
9+
10+
def initialize(path)
11+
@path = path
12+
parts = @path.split('/').delete_if { |e| e.empty? }
13+
if parts.include? WEB_URI_PART
14+
@user, @project_name, @blob, @commitish_or_branch = parts[0..3]
15+
@filename = File.join(parts[4..-1])
16+
else
17+
@user, @project_name, @commitish_or_branch = parts[0..2]
18+
@filename = File.join(parts[3..-1])
19+
end
20+
end
21+
22+
def user_uri
23+
File.join(GITHUB_URI, @user)
24+
end
25+
26+
def web_uri
27+
File.join(GITHUB_URI, @user, @project_name, WEB_URI_PART, @commitish_or_branch, @filename)
28+
end
29+
30+
def raw_uri
31+
File.join(GITHUB_RAW_URI, @user, @project_name, @commitish_or_branch, @filename)
32+
end
33+
end
34+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module JekyllGithubSample
2+
class ReferenceTag < CodeTag
3+
4+
def render(context)
5+
<<MARKUP.strip
6+
<div class="github-sample-reference">
7+
<div class="author-info">
8+
<a href="#{@github_file.web_uri}">This Github Sample</a> is by <a href="#{@github_file.user_uri}">#{@github_file.user}</a>
9+
</div>
10+
<div class="meta-info">
11+
#{@github_file.filename} <a href="#{@github_file.web_uri}">view</a> <a href="#{@github_file.raw_uri}">raw</a>
12+
</div>
13+
</div>
14+
MARKUP
15+
end
16+
end
17+
end
18+
19+
Liquid::Template.register_tag('github_sample_ref', JekyllGithubSample::ReferenceTag)

0 commit comments

Comments
 (0)