diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..62ba652 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,11 @@ +inherit_from: .rubocop_todo.yml + +require: rubocop-jekyll +inherit_gem: + rubocop-jekyll: .rubocop.yml + +AllCops: + TargetRubyVersion: 2.3 + Exclude: + - vendor/**/* + - spec/*.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..3db0a29 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,21 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2019-07-16 18:09:04 +0200 using RuboCop version 0.71.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +Lint/ShadowedException: + Exclude: + - 'lib/jekyll-gist/gist_tag.rb' + +# Offense count: 1 +Metrics/AbcSize: + Max: 24 + +# Offense count: 1 +# Configuration parameters: CountComments, ExcludedMethods. +Metrics/MethodLength: + Max: 21 diff --git a/.travis.yml b/.travis.yml index 5aed6ab..5918f53 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,25 +1,24 @@ language: ruby +cache: bundler +rvm: + - &latest_ruby 2.7 + - 2.5 + script : script/cibuild -sudo: false -notifications: - email: false + branches: only: - master - -rvm: - - 2.2 - - 2.1 - - 2.0 env: - - "" - - JEKYLL_VERSION=3.0.0.beta8 - - JEKYLL_VERSION=2.0 + matrix: + - JEKYLL_VERSION="~> 3.8" matrix: include: - # GitHub Pages - rvm: 2.1.1 - env: GH_PAGES=true - - # Ruby 1.9 - rvm: 1.9 - env: JEKYLL_VERSION=2.0 + rvm: 2.5.3 + env: JEKYLL_VERSION="~> 3.8.5" + - rvm: *latest_ruby + env: JEKYLL_VERSION="~> 4.0" + +notifications: + email: false diff --git a/Gemfile b/Gemfile index 4296d7f..46cdbf0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,6 @@ -source 'https://rubygems.org' +# frozen_string_literal: true + +source "https://rubygems.org" gemspec -if ENV["GH_PAGES"] - gem "github-pages" -elsif ENV["JEKYLL_VERSION"] - gem "jekyll", "~> #{ENV["JEKYLL_VERSION"]}" -end +gem "jekyll", ENV["JEKYLL_VERSION"] if ENV["JEKYLL_VERSION"] diff --git a/History.markdown b/History.markdown index 7405d33..a254054 100644 --- a/History.markdown +++ b/History.markdown @@ -1,3 +1,42 @@ +## HEAD + +### Development Fixes + + * Test against Ruby 2.5 (#57) + * Rely on rubocop-jekyll (#62) + * chore(deps): rubocop-jekyll 0.3 (#64) + * Refactor GistTag (#65) + +### Major Enhancements + + * chore(deps): drop support for Liquid < 4.0 (#66) + +### Bug Fixes + + * Re-introduce Ruby 2.3 support and test Jekyll 3.7+ (#72) + * Use Liquid::Tag#raw to clarify error message (#73) + +## 1.5.0 / 2017-12-03 + +### Documentation + + * replace 'plugins' key in config with 'gems' (#46) + * Docs: Remove username in gist (#54) + +### Development Fixes + + * Remove testing for Jekyll 2.x + * Requires Ruby > 2.1 + * Add release script + * Inherit Jekyll's rubocop config for consistency (#48) + * define path with __dir__ (#47) + +## 1.4.1 / 2017-06-21 + + * Don't ask .empty? until it's a String. (#38) + * rename Liquid 4 `has_key?` to `key?` to add compatibility for liquid 4 (#41) + * Test against Ruby 2.1 to 2.4 (#45) + ## 1.4.0 / 2015-12-01 * Allow `noscript` fallback to be disabled (#29) @@ -21,7 +60,7 @@ * Re-add support for Ruby 1.9.3. Fixes #11 for 1.9.3 (#14) * Replaced `OpenURI` with `Net::HTTP` and introduced timeout of 3 seconds (#11) -## 1.3.1 / 2015-08-16 (yanked) +## 1.3.1 / 2015-08-16 * Replaced `OpenURI` with `Net::HTTP` and introduced timeout of 3 seconds (#11) diff --git a/LICENSE.txt b/LICENSE.txt index 3ed9f59..9bb9d93 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2014 Parker Moore +Copyright (c) 2014-present Parker Moore and jekyll-gist contributors MIT License diff --git a/README.md b/README.md index cd39080..c657c5d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Liquid tag for displaying GitHub Gists in Jekyll sites: `{% gist %}`. Add this line to your application's Gemfile: - gem 'jekyll-gist' + $ gem 'jekyll-gist' And then execute: @@ -18,19 +18,21 @@ Or install it yourself as: $ gem install jekyll-gist -Finally, add the following to your site's `_config.yml`: +Then add the following to your site's `_config.yml`: ``` -gems: +plugins: - jekyll-gist ``` +💡 If you are using a Jekyll version less than 3.5.0, use the `gems` key instead of `plugins`. + ## Usage Use the tag as follows in your Jekyll pages, posts and collections: ```liquid -{% gist parkr/c08ee0f2726fd0e3909d %} +{% gist c08ee0f2726fd0e3909d %} ``` This will create the associated script tag: @@ -42,7 +44,7 @@ This will create the associated script tag: You may optionally specify a `filename` after the `gist_id`: ```liquid -{% gist parkr/c08ee0f2726fd0e3909d test.md %} +{% gist c08ee0f2726fd0e3909d test.md %} ``` This will produce the correct URL to show just the specified file in your post rather than the entire Gist. @@ -51,7 +53,7 @@ This will produce the correct URL to show just the specified file in your post r ## Disabling `noscript` support -By default, Jekyll Gist will make an HTTP call per Gist to retrieve the raw content of the Gist. This information is used to propagate `noscript` tags for search engines and browsers without Javascript support. If you'd like to disable this feature, for example, to speed up builds locally, simply add the following to your site's `_config.yml`: +By default, Jekyll Gist will make an HTTP call per Gist to retrieve the raw content of the Gist. This information is used to propagate `noscript` tags for search engines and browsers without JavaScript support. If you'd like to disable this feature, for example, to speed up builds locally, add the following to your site's `_config.yml`: ```yml gist: diff --git a/Rakefile b/Rakefile index 809eb56..5263b58 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,3 @@ -require "bundler/gem_tasks" +# frozen_string_literal: true +require "bundler/gem_tasks" diff --git a/jekyll-gist.gemspec b/jekyll-gist.gemspec index 8b62a2a..ba7a55e 100644 --- a/jekyll-gist.gemspec +++ b/jekyll-gist.gemspec @@ -1,28 +1,28 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'jekyll-gist/version' +# frozen_string_literal: true + +require_relative "lib/jekyll-gist/version" Gem::Specification.new do |spec| spec.name = "jekyll-gist" spec.version = Jekyll::Gist::VERSION spec.authors = ["Parker Moore"] spec.email = ["parkrmoore@gmail.com"] - spec.summary = %q{Liquid tag for displaying GitHub Gists in Jekyll sites.} + spec.summary = "Liquid tag for displaying GitHub Gists in Jekyll sites." spec.homepage = "https://github.com/jekyll/jekyll-gist" spec.license = "MIT" - spec.required_ruby_version = '>= 1.9.3' - spec.files = `git ls-files -z`.split("\x0") - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.test_files = spec.files.grep(%r!^spec/!) spec.require_paths = ["lib"] - spec.add_dependency "octokit", "~> 4.2" - spec.add_development_dependency "bundler", "~> 1.6" + spec.required_ruby_version = ">= 2.3.0" + + spec.add_runtime_dependency "jekyll", ">= 3.7", "< 5.0" + spec.add_runtime_dependency "octokit", "~> 4.2" + + spec.add_development_dependency "bundler" spec.add_development_dependency "rake" spec.add_development_dependency "rspec" + spec.add_development_dependency "rubocop-jekyll", "~> 0.4" spec.add_development_dependency "webmock" - spec.add_development_dependency "jekyll", ">= 2.0" end diff --git a/lib/jekyll-gist.rb b/lib/jekyll-gist.rb index 3a82b0a..728e7ce 100644 --- a/lib/jekyll-gist.rb +++ b/lib/jekyll-gist.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "jekyll-gist/version" require "jekyll-gist/gist_tag" diff --git a/lib/jekyll-gist/gist_tag.rb b/lib/jekyll-gist/gist_tag.rb index 83a1595..ae8ae34 100644 --- a/lib/jekyll-gist/gist_tag.rb +++ b/lib/jekyll-gist/gist_tag.rb @@ -1,6 +1,8 @@ -require 'cgi' -require 'net/http' -require 'octokit' +# frozen_string_literal: true + +require "cgi" +require "net/http" +require "octokit" Net::OpenTimeout = Class.new(RuntimeError) unless Net.const_defined?(:OpenTimeout) Net::ReadTimeout = Class.new(RuntimeError) unless Net.const_defined?(:ReadTimeout) @@ -8,65 +10,66 @@ module Jekyll module Gist class GistTag < Liquid::Tag + def self.client + @client ||= Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"] + end def render(context) - @encoding = context.registers[:site].config['encoding'] || 'utf-8' - @settings = context.registers[:site].config['gist'] - if tag_contents = determine_arguments(@markup.strip) - gist_id, filename = tag_contents[0], tag_contents[1] - if context.has_key?(gist_id) - gist_id = context[gist_id] - end - if context.has_key?(filename) - filename = context[filename] - end + @encoding = context.registers[:site].config["encoding"] || "utf-8" + @settings = context.registers[:site].config["gist"] + if (tag_contents = determine_arguments(@markup.strip)) + gist_id = tag_contents[0] + filename = tag_contents[1] + gist_id = context[gist_id] if context.key?(gist_id) + filename = context[filename] if context.key?(filename) + noscript_tag = gist_noscript_tag(gist_id, filename) - script_tag = gist_script_tag(gist_id, filename) + script_tag = gist_script_tag(gist_id, filename) + "#{noscript_tag}#{script_tag}" else - raise ArgumentError.new <<-eos - Syntax error in tag 'gist' while parsing the following markup: + raise ArgumentError, <<~ERROR + Syntax error in tag 'gist' while parsing the following markup: + + '{% #{raw.strip} %}' - #{@markup} + Valid syntax: + {% gist user/1234567 %} + {% gist user/1234567 foo.js %} + {% gist 28949e1d5ee2273f9fd3 %} + {% gist 28949e1d5ee2273f9fd3 best.md %} - Valid syntax: - {% gist user/1234567 %} - {% gist user/1234567 foo.js %} - {% gist 28949e1d5ee2273f9fd3 %} - {% gist 28949e1d5ee2273f9fd3 best.md %} - eos + ERROR end end private def determine_arguments(input) - matched = input.match(/\A([\S]+|.*(?=\/).+)\s?(\S*)\Z/) + matched = input.match(%r!\A([\S]+|.*(?=\/).+)\s?(\S*)\Z!) [matched[1].strip, matched[2].strip] if matched && matched.length >= 3 end def gist_script_tag(gist_id, filename = nil) url = "https://gist.github.com/#{gist_id}.js" - url = "#{url}?file=#{filename}" unless filename.empty? + url = "#{url}?file=#{filename}" unless filename.to_s.empty? + "" end def gist_noscript_tag(gist_id, filename = nil) return if @settings && @settings["noscript"] == false + code = fetch_raw_code(gist_id, filename) - if !code.nil? + if code code = code.force_encoding(@encoding) - code = CGI.escapeHTML(code) - - # CGI.escapeHTML behavior differs in Ruby < 2.0 - # See https://github.com/jekyll/jekyll-gist/pull/28 - code = code.gsub("'", "'") if RUBY_VERSION < "2.0" + code = CGI.escapeHTML(code).gsub("'", "'") "" else - Jekyll.logger.warn "Warning:", "The