Skip to content

Commit

Permalink
Add Rubocop (#59)
Browse files Browse the repository at this point in the history
Merge pull request 59
  • Loading branch information
benbalter authored and jekyllbot committed Sep 21, 2017
1 parent a7db18f commit a0a9921
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 54 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/vendor/gems
.bundle
bin
.bundle
/*.gem
_site
Gemfile.lock
/pkg
_site
/vendor/gems
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
11 changes: 11 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
inherit_gem:
jekyll: .rubocop.yml

AllCops:
Exclude:
- 'vendor/**/*'


Metrics/BlockLength:
Exclude:
- spec/**/*
10 changes: 0 additions & 10 deletions Rakefile

This file was deleted.

29 changes: 14 additions & 15 deletions jemoji.gemspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
Gem::Specification.new do |s|
s.name = 'jemoji'
s.summary = 'GitHub-flavored emoji plugin for Jekyll'
s.version = '0.8.1'
s.authors = ['GitHub, Inc.']
s.email = 'support@github.com'
s.name = "jemoji"
s.summary = "GitHub-flavored emoji plugin for Jekyll"
s.version = "0.8.1"
s.authors = ["GitHub, Inc."]
s.email = "support@github.com"

s.homepage = 'https://github.com/jekyll/jemoji'
s.licenses = ['MIT']
s.files = [ 'lib/jemoji.rb' ]
s.homepage = "https://github.com/jekyll/jemoji"
s.licenses = ["MIT"]
s.files = [ "lib/jemoji.rb" ]

s.add_dependency 'jekyll', '>= 3.0'
s.add_dependency 'html-pipeline', '~> 2.2'
s.add_dependency 'activesupport', '~> 4.0', '>= 4.2.9'
s.add_dependency 'gemoji', '~> 3.0'
s.add_dependency "jekyll", "~> 3.0"
s.add_dependency "html-pipeline", "~> 2.2"
s.add_dependency "activesupport", "~> 4.0", ">= 4.2.9"
s.add_dependency "gemoji", "~> 3.0"

s.add_development_dependency 'rake'
s.add_development_dependency 'rdoc'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency "rspec", "~> 3.0"
s.add_development_dependency "rubocop", "~> 0.38"
end
35 changes: 20 additions & 15 deletions lib/jemoji.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'jekyll'
require 'gemoji'
require 'html/pipeline'
require "jekyll"
require "gemoji"
require "html/pipeline"

module Jekyll
class Emoji
Expand All @@ -11,15 +11,12 @@ class Emoji
class << self
def emojify(doc)
return unless doc.output =~ HTML::Pipeline::EmojiFilter.emoji_pattern
src = emoji_src(doc.site.config)
if doc.output.include? BODY_START_TAG
parsed_doc = Nokogiri::HTML::Document.parse(doc.output)
body = parsed_doc.at_css('body')
body.children = filter_with_emoji(src).call(body.inner_html)[:output].to_s
doc.output = parsed_doc.to_html
else
doc.output = filter_with_emoji(src).call(doc.output)[:output].to_s
end
doc.output = if doc.output.include? BODY_START_TAG
replace_document_body(doc)
else
src = emoji_src(doc.site.config)
filter_with_emoji(src).call(doc.output)[:output].to_s
end
end

# Public: Create or fetch the filter for the given {{src}} asset root.
Expand All @@ -29,8 +26,8 @@ def emojify(doc)
# Returns an HTML::Pipeline instance for the given asset root.
def filter_with_emoji(src)
filters[src] ||= HTML::Pipeline.new([
HTML::Pipeline::EmojiFilter
], { :asset_root => src, img_attrs: { align: nil } })
HTML::Pipeline::EmojiFilter,
], { :asset_root => src, :img_attrs => { :align => nil } })
end

# Public: Filters hash where the key is the asset root source.
Expand Down Expand Up @@ -78,10 +75,18 @@ def default_asset_root
"#{GITHUB_DOT_COM_ASSET_HOST_URL}#{ASSET_PATH}"
end
end

def replace_document_body(doc)
src = emoji_src(doc.site.config)
parsed_doc = Nokogiri::HTML::Document.parse(doc.output)
body = parsed_doc.at_css("body")
body.children = filter_with_emoji(src).call(body.inner_html)[:output].to_s
parsed_doc.to_html
end
end
end
end

Jekyll::Hooks.register [:pages, :documents], :post_render do |doc|
Jekyll::Hooks.register %i[pages documents], :post_render do |doc|
Jekyll::Emoji.emojify(doc) if Jekyll::Emoji.emojiable?(doc)
end
4 changes: 3 additions & 1 deletion script/cibuild
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#! /bin/sh

bundle exec rspec --color --require spec_helper $@
bundle exec rspec $@
script/fmt -S -D
bundle exec gem build jemoji.gemspec
2 changes: 2 additions & 0 deletions script/fmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
bundle exec rubocop $@
20 changes: 13 additions & 7 deletions spec/jemoji_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
require 'spec_helper'
# frozen_string_literal: true

require "spec_helper"

RSpec.describe(Jekyll::Emoji) do
Jekyll.logger.log_level = :error

let(:config_overrides) { {} }
let(:configs) do
Jekyll.configuration(config_overrides.merge({
'skip_config_files' => false,
'collections' => { 'docs' => { 'output' => true }, 'secret' => {} },
'source' => fixtures_dir,
'destination' => fixtures_dir('_site')
"skip_config_files" => false,
"collections" => { "docs" => { "output" => true }, "secret" => {} },
"source" => fixtures_dir,
"destination" => fixtures_dir("_site"),
}))
end
let(:emoji) { described_class }
let(:site) { Jekyll::Site.new(configs) }
let(:default_src) { "https://assets-cdn.github.com/images/icons/" }
let(:result) { "<img class=\"emoji\" title=\":+1:\" alt=\":+1:\" src=\"#{default_src}emoji/unicode/1f44d.png\" height=\"20\" width=\"20\">" }
let(:result) do
<<-STR.strip
<img class="emoji" title=":+1:" alt=":+1:" src="#{default_src}emoji/unicode/1f44d.png" height="20" width="20">
STR
end

let(:posts) { site.posts.docs }
let(:basic_post) { find_by_title(posts, "Refactor") }
Expand Down Expand Up @@ -88,7 +94,7 @@ def para(content)
let(:emoji_src) { "http://mine.club/" }
let(:config_overrides) do
{
"emoji" => { "src" => emoji_src }
"emoji" => { "src" => emoji_src },
}
end

Expand Down
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require File.expand_path('../lib/jemoji.rb', __dir__)
require File.expand_path("../lib/jemoji.rb", __dir__)

RSpec.configure do |config|
FIXTURES_DIR = File.expand_path('fixture_site', __dir__)
FIXTURES_DIR = File.expand_path("fixture_site", __dir__)

def fixtures_dir(*paths)
File.join(FIXTURES_DIR, *paths)
end

def find_by_title(docs, title)
docs.find { |d| d.title == title }
docs.find { |d| d.data["title"] == title }
end

def index_fixture
Expand Down

0 comments on commit a0a9921

Please sign in to comment.