jekyll-pdf-thumbnail 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
+ SHA256:
3
+ metadata.gz: 3f7bf101fc85d603f19be64eb90a542d164e64fe9428169113dbbb57742c9f91
4
+ data.tar.gz: ed78e574d487215b44672ca301c59e26b0741f38f0da5e71f0f82e7a1e04d9eb
5
+ SHA512:
6
+ metadata.gz: cccb40e7c9b65f44697863c55700474ab1993d4752377579e731fd6f4c0c62803a60066fcac7bb487a74df885e0efa2977e0dc6fe52bb090d0aeb676a7cb2db8
7
+ data.tar.gz: e3072a706e5a0620a66920a2c95b845541ed3ece2f9a60ec7ff77ed2192c442355c974abf71fe7f361ef5dd444309487c0d1d72f2f405f4b54dd890df652948b
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2004 Noe Nieto<nnieto@noenieto.com>
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Jekyll PDF thumbnails generator
2
+
3
+ A Jekyll plugin to generate thumbnails for all PDF files.
4
+
5
+
6
+ ## Usage
7
+
8
+ 1. Add the following to your site's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'jekyll-pdf-thumbnail'
12
+ ```
13
+
14
+ 2. Add the following to your site's config file:
15
+
16
+ ```yml
17
+ plugins:
18
+ - jekyll-pdf-thumbnail
19
+ ```
20
+
21
+ 3. Use it in your `.md` files and liquid templates. Example markdown file:
22
+
23
+ ```md
24
+ ---
25
+ title: Homepage
26
+ layout: null
27
+ pdf_file: /assets/sample_1.pdf
28
+ ---
29
+ # {{ page.title }}
30
+
31
+ {% assign other_pdf = 'sample_2.pdf' %}
32
+
33
+ - This is a link to [{{page.pdf_file}}]({{page.pdf_file | absolute_url}})
34
+ - This is a preview of ![sample_1.pdf]({{ page.pdf_file| pdf_thumbnail }})
35
+ - This is a link to [sample_2.pdf]({{other_pdf | absolute_url }})
36
+ - This is a preview of ![sample_2.pdf]({{ other_pdf | pdf_thumbnail }})
37
+ ```
38
+
@@ -0,0 +1,78 @@
1
+ require 'pdftoimage'
2
+ require 'jekyll'
3
+ require "digest"
4
+
5
+
6
+
7
+ module PDFThumbnail
8
+ CACHE_DIR = "/assets/pdf_thumbnails/"
9
+ HASH_LENGTH = 32
10
+
11
+ def _dest_filename(src_path)
12
+ # Generates a thumbnail name using the SHA256 digest of the PDF file
13
+ #
14
+ # Example:
15
+ # >> _dest_filename("sample_1.pdf")
16
+ # => a35383ccca791ba6aa67ab3acde65287.png
17
+ #
18
+ # Arguments:
19
+ # src_path: (String)
20
+ hash = Digest::SHA256.file(src_path)
21
+ short_hash = hash.hexdigest()[0, HASH_LENGTH]
22
+ "#{short_hash}.png"
23
+ end
24
+
25
+ def _must_create?(src_path, dest_path)
26
+ # Returns true if dest_path doesn't exists or src_path is newer than dest_path
27
+ !File.exist?(dest_path) || File.mtime(dest_path) <= File.mtime(src_path)
28
+ end
29
+
30
+
31
+ # The PDF thumbnail generator
32
+ class Generator < Jekyll::Generator
33
+ include PDFThumbnail
34
+ def generate(site)
35
+ # Ensure the cache dir exists
36
+ full_cache_path = File.join(site.source, CACHE_DIR)
37
+ FileUtils.mkdir_p(full_cache_path)
38
+
39
+ site.static_files.each do |static_file|
40
+ if static_file.extname.downcase == ".pdf"
41
+ full_pdf_path = File.join(site.source, static_file.relative_path)
42
+ thumb = _dest_filename(full_pdf_path)
43
+ full_thumb_path = File.join(full_cache_path, thumb)
44
+ rel_thumb_path = File.join(CACHE_DIR, thumb)
45
+ if _must_create?(full_pdf_path, full_thumb_path)
46
+ puts "Creating thumbnail of' #{static_file.relative_path}' to '#{rel_thumb_path}'"
47
+ PDFToImage.open(full_pdf_path).first.resize('50%').save(full_thumb_path)
48
+ site.static_files << Jekyll::StaticFile.new(site, site.source, CACHE_DIR, thumb)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ module Filters
56
+ include PDFThumbnail
57
+
58
+ def pdf_thumbnail(pdf)
59
+ # Returns the thumbnail path for a given pdf file.
60
+ # Example:
61
+ # >> pdf_thumbnail "/path/to/sample_1.pdf"
62
+ # => /assets/pdf_thumbnails/a35383ccca791ba6aa67ab3acde65287.png
63
+ #
64
+ # Or as a liquid filter:
65
+ # {% assign my_pdf = 'sample_2.pdf' %}
66
+ # {{ my_pdf | pdf_preview }}
67
+ #
68
+ # Arguments:
69
+ # pdf: (String)
70
+ site = @context.registers[:site]
71
+ full_pdf_path = File.join(site.source, pdf)
72
+ File.join(CACHE_DIR, _dest_filename(full_pdf_path))
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ Liquid::Template.register_filter(PDFThumbnail::Filters)
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-pdf-thumbnail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Noe Nieto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pdftoimage
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3.7'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '12.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '12.3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.8'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.8'
75
+ - !ruby/object:Gem::Dependency
76
+ name: debug
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.0'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.0.0
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.0'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.0.0
95
+ description: This plugin generates thumbnails for each PDF in your site using the
96
+ pdftoimage gem
97
+ email: nnieto@noenieto.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files:
101
+ - README.md
102
+ - LICENSE.txt
103
+ files:
104
+ - LICENSE.txt
105
+ - README.md
106
+ - lib/jekyll-pdf-thumbnail.rb
107
+ homepage: https://rubygems.org/gems/jekyll-pdf-thumbnail
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 2.5.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.4.10
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Generates PNG thumbnails of your PDFs
130
+ test_files: []