jekyll-pdf-thumbnail 0.1.0 → 0.3.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +22 -2
  3. data/lib/jekyll-pdf-thumbnail.rb +50 -28
  4. metadata +2 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f7bf101fc85d603f19be64eb90a542d164e64fe9428169113dbbb57742c9f91
4
- data.tar.gz: ed78e574d487215b44672ca301c59e26b0741f38f0da5e71f0f82e7a1e04d9eb
3
+ metadata.gz: 6aa634af38ffa22e2b31b0710d02544aeb1448ba322861049f3df1a1caf71999
4
+ data.tar.gz: a29926988e56c70194d8f3822ff282c02444665dff20123aab60cec16f2b9295
5
5
  SHA512:
6
- metadata.gz: cccb40e7c9b65f44697863c55700474ab1993d4752377579e731fd6f4c0c62803a60066fcac7bb487a74df885e0efa2977e0dc6fe52bb090d0aeb676a7cb2db8
7
- data.tar.gz: e3072a706e5a0620a66920a2c95b845541ed3ece2f9a60ec7ff77ed2192c442355c974abf71fe7f361ef5dd444309487c0d1d72f2f405f4b54dd890df652948b
6
+ metadata.gz: 2a0be4b925cf86697ba75aad72c448ad35bd20c28c861e3154e34f0deb0382e20a3d9f81c315160205e1f2f8acf6c679426c7e9889d6eabfea9fbb380a0ee94d
7
+ data.tar.gz: c63da7b588b7279ce58ad7c486d2a565d38cb03e4b479618acb9612f4c05a44da36df524b8434004900080ee43eb30c09620ce25cda4ed6c4f53357295efd54b
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Jekyll PDF thumbnails generator
2
2
 
3
- A Jekyll plugin to generate thumbnails for all PDF files.
3
+ A Jekyll plugin to generate thumbnails for your PDF files
4
4
 
5
5
 
6
6
  ## Usage
@@ -20,7 +20,7 @@ A Jekyll plugin to generate thumbnails for all PDF files.
20
20
 
21
21
  3. Use it in your `.md` files and liquid templates. Example markdown file:
22
22
 
23
- ```md
23
+ ```markdown
24
24
  ---
25
25
  title: Homepage
26
26
  layout: null
@@ -34,5 +34,25 @@ A Jekyll plugin to generate thumbnails for all PDF files.
34
34
  - This is a preview of ![sample_1.pdf]({{ page.pdf_file| pdf_thumbnail }})
35
35
  - This is a link to [sample_2.pdf]({{other_pdf | absolute_url }})
36
36
  - This is a preview of ![sample_2.pdf]({{ other_pdf | pdf_thumbnail }})
37
+ - 50% resize: {{page.pdf_file | pdf_thumbnail: resize:'50%' }}
38
+ - 25% resize: {{page.pdf_file | pdf_thumbnail: resize:'25%' }}
39
+ - 25% resize, 50% quality: {{page.pdf_file | pdf_thumbnail: resize:'25%', quality:'50'}}
37
40
  ```
38
41
 
42
+ - The format of the `resize` parameter is the **image geometry** as defined in the [ImageMagick manual](https://imagemagick.org/script/command-line-processing.php#geometry).
43
+ - The format of the `quality` parameter is a number between 1 and 100 as defined in the [Image Magick manual](https://imagemagick.org/script/command-line-options.php#quality). Currently, only png thumbnails are supported. According to the manual:
44
+ > ... the quality value sets the zlib compression level (quality / 10) and filter-type (quality % 10). The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering, unless the image has a color map, in which case it means compression level 7 with no PNG filtering.
45
+
46
+ ## Developing this extension
47
+
48
+ - **Setup**: Clone the extension and execute `bundle install` to install the dependencies
49
+ - **Run tests**: ``bundle exec rspec``
50
+ - **Releasing a new version**
51
+ - Update [jekyll-pdf-thumbnail.gemspec](jekyll-pdf-thumbnail.gemspec) with new version and metadata.
52
+ - Commit your changes.
53
+ - Create a new git tag: `git tag -a vx.y.z -m "Version x.y.z"`
54
+ - Push all the changes to github: `git push origin && git push origin --tags`
55
+ - Build the Gem: ``gem build``
56
+ - Upload the new gem: ``gem push jekyll-pdf-thumbnail-x.y.z.gem``
57
+
58
+
@@ -3,12 +3,23 @@ require 'jekyll'
3
3
  require "digest"
4
4
 
5
5
 
6
-
7
6
  module PDFThumbnail
7
+
8
+
8
9
  CACHE_DIR = "/assets/pdf_thumbnails/"
9
10
  HASH_LENGTH = 32
11
+ @@cache_dir_present = false
12
+
13
+ def _get_cache_dir(site)
14
+ full_cache_dir = File.join(site.source, CACHE_DIR)
15
+ if not @@cache_dir_present
16
+ FileUtils.mkdir_p(full_cache_dir)
17
+ @@cache_dir_present = true
18
+ end
19
+ full_cache_dir
20
+ end
10
21
 
11
- def _dest_filename(src_path)
22
+ def _dest_filename(src_path, resize, quality)
12
23
  # Generates a thumbnail name using the SHA256 digest of the PDF file
13
24
  #
14
25
  # Example:
@@ -17,9 +28,20 @@ module PDFThumbnail
17
28
  #
18
29
  # Arguments:
19
30
  # src_path: (String)
31
+ # resize: (String) as used in the -resize parameter of convert
32
+ # quality: (String) as used in the -quality parameter of convert
20
33
  hash = Digest::SHA256.file(src_path)
21
34
  short_hash = hash.hexdigest()[0, HASH_LENGTH]
22
- "#{short_hash}.png"
35
+ basename = short_hash
36
+ if resize
37
+ cleaned = resize.gsub(/[^\da-z]+/i, "")
38
+ basename << "_r#{cleaned}"
39
+ end
40
+ if quality
41
+ cleaned = quality.gsub(/[^\da-z]+/i, "")
42
+ basename << "_q#{cleaned}"
43
+ end
44
+ basename << ".png"
23
45
  end
24
46
 
25
47
  def _must_create?(src_path, dest_path)
@@ -27,35 +49,16 @@ module PDFThumbnail
27
49
  !File.exist?(dest_path) || File.mtime(dest_path) <= File.mtime(src_path)
28
50
  end
29
51
 
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
52
+ def generate_thumbnail(pdf, thumbnail, resize='50%', quality="80%")
53
+ first = PDFToImage.open(pdf).first
54
+ first.args << "-thumbnail #{resize}"
55
+ first.save(thumbnail)
53
56
  end
54
57
 
55
58
  module Filters
56
59
  include PDFThumbnail
57
60
 
58
- def pdf_thumbnail(pdf)
61
+ def pdf_thumbnail(pdf, args=nil)
59
62
  # Returns the thumbnail path for a given pdf file.
60
63
  # Example:
61
64
  # >> pdf_thumbnail "/path/to/sample_1.pdf"
@@ -67,9 +70,28 @@ module PDFThumbnail
67
70
  #
68
71
  # Arguments:
69
72
  # pdf: (String)
73
+ # args: a dict that can contain "resize" and "quality"
74
+ if args == nil
75
+ args = {"resize"=> "50%", "quality": '80%'}
76
+ end
77
+ resize = args.fetch("resize", "50%")
78
+ quality = args.fetch("quality", "80%")
70
79
  site = @context.registers[:site]
80
+ full_cache_dir = _get_cache_dir(site)
71
81
  full_pdf_path = File.join(site.source, pdf)
72
- File.join(CACHE_DIR, _dest_filename(full_pdf_path))
82
+ thumbnail = _dest_filename(full_pdf_path, resize, quality)
83
+ rel_thumb_path = File.join(CACHE_DIR, thumbnail)
84
+ full_thumb_path = File.join(_get_cache_dir(site), thumbnail)
85
+ if _must_create?(full_pdf_path, full_thumb_path)
86
+ puts "Creating thumbnail of' #{pdf}' to '#{rel_thumb_path}'"
87
+ generate_thumbnail(full_pdf_path, full_thumb_path, resize, quality)
88
+ # site - The Site.
89
+ # base - The String path to the <source>.
90
+ # dir - The String path between <source> and the file.
91
+ # name - The String filename of the file.
92
+ site.static_files << Jekyll::StaticFile.new(site, site.source, CACHE_DIR, thumbnail)
93
+ end
94
+ rel_thumb_path
73
95
  end
74
96
  end
75
97
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-pdf-thumbnail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noe Nieto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-10 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pdftoimage
@@ -72,26 +72,6 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
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
75
  description: This plugin generates thumbnails for each PDF in your site using the
96
76
  pdftoimage gem
97
77
  email: nnieto@noenieto.com