class Portfolio

Constants

TEMPLATES_DIR

Attributes

sitemap[RW]

Public Class Methods

cleanup() click to toggle source
# File lib/middleman-portfolio.rb, line 17
def cleanup
  # Delete tmp files
  tmp_files = Dir.glob(File.join(tmp_dir, "*")).select {|f| File.file?(f)}
  tmp_files.each {|f| 
    File.delete(f)
    debug "#{f} not deleted" if File.exist?(f)
  }
  Dir.rmdir(tmp_dir)
end
new(app, options_hash={}, &block) click to toggle source
Calls superclass method
# File lib/middleman-portfolio.rb, line 33
def initialize(app, options_hash={}, &block)
  # Call super to build options from the options_hash
  super

  # Create tmp dir
  Dir.mkdir(Portfolio.tmp_dir) unless Dir.exist?(Portfolio.tmp_dir)

  require "mini_magick"

  # set up your extension
  app.after_build do
    Portfolio.cleanup
  end 
end
tmp_dir() click to toggle source

path to temp dir for storing intermediate files

# File lib/middleman-portfolio.rb, line 28
def tmp_dir
  File.join(Dir.tmpdir, "middleman-portfolio")
end

Public Instance Methods

after_configuration() click to toggle source
# File lib/middleman-portfolio.rb, line 48
def after_configuration
  register_extension_templates
end
debug(str) click to toggle source
# File lib/middleman-portfolio.rb, line 250
def debug(str)
  #puts str unless app.build?
end
generate_thumbnail(image) click to toggle source

generate thumbnail OUTSIDE of build dir

# File lib/middleman-portfolio.rb, line 53
def generate_thumbnail(image)
  debug "Generating thumbnail of #{image}"
  dst = File.join(Portfolio.tmp_dir, thumbnail_name(image))
  if !File.exist?(dst)
    img = ::MiniMagick::Image.open(image)
    #img.resize "#{options.thumbnail_width}x#{options.thumbnail_height}"
    img = resize_to_fill(img, options.thumbnail_width, options.thumbnail_height)
    img.write(dst)
    raise "Thumbnail not generated at #{dst}" unless File.exist?(dst)
  else
    debug "#{dst} already exists"
  end 
  return dst
end
manipulate_resource_list(resources) click to toggle source
# File lib/middleman-portfolio.rb, line 106
def manipulate_resource_list(resources)
  # Load in reverse order for easier building
  proj_resources = projects.collect {|project|
    thumbs = project_thumbnail_resources(project)
    resources += thumbs
    project_resource(project, thumbs)
  }

  # Add project resources to main array
  resources += proj_resources
  # resources += project_thumbnail_resources(project)
  resources << portfolio_index_resource(proj_resources)
  return resources
end
portfolio_index_path() click to toggle source
# File lib/middleman-portfolio.rb, line 126
def portfolio_index_path
  "#{options.portfolio_dir}.html"
end
portfolio_index_resource(project_resources) click to toggle source
# File lib/middleman-portfolio.rb, line 130
def portfolio_index_resource(project_resources) 
  Middleman::Sitemap::Resource.new(app.sitemap, portfolio_index_path, source_file(:portfolio)).tap do |resource|
    resource.add_metadata(
      # options: { layout: false },
      locals: {
        projects: projects,
        options: options,
        project_resources: project_resources 
      }
    )
  end
end
portfolio_path() click to toggle source

get abs path to portfolio dir

# File lib/middleman-portfolio.rb, line 122
def portfolio_path
  File.join(app.source_dir, options.portfolio_dir) 
end
project_dir(project) click to toggle source

get absolute path to project directory, eg: /path/to/site/portfolio/example-project/

# File lib/middleman-portfolio.rb, line 144
def project_dir(project)
  File.join(portfolio_path, project)
end
project_dirs() click to toggle source

Get all projects located in options.portfolio_dir

# File lib/middleman-portfolio.rb, line 163
def project_dirs
  #debug "Looking in #{options.portfolio_dir} for project subdirectories"
  Dir.glob(File.join(portfolio_path, '*')).select {|f| File.directory? f}
end
project_image_resource(project, image) click to toggle source
# File lib/middleman-portfolio.rb, line 153
def project_image_resource(project, image)
  Middleman::Sitemap::Resource.new(app.sitemap, project_image_resource_path(project, image), image)
end
project_image_resource_path(project, image) click to toggle source

project_image_resource_path(“/path/to/image.png”) => “portfolio/project/image.png”

# File lib/middleman-portfolio.rb, line 158
def project_image_resource_path(project, image)
  File.join(options.portfolio_dir, project, File.basename(image))
end
project_images(project) click to toggle source

array of images for a project

# File lib/middleman-portfolio.rb, line 149
def project_images(project)
  Dir.glob(File.join(project_dir(project), '*'))
end
project_resource(project, thumbnail_resources) click to toggle source
# File lib/middleman-portfolio.rb, line 178
def project_resource(project, thumbnail_resources)
  Middleman::Sitemap::Resource.new(app.sitemap, project_resource_path(project), source_file(:project)).tap do |resource|
    resource.add_metadata(
      locals: {
        name: project,
        options: options,
        thumbnail_resources: thumbnail_resources,
      }
    )
  end
end
project_resource_path(project) click to toggle source

portfolio/example-project.html

# File lib/middleman-portfolio.rb, line 174
def project_resource_path(project)
  File.join(options.portfolio_dir, "#{project}.html")
end
project_resources(thumbnail_resources) click to toggle source

create a resource for each portfolio project

# File lib/middleman-portfolio.rb, line 191
def project_resources(thumbnail_resources)
  projects.collect {|project| project_resource(project, thumbnail_resources)}
end
project_thumbnail_resource(project, image) click to toggle source

generate thumbnail and resource for an image in a project

# File lib/middleman-portfolio.rb, line 196
def project_thumbnail_resource(project, image)
  debug "Generating thumbnail of #{project}/#{image}"
  tmp_image = generate_thumbnail(image)

  # Add image to sitemap
  path = project_thumbnail_resource_path(project, File.basename(tmp_image))
  debug "Adding #{path} to #{project}"
  Middleman::Sitemap::Resource.new(app.sitemap, path, tmp_image).tap do |resource|
    resource.add_metadata(
      locals: {
        project: project,
        project_image_resource: project_image_resource(project, image),
        image: project_image_resource_path(project, image)
      }
    )
  end    
end
project_thumbnail_resource_path(project, thumbnail) click to toggle source

Generate resource path to project thumbnail, eg: “portfolio/example-project/1-thumbnail.jpg”

# File lib/middleman-portfolio.rb, line 225
def project_thumbnail_resource_path(project, thumbnail)
  File.join(options.portfolio_dir, project, thumbnail)
end
project_thumbnail_resources(project) click to toggle source

generate thumbnail resource for each image in project dir

# File lib/middleman-portfolio.rb, line 215
def project_thumbnail_resources(project)
  resources = Array.new
  for image in project_images(project)
    resources << project_thumbnail_resource(project, image)
  end 

  return resources
end
projects() click to toggle source
# File lib/middleman-portfolio.rb, line 168
def projects
  # Look for project directories
  projects = project_dirs.collect {|d| File.basename(d) }    
end
register_extension_templates() click to toggle source
# File lib/middleman-portfolio.rb, line 93
def register_extension_templates
  # We call reload_path to register the templates directory with Middleman.
  # The path given to app.files must be relative to the Middleman site's root.
  templates_dir_relative_from_root = Pathname(TEMPLATES_DIR).relative_path_from(Pathname(app.root))
  app.files.reload_path(templates_dir_relative_from_root)
end
resize_to_fill(img, width, height, gravity = 'Center') { |img| ... } click to toggle source

Resize to fill target dims. Crop any excess. Will upscale.

# File lib/middleman-portfolio.rb, line 69
def resize_to_fill(img, width, height, gravity = 'Center')
  cols, rows = img[:dimensions]
  img.combine_options do |cmd|
    if width != cols || height != rows
      scale_x = width/cols.to_f
      scale_y = height/rows.to_f
      if scale_x >= scale_y
        cols = (scale_x * (cols + 0.5)).round
        rows = (scale_x * (rows + 0.5)).round
        cmd.resize "#{cols}"
      else
        cols = (scale_y * (cols + 0.5)).round
        rows = (scale_y * (rows + 0.5)).round
        cmd.resize "x#{rows}"
      end
    end
    cmd.gravity gravity
    cmd.background "rgba(255,255,255,0.0)"
    cmd.extent "#{width}x#{height}" if cols != width || rows != height
  end
  img = yield(img) if block_given?
  img
end
source_file(page) click to toggle source

get path to source file for page, use default if not set, freak out if missing

# File lib/middleman-portfolio.rb, line 230
def source_file(page)
  # Load custom template or default
  opt = options.send("#{page}_template")

  if opt
    path = File.join(app.source_dir, opt)  
    raise "#{path} doesn't exist" unless File.exist?(path)
    return path 
  else
    return template("#{page}.html.erb") 
  end
end
template(path) click to toggle source
# File lib/middleman-portfolio.rb, line 100
def template(path)
  full_path = File.join(TEMPLATES_DIR, path)
  raise "Template #{full_path} not found" if !File.exist?(full_path)
  full_path
end
thumbnail_name(image) click to toggle source

thumbnail_name(“1.jpg”) => “1-200x150.jpg”

# File lib/middleman-portfolio.rb, line 244
def thumbnail_name(image)
  name = "#{File.basename(image, '.*')}-#{options.thumbnail_width}x#{options.thumbnail_height}#{File.extname(image)}"
  name.gsub!(/ /, "-")
  return name
end