class JekyllPig::JekyllPig

Public Instance Methods

augment_image_data(gallery, image_data, images) click to toggle source
# File lib/jekyll_pig.rb, line 205
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 38
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 222
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)
        
        #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'] }
        
        #process images
        images.each do |image_name|
            #create thumbs, full size, and html assets for each image
            process_image(image_data, gallery.name, gallery.path, image_name)
        end
        
        #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
get_galleries() click to toggle source
# File lib/jekyll_pig.rb, line 177
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 93
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 75
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 102
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(gallery_path) click to toggle source

read images that require processing from gallery

# File lib/jekyll_pig.rb, line 88
def get_images(gallery_path)
    patterns = ['*.jpg', '*.jpeg', '*.png'].map { |ext| File.join(gallery_path, ext) }
    Dir.glob(patterns).map { |path| File.basename(path) }
end
get_next_url(image_data, gallery_name, image_name) click to toggle source
# File lib/jekyll_pig.rb, line 130
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 168
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 121
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 70
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 196
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 thumbnails and fullsize image assets, and create full size html page for a given image

# File lib/jekyll_pig.rb, line 140
def process_image(image_data, gallery_id, gallery_path, image_name)
    #puts "jekyll-pig: processing " << image_name
    #create thumbs
    [1024, 500, 250, 100, 20].each { |size|
        size_out_path = File.join(@img_path, gallery_id, size.to_s)
        resized_img_path = File.join(size_out_path, image_name)
        if not File.exists? resized_img_path
            image = get_image(gallery_path, image_name)
            image.resize("x" + size.to_s)
            FileUtils.mkdir_p size_out_path unless File.exists? size_out_path
            image.write(resized_img_path)
        end
    }
    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