bunto-gist 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12fdebb1d1d8e0fa0fb0f80a341ec19c4af07049
4
+ data.tar.gz: 3f5051879f98faaf29730cf1ebb1b86f15e43db8
5
+ SHA512:
6
+ metadata.gz: 8ab1191c1b41ba13e9a928717620a8f2f080dbed525f75cd3994fe83cee967c2cb59b352241cfe9730b8f48e052100ba4c4aae5d1013604734d3f54d43b81012
7
+ data.tar.gz: 646eb40cb0dfa43ed1ab5d06fc69586ffadad56c970de56389c2e823750e7688b98b8aaf30a262e0065d4fc464ad3a97329f614c21f3b25092c1c46fc81d474c
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,25 @@
1
+ language: ruby
2
+ script : script/cibuild
3
+ sudo: false
4
+ notifications:
5
+ email: false
6
+ branches:
7
+ only:
8
+ - master
9
+
10
+ rvm:
11
+ - 2.2
12
+ - 2.1
13
+ - 2.0
14
+ env:
15
+ - ""
16
+ - BUNTO_VERSION=2.0.0.beta8
17
+ - BUNTO_VERSION=1.0
18
+ matrix:
19
+ include:
20
+ - # GitHub Pages
21
+ rvm: 2.1.1
22
+ env: GH_PAGES=true
23
+ - # Ruby 1.9
24
+ rvm: 1.9
25
+ env: BUNTO_VERSION=1.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ if ENV["GH_PAGES"]
5
+ gem "github-pages"
6
+ elsif ENV["BUNTO_VERSION"]
7
+ gem "bunto", "~> #{ENV["BUNTO_VERSION"]}"
8
+ end
data/History.markdown ADDED
@@ -0,0 +1,3 @@
1
+ ## 1.0.0 / 2016-02-08
2
+
3
+ * Birthday!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016-present Parker Moore
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Bunto::Gist
2
+
3
+ Liquid tag for displaying GitHub Gists in Bunto sites: `{% gist %}`.
4
+
5
+ [![Build Status](https://travis-ci.org/bunto/bunto-gist.svg?branch=master)](https://travis-ci.org/bunto/bunto-gist)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'bunto-gist'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bunto-gist
20
+
21
+ Finally, add the following to your site's `_config.yml`:
22
+
23
+ ```
24
+ gems:
25
+ - bunto-gist
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Use the tag as follows in your Bunto pages, posts and collections:
31
+
32
+ ```liquid
33
+ {% gist parkr/c08ee0f2726fd0e3909d %}
34
+ ```
35
+
36
+ This will create the associated script tag:
37
+
38
+ ```html
39
+ <script src="https://gist.github.com/parkr/c08ee0f2726fd0e3909d.js"> </script>
40
+ ```
41
+
42
+ You may optionally specify a `filename` after the `gist_id`:
43
+
44
+ ```liquid
45
+ {% gist parkr/c08ee0f2726fd0e3909d test.md %}
46
+ ```
47
+
48
+ This will produce the correct URL to show just the specified file in your post rather than the entire Gist.
49
+
50
+ **Pro-tip**: If you provide a personal access token with Gist scope, as the environmental variable `JEKYLL_GITHUB_TOKEN`, Bunto Gist will use the Gist API to speed up site generation.
51
+
52
+ ## Disabling `noscript` support
53
+
54
+ By default, Bunto 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`:
55
+
56
+ ```yml
57
+ gist:
58
+ noscript: false
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/bunto/bunto-gist/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bunto-gist/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bunto-gist"
8
+ spec.version = Bunto::Gist::VERSION
9
+ spec.authors = ["Parker Moore", "Suriyaa Kudo"]
10
+ spec.email = ["parkrmoore@gmail.com", "SuriyaaKudoIsc@users.noreply.github.com"]
11
+ spec.summary = %q{Liquid tag for displaying GitHub Gists in Bunto sites.}
12
+ spec.homepage = "https://github.com/bunto/bunto-gist"
13
+ spec.license = "MIT"
14
+
15
+ spec.required_ruby_version = '>= 1.9.3'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "octokit", "~> 4.2"
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "webmock"
27
+ spec.add_development_dependency "bunto", ">= 1.0"
28
+ end
data/lib/bunto-gist.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "bunto-gist/version"
2
+ require "bunto-gist/gist_tag"
3
+
4
+ module Bunto
5
+ module Gist
6
+ end
7
+ end
@@ -0,0 +1,116 @@
1
+ require 'cgi'
2
+ require 'net/http'
3
+ require 'octokit'
4
+
5
+ Net::OpenTimeout = Class.new(RuntimeError) unless Net.const_defined?(:OpenTimeout)
6
+ Net::ReadTimeout = Class.new(RuntimeError) unless Net.const_defined?(:ReadTimeout)
7
+
8
+ module Bunto
9
+ module Gist
10
+ class GistTag < Liquid::Tag
11
+
12
+ def render(context)
13
+ @encoding = context.registers[:site].config['encoding'] || 'utf-8'
14
+ @settings = context.registers[:site].config['gist']
15
+ if tag_contents = determine_arguments(@markup.strip)
16
+ gist_id, filename = tag_contents[0], tag_contents[1]
17
+ if context.has_key?(gist_id)
18
+ gist_id = context[gist_id]
19
+ end
20
+ if context.has_key?(filename)
21
+ filename = context[filename]
22
+ end
23
+ noscript_tag = gist_noscript_tag(gist_id, filename)
24
+ script_tag = gist_script_tag(gist_id, filename)
25
+ "#{noscript_tag}#{script_tag}"
26
+ else
27
+ raise ArgumentError.new <<-eos
28
+ Syntax error in tag 'gist' while parsing the following markup:
29
+
30
+ #{@markup}
31
+
32
+ Valid syntax:
33
+ {% gist user/1234567 %}
34
+ {% gist user/1234567 foo.js %}
35
+ {% gist 28949e1d5ee2273f9fd3 %}
36
+ {% gist 28949e1d5ee2273f9fd3 best.md %}
37
+ eos
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def determine_arguments(input)
44
+ matched = input.match(/\A([\S]+|.*(?=\/).+)\s?(\S*)\Z/)
45
+ [matched[1].strip, matched[2].strip] if matched && matched.length >= 3
46
+ end
47
+
48
+ def gist_script_tag(gist_id, filename = nil)
49
+ url = "https://gist.github.com/#{gist_id}.js"
50
+ url = "#{url}?file=#{filename}" unless filename.empty?
51
+ "<script src=\"#{url}\"> </script>"
52
+ end
53
+
54
+ def gist_noscript_tag(gist_id, filename = nil)
55
+ return if @settings && @settings["noscript"] == false
56
+ code = fetch_raw_code(gist_id, filename)
57
+ if !code.nil?
58
+ code = code.force_encoding(@encoding)
59
+ code = CGI.escapeHTML(code)
60
+
61
+ # CGI.escapeHTML behavior differs in Ruby < 2.0
62
+ # See https://github.com/bunto/bunto-gist/pull/28
63
+ code = code.gsub("'", "&#39;") if RUBY_VERSION < "2.0"
64
+
65
+ "<noscript><pre>#{code}</pre></noscript>"
66
+ else
67
+ Bunto.logger.warn "Warning:", "The <noscript> tag for your gist #{gist_id} could not"
68
+ Bunto.logger.warn "", "be generated. This will affect users who do not have"
69
+ Bunto.logger.warn "", "JavaScript available or enabled in their browsers."
70
+ end
71
+ end
72
+
73
+ def fetch_raw_code(gist_id, filename = nil)
74
+ return code_from_api(gist_id, filename) if ENV["BUNTO_GITHUB_TOKEN"]
75
+
76
+ url = "https://gist.githubusercontent.com/#{gist_id}/raw"
77
+ url = "#{url}/#{filename}" unless filename.empty?
78
+ uri = URI(url)
79
+ Net::HTTP.start(uri.host, uri.port,
80
+ use_ssl: uri.scheme == 'https',
81
+ read_timeout: 3, open_timeout: 3) do |http|
82
+ request = Net::HTTP::Get.new uri.to_s
83
+ response = http.request(request)
84
+ response.body
85
+ end
86
+ rescue SocketError, Net::HTTPError, Net::OpenTimeout, Net::ReadTimeout, TimeoutError
87
+ nil
88
+ end
89
+
90
+ private
91
+
92
+ def code_from_api(gist_id, filename = nil)
93
+ gist = GistTag.client.gist gist_id
94
+
95
+ file = if filename.to_s.empty?
96
+ # No file specified, return the value of the first key/value pair
97
+ gist.files.first[1]
98
+ else
99
+ # .files is a hash of :"filename.extension" => data pairs
100
+ # Rather than using to_sym on arbitrary user input,
101
+ # Find our file by calling to_s on the keys
102
+ match = gist.files.find { |name, data| name.to_s == filename }
103
+ match[1] if match
104
+ end
105
+
106
+ file[:content] if file
107
+ end
108
+
109
+ def self.client
110
+ @client ||= Octokit::Client.new :access_token => ENV["BUNTO_GITHUB_TOKEN"]
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ Liquid::Template.register_tag('gist', Bunto::Gist::GistTag)
@@ -0,0 +1,5 @@
1
+ module Bunto
2
+ module Gist
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
data/script/bootstrap ADDED
@@ -0,0 +1,3 @@
1
+ #! /usr/bin/env bash
2
+
3
+ bundle install
data/script/cibuild ADDED
@@ -0,0 +1,4 @@
1
+ #! /usr/bin/env bash
2
+ set -e
3
+
4
+ bundle exec rspec
@@ -0,0 +1,111 @@
1
+ {
2
+ "url": "https://api.github.com/gists/aa5a315d61ae9438b18d",
3
+ "forks_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/forks",
4
+ "commits_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/commits",
5
+ "id": "aa5a315d61ae9438b18d",
6
+ "description": "description of gist",
7
+ "public": true,
8
+ "owner": {
9
+ "login": "octocat",
10
+ "id": 1,
11
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
12
+ "gravatar_id": "",
13
+ "url": "https://api.github.com/users/octocat",
14
+ "html_url": "https://github.com/octocat",
15
+ "followers_url": "https://api.github.com/users/octocat/followers",
16
+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",
17
+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
18
+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
19
+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
20
+ "organizations_url": "https://api.github.com/users/octocat/orgs",
21
+ "repos_url": "https://api.github.com/users/octocat/repos",
22
+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",
23
+ "received_events_url": "https://api.github.com/users/octocat/received_events",
24
+ "type": "User",
25
+ "site_admin": false
26
+ },
27
+ "user": null,
28
+ "files": {
29
+ "ring.erl": {
30
+ "size": 932,
31
+ "raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
32
+ "type": "text/plain",
33
+ "language": "Erlang",
34
+ "truncated": false,
35
+ "content": "contents of gist"
36
+ },
37
+ "hello-world.rb": {
38
+ "size": 932,
39
+ "raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
40
+ "type": "text/plain",
41
+ "language": "Ruby",
42
+ "truncated": false,
43
+ "content": "puts 'hello world'"
44
+ }
45
+ },
46
+ "comments": 0,
47
+ "comments_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/comments/",
48
+ "html_url": "https://gist.github.com/aa5a315d61ae9438b18d",
49
+ "git_pull_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
50
+ "git_push_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
51
+ "created_at": "2010-04-14T02:15:15Z",
52
+ "updated_at": "2011-06-20T11:34:15Z",
53
+ "forks": [
54
+ {
55
+ "user": {
56
+ "login": "octocat",
57
+ "id": 1,
58
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
59
+ "gravatar_id": "",
60
+ "url": "https://api.github.com/users/octocat",
61
+ "html_url": "https://github.com/octocat",
62
+ "followers_url": "https://api.github.com/users/octocat/followers",
63
+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",
64
+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
65
+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
66
+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
67
+ "organizations_url": "https://api.github.com/users/octocat/orgs",
68
+ "repos_url": "https://api.github.com/users/octocat/repos",
69
+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",
70
+ "received_events_url": "https://api.github.com/users/octocat/received_events",
71
+ "type": "User",
72
+ "site_admin": false
73
+ },
74
+ "url": "https://api.github.com/gists/dee9c42e4998ce2ea439",
75
+ "id": "dee9c42e4998ce2ea439",
76
+ "created_at": "2011-04-14T16:00:49Z",
77
+ "updated_at": "2011-04-14T16:00:49Z"
78
+ }
79
+ ],
80
+ "history": [
81
+ {
82
+ "url": "https://api.github.com/gists/aa5a315d61ae9438b18d/57a7f021a713b1c5a6a199b54cc514735d2d462f",
83
+ "version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
84
+ "user": {
85
+ "login": "octocat",
86
+ "id": 1,
87
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
88
+ "gravatar_id": "",
89
+ "url": "https://api.github.com/users/octocat",
90
+ "html_url": "https://github.com/octocat",
91
+ "followers_url": "https://api.github.com/users/octocat/followers",
92
+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",
93
+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
94
+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
95
+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
96
+ "organizations_url": "https://api.github.com/users/octocat/orgs",
97
+ "repos_url": "https://api.github.com/users/octocat/repos",
98
+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",
99
+ "received_events_url": "https://api.github.com/users/octocat/received_events",
100
+ "type": "User",
101
+ "site_admin": false
102
+ },
103
+ "change_status": {
104
+ "deletions": 0,
105
+ "additions": 180,
106
+ "total": 180
107
+ },
108
+ "committed_at": "2010-04-14T02:15:15Z"
109
+ }
110
+ ]
111
+ }
@@ -0,0 +1,103 @@
1
+ {
2
+ "url": "https://api.github.com/gists/aa5a315d61ae9438b18d",
3
+ "forks_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/forks",
4
+ "commits_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/commits",
5
+ "id": "aa5a315d61ae9438b18d",
6
+ "description": "description of gist",
7
+ "public": true,
8
+ "owner": {
9
+ "login": "octocat",
10
+ "id": 1,
11
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
12
+ "gravatar_id": "",
13
+ "url": "https://api.github.com/users/octocat",
14
+ "html_url": "https://github.com/octocat",
15
+ "followers_url": "https://api.github.com/users/octocat/followers",
16
+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",
17
+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
18
+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
19
+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
20
+ "organizations_url": "https://api.github.com/users/octocat/orgs",
21
+ "repos_url": "https://api.github.com/users/octocat/repos",
22
+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",
23
+ "received_events_url": "https://api.github.com/users/octocat/received_events",
24
+ "type": "User",
25
+ "site_admin": false
26
+ },
27
+ "user": null,
28
+ "files": {
29
+ "ring.erl": {
30
+ "size": 932,
31
+ "raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
32
+ "type": "text/plain",
33
+ "language": "Erlang",
34
+ "truncated": false,
35
+ "content": "contents of gist"
36
+ }
37
+ },
38
+ "comments": 0,
39
+ "comments_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/comments/",
40
+ "html_url": "https://gist.github.com/aa5a315d61ae9438b18d",
41
+ "git_pull_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
42
+ "git_push_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
43
+ "created_at": "2010-04-14T02:15:15Z",
44
+ "updated_at": "2011-06-20T11:34:15Z",
45
+ "forks": [
46
+ {
47
+ "user": {
48
+ "login": "octocat",
49
+ "id": 1,
50
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
51
+ "gravatar_id": "",
52
+ "url": "https://api.github.com/users/octocat",
53
+ "html_url": "https://github.com/octocat",
54
+ "followers_url": "https://api.github.com/users/octocat/followers",
55
+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",
56
+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
57
+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
58
+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
59
+ "organizations_url": "https://api.github.com/users/octocat/orgs",
60
+ "repos_url": "https://api.github.com/users/octocat/repos",
61
+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",
62
+ "received_events_url": "https://api.github.com/users/octocat/received_events",
63
+ "type": "User",
64
+ "site_admin": false
65
+ },
66
+ "url": "https://api.github.com/gists/dee9c42e4998ce2ea439",
67
+ "id": "dee9c42e4998ce2ea439",
68
+ "created_at": "2011-04-14T16:00:49Z",
69
+ "updated_at": "2011-04-14T16:00:49Z"
70
+ }
71
+ ],
72
+ "history": [
73
+ {
74
+ "url": "https://api.github.com/gists/aa5a315d61ae9438b18d/57a7f021a713b1c5a6a199b54cc514735d2d462f",
75
+ "version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
76
+ "user": {
77
+ "login": "octocat",
78
+ "id": 1,
79
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
80
+ "gravatar_id": "",
81
+ "url": "https://api.github.com/users/octocat",
82
+ "html_url": "https://github.com/octocat",
83
+ "followers_url": "https://api.github.com/users/octocat/followers",
84
+ "following_url": "https://api.github.com/users/octocat/following{/other_user}",
85
+ "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
86
+ "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
87
+ "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
88
+ "organizations_url": "https://api.github.com/users/octocat/orgs",
89
+ "repos_url": "https://api.github.com/users/octocat/repos",
90
+ "events_url": "https://api.github.com/users/octocat/events{/privacy}",
91
+ "received_events_url": "https://api.github.com/users/octocat/received_events",
92
+ "type": "User",
93
+ "site_admin": false
94
+ },
95
+ "change_status": {
96
+ "deletions": 0,
97
+ "additions": 180,
98
+ "total": 180
99
+ },
100
+ "committed_at": "2010-04-14T02:15:15Z"
101
+ }
102
+ ]
103
+ }
@@ -0,0 +1,180 @@
1
+ require 'spec_helper'
2
+
3
+ describe(Bunto::Gist::GistTag) do
4
+ let(:http_output) { "<test>true</test>" }
5
+ let(:doc) { doc_with_content(content) }
6
+ let(:content) { "{% gist #{gist} %}" }
7
+ let(:output) do
8
+ doc.content = content
9
+ doc.output = Bunto::Renderer.new(doc.site, doc).run
10
+ end
11
+
12
+ before(:each) { ENV["BUNTO_GITHUB_TOKEN"] = nil }
13
+
14
+ context "valid gist" do
15
+ context "with user prefix" do
16
+ before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(body: http_output) }
17
+ let(:gist) { "mattr-/24081a1d93d2898ecf0f" }
18
+
19
+ it "produces the correct script tag" do
20
+ expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
21
+ end
22
+ it "produces the correct noscript tag" do
23
+ expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
24
+ end
25
+ end
26
+
27
+ context "without user prefix" do
28
+ before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(body: http_output) }
29
+ let(:gist) { "28949e1d5ee2273f9fd3" }
30
+
31
+ it "produces the correct script tag" do
32
+ expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
33
+ end
34
+ it "produces the correct noscript tag" do
35
+ expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
36
+ end
37
+ end
38
+
39
+ context "classic Gist id style" do
40
+ before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(body: http_output) }
41
+ let(:gist) { "1234321" }
42
+
43
+ it "produces the correct script tag" do
44
+ expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
45
+ end
46
+ it "produces the correct noscript tag" do
47
+ expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
48
+ end
49
+ end
50
+
51
+ context "with file specified" do
52
+ before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw/#{filename}").to_return(body: http_output) }
53
+ let(:gist) { "mattr-/24081a1d93d2898ecf0f" }
54
+ let(:filename) { "myfile.ext" }
55
+ let(:content) { "{% gist #{gist} #{filename} %}" }
56
+
57
+ it "produces the correct script tag" do
58
+ expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>/)
59
+ end
60
+ it "produces the correct noscript tag" do
61
+ expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
62
+ end
63
+ end
64
+
65
+ context "with variable gist id" do
66
+ before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(body: http_output) }
67
+ let(:gist_id) { "1342013" }
68
+ let(:gist) { "page.gist_id" }
69
+ let(:output) do
70
+ doc.data['gist_id'] = gist_id
71
+ doc.content = content
72
+ doc.output = Bunto::Renderer.new(doc.site, doc).run
73
+ end
74
+
75
+ it "produces the correct script tag" do
76
+ expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js">\s<\/script>/)
77
+ end
78
+ it "produces the correct noscript tag" do
79
+ expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
80
+ end
81
+ end
82
+
83
+ context "with variable gist id and filename" do
84
+ before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(body: http_output) }
85
+ let(:gist_id) { "1342013" }
86
+ let(:gist_filename) { "atom.xml" }
87
+ let(:gist) { "page.gist_id" }
88
+ let(:filename) { "page.gist_filename" }
89
+ let(:content) { "{% gist #{gist} #{filename} %}" }
90
+ let(:output) do
91
+ doc.data['gist_id'] = "1342013"
92
+ doc.data['gist_filename'] = "atom.xml"
93
+ doc.content = content
94
+ doc.output = Bunto::Renderer.new(doc.site, doc).run
95
+ end
96
+
97
+ it "produces the correct script tag" do
98
+ expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>/)
99
+ end
100
+
101
+ it "produces the correct noscript tag" do
102
+ expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
103
+ end
104
+ end
105
+
106
+ context "with valid gist id and invalid filename" do
107
+ before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(status: 404) }
108
+ let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" }
109
+ let(:gist_filename) { "myfile.ext" }
110
+ let(:content) { "{% gist #{gist_id} #{gist_filename} %}" }
111
+
112
+ it "produces the correct script tag" do
113
+ expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>/)
114
+ end
115
+
116
+ it "does not produce the noscript tag" do
117
+ expect(output).to_not match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
118
+ end
119
+
120
+ end
121
+
122
+ context "with token" do
123
+ before { ENV["BUNTO_GITHUB_TOKEN"] = "1234" }
124
+ before {
125
+ stub_request(:get, "https://api.github.com/gists/1342013").
126
+ to_return(:status => 200, :body => fixture("single-file"), :headers => {"Content-Type" => "application/json"})
127
+ }
128
+ let(:gist_id) { "1342013" }
129
+ let(:gist) { "page.gist_id" }
130
+ let(:output) do
131
+ doc.data['gist_id'] = gist_id
132
+ doc.content = content
133
+ doc.output = Bunto::Renderer.new(doc.site, doc).run
134
+ end
135
+
136
+ it "produces the noscript tag" do
137
+ expect(output).to match(/<noscript><pre>contents of gist<\/pre><\/noscript>/)
138
+ end
139
+
140
+ context "with a filename" do
141
+ before {
142
+ stub_request(:get, "https://api.github.com/gists/1342013").
143
+ to_return(:status => 200, :body => fixture("multiple-files"), :headers => {"Content-Type" => "application/json"})
144
+ }
145
+ let(:content) { "{% gist 1342013 hello-world.rb %}" }
146
+
147
+ it "produces the noscript tag" do
148
+ expect(output).to match(/<noscript><pre>puts &#39;hello world&#39;<\/pre><\/noscript>/)
149
+ end
150
+ end
151
+ end
152
+
153
+ context "with noscript disabled" do
154
+ let(:doc) { doc_with_content(content, { "gist" => { "noscript" => false } }) }
155
+ let(:output) do
156
+ doc.content = content
157
+ doc.output = Bunto::Renderer.new(doc.site, doc).run
158
+ end
159
+ let(:gist) { "mattr-/24081a1d93d2898ecf0f" }
160
+
161
+ it "does not produce the noscript tag" do
162
+ expect(output).to_not match(/<noscript>/)
163
+ end
164
+ end
165
+
166
+ end
167
+
168
+ context "invalid gist" do
169
+
170
+ context "no gist id present" do
171
+ let(:gist) { "" }
172
+
173
+ it "raises an error" do
174
+ expect(->{ output }).to raise_error
175
+ end
176
+ end
177
+
178
+ end
179
+
180
+ end
@@ -0,0 +1,49 @@
1
+ TEST_DIR = File.dirname(__FILE__)
2
+ TMP_DIR = File.expand_path("../tmp", TEST_DIR)
3
+
4
+ require 'webmock/rspec'
5
+ require 'cgi'
6
+ require 'bunto'
7
+ require File.expand_path("../lib/bunto-gist.rb", TEST_DIR)
8
+
9
+ Bunto.logger.log_level = :error
10
+
11
+ RSpec.configure do |config|
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+ config.order = 'random'
15
+
16
+ def tmp_dir(*files)
17
+ File.join(TMP_DIR, *files)
18
+ end
19
+
20
+ def source_dir(*files)
21
+ tmp_dir('source', *files)
22
+ end
23
+
24
+ def dest_dir(*files)
25
+ tmp_dir('dest', *files)
26
+ end
27
+
28
+ def doc_with_content(content, opts = {})
29
+ my_site = site(opts)
30
+ Bunto::Document.new(source_dir('_test/doc.md'), {site: my_site, collection: collection(my_site)})
31
+ end
32
+
33
+ def collection(site, label = 'test')
34
+ Bunto::Collection.new(site, label)
35
+ end
36
+
37
+ def site(opts = {})
38
+ conf = Bunto::Utils.deep_merge_hashes(Bunto::Configuration::DEFAULTS, opts.merge({
39
+ "source" => source_dir,
40
+ "destination" => dest_dir
41
+ }))
42
+ Bunto::Site.new(conf)
43
+ end
44
+
45
+ def fixture(name)
46
+ path = File.expand_path "./fixtures/#{name}.json", File.dirname(__FILE__)
47
+ File.open(path).read
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunto-gist
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Parker Moore
8
+ - Suriyaa Kudo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-02-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: octokit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.6'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.6'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: webmock
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: bunto
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '1.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '1.0'
98
+ description:
99
+ email:
100
+ - parkrmoore@gmail.com
101
+ - SuriyaaKudoIsc@users.noreply.github.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - History.markdown
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bunto-gist.gemspec
115
+ - lib/bunto-gist.rb
116
+ - lib/bunto-gist/gist_tag.rb
117
+ - lib/bunto-gist/version.rb
118
+ - script/bootstrap
119
+ - script/cibuild
120
+ - spec/fixtures/multiple-files.json
121
+ - spec/fixtures/single-file.json
122
+ - spec/gist_tag_spec.rb
123
+ - spec/spec_helper.rb
124
+ homepage: https://github.com/bunto/bunto-gist
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 1.9.3
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.2.2
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Liquid tag for displaying GitHub Gists in Bunto sites.
148
+ test_files:
149
+ - spec/fixtures/multiple-files.json
150
+ - spec/fixtures/single-file.json
151
+ - spec/gist_tag_spec.rb
152
+ - spec/spec_helper.rb