Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Jan 10, 2016
0 parents commit f9a6674
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.gem
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Style/AlignHash:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table

Style/Documentation:
Enabled: false

Lint/EndAlignment:
AlignWith: variable
AutoCorrect: true
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: ruby
rvm:
- 2.3.0
before_install: gem install bundler -v 1.11.2
sudo: false
cache: bundler
script: script/cibuild
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in jekyll-avatar.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Ben Balter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Jekyll Avatar

*A Jekyll plugin for rendering GitHub avatars*

Jekyll Avatar makes it easy to add GitHub avatars to your Jekyll site by specifying a username. If performance is a concern, Jekyll Avatar is deeply integrated with the GitHub avatar API, ensuring avatars are cached and load in parallel.

## Installation

Add the following to your site's `Gemfile`:

```ruby
gem 'jekyll-avatar'
```

And add the following to your site's `_config.yml` file:

```yaml
gems:
- jekyll-avatar
```
## Usage
Simply add the following, anywhere you'd like a user's avatar to appear:
```
{% avatar [USERNAME] %}
```

With `[USERNAME]` being the user's GitHub username:

```
{% avatar hubot %}
```

That will output:

```html
<img class="avatar avatar-small" src="https://avatars3.githubusercontent.com/hubot?v=3&amp;s=40" alt="hubot" width="40" height="40" />
```

### Customizing

You can customize the size of the resulting avatar by passing the size arugment:

```
{% avatar hubot size=25 %}
```

That will output:

```html
<img class="avatar avatar-small" src="https://avatars3.githubusercontent.com/hubot?v=3&amp;s=25" alt="hubot" width="25" height="25" />
```
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
27 changes: 27 additions & 0 deletions jekyll-avatar.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'jekyll/avatar/version'

Gem::Specification.new do |spec|
spec.name = 'jekyll-avatar'
spec.version = Jekyll::Avatar::VERSION
spec.authors = ['Ben Balter']
spec.email = ['[email protected]']

spec.summary = 'A Jekyll plugin for rendering GitHub avatars'
spec.homepage = 'https://github.com/benbalter/jekyll-avatar'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject do |file|
file.match(%r{^(test|spec|features)/})
end

spec.require_paths = ['lib']

spec.add_dependency 'jekyll', '>= 2.4'
spec.add_development_dependency 'bundler', '~> 1.11'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop'
end
61 changes: 61 additions & 0 deletions lib/jekyll/avatar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'jekyll/avatar/version'

module Jekyll
class Avatar < Liquid::Tag
SERVERS = 4
DEFAULT_SIZE = 40
VERSION = 3

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

def render(_context)
tag = '<img '

# See http://primercss.io/avatars/#small-avatars
if size < 48
tag << 'class="avatar avatar-small" '
else
tag << 'class="avatar" '
end

tag << "src=\"#{url}\" alt=\"#{username}\" "
tag << "width=\"#{size}\" height=\"#{size}\" "
tag << '/>'
tag
end

private

def username
@username ||= @text.split(' ').first.sub('@', '')
end

def size
@size ||= begin
matches = @text.match(/\bsize=(\d+)\b/i)
matches ? matches[1].to_i : DEFAULT_SIZE
end
end

def path
@path ||= "#{username}?v=#{VERSION}&s=#{size}"
end

def server_number
@server_number ||= Zlib.crc32(path) % SERVERS
end

def host
@host ||= "avatars#{server_number}.githubusercontent.com"
end

def url
@url ||= "https://#{host}/#{path}"
end
end
end

Liquid::Template.register_tag('avatar', Jekyll::Avatar)
6 changes: 6 additions & 0 deletions lib/jekyll/avatar/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Liquid; class Tag; end; end
module Jekyll
class Avatar < Liquid::Tag
VERSION = '0.1.0'
end
end
5 changes: 5 additions & 0 deletions script/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -ex

bundle install
6 changes: 6 additions & 0 deletions script/cibuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -ex

bundle exec rake spec
bundle exec rubocop
82 changes: 82 additions & 0 deletions spec/jekyll/avatar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'spec_helper'

describe Jekyll::Avatar do
let(:doc) { doc_with_content(content) }
let(:username) { 'hubot' }
let(:args) { '' }
let(:text) { "#{username} #{args}".squeeze(' ') }
let(:content) { "{% avatar #{text} %}" }
let(:rendered) { subject.render(nil) }
let(:output) do
doc.content = content
doc.output = Jekyll::Renderer.new(doc.site, doc).run
end
subject { Jekyll::Avatar.send(:new, 'avatar', text, nil) }

it 'has a version number' do
expect(Jekyll::Avatar::VERSION).not_to be nil
end

it 'outputs the HTML' do
expected = '<img class="avatar avatar-small" '
expected << 'src="https://avatars3.githubusercontent.com/'
expected << 'hubot?v=3&amp;s=40" alt="hubot" width="40" height="40" />'
expect(rendered)
expect(output).to eql("<p>#{expected}</p>\n")
end

it 'parses the username' do
expect(subject.send(:username)).to eql('hubot')
end

it 'determines the server number' do
expect(subject.send(:server_number)).to eql(3)
end

it 'builds the host' do
expect(subject.send(:host)).to eql('avatars3.githubusercontent.com')
end

it 'builds the path' do
expect(subject.send(:path)).to eql('hubot?v=3&s=40')
end

it 'defaults to the default size' do
expect(subject.send(:size)).to eql(40)
end

it 'builds the URL' do
expected = 'https://avatars3.githubusercontent.com/hubot?v=3&s=40'
expect(subject.send(:url)).to eql(expected)
end

context 'when passed @hubot as a username' do
let(:username) { '@hubot' }

it 'parses the username' do
expect(subject.send(:username)).to eql('hubot')
end
end

context 'with a size is passed' do
let(:args) { 'size=45' }

it "parses the user's requested size" do
expect(subject.send(:size)).to eql(45)
end
end

context 'with a size < 48' do
it 'includes the avatar-small class' do
expect(rendered).to match(/avatar-small/)
end
end

context 'with a size > 48' do
let(:args) { 'size=80' }

it "doesn't include the avatar-small class" do
expect(rendered).to_not match(/avatar-small/)
end
end
end
44 changes: 44 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require 'jekyll'
require 'jekyll/avatar'

TEST_DIR = File.dirname(__FILE__)
TMP_DIR = File.expand_path('../tmp', TEST_DIR)

def doc_with_content(_content, opts = {})
my_site = site(opts)
options = { site: my_site, collection: collection(my_site) }
Jekyll::Document.new(source_dir('_test/doc.md'), options)
end

def tmp_dir(*files)
File.join(TMP_DIR, *files)
end

def source_dir(*files)
tmp_dir('source', *files)
end

def dest_dir(*files)
tmp_dir('dest', *files)
end

def collection(site, label = 'test')
Jekyll::Collection.new(site, label)
end

def site(opts = {})
defaults = Jekyll::Configuration::DEFAULTS
opts = opts.merge(
'source' => source_dir,
'destination' => dest_dir
)
conf = Jekyll::Utils.deep_merge_hashes(defaults, opts)
Jekyll::Site.new(conf)
end

def fixture(name)
path = File.expand_path "./fixtures/#{name}.json", File.dirname(__FILE__)
File.open(path).read
end

0 comments on commit f9a6674

Please sign in to comment.