jekyll-linkpreview 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e553cbd1585c7303612cb32a21651039f3ebba2f
4
+ data.tar.gz: 58bf7022cf2eb09e8661292b5143bd8b8750d201
5
+ SHA512:
6
+ metadata.gz: d41da5f3352c0fee0e9fcdb5c5d91f8b3f75d3943d2fe00f41593ed00b78f0d94e55d8d5bb8474022a815dcbf532622b6afc8a8df62d84bfde0cb0e39ae2075e
7
+ data.tar.gz: e6a23a9bb9c3170780aadd984af4977034055eaa4d53fc6ef008ce4bb92818057877894cb1bd43d2927b04e09c389d36af1572f6e054151272b74851d2b171aa
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.2
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jekyll-linkpreview.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Yusuke NISHIOKA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Jekyll::Linkpreview
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.
4
+
5
+ For example, `{% linkpreview https://github.com %}` tag generates following HTML when you run `jekyll build`,
6
+
7
+ ```html
8
+ <div class="jekyll-linkpreview-wrapper">
9
+ <p><a href="https://github.com" target="_blank">https://github.com</a></p>
10
+ <div class="jekyll-linkpreview-wrapper-inner">
11
+ <div class="jekyll-linkpreview-content">
12
+ <div class="jekyll-linkpreview-image">
13
+ <a href="https://github.com" target="_blank">
14
+ <img src="https://github.githubassets.com/images/modules/open_graph/github-logo.png" />
15
+ </a>
16
+ </div>
17
+ <div class="jekyll-linkpreview-body">
18
+ <h2 class="jekyll-linkpreview-title">
19
+ <a href="https://github.com" target="_blank">Build software better, together</a>
20
+ </h2>
21
+ <div class="jekyll-linkpreview-description">GitHub is where people build software. More than 31 million people use GitHub to discover, fork, and contribute to over 100 million projects.</div>
22
+ </div>
23
+ </div>
24
+ <div class="jekyll-linkpreview-footer">
25
+ <a href="https://github.com" target="_blank">github.com</a>
26
+ </div>
27
+ </div>
28
+ </div>
29
+ ```
30
+
31
+ Applying appropriate CSS, the link preview will be like this.
32
+
33
+ <img width="613" alt="スクリーンショット 2019-04-03 20 52 50" src="https://user-images.githubusercontent.com/3449164/55479970-35baf100-565a-11e9-8c5d-709213917f74.png">
34
+
35
+ ## Installation
36
+
37
+ See https://jekyllrb.com/docs/plugins/installation/ .
38
+
39
+ ## Usage
40
+
41
+ 1. Create `_cache` directory in the same hierarchy as `_site` directory.
42
+
43
+ 1. Embed [linkpreview.css](assets/css/linkpreview.css) into your Website.
44
+
45
+ 1. Use `{% linkpreview %}` tag.
46
+
47
+ 1. Run `jekyll build` or `jekyll serve`.
48
+
49
+ ## Development
50
+
51
+ 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.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ysk24ok/jekyll-linkpreview.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,46 @@
1
+ .jekyll-linkpreview-wrapper {
2
+ max-width: 600px;
3
+ margin-bottom: 20px;
4
+ }
5
+
6
+ .jekyll-linkpreview-wrapper-inner {
7
+ border: 1px solid rgba(0,0,0,.1);
8
+ padding: 12px;
9
+ }
10
+
11
+ .jekyll-linkpreview-content {
12
+ position: relative;
13
+ height: 100px;
14
+ overflow: hidden;
15
+ margin-bottom: 10px;
16
+ }
17
+
18
+ .jekyll-linkpreview-image {
19
+ position: absolute;
20
+ top: 0;
21
+ right: 0;
22
+ }
23
+
24
+ .jekyll-linkpreview-image img {
25
+ width: 100px;
26
+ height: 100px;
27
+ }
28
+
29
+ .jekyll-linkpreview-body {
30
+ margin-right: 110px;
31
+ }
32
+
33
+ .jekyll-linkpreview-title {
34
+ font-size: 17px;
35
+ margin: 0 0 2px;
36
+ line-height: 1.3;
37
+ }
38
+
39
+ .jekyll-linkpreview-description {
40
+ line-height: 1.5;
41
+ font-size: 13px;
42
+ }
43
+
44
+ .jekyll-linkpreview-footer {
45
+ font-size: 11px;
46
+ }
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jekyll/linkpreview"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jekyll-linkpreview/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-linkpreview"
8
+ spec.version = Jekyll::Linkpreview::VERSION
9
+ spec.authors = ["Yusuke Nishioka"]
10
+ spec.email = ["yusuke.nishioka.0713@gmail.com"]
11
+
12
+ spec.summary = %q{Jekyll tag plugin to generate link preview}
13
+ spec.homepage = "https://github.com/ysk24ok/jekyll-linkpreview"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "jekyll", "~> 3.5"
22
+ spec.add_dependency "metainspector"
23
+ spec.add_development_dependency "bundler", "~> 2.0"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,109 @@
1
+ require "digest"
2
+ require "json"
3
+
4
+ require "metainspector"
5
+ require "jekyll-linkpreview/version"
6
+
7
+ module Jekyll
8
+ module Linkpreview
9
+ class LinkpreviewTag < Liquid::Tag
10
+ def initialize(tag_name, markup, parse_context)
11
+ super
12
+ @markup = markup.rstrip()
13
+ @cache_filepath = "_cache/%s.json" % Digest::MD5.hexdigest(@markup)
14
+ end
15
+
16
+ def render(context)
17
+ properties = get_properties()
18
+ title = properties['title']
19
+ url = properties['url']
20
+ image = properties['image']
21
+ description = properties['description']
22
+ domain = properties['domain']
23
+ if title.nil? || url.nil? || image.nil? then
24
+ html = <<-EOS
25
+ <div class="jekyll-linkpreview-wrapper">
26
+ <p><a href="#{@markup}" target="_blank">#{@markup}</a></p>
27
+ </div>
28
+ EOS
29
+ return html
30
+ end
31
+ html = <<-EOS
32
+ <div class="jekyll-linkpreview-wrapper">
33
+ <p><a href="#{@markup}" target="_blank">#{@markup}</a></p>
34
+ <div class="jekyll-linkpreview-wrapper-inner">
35
+ <div class="jekyll-linkpreview-content">
36
+ <div class="jekyll-linkpreview-image">
37
+ <a href="#{url}" target="_blank">
38
+ <img src="#{image}" />
39
+ </a>
40
+ </div>
41
+ <div class="jekyll-linkpreview-body">
42
+ <h2 class="jekyll-linkpreview-title">
43
+ <a href="#{url}" target="_blank">#{title}</a>
44
+ </h2>
45
+ <div class="jekyll-linkpreview-description">#{description}</div>
46
+ </div>
47
+ </div>
48
+ <div class="jekyll-linkpreview-footer">
49
+ <a href="#{url}" target="_blank">#{domain}</a>
50
+ </div>
51
+ </div>
52
+ </div>
53
+ EOS
54
+ html
55
+ end
56
+
57
+ def get_properties()
58
+ if File.exist?(@cache_filepath) then
59
+ return load_cache_file
60
+ else
61
+ og_properties = fetch_og_properties()
62
+ url = get_og_property(og_properties, 'og:url')
63
+ properties = {
64
+ 'title' => get_og_property(og_properties, 'og:title'),
65
+ 'url' => url,
66
+ 'image' => get_og_property(og_properties, 'og:image'),
67
+ 'description' => get_og_property(og_properties, 'og:description'),
68
+ 'domain' => extract_domain(url)
69
+ }
70
+ save_cache_file(properties)
71
+ return properties
72
+ end
73
+ end
74
+
75
+ private
76
+ def fetch_og_properties()
77
+ MetaInspector.new(@markup).meta_tags['property']
78
+ end
79
+
80
+ private
81
+ def get_og_property(properties, key)
82
+ if !properties.key? key then
83
+ return nil
84
+ end
85
+ properties[key][0]
86
+ end
87
+
88
+ private
89
+ def extract_domain(url)
90
+ if url.nil? then
91
+ return nil
92
+ end
93
+ url.match(%r{(http|https)://([^/]+).*})[-1]
94
+ end
95
+
96
+ private
97
+ def load_cache_file()
98
+ JSON.parse(File.open(@cache_filepath).read)
99
+ end
100
+
101
+ private
102
+ def save_cache_file(properties)
103
+ File.open(@cache_filepath, 'w').write(JSON.generate(properties))
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ Liquid::Template.register_tag("linkpreview", Jekyll::Linkpreview::LinkpreviewTag)
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Linkpreview
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-linkpreview
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke Nishioka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: metainspector
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - yusuke.nishioka.0713@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - assets/css/linkpreview.css
98
+ - bin/console
99
+ - bin/setup
100
+ - jekyll-linkpreview.gemspec
101
+ - lib/jekyll-linkpreview.rb
102
+ - lib/jekyll-linkpreview/version.rb
103
+ homepage: https://github.com/ysk24ok/jekyll-linkpreview
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.13
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Jekyll tag plugin to generate link preview
127
+ test_files: []