From bb66a6244d94ad28d1e09aa4c3eb80b61b182945 Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta <guptautkarsh2102@gmail.com> Date: Thu, 24 Jan 2019 01:11:21 +0530 Subject: [PATCH] New upstream version 1.5.0 --- .rubocop.yml | 28 ++++++ .travis.yml | 22 +++++ Gemfile | 10 ++- History.markdown | 65 ++++++++++++++ README.md | 26 +++++- Rakefile | 3 +- checksums.yaml.gz | Bin 271 -> 0 bytes jekyll-gist.gemspec | 21 +++-- lib/jekyll-gist.rb | 2 + lib/jekyll-gist/gist_tag.rb | 107 ++++++++++++++++++++--- lib/jekyll-gist/version.rb | 4 +- metadata.yml | 115 ------------------------- script/bootstrap | 4 + script/cibuild | 5 ++ script/fmt | 10 +++ script/release | 3 + spec/fixtures/multiple-files.json | 111 ++++++++++++++++++++++++ spec/fixtures/single-file.json | 103 ++++++++++++++++++++++ spec/gist_tag_spec.rb | 136 +++++++++++++++++++++++++++--- spec/spec_helper.rb | 31 ++++--- 20 files changed, 643 insertions(+), 163 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .travis.yml delete mode 100644 checksums.yaml.gz delete mode 100644 metadata.yml create mode 100755 script/bootstrap create mode 100755 script/cibuild create mode 100755 script/fmt create mode 100755 script/release create mode 100644 spec/fixtures/multiple-files.json create mode 100644 spec/fixtures/single-file.json diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..e86cc3d --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,28 @@ +inherit_gem: + jekyll: .rubocop.yml + +AllCops: + TargetRubyVersion: 2.1 + Exclude: + - vendor/**/* + - spec/*.rb + +Lint/IneffectiveAccessModifier: + Exclude: + - lib/jekyll-gist/gist_tag.rb + +Lint/ShadowedException: + Exclude: + - lib/jekyll-gist/gist_tag.rb + +Metrics/AbcSize: + Exclude: + - lib/jekyll-gist/gist_tag.rb + +Metrics/MethodLength: + Exclude: + - lib/jekyll-gist/gist_tag.rb + +Style/PreferredHashMethods: + Exclude: + - lib/jekyll-gist/gist_tag.rb diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4eb8a4b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,22 @@ +language: ruby +script : script/cibuild +sudo: false +notifications: + email: false +branches: + only: + - master + +rvm: + - 2.4 + - 2.3 + - 2.2 + - 2.1 +env: + - "" + - JEKYLL_VERSION=3.6.2 +matrix: + include: + - # GitHub Pages + rvm: 2.4.0 + env: GH_PAGES=true diff --git a/Gemfile b/Gemfile index e93fa5b..c24711a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,10 @@ -source 'https://rubygems.org' +# frozen_string_literal: true -# Specify your gem's dependencies in jekyll-gist.gemspec +source "https://rubygems.org" gemspec + +if ENV["GH_PAGES"] + gem "github-pages" +elsif ENV["JEKYLL_VERSION"] + gem "jekyll", "~> #{ENV["JEKYLL_VERSION"]}" +end diff --git a/History.markdown b/History.markdown index 23525ae..586a6c2 100644 --- a/History.markdown +++ b/History.markdown @@ -1,5 +1,70 @@ ## HEAD +### 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) + * Use Octokit to fetch Gist content when passed `JEKYLL_GITHUB_TOKEN` in env(#28) + +## 1.3.5 / 2015-10-23 + + * Fix encoding error for `noscript` code (#23) + * Test against Jekyll 3, 2, and the github-pages gem (#19) + +## 1.3.4 / 2015-08-28 + + * Catch `TimeoutError` to further support 1.9.3 (#16) + +## 1.3.3 / 2015-08-20 + + * Fix gemspec to allow Ruby 1.9.3 (relates to #14) + +## 1.3.2 / 2016-08-19 + + * 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 + + * Replaced `OpenURI` with `Net::HTTP` and introduced timeout of 3 seconds (#11) + +## 1.3.0 / 2015-08-05 + + * Added an `noscript` fallback for browsers without JavaScript enabled. (#7) + +## 1.2.1 / 2015-03-22 + + * Use `has_key?` (#6) + +## 1.2.0 / 2015-03-21 + +### Minor Enhancements + + * Allow variables as parameters (#4) + +### Development Fixes + + * Fix RSpec deprecation warning (#5) + ## 1.1.0 / 2014-06-18 ### Minor Enhancements diff --git a/README.md b/README.md index ccd2f0e..9bbba39 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,12 +18,21 @@ Or install it yourself as: $ gem install jekyll-gist +Then add the following to your site's `_config.yml`: + +``` +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: @@ -35,11 +44,22 @@ 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. +**Pro-tip**: If you provide a personal access token with Gist scope, as the environmental variable `JEKYLL_GITHUB_TOKEN`, Jekyll Gist will use the Gist API to speed up site generation. + +## 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, add the following to your site's `_config.yml`: + +```yml +gist: + noscript: false +``` + ## Contributing 1. Fork it ( https://github.com/jekyll/jekyll-gist/fork ) 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/checksums.yaml.gz b/checksums.yaml.gz deleted file mode 100644 index cfb10c8cfe085cea581b6311bc0ff13c8335f3f1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 271 zcmV+q0r37GiwFSj?x9lx17(jna$7+RM01{k7oZEkU>Pt|=G-U1GH~QX#mJ}kr$RaH z!bA6aJRY}SKR@t!yWjtuS9HAM$M3(-yNh-oS!cQ|1Ax<5qIJ278U<|XV|j63%qu>> zILzd|6-Rrk=>QAl64=XOXm^0*^$gyvg7jann4cw9iCN^XnbceqI>{X)gXOW-G3Q7- zXXsjGMSyPAdsmc0*T$MSdW~Nzu~GUU0&c*gi?L3wayvZG9)uu6)~htW`>aD-H({>u zO;8Bz4!g192p>=Dq1Nqm<+CTc*d&%F1CB+Nk-n7>l4zvk(y=QiVIBv};oaA@y5Mpt VR)u6&sJC_B{sHt<aJ8ZV008!UhaLa` diff --git a/jekyll-gist.gemspec b/jekyll-gist.gemspec index d1ec8f5..dfdd91f 100644 --- a/jekyll-gist.gemspec +++ b/jekyll-gist.gemspec @@ -1,24 +1,31 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path("lib", __dir__) + $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'jekyll-gist/version' +require "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 = ">= 2.1" + 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.executables = spec.files.grep(%r!^bin/!) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r!^(test|spec|features)/!) spec.require_paths = ["lib"] + spec.add_dependency "octokit", "~> 4.2" spec.add_development_dependency "bundler", "~> 1.6" + spec.add_development_dependency "jekyll", ">= 3.0" spec.add_development_dependency "rake" spec.add_development_dependency "rspec" - spec.add_development_dependency "jekyll", "~> 2.0" + spec.add_development_dependency "rubocop", "~> 0.51" + spec.add_development_dependency "webmock" 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 1df375c..c265063 100644 --- a/lib/jekyll-gist/gist_tag.rb +++ b/lib/jekyll-gist/gist_tag.rb @@ -1,13 +1,32 @@ +# 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) + module Jekyll module Gist class GistTag < Liquid::Tag - def render(context) - if tag_contents = determine_arguments(@markup.strip) - gist_id, filename = tag_contents[0], tag_contents[1] - gist_script_tag(gist_id, filename) + @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] + if context_contains_key?(context, gist_id) + gist_id = context[gist_id] + end + if context_contains_key?(context, filename) + filename = context[filename] + end + noscript_tag = gist_noscript_tag(gist_id, filename) + script_tag = gist_script_tag(gist_id, filename) + "#{noscript_tag}#{script_tag}" else - raise ArgumentError.new <<-eos + raise ArgumentError, <<-EOS Syntax error in tag 'gist' while parsing the following markup: #{@markup} @@ -17,27 +36,93 @@ module Jekyll {% gist user/1234567 foo.js %} {% gist 28949e1d5ee2273f9fd3 %} {% gist 28949e1d5ee2273f9fd3 best.md %} - eos + EOS 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 + private + + def context_contains_key?(context, key) + if context.respond_to?(:has_key?) + context.has_key?(key) + else + context.key?(key) + end + end + def gist_script_tag(gist_id, filename = nil) - if filename.empty? - "<script src=\"https://gist.github.com/#{gist_id}.js\"> </script>" + url = "https://gist.github.com/#{gist_id}.js" + url = "#{url}?file=#{filename}" unless filename.to_s.empty? + "<script src=\"#{url}\"> </script>" + 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? + 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" + + "<noscript><pre>#{code}</pre></noscript>" else - "<script src=\"https://gist.github.com/#{gist_id}.js?file=#{filename}\"> </script>" + Jekyll.logger.warn "Warning:", "The <noscript> tag for your gist #{gist_id} " + Jekyll.logger.warn "", "could not be generated. This will affect users who do " + Jekyll.logger.warn "", "not have JavaScript enabled in their browsers." + end + end + + def fetch_raw_code(gist_id, filename = nil) + return code_from_api(gist_id, filename) if ENV["JEKYLL_GITHUB_TOKEN"] + + url = "https://gist.githubusercontent.com/#{gist_id}/raw" + url = "#{url}/#{filename}" unless filename.to_s.empty? + uri = URI(url) + Net::HTTP.start(uri.host, uri.port, + :use_ssl => uri.scheme == "https", + :read_timeout => 3, :open_timeout => 3) do |http| + request = Net::HTTP::Get.new uri.to_s + response = http.request(request) + response.body end + rescue SocketError, Net::HTTPError, Net::OpenTimeout, Net::ReadTimeout, TimeoutError + nil + end + + private + + def code_from_api(gist_id, filename = nil) + gist = GistTag.client.gist gist_id + + file = if filename.to_s.empty? + # No file specified, return the value of the first key/value pair + gist.files.first[1] + else + # .files is a hash of :"filename.extension" => data pairs + # Rather than using to_sym on arbitrary user input, + # Find our file by calling to_s on the keys + match = gist.files.find { |name, _data| name.to_s == filename } + match[1] if match + end + + file[:content] if file end + def self.client + @client ||= Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"] + end end end end -Liquid::Template.register_tag('gist', Jekyll::Gist::GistTag) +Liquid::Template.register_tag("gist", Jekyll::Gist::GistTag) diff --git a/lib/jekyll-gist/version.rb b/lib/jekyll-gist/version.rb index f585820..0de8671 100644 --- a/lib/jekyll-gist/version.rb +++ b/lib/jekyll-gist/version.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Jekyll module Gist - VERSION = "1.1.0" + VERSION = "1.5.0".freeze end end diff --git a/metadata.yml b/metadata.yml deleted file mode 100644 index bd4196c..0000000 --- a/metadata.yml +++ /dev/null @@ -1,115 +0,0 @@ ---- !ruby/object:Gem::Specification -name: jekyll-gist -version: !ruby/object:Gem::Version - version: 1.1.0 -platform: ruby -authors: -- Parker Moore -autorequire: -bindir: bin -cert_chain: [] -date: 2014-06-18 00:00:00.000000000 Z -dependencies: -- !ruby/object:Gem::Dependency - name: bundler - requirement: !ruby/object:Gem::Requirement - requirements: - - - "~>" - - !ruby/object:Gem::Version - version: '1.6' - type: :development - prerelease: false - version_requirements: !ruby/object:Gem::Requirement - requirements: - - - "~>" - - !ruby/object:Gem::Version - version: '1.6' -- !ruby/object:Gem::Dependency - name: rake - requirement: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: '0' - type: :development - prerelease: false - version_requirements: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: '0' -- !ruby/object:Gem::Dependency - name: rspec - requirement: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: '0' - type: :development - prerelease: false - version_requirements: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: '0' -- !ruby/object:Gem::Dependency - name: jekyll - requirement: !ruby/object:Gem::Requirement - requirements: - - - "~>" - - !ruby/object:Gem::Version - version: '2.0' - type: :development - prerelease: false - version_requirements: !ruby/object:Gem::Requirement - requirements: - - - "~>" - - !ruby/object:Gem::Version - version: '2.0' -description: -email: -- parkrmoore@gmail.com -executables: [] -extensions: [] -extra_rdoc_files: [] -files: -- ".gitignore" -- ".rspec" -- Gemfile -- History.markdown -- LICENSE.txt -- README.md -- Rakefile -- jekyll-gist.gemspec -- lib/jekyll-gist.rb -- lib/jekyll-gist/gist_tag.rb -- lib/jekyll-gist/version.rb -- spec/gist_tag_spec.rb -- spec/spec_helper.rb -homepage: https://github.com/jekyll/jekyll-gist -licenses: -- MIT -metadata: {} -post_install_message: -rdoc_options: [] -require_paths: -- lib -required_ruby_version: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: '0' -required_rubygems_version: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: '0' -requirements: [] -rubyforge_project: -rubygems_version: 2.2.2 -signing_key: -specification_version: 4 -summary: Liquid tag for displaying GitHub Gists in Jekyll sites. -test_files: -- spec/gist_tag_spec.rb -- spec/spec_helper.rb diff --git a/script/bootstrap b/script/bootstrap new file mode 100755 index 0000000..31a01b7 --- /dev/null +++ b/script/bootstrap @@ -0,0 +1,4 @@ +#! /usr/bin/env bash +set -ex + +bundle install diff --git a/script/cibuild b/script/cibuild new file mode 100755 index 0000000..70e6da6 --- /dev/null +++ b/script/cibuild @@ -0,0 +1,5 @@ +#! /usr/bin/env bash +set -e + +script/fmt +bundle exec rspec diff --git a/script/fmt b/script/fmt new file mode 100755 index 0000000..c5351ec --- /dev/null +++ b/script/fmt @@ -0,0 +1,10 @@ +#!/bin/bash +set -e + +echo "Rubocop $(bundle exec rubocop --version)" +bundle exec rubocop -D -E $@ +success=$? +if ((success != 0)); then + echo -e "\nTry running \`script/fmt -a\` to automatically fix errors" +fi +exit $success diff --git a/script/release b/script/release new file mode 100755 index 0000000..c204a27 --- /dev/null +++ b/script/release @@ -0,0 +1,3 @@ +#! /bin/bash + +rake release diff --git a/spec/fixtures/multiple-files.json b/spec/fixtures/multiple-files.json new file mode 100644 index 0000000..338d3a7 --- /dev/null +++ b/spec/fixtures/multiple-files.json @@ -0,0 +1,111 @@ +{ + "url": "https://api.github.com/gists/aa5a315d61ae9438b18d", + "forks_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/forks", + "commits_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/commits", + "id": "aa5a315d61ae9438b18d", + "description": "description of gist", + "public": true, + "owner": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "user": null, + "files": { + "ring.erl": { + "size": 932, + "raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl", + "type": "text/plain", + "language": "Erlang", + "truncated": false, + "content": "contents of gist" + }, + "hello-world.rb": { + "size": 932, + "raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl", + "type": "text/plain", + "language": "Ruby", + "truncated": false, + "content": "puts 'hello world'" + } + }, + "comments": 0, + "comments_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/comments/", + "html_url": "https://gist.github.com/aa5a315d61ae9438b18d", + "git_pull_url": "https://gist.github.com/aa5a315d61ae9438b18d.git", + "git_push_url": "https://gist.github.com/aa5a315d61ae9438b18d.git", + "created_at": "2010-04-14T02:15:15Z", + "updated_at": "2011-06-20T11:34:15Z", + "forks": [ + { + "user": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "url": "https://api.github.com/gists/dee9c42e4998ce2ea439", + "id": "dee9c42e4998ce2ea439", + "created_at": "2011-04-14T16:00:49Z", + "updated_at": "2011-04-14T16:00:49Z" + } + ], + "history": [ + { + "url": "https://api.github.com/gists/aa5a315d61ae9438b18d/57a7f021a713b1c5a6a199b54cc514735d2d462f", + "version": "57a7f021a713b1c5a6a199b54cc514735d2d462f", + "user": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "change_status": { + "deletions": 0, + "additions": 180, + "total": 180 + }, + "committed_at": "2010-04-14T02:15:15Z" + } + ] +} diff --git a/spec/fixtures/single-file.json b/spec/fixtures/single-file.json new file mode 100644 index 0000000..9ac7921 --- /dev/null +++ b/spec/fixtures/single-file.json @@ -0,0 +1,103 @@ +{ + "url": "https://api.github.com/gists/aa5a315d61ae9438b18d", + "forks_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/forks", + "commits_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/commits", + "id": "aa5a315d61ae9438b18d", + "description": "description of gist", + "public": true, + "owner": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "user": null, + "files": { + "ring.erl": { + "size": 932, + "raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl", + "type": "text/plain", + "language": "Erlang", + "truncated": false, + "content": "contents of gist" + } + }, + "comments": 0, + "comments_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/comments/", + "html_url": "https://gist.github.com/aa5a315d61ae9438b18d", + "git_pull_url": "https://gist.github.com/aa5a315d61ae9438b18d.git", + "git_push_url": "https://gist.github.com/aa5a315d61ae9438b18d.git", + "created_at": "2010-04-14T02:15:15Z", + "updated_at": "2011-06-20T11:34:15Z", + "forks": [ + { + "user": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "url": "https://api.github.com/gists/dee9c42e4998ce2ea439", + "id": "dee9c42e4998ce2ea439", + "created_at": "2011-04-14T16:00:49Z", + "updated_at": "2011-04-14T16:00:49Z" + } + ], + "history": [ + { + "url": "https://api.github.com/gists/aa5a315d61ae9438b18d/57a7f021a713b1c5a6a199b54cc514735d2d462f", + "version": "57a7f021a713b1c5a6a199b54cc514735d2d462f", + "user": { + "login": "octocat", + "id": 1, + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "change_status": { + "deletions": 0, + "additions": 180, + "total": 180 + }, + "committed_at": "2010-04-14T02:15:15Z" + } + ] +} diff --git a/spec/gist_tag_spec.rb b/spec/gist_tag_spec.rb index ad4f334..f04618c 100644 --- a/spec/gist_tag_spec.rb +++ b/spec/gist_tag_spec.rb @@ -1,61 +1,175 @@ -require 'spec_helper' +require "spec_helper" describe(Jekyll::Gist::GistTag) do + let(:http_output) { "<test>true</test>" } let(:doc) { doc_with_content(content) } - let(:content) { "{% gist #{gist} %}" } + let(:content) { "{% gist #{gist} %}" } let(:output) do doc.content = content doc.output = Jekyll::Renderer.new(doc.site, doc).run end + before(:each) { ENV["JEKYLL_GITHUB_TOKEN"] = nil } context "valid gist" do context "with user prefix" do + before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) } let(:gist) { "mattr-/24081a1d93d2898ecf0f" } it "produces the correct script tag" do - expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/) + expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!) + end + it "produces the correct noscript tag" do + expect(output).to match(%r!<noscript><pre><test>true<\/test><\/pre><\/noscript>\n!) end end context "without user prefix" do + before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) } let(:gist) { "28949e1d5ee2273f9fd3" } it "produces the correct script tag" do - expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/) + expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!) + end + it "produces the correct noscript tag" do + expect(output).to match(%r!<noscript><pre><test>true<\/test><\/pre><\/noscript>\n!) end end context "classic Gist id style" do + before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) } let(:gist) { "1234321" } it "produces the correct script tag" do - expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/) + expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!) + end + it "produces the correct noscript tag" do + expect(output).to match(%r!<noscript><pre><test>true<\/test><\/pre><\/noscript>\n!) end end context "with file specified" do + before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw/#{filename}").to_return(:body => http_output) } let(:gist) { "mattr-/24081a1d93d2898ecf0f" } let(:filename) { "myfile.ext" } let(:content) { "{% gist #{gist} #{filename} %}" } it "produces the correct script tag" do - expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>/) + expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>!) + end + it "produces the correct noscript tag" do + expect(output).to match(%r!<noscript><pre><test>true<\/test><\/pre><\/noscript>\n!) end end - end + context "with variable gist id" do + before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(:body => http_output) } + let(:gist_id) { "1342013" } + let(:gist) { "page.gist_id" } + let(:output) do + doc.data["gist_id"] = gist_id + doc.content = content + doc.output = Jekyll::Renderer.new(doc.site, doc).run + end + + it "produces the correct script tag" do + expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js">\s<\/script>!) + end + it "produces the correct noscript tag" do + expect(output).to match(%r!<noscript><pre><test>true<\/test><\/pre><\/noscript>\n!) + end + end - context "invalid gist" do + context "with variable gist id and filename" do + before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(:body => http_output) } + let(:gist_id) { "1342013" } + let(:gist_filename) { "atom.xml" } + let(:gist) { "page.gist_id" } + let(:filename) { "page.gist_filename" } + let(:content) { "{% gist #{gist} #{filename} %}" } + let(:output) do + doc.data["gist_id"] = "1342013" + doc.data["gist_filename"] = "atom.xml" + doc.content = content + doc.output = Jekyll::Renderer.new(doc.site, doc).run + end + + it "produces the correct script tag" do + expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>!) + end + + it "produces the correct noscript tag" do + expect(output).to match(%r!<noscript><pre><test>true<\/test><\/pre><\/noscript>\n!) + end + end + + context "with valid gist id and invalid filename" do + before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(:status => 404) } + let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" } + let(:gist_filename) { "myfile.ext" } + let(:content) { "{% gist #{gist_id} #{gist_filename} %}" } + + it "produces the correct script tag" do + expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>!) + end + it "does not produce the noscript tag" do + expect(output).to_not match(%r!<noscript><pre><test>true<\/test><\/pre><\/noscript>\n!) + end + end + + context "with token" do + before { ENV["JEKYLL_GITHUB_TOKEN"] = "1234" } + before do + stub_request(:get, "https://api.github.com/gists/1342013") + .to_return(:status => 200, :body => fixture("single-file"), :headers => { "Content-Type" => "application/json" }) + end + let(:gist_id) { "1342013" } + let(:gist) { "page.gist_id" } + let(:output) do + doc.data["gist_id"] = gist_id + doc.content = content + doc.output = Jekyll::Renderer.new(doc.site, doc).run + end + + it "produces the noscript tag" do + expect(output).to match(%r!<noscript><pre>contents of gist<\/pre><\/noscript>!) + end + + context "with a filename" do + before do + stub_request(:get, "https://api.github.com/gists/1342013") + .to_return(:status => 200, :body => fixture("multiple-files"), :headers => { "Content-Type" => "application/json" }) + end + let(:content) { "{% gist 1342013 hello-world.rb %}" } + + it "produces the noscript tag" do + expect(output).to match(%r!<noscript><pre>puts 'hello world'<\/pre><\/noscript>!) + end + end + end + + context "with noscript disabled" do + let(:doc) { doc_with_content(content, { "gist" => { "noscript" => false } }) } + let(:output) do + doc.content = content + doc.output = Jekyll::Renderer.new(doc.site, doc).run + end + let(:gist) { "mattr-/24081a1d93d2898ecf0f" } + + it "does not produce the noscript tag" do + expect(output).to_not match(%r!<noscript>!) + end + end + end + + context "invalid gist" do context "no gist id present" do let(:gist) { "" } it "raises an error" do - expect(->{ output }).to raise_error + expect(-> { output }).to raise_error end end - end - end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a7df399..f2d0a7a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,44 +1,51 @@ -TEST_DIR = File.dirname(__FILE__) +# frozen_string_literal: true + +TEST_DIR = __dir__ TMP_DIR = File.expand_path("../tmp", TEST_DIR) -require 'jekyll' +require "webmock/rspec" +require "cgi" +require "jekyll" require File.expand_path("../lib/jekyll-gist.rb", TEST_DIR) Jekyll.logger.log_level = :error -STDERR.reopen(test(?e, '/dev/null') ? '/dev/null' : 'NUL:') RSpec.configure do |config| - config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus - config.order = 'random' + config.order = "random" def tmp_dir(*files) File.join(TMP_DIR, *files) end def source_dir(*files) - tmp_dir('source', *files) + tmp_dir("source", *files) end def dest_dir(*files) - tmp_dir('dest', *files) + tmp_dir("dest", *files) end - def doc_with_content(content, opts = {}) - my_site = site - Jekyll::Document.new(source_dir('_test/doc.md'), {site: my_site, collection: collection(my_site)}) + def doc_with_content(_content, opts = {}) + my_site = site(opts) + Jekyll::Document.new(source_dir("_test/doc.md"), { :site => my_site, :collection => collection(my_site) }) end - def collection(site, label = 'test') + def collection(site, label = "test") Jekyll::Collection.new(site, label) end def site(opts = {}) conf = Jekyll::Utils.deep_merge_hashes(Jekyll::Configuration::DEFAULTS, opts.merge({ "source" => source_dir, - "destination" => dest_dir + "destination" => dest_dir, })) Jekyll::Site.new(conf) end + + def fixture(name) + path = File.expand_path "fixtures/#{name}.json", __dir__ + File.open(path).read + end end -- GitLab