jekyll-pdf-thumbnail 0.1.0 → 0.2.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 +8 -2
  3. data/lib/jekyll-pdf-thumbnail.rb +51 -28
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f7bf101fc85d603f19be64eb90a542d164e64fe9428169113dbbb57742c9f91
4
- data.tar.gz: ed78e574d487215b44672ca301c59e26b0741f38f0da5e71f0f82e7a1e04d9eb
3
+ metadata.gz: 3c5758b91a30f57a637f6c92a7ef8d3998127a29fa8393936487faffc406fa42
4
+ data.tar.gz: 10d01019d06fd365c4bc66134ba8211a39389caa4ee07546ad4674d9e0b172af
5
5
  SHA512:
6
- metadata.gz: cccb40e7c9b65f44697863c55700474ab1993d4752377579e731fd6f4c0c62803a60066fcac7bb487a74df885e0efa2977e0dc6fe52bb090d0aeb676a7cb2db8
7
- data.tar.gz: e3072a706e5a0620a66920a2c95b845541ed3ece2f9a60ec7ff77ed2192c442355c974abf71fe7f361ef5dd444309487c0d1d72f2f405f4b54dd890df652948b
6
+ metadata.gz: 0e80b2fb091c471a341829ad3702917f2f90dad9891c5133cde2a699a597f456bd0c06c976c66be60120b62b917b75101ee0b5c52c9893e954513ce90b571049
7
+ data.tar.gz: facd117749b10a4aa7d48d643cabbcbf8fe578bd9c81207088b6be73e6167a82f4b3d299470994f280d90419c8b6dd2c1c7602e72e65fd050d83529173852a7d
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,11 @@ 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.
@@ -1,14 +1,26 @@
1
1
  require 'pdftoimage'
2
2
  require 'jekyll'
3
3
  require "digest"
4
-
4
+ require 'debug'
5
5
 
6
6
 
7
7
  module PDFThumbnail
8
+
9
+
8
10
  CACHE_DIR = "/assets/pdf_thumbnails/"
9
11
  HASH_LENGTH = 32
12
+ @@cache_dir_present = false
13
+
14
+ def _get_cache_dir(site)
15
+ full_cache_dir = File.join(site.source, CACHE_DIR)
16
+ if not @@cache_dir_present
17
+ FileUtils.mkdir_p(full_cache_dir)
18
+ @@cache_dir_present = true
19
+ end
20
+ full_cache_dir
21
+ end
10
22
 
11
- def _dest_filename(src_path)
23
+ def _dest_filename(src_path, resize, quality)
12
24
  # Generates a thumbnail name using the SHA256 digest of the PDF file
13
25
  #
14
26
  # Example:
@@ -17,9 +29,20 @@ module PDFThumbnail
17
29
  #
18
30
  # Arguments:
19
31
  # src_path: (String)
32
+ # resize: (String) as used in the -resize parameter of convert
33
+ # quality: (String) as used in the -quality parameter of convert
20
34
  hash = Digest::SHA256.file(src_path)
21
35
  short_hash = hash.hexdigest()[0, HASH_LENGTH]
22
- "#{short_hash}.png"
36
+ basename = short_hash
37
+ if resize
38
+ cleaned = resize.gsub(/[^\da-z]+/i, "")
39
+ basename << "_r#{cleaned}"
40
+ end
41
+ if quality
42
+ cleaned = quality.gsub(/[^\da-z]+/i, "")
43
+ basename << "_q#{cleaned}"
44
+ end
45
+ basename << ".png"
23
46
  end
24
47
 
25
48
  def _must_create?(src_path, dest_path)
@@ -27,35 +50,16 @@ module PDFThumbnail
27
50
  !File.exist?(dest_path) || File.mtime(dest_path) <= File.mtime(src_path)
28
51
  end
29
52
 
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
+ def generate_thumbnail(pdf, thumbnail, resize='50%', quality="80%")
54
+ first = PDFToImage.open(pdf).first
55
+ first.args << "-thumbnail #{resize}"
56
+ first.save(thumbnail)
53
57
  end
54
58
 
55
59
  module Filters
56
60
  include PDFThumbnail
57
61
 
58
- def pdf_thumbnail(pdf)
62
+ def pdf_thumbnail(pdf, args=nil)
59
63
  # Returns the thumbnail path for a given pdf file.
60
64
  # Example:
61
65
  # >> pdf_thumbnail "/path/to/sample_1.pdf"
@@ -67,9 +71,28 @@ module PDFThumbnail
67
71
  #
68
72
  # Arguments:
69
73
  # pdf: (String)
74
+ # args: a dict that can contain "resize" and "quality"
75
+ if args == nil
76
+ args = {"resize"=> "50%", "quality": '80%'}
77
+ end
78
+ resize = args.fetch("resize", "50%")
79
+ quality = args.fetch("quality", "80%")
70
80
  site = @context.registers[:site]
81
+ full_cache_dir = _get_cache_dir(site)
71
82
  full_pdf_path = File.join(site.source, pdf)
72
- File.join(CACHE_DIR, _dest_filename(full_pdf_path))
83
+ thumbnail = _dest_filename(full_pdf_path, resize, quality)
84
+ rel_thumb_path = File.join(CACHE_DIR, thumbnail)
85
+ full_thumb_path = File.join(_get_cache_dir(site), thumbnail)
86
+ if _must_create?(full_pdf_path, full_thumb_path)
87
+ puts "Creating thumbnail of' #{pdf}' to '#{rel_thumb_path}'"
88
+ generate_thumbnail(full_pdf_path, full_thumb_path, resize, quality)
89
+ # site - The Site.
90
+ # base - The String path to the <source>.
91
+ # dir - The String path between <source> and the file.
92
+ # name - The String filename of the file.
93
+ site.static_files << Jekyll::StaticFile.new(site, site.source, CACHE_DIR, thumbnail)
94
+ end
95
+ rel_thumb_path
73
96
  end
74
97
  end
75
98
 
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.2.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-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pdftoimage