Skip to content

Commit

Permalink
Replaced API url with github user & repo for convenience.
Browse files Browse the repository at this point in the history
  • Loading branch information
polarblau committed Apr 17, 2012
1 parent 5cf8ece commit ed06a24
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 28 deletions.
48 changes: 30 additions & 18 deletions lib/jekyll/tags/hasty_comments.rb
@@ -1,53 +1,65 @@
module Jekyll
class HastyCommentsTag < Liquid::Tag

API_REPOS_URL = 'https://api.github.com/repos/'

def initialize(tag_name, text, tokens)
super
@text = text
end

def render(context)
file = file_name(context)

attributes = {
'id' => 'comments',
'data-commits-url' => github_commits_url,
'data-commit-ids' => commit_ids(file)
}
generate_tag(attributes)
file = current_file(context)
generate_tag(file)
end

#

def file_name(context)
# the "caller" file
def current_file(context)
context.environments.first["page"]["file_name"]
end

# gather all commit hashes affecting [file]
def commit_ids(file)
cmd = "git log --pretty=format:'%H' --follow #{file}"
`#{cmd}`.split(/\W+/)
end

# get github repository information for current directoy
def repo
return @repo if defined?(@repo)
url = `git config --get remote.origin.url`.chomp
url.gsub!(%r{git://github.com/(.*\.git)}, 'git@github.com:\1')

if url =~ /^git@github/
url.scan(%r{git@github.com:(.*).git}).flatten.first
@repo = url.scan(%r{git@github.com:(.*).git}).flatten.first
else
# TODO: proper exception
raise "only supports github URLs"
end
end

def github_commits_url
File.join(API_REPOS_URL, repo, 'commits')
# extract github user
def github_user
repo.split('/').first
end

# extract github repository name
def github_repo
repo.split('/').last
end

# gather attributes for the tag
def attributes(file)
{
'id' => 'comments',
'data-github-user' => github_user,
'data-github-repo' => github_repo,
'data-commit-ids' => commit_ids(file)
}
end

def generate_tag(attributes)
attr = attributes.map{|k, v| "#{k}='#{v}'"}.join(' ')
# generates <div/> tag and assigns [attributes]
# as attributes to it
def generate_tag(file)
attr = attributes(file).map{|k, v| "#{k}='#{v}'"}.join(' ')
"<div #{attr}>#{@text}</div>"
end

Expand Down
64 changes: 54 additions & 10 deletions test/jekyll/tags/hasty_comments_test.rb
Expand Up @@ -26,11 +26,15 @@ def setup
@tag = Jekyll::HastyCommentsTag.new('foo', 'bar', 'bat')
end

def test_file_name
def teardown
@tag = nil
end

def test_current_file
context = mock
context.expects(:environments).
returns([{'page' => {'file_name' => 'foo.md'}}])
assert_equal @tag.file_name(context), 'foo.md'
assert_equal @tag.current_file(context), 'foo.md'
end

def test_commit_ids
Expand Down Expand Up @@ -73,24 +77,64 @@ def test_repo_system_call_params
assert_equal @tag.repo, 'polarblau/jekyll-hasty-test-blog'
end


def test_repo_return
@tag.expects(:`).
with('git config --get remote.origin.url').
returns(VALID_GITHUB_REPO)
@tag.repo
end

def test_github_commits_url
@tag.expects(:repo).returns('the/repository')
assert_equal @tag.github_commits_url,
"#{Jekyll::HastyCommentsTag::API_REPOS_URL}the/repository/commits"
def test_github_user
@tag.expects(:repo).returns('user/repository')
assert_equal @tag.github_user, 'user'
end

def test_github_repo
@tag.expects(:repo).returns('user/repository')
assert_equal @tag.github_repo, 'repository'
end

def test_generate_tag
attr = {'foo' => 'bar'}
assert_equal @tag.generate_tag(attr),
"<div foo='bar'>bar</div>"
@tag.expects(:commit_ids).returns(['123abc'])
@tag.expects(:github_user).returns('user')
@tag.expects(:github_repo).returns('repo')

assert_equal @tag.generate_tag('file.md'),
"<div id='comments' data-github-user='user' data-github-repo='repo' data-commit-ids='[\"123abc\"]'>bar</div>"
end

def test_attributes_id
@tag.expects(:commit_ids).returns(['123'])

attr = @tag.attributes('foo.md')
assert attr.keys.include? 'id'
assert_equal attr['id'], 'comments'
end

def test_attributes_github_user
@tag.expects(:github_user).returns('user')
@tag.expects(:commit_ids).returns(['123'])

attr = @tag.attributes('foo.md')
assert attr.keys.include? 'data-github-user'
assert_equal attr['data-github-user'], 'user'
end

def test_attributes_github_repo
@tag.expects(:github_repo).returns('repo')
@tag.expects(:commit_ids).returns(['123'])

attr = @tag.attributes('foo.md')
assert attr.keys.include? 'data-github-repo'
assert_equal attr['data-github-repo'], 'repo'
end

def test_attributes_commit_ids
@tag.expects(:commit_ids).returns(['123'])

attr = @tag.attributes('foo.md')
assert attr.keys.include? 'data-commit-ids'
assert_equal attr['data-commit-ids'], ['123']
end

end

0 comments on commit ed06a24

Please sign in to comment.