Module: PDFThumbnail::Filters

Includes:
PDFThumbnail
Defined in:
lib/jekyll-pdf-thumbnail.rb

Constant Summary

Constants included from PDFThumbnail

CACHE_DIR, HASH_LENGTH

Instance Method Summary collapse

Methods included from PDFThumbnail

#_dest_filename, #_get_cache_dir, #_must_create?, #generate_thumbnail

Instance Method Details

#pdf_thumbnail(pdf, args = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll-pdf-thumbnail.rb', line 61

def pdf_thumbnail(pdf, args=nil)
  # Returns the thumbnail path for a given pdf file.
  # Example:
  #   >> pdf_thumbnail "/path/to/sample_1.pdf"
  #   => /assets/pdf_thumbnails/a35383ccca791ba6aa67ab3acde65287.png
  #
  # Or as a liquid filter:
  #  {% assign my_pdf = 'sample_2.pdf' %}
  #  {{ my_pdf | pdf_preview }}
  #
  # Arguments:
  #   pdf: (String)
  #   args: a dict that can contain "resize" and "quality"
  if args == nil
    args = {"resize"=> "50%", "quality": '80%'}
  end
  resize = args.fetch("resize", "50%")
  quality = args.fetch("quality", "80%")
  site = @context.registers[:site]
  full_cache_dir = _get_cache_dir(site)
  full_pdf_path = File.join(site.source, pdf)
  thumbnail = _dest_filename(full_pdf_path, resize, quality)
  rel_thumb_path = File.join(CACHE_DIR, thumbnail)
  full_thumb_path = File.join(_get_cache_dir(site), thumbnail)
  if _must_create?(full_pdf_path, full_thumb_path)
    puts "Creating thumbnail of' #{pdf}' to '#{rel_thumb_path}'"
    generate_thumbnail(full_pdf_path, full_thumb_path, resize, quality)
    # site - The Site.
    # base - The String path to the <source>.
    # dir - The String path between <source> and the file.
    # name - The String filename of the file.
    site.static_files << Jekyll::StaticFile.new(site, site.source, CACHE_DIR, thumbnail)
  end
  rel_thumb_path
end