jekyll-linkpreview 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 597a9685dd537f7fddef541e75b596cdcda260cc
4
- data.tar.gz: f4a9201088c0881410bf7e87d3963f04e9942de5
2
+ SHA256:
3
+ metadata.gz: 3984e2c6d457274fc633918eaca10fe60d9ea2c38fe04a7c79ff40964b95ea82
4
+ data.tar.gz: ba8cdb6e832ac5fd46d5102ebe630b723a841708628f9c691ef43d078076ed42
5
5
  SHA512:
6
- metadata.gz: e9affa10e185284257a77d68d44736f8c3f0f0d9a324aa4578fdb7fc454504dad883f69b0b2875ef5cdbd0766768709dc0c81a140b219c88bd607714cd1bbe9e
7
- data.tar.gz: bd20c259e0becb55c2976f384f1e220b835e53778b7edc1a0382e37ccf49e00d6eb0aac38b77a453d9281f3cb11802925ac5095b1aa3a9d14c61e072afecf7db
6
+ metadata.gz: 2d4fd1726eb2384afb0363d918b6e3e2698577570682df2f289df8fe0bb4e98024154d5266f262fb3248441792ab2c1b27aeb9eb12c4396ee6bd51dc4f4c12a7
7
+ data.tar.gz: 4a67e9fb18a940ef4f32312d3ea84103fc90751e272a2523ab30fd2ea21a2168260dd8037ae77b35d57b8fd80acff94aaa79c8dd9d51a440bccb448ed220ba0a
data/.gitignore CHANGED
@@ -9,3 +9,6 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ # Appraisal
14
+ *.gemfile.lock
data/.travis.yml CHANGED
@@ -4,4 +4,13 @@ language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
6
  - 2.4.2
7
+ - 2.7.0
7
8
  before_install: gem install bundler -v 2.0.1
9
+ gemfile:
10
+ - gemfiles/jekyll_3.gemfile
11
+ - gemfiles/jekyll_4.gemfile
12
+ script: "bundle exec rake test"
13
+ matrix:
14
+ exclude:
15
+ - rvm: 2.4.2
16
+ gemfile: gemfiles/jekyll_4.gemfile
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "jekyll-3" do
2
+ gem "jekyll", "3.5.0"
3
+ end
4
+
5
+ appraise "jekyll-4" do
6
+ gem "jekyll", "4.0.0"
7
+ end
data/Dockerfile ADDED
@@ -0,0 +1,4 @@
1
+ FROM ruby:2.6.5
2
+
3
+ RUN apt-get -y update
4
+ RUN gem install bundler
data/README.md CHANGED
@@ -1,21 +1,23 @@
1
1
  # Jekyll::Linkpreview
2
2
 
3
- Jekyll plugin to generate link preview by `{% linkpreview %}` tag. The plugin fetches [Open Graph protocols](http://ogp.me/) of the designated page to generate preview. The og properties are saved as JSON for caching and it is used when rebuilding the site.
3
+ [![Build Status](https://travis-ci.org/ysk24ok/jekyll-linkpreview.svg?branch=master)](https://travis-ci.org/ysk24ok/jekyll-linkpreview)
4
4
 
5
- You can pass url directly to the tag,
5
+ Jekyll plugin to generate link preview by `{% linkpreview %}` tag. The plugin fetches [Open Graph protocol](http://ogp.me/) metadata of the designated page to generate preview. The og properties are saved as JSON for caching and it is used when rebuilding the site.
6
+
7
+ You can pass url directly to the tag,
6
8
 
7
9
  ```
8
- {% linkpreview https://github.com %}
10
+ {% linkpreview "https://github.com" %}
9
11
  ```
10
12
 
11
13
  or, can pass a url variable.
12
14
 
13
15
  ```
14
- {% assign github_toppage = 'https://github.com'%}
16
+ {% assign github_toppage = 'https://github.com' %}
15
17
  {% linkpreview github_toppage %}
16
18
  ```
17
19
 
18
- The tag above generates following HTML when you run `jekyll build`,
20
+ The tag above generates following HTML when you run `jekyll build`.
19
21
 
20
22
  ```html
21
23
  <div class="jekyll-linkpreview-wrapper">
@@ -41,17 +43,27 @@ The tag above generates following HTML when you run `jekyll build`,
41
43
  </div>
42
44
  ```
43
45
 
44
- Applying appropriate CSS, the link preview will be like this.
46
+ By applying appropriate CSS, the link preview will be like this.
45
47
 
46
48
  <img width="613" alt="スクリーンショット 2019-04-03 20 52 50" src="https://user-images.githubusercontent.com/3449164/55479970-35baf100-565a-11e9-8c5d-709213917f74.png">
47
49
 
50
+ When the page does not have Open Graph protocol metadata, following simple HTML will be generated.
51
+
52
+ ```html
53
+ <div class="jekyll-linkpreview-wrapper">
54
+ <p><a href="https://example.com" target="_blank">https://example.com</a></p>
55
+ </div>
56
+ ```
57
+
58
+ You can override the default templates, see [Custom templates](#user-content-custom-templates).
59
+
48
60
  ## Installation
49
61
 
50
62
  See https://jekyllrb.com/docs/plugins/installation/ .
51
63
 
52
64
  ## Usage
53
65
 
54
- 1. Create `_cache` directory where you run `jekyll build` or `jekyll serve`.
66
+ 1. Create `_cache` directory.
55
67
 
56
68
  1. Embed [linkpreview.css](assets/css/linkpreview.css) into your Website.
57
69
 
@@ -59,6 +71,28 @@ See https://jekyllrb.com/docs/plugins/installation/ .
59
71
 
60
72
  1. Run `jekyll build` or `jekyll serve`.
61
73
 
74
+
75
+ ## Custom templates
76
+
77
+ You can override the default templates used for generating previews, both in case Open Graph protocol metadata exists or does not exist for a given page.
78
+
79
+ ### Template for pages where Open Graph protocol metadata exists
80
+
81
+ 1. Place `linkpreview.html` file inside `_includes/` folder of your Jekyll site (`_includes/linkpreview.html`)
82
+
83
+ 2. Use built-in variables to extract data which you would like to render. Available variables are:
84
+ * **link_url** i.e. `{{ link_url }}`
85
+ * **link_title** i.e. `{{ link_title }}`
86
+ * **link_image** i.e. `{{ link_image }}`
87
+ * **link_description** i.e. `{{ link_description }}`
88
+ * **link_domain** i.e. `{{ link_domain }}`
89
+
90
+ ### Template for pages where Open Graph protocol metadata does not exist
91
+
92
+ 1. Place `linkpreview_nog.html` file inside `_includes/` folder of your Jekyll site (`_includes/linkpreview_nog.html`)
93
+
94
+ 2. Use built-in **link_url** variable to render URL data, i.e. `{{ link_url }}`
95
+
62
96
  ## Development
63
97
 
64
98
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -4,3 +4,4 @@ require "rspec/core/rake_task"
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
6
  task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "jekyll", "3.5.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "jekyll", "4.0.0"
6
+
7
+ gemspec path: "../"
@@ -18,9 +18,11 @@ Gem::Specification.new do |spec|
18
18
  end
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "jekyll", "~> 3.5"
21
+ spec.add_dependency "jekyll", ">= 3.5", "< 4.1"
22
22
  spec.add_dependency "metainspector"
23
23
  spec.add_development_dependency "bundler", "~> 2.0"
24
- spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rake", "~> 12.3.3"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "appraisal", "~> 2.2.0"
27
+ spec.add_development_dependency "wwtd", "~> 1.4.1"
26
28
  end
@@ -51,7 +51,11 @@ module Jekyll
51
51
  if url.nil? then
52
52
  return nil
53
53
  end
54
- url.match(%r{(http|https)://([^/]+).*})[-1]
54
+ m = url.match(%r{(http|https)://([^/]+).*})
55
+ if m.nil? then
56
+ return nil
57
+ end
58
+ m[-1]
55
59
  end
56
60
  end
57
61
 
@@ -60,7 +64,7 @@ module Jekyll
60
64
 
61
65
  def initialize(tag_name, markup, parse_context)
62
66
  super
63
- @markup = markup.rstrip()
67
+ @markup = markup.strip()
64
68
  @og_properties = OpenGraphProperties.new
65
69
  end
66
70
 
@@ -71,15 +75,53 @@ module Jekyll
71
75
  image = properties['image']
72
76
  description = properties['description']
73
77
  domain = properties['domain']
74
- if title.nil? || image.nil? || description.nil? || domain.nil? then
75
- html = <<-EOS
76
- <div class="jekyll-linkpreview-wrapper">
77
- <p><a href="#{url}" target="_blank">#{url}</a></p>
78
- </div>
79
- EOS
80
- return html
78
+
79
+ if title.nil? || image.nil? || domain.nil? then
80
+ render_linkpreview_nog(context, url)
81
+ else
82
+ render_linkpreview_og(context, url, title, image, description, domain)
83
+ end
84
+ end
85
+
86
+ def get_properties(url)
87
+ cache_filepath = "#{@@cache_dir}/%s.json" % Digest::MD5.hexdigest(url)
88
+ if File.exist?(cache_filepath) then
89
+ return load_cache_file(cache_filepath)
90
+ end
91
+ properties = @og_properties.get(url)
92
+ if Dir.exists?(@@cache_dir) then
93
+ save_cache_file(cache_filepath, properties)
94
+ else
95
+ # TODO: This message will be shown at all linkprevew tag
96
+ warn "'#{@@cache_dir}' directory does not exist. Create it for caching."
81
97
  end
82
- html = <<-EOS
98
+ properties
99
+ end
100
+
101
+ private
102
+ def get_url_from(context)
103
+ context[@markup]
104
+ end
105
+
106
+ private
107
+ def load_cache_file(filepath)
108
+ JSON.parse(File.open(filepath).read)
109
+ end
110
+
111
+ protected
112
+ def save_cache_file(filepath, properties)
113
+ File.open(filepath, 'w') { |f| f.write JSON.generate(properties) }
114
+ end
115
+
116
+ private
117
+ def render_linkpreview_og(context, url, title, image, description, domain)
118
+ template_path = get_linkpreview_og_template()
119
+ if File.exist?(template_path)
120
+ template_file = File.read template_path
121
+ site = context.registers[:site]
122
+ template_file = (Liquid::Template.parse template_file).render site.site_payload.merge!({"link_url" => url, "link_title" => title, "link_image" => image, "link_description" => description, "link_domain" => domain})
123
+ else
124
+ html = <<-EOS
83
125
  <div class="jekyll-linkpreview-wrapper">
84
126
  <p><a href="#{url}" target="_blank">#{url}</a></p>
85
127
  <div class="jekyll-linkpreview-wrapper-inner">
@@ -101,38 +143,36 @@ module Jekyll
101
143
  </div>
102
144
  </div>
103
145
  </div>
104
- EOS
105
- html
146
+ EOS
147
+ html
148
+ end
106
149
  end
107
150
 
108
- def get_properties(url)
109
- cache_filepath = "#{@@cache_dir}/%s.json" % Digest::MD5.hexdigest(url)
110
- if File.exist?(cache_filepath) then
111
- return load_cache_file(cache_filepath)
112
- end
113
- properties = @og_properties.get(url)
114
- if Dir.exists?(@@cache_dir) then
115
- save_cache_file(cache_filepath, properties)
151
+ private
152
+ def render_linkpreview_nog(context, url)
153
+ template_path = get_linkpreview_nog_template()
154
+ if File.exist?(template_path)
155
+ template_file = File.read template_path
156
+ site = context.registers[:site]
157
+ template_file = (Liquid::Template.parse template_file).render site.site_payload.merge!({"link_url" => url})
116
158
  else
117
- # TODO: This message will be shown at all linkprevew tag
118
- warn "'#{@@cache_dir}' directory does not exist. Create it for caching."
159
+ html = <<-EOS
160
+ <div class="jekyll-linkpreview-wrapper">
161
+ <p><a href="#{url}" target="_blank">#{url}</a></p>
162
+ </div>
163
+ EOS
164
+ html
119
165
  end
120
- properties
121
166
  end
122
167
 
123
168
  private
124
- def get_url_from(context)
125
- context.scopes[0].key?(@markup) ? context.scopes[0][@markup] : @markup
169
+ def get_linkpreview_og_template()
170
+ File.join Dir.pwd, "_includes", "linkpreview.html"
126
171
  end
127
172
 
128
173
  private
129
- def load_cache_file(filepath)
130
- JSON.parse(File.open(filepath).read)
131
- end
132
-
133
- protected
134
- def save_cache_file(filepath, properties)
135
- File.open(filepath, 'w').write(JSON.generate(properties))
174
+ def get_linkpreview_nog_template()
175
+ File.join Dir.pwd, "_includes", "linkpreview_nog.html"
136
176
  end
137
177
  end
138
178
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Linkpreview
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-linkpreview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yusuke Nishioka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-21 00:00:00.000000000 Z
11
+ date: 2020-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.5'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.1'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '3.5'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.1'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: metainspector
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +64,14 @@ dependencies:
58
64
  requirements:
59
65
  - - "~>"
60
66
  - !ruby/object:Gem::Version
61
- version: '10.0'
67
+ version: 12.3.3
62
68
  type: :development
63
69
  prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
72
  - - "~>"
67
73
  - !ruby/object:Gem::Version
68
- version: '10.0'
74
+ version: 12.3.3
69
75
  - !ruby/object:Gem::Dependency
70
76
  name: rspec
71
77
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +86,34 @@ dependencies:
80
86
  - - "~>"
81
87
  - !ruby/object:Gem::Version
82
88
  version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: appraisal
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 2.2.0
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 2.2.0
103
+ - !ruby/object:Gem::Dependency
104
+ name: wwtd
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 1.4.1
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 1.4.1
83
117
  description:
84
118
  email:
85
119
  - yusuke.nishioka.0713@gmail.com
@@ -90,6 +124,8 @@ files:
90
124
  - ".gitignore"
91
125
  - ".rspec"
92
126
  - ".travis.yml"
127
+ - Appraisals
128
+ - Dockerfile
93
129
  - Gemfile
94
130
  - LICENSE.txt
95
131
  - README.md
@@ -97,6 +133,9 @@ files:
97
133
  - assets/css/linkpreview.css
98
134
  - bin/console
99
135
  - bin/setup
136
+ - gemfiles/.bundle/config
137
+ - gemfiles/jekyll_3.gemfile
138
+ - gemfiles/jekyll_4.gemfile
100
139
  - jekyll-linkpreview.gemspec
101
140
  - lib/jekyll-linkpreview.rb
102
141
  - lib/jekyll-linkpreview/version.rb
@@ -119,8 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
158
  - !ruby/object:Gem::Version
120
159
  version: '0'
121
160
  requirements: []
122
- rubyforge_project:
123
- rubygems_version: 2.5.2.3
161
+ rubygems_version: 3.0.3
124
162
  signing_key:
125
163
  specification_version: 4
126
164
  summary: Jekyll tag plugin to generate link preview