Class: Jekyll::GalleryPhoto
- Inherits:
-
Object
- Object
- Jekyll::GalleryPhoto
- Defined in:
- lib/jekyll-photo-gallery.rb
Instance Method Summary collapse
- #create_symlink ⇒ Object
- #create_thumbnail ⇒ Object
-
#initialize(site, base, basepath, name) ⇒ GalleryPhoto
constructor
A new instance of GalleryPhoto.
- #read_exif ⇒ Object
- #to_liquid ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(site, base, basepath, name) ⇒ GalleryPhoto
Returns a new instance of GalleryPhoto.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jekyll-photo-gallery.rb', line 13 def initialize(site, base, basepath, name) @site = site @base = base @name = name @basepath = basepath @path = File.join(basepath, name) # Create symlink in site destination. create_symlink # Read exif data. @exif = read_exif # Fetch date time field from exif data. @date_time = nil if !@exif.nil? @date_time = @exif[:date_time] end # Create thumbnail. @thumbnail = create_thumbnail end |
Instance Method Details
#create_symlink ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/jekyll-photo-gallery.rb', line 32 def create_symlink link_src = @site.in_source_dir(@path) link_dest = @site.in_dest_dir(@path) link_dest_basepath = @site.in_dest_dir(@basepath) # Delete existing static asset. @site.static_files.delete_if { |sf| sf.relative_path == "/#{@path}" } # Add new entry which prevents writing. @site.static_files << GalleryFile.new(@site, @base, @basepath, @name) # Create symlink in file system. if File.exists?(link_dest) # Correct link already exists. if File.readlink(link_dest) == link_src return end File.delete(link_dest) end puts "Creating symlink for #{@path}" # Create symlink FileUtils.mkdir_p(link_dest_basepath, :mode => 0755) File.symlink(link_src, link_dest) end |
#create_thumbnail ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jekyll-photo-gallery.rb', line 56 def create_thumbnail config = @site.config["photo_gallery"] || {} thumbnail_size = config["thumbnail_size"] || 256 # Compile thumbnail path thumb_dir = File.join(@site.dest, @basepath, "thumbs") thumb = File.join(thumb_dir, @name) thumb_name = File.join("thumbs", @name) # Add thumbnail to static assets @site.static_files << GalleryFile.new(@site, @base, thumb_dir, @name) # Create if it does not exist. FileUtils.mkdir_p(thumb_dir, :mode => 0755) if File.exists?(thumb) return thumb_name end begin puts "Creating thumbnail for #{@path}" # Read image. img = Magick::Image.read(@path).first # Resize Flickr style img = img.resize_to_fill(thumbnail_size, thumbnail_size) # Write thumbnail img.write(thumb) rescue Exception => e puts "Error generating thumbnail for #{@path}: #{e}" puts e.backtrace thumb_name = nil end thumb_name end |
#read_exif ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jekyll-photo-gallery.rb', line 86 def read_exif exif = nil begin exif = EXIFR::JPEG.new(@path).to_hash rescue EXIFR::MalformedJPEG puts "No EXIF data in #{@path}" rescue Exception => e puts "Error reading EXIF data for #{@path}: #{e}" end exif end |
#to_liquid ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/jekyll-photo-gallery.rb', line 98 def to_liquid { 'name' => @name, 'date_time' => @date_time, 'thumbnail' => @thumbnail, 'exif' => @exif.nil? ? nil : @exif.collect{|k,v| [k.to_s, v]}.to_h } end |
#to_s ⇒ Object
107 108 109 |
# File 'lib/jekyll-photo-gallery.rb', line 107 def to_s @path end |