class JekyllPig::JekyllPig

Public Instance Methods

augment_image_data(gallery, image_data, images) click to toggle source
# File lib/jekyll-pig.rb, line 225
def augment_image_data(gallery, image_data, images) 
    images.each do |image_name|
        #append data to image_data array if it's not already there
        if not image_data.any? { |data| data['filename'] == image_name }
            #get image date
            image_date = get_image_date(gallery.path, image_name)
            image = get_image(gallery.path, image_name)
            image_data << 
                {
                'datetime' => image_date.to_s,
                'filename' => image_name,
                'aspectRatio' => image.width.to_f / image.height
                }
        end
    end
end
full_size_html(gallery_name, name, date, prev_url, next_url) click to toggle source
# File lib/jekyll-pig.rb, line 30
def full_size_html(gallery_name, name, date, prev_url, next_url)
    "---\n"                                                                                                                                         \
    "layout: post\n"                                                                                                                                \
    "title: #{name}\n"                                                                                                                              \
    "date: #{date.strftime("%Y-%m-%d %H:%M:%S")}\n"                                                                                                 \
    "permalink: /assets/html/#{gallery_name}/#{name}.html\n"                                                                                        \
    "exclude: true\n"                                                                                                                               \
    "---\n"                                                                                                                                         \
    "<div><a href=\"#{prev_url}\" style=\"display:inline;\">prev</a><a href=\"#{next_url}\" style=\"display:inline; float:right\">next</a></div>\n" \
    "<img src=\"{{site.baseurl}}/assets/img/#{gallery_name}/1024/#{name}\"/>\n"                                                                                     
end
generate(site) click to toggle source
# File lib/jekyll-pig.rb, line 242
def generate(site)
    @site = site
    get_paths()
    make_output_paths()
    galleries = get_galleries()
    galleries.each do |gallery|
        
        #make gallery specific html and image output paths
        html_output_path = File.join(@html_path, gallery.name)
        FileUtils.mkdir_p html_output_path unless File.exists? html_output_path
        img_output_path = File.join(@img_path, gallery.name)
        FileUtils.mkdir_p img_output_path unless File.exists? img_output_path
        
        #write pig.min.js to js path
        if not File.exists? File.join(@js_path, 'pig.min.js')
            File.open(File.join(@js_path, 'pig.min.js'), 'w') { |file| file.write(@@pig_min_js) }
        end
        
        #get image data from _data
        image_data = get_image_data(gallery.name)
        old_image_data = image_data.clone
        
        #get images from gallery
        images = get_images(gallery.path)
        
        #add any additional images to image_data
        augment_image_data(gallery, image_data, images)
        
        #sort image data
        image_data = image_data.sort_by { |data| data['datetime'] }
        
        #create thumbs
        process_images(image_data, gallery.name, gallery.path, images)
        
        images.each do |image_name|
            #create html assets for each image
            process_image(image_data, gallery.name, gallery.path, image_name)
        end
        
        if image_data != old_image_data
            #write image_data
            File.open(File.join(@data_path, "#{gallery.name}.json"), 'w') { |file|
                file.write(image_data.to_json)
            }
            
            #save this gallery's includable content
            File.open(File.join(@includes_path, "#{gallery.name}.html"), 'w') { |file|
                file.write(gallery_html(gallery.name, image_data))
            }
        end
    end
end
get_galleries() click to toggle source
# File lib/jekyll-pig.rb, line 197
def get_galleries
    galleries = []
    config_galleries = Jekyll.configuration({})['galleries']
    if config_galleries != nil
        config_galleries.each do |gallery|
            full_path = File.join(@site.source, gallery['path'])
            if File.directory?(full_path)
                galleries << SourceGallery.new(full_path, gallery['name'])
            end
        end
    else
        default_gallery_path = File.join(@site.source, 'gallery')
        if File.directory?(default_gallery_path)
            galleries << SourceGallery.new(default_gallery_path, 'gallery')
        end
    end
    galleries
end
get_image(gallery_path, image_name) click to toggle source
# File lib/jekyll-pig.rb, line 85
def get_image(gallery_path, image_name)
    image = @@image_cache[File.join(gallery_path, image_name)]
    if image == nil
        image = MiniMagick::Image.open(File.join(gallery_path, image_name))
        @@image_cache[File.join(gallery_path, image_name)] = image
    end
    image
end
get_image_data(gallery_name) click to toggle source

read the image data from the _includes folder

# File lib/jekyll-pig.rb, line 67
def get_image_data(gallery_name)
    image_data = []
    #read image_data if existing
    if File.exists?(File.join(@data_path, "#{gallery_name}.json"))
        File.open(File.join(@data_path, "#{gallery_name}.json"), 'r') { |file|
            #get array of image data (drop 'var imageData = ' and ';')
            image_data = JSON.parse(file.read)
        }
    end
    image_data
end
get_image_date(gallery_path, image_name) click to toggle source
# File lib/jekyll-pig.rb, line 94
def get_image_date(gallery_path, image_name)
    image_date = nil
    begin
        image = get_image(gallery_path, image_name)
        exif_date = image.exif['DateTimeOriginal']
        if exif_date == nil
            #no exif date, try to get from file name
            image_date = Time.strptime(image_name, "%Y-%m-%d")
        else
            #try to get the image date from exif
            image_date = Time.strptime(exif_date, "%Y:%m:%d %H:%M:%S")
        end
    rescue
        #get the date from file if possible
        image_date = File.mtime(File.join(gallery_path, image_name))
    end
    image_date
end
get_images(path) click to toggle source

get a list of image file names from a given path

# File lib/jekyll-pig.rb, line 80
def get_images(path)
    patterns = ['*.jpg', '*.jpeg', '*.png'].map { |ext| File.join(path, ext) }
    Dir.glob(patterns).map { |filepath| File.basename(filepath) }
end
get_next_url(image_data, gallery_name, image_name) click to toggle source
# File lib/jekyll-pig.rb, line 122
def get_next_url(image_data, gallery_name, image_name)
    index = image_data.index { |data| data['filename'] == image_name }
    index = index + 1
    if index >= image_data.length
        index = 0
    end
    image_html_url(gallery_name, image_data[index]['filename'])
end
get_paths() click to toggle source
# File lib/jekyll-pig.rb, line 188
def get_paths
    @assets_path = File.join(@site.source, "assets")
    @js_path = File.join(@assets_path, "js")
    @data_path = File.join(@site.source, "_data")
    @img_path = File.join(@assets_path, "img")
    @html_path = File.join(@assets_path, "html")
    @includes_path = File.join(@site.source, "_includes")
end
get_previous_url(image_data, gallery_name, image_name) click to toggle source
# File lib/jekyll-pig.rb, line 113
def get_previous_url(image_data, gallery_name, image_name)
    index = image_data.index { |data| data['filename'] == image_name }
    index = index - 1
    if index < 0
        index = image_data.length - 1
    end
    image_html_url(gallery_name, image_data[index]['filename'])
end
image_html_url(gallery_name, image_name) click to toggle source
# File lib/jekyll-pig.rb, line 62
def image_html_url(gallery_name, image_name)
    "/assets/html/#{gallery_name}/#{image_name}.html"
end
make_output_paths() click to toggle source
# File lib/jekyll-pig.rb, line 216
def make_output_paths
    FileUtils.mkdir_p @assets_path unless File.exists? @assets_path
    FileUtils.mkdir_p @js_path unless File.exists? @js_path
    FileUtils.mkdir_p @img_path unless File.exists? @img_path
    FileUtils.mkdir_p @html_path unless File.exists? @html_path
    FileUtils.mkdir_p @includes_path unless File.exists? @includes_path
    FileUtils.mkdir_p @data_path unless File.exists? @data_path
end
process_image(image_data, gallery_id, gallery_path, image_name) click to toggle source

create full size html page for a given image

# File lib/jekyll-pig.rb, line 172
def process_image(image_data, gallery_id, gallery_path, image_name)
    full_size_html_path = File.join(@html_path, gallery_id, image_name + ".html")
    #create full size html if it doesn't exist
    if not File.exists? full_size_html_path
        #get image date
        image_date = get_image_date(gallery_path, image_name)
        #create full size html text
        full_size_html = full_size_html(gallery_id, image_name, image_date, 
                                        get_previous_url(image_data, gallery_id, image_name), 
                                        get_next_url(image_data, gallery_id, image_name))
        File.open(full_size_html_path, 'w') { |file| 
            file.write(full_size_html) 
        }
    end
end
process_images(image_data, gallery_id, gallery_path, images) click to toggle source

create thumbnails and fullsize image assets

# File lib/jekyll-pig.rb, line 132
def process_images(image_data, gallery_id, gallery_path, images)
    #create thumbs
    sizes = [1024, 500, 250, 100, 20]
    sizes.each { |size|
        #output path for current size
        size_out_path = File.join(@img_path, gallery_id, size.to_s)
        FileUtils.mkdir_p size_out_path unless File.exists? size_out_path
        
        #images that have already been processed for the current size
        done_images = get_images(size_out_path)
        #all images in the gallery with the ones already done taken away
        todo_images = images - done_images
        
        #function to get the source path to use for creating the given size thumbnail
        #i.e. use the 500px sized images to make the 250px versions
        source_for_size = -> (size) {
            index = sizes.index(size)
            source = gallery_path
            if index != nil && index != 0
                source = File.join(@img_path, gallery_id, sizes[index - 1].to_s)
            end
            source
        }
        
        #do the processing in a batch
        mog = MiniMagick::Tool::Mogrify.new
        mog.resize("x#{size}")
        mog.sampling_factor('4:2:0')
        mog.colorspace('RGB')
        mog.interlace('Plane')
        mog.strip()
        mog.quality('75')
        mog.path(size_out_path)
        source_path = source_for_size.call(size)
        todo_images.each { |todo| mog << File.join(source_path, todo) }
        mog.call
    }
end