Class: Portfolio
- Inherits:
-
Middleman::Extension
- Object
- Middleman::Extension
- Portfolio
- Defined in:
- lib/middleman-portfolio.rb
Constant Summary collapse
- TEMPLATES_DIR =
File.('../template/source/', __FILE__)
Instance Attribute Summary collapse
-
#sitemap ⇒ Object
Returns the value of attribute sitemap.
Class Method Summary collapse
- .cleanup ⇒ Object
-
.tmp_dir ⇒ Object
path to temp dir for storing intermediate files.
Instance Method Summary collapse
- #after_configuration ⇒ Object
- #debug(str) ⇒ Object
-
#generate_thumbnail(image) ⇒ Object
generate thumbnail OUTSIDE of build dir.
-
#initialize(app, options_hash = {}, &block) ⇒ Portfolio
constructor
A new instance of Portfolio.
- #manipulate_resource_list(resources) ⇒ Object
- #portfolio_index_path ⇒ Object
- #portfolio_index_resource(project_resources) ⇒ Object
-
#portfolio_path ⇒ Object
get abs path to portfolio dir.
-
#project_dir(project) ⇒ Object
get absolute path to project directory, eg: /path/to/site/portfolio/example-project/.
-
#project_dirs ⇒ Object
Get all projects located in options.portfolio_dir.
- #project_image_resource(project, image) ⇒ Object
-
#project_image_resource_path(project, image) ⇒ Object
project_image_resource_path(“/path/to/image.png”) => “portfolio/project/image.png”.
-
#project_images(project) ⇒ Object
array of images for a project.
- #project_resource(project, thumbnail_resources) ⇒ Object
-
#project_resource_path(project) ⇒ Object
portfolio/example-project.html.
-
#project_resources(thumbnail_resources) ⇒ Object
create a resource for each portfolio project.
-
#project_thumbnail_resource(project, image) ⇒ Object
generate thumbnail and resource for an image in a project.
-
#project_thumbnail_resource_path(project, thumbnail) ⇒ Object
Generate resource path to project thumbnail, eg: “portfolio/example-project/1-thumbnail.jpg”.
-
#project_thumbnail_resources(project) ⇒ Object
generate thumbnail resource for each image in project dir.
- #projects ⇒ Object
- #register_extension_templates ⇒ Object
-
#resize_to_fill(img, width, height, gravity = 'Center') ⇒ Object
Resize to fill target dims.
-
#source_file(page) ⇒ Object
get path to source file for page, use default if not set, freak out if missing.
- #template(path) ⇒ Object
-
#thumbnail_name(image) ⇒ Object
thumbnail_name(“1.jpg”) => “1-200x150.jpg”.
Constructor Details
#initialize(app, options_hash = {}, &block) ⇒ Portfolio
Returns a new instance of Portfolio.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/middleman-portfolio.rb', line 33 def initialize(app, ={}, &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 |
Instance Attribute Details
#sitemap ⇒ Object
Returns the value of attribute sitemap.
13 14 15 |
# File 'lib/middleman-portfolio.rb', line 13 def sitemap @sitemap end |
Class Method Details
.cleanup ⇒ Object
17 18 19 20 21 22 23 24 25 |
# 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 |
.tmp_dir ⇒ Object
path to temp dir for storing intermediate files
28 29 30 |
# File 'lib/middleman-portfolio.rb', line 28 def tmp_dir File.join(Dir.tmpdir, "middleman-portfolio") end |
Instance Method Details
#after_configuration ⇒ Object
48 49 50 |
# File 'lib/middleman-portfolio.rb', line 48 def after_configuration register_extension_templates end |
#debug(str) ⇒ Object
250 251 252 |
# File 'lib/middleman-portfolio.rb', line 250 def debug(str) #puts str unless app.build? end |
#generate_thumbnail(image) ⇒ Object
generate thumbnail OUTSIDE of build dir
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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, .thumbnail_width, .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) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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 ⇒ Object
126 127 128 |
# File 'lib/middleman-portfolio.rb', line 126 def portfolio_index_path "#{.portfolio_dir}.html" end |
#portfolio_index_resource(project_resources) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# 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.( # options: { layout: false }, locals: { projects: projects, options: , project_resources: project_resources } ) end end |
#portfolio_path ⇒ Object
get abs path to portfolio dir
122 123 124 |
# File 'lib/middleman-portfolio.rb', line 122 def portfolio_path File.join(app.source_dir, .portfolio_dir) end |
#project_dir(project) ⇒ Object
get absolute path to project directory, eg: /path/to/site/portfolio/example-project/
144 145 146 |
# File 'lib/middleman-portfolio.rb', line 144 def project_dir(project) File.join(portfolio_path, project) end |
#project_dirs ⇒ Object
Get all projects located in options.portfolio_dir
163 164 165 166 |
# 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) ⇒ Object
153 154 155 |
# 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) ⇒ Object
project_image_resource_path(“/path/to/image.png”) => “portfolio/project/image.png”
158 159 160 |
# File 'lib/middleman-portfolio.rb', line 158 def project_image_resource_path(project, image) File.join(.portfolio_dir, project, File.basename(image)) end |
#project_images(project) ⇒ Object
array of images for a project
149 150 151 |
# File 'lib/middleman-portfolio.rb', line 149 def project_images(project) Dir.glob(File.join(project_dir(project), '*')) end |
#project_resource(project, thumbnail_resources) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# 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.( locals: { name: project, options: , thumbnail_resources: thumbnail_resources, } ) end end |
#project_resource_path(project) ⇒ Object
portfolio/example-project.html
174 175 176 |
# File 'lib/middleman-portfolio.rb', line 174 def project_resource_path(project) File.join(.portfolio_dir, "#{project}.html") end |
#project_resources(thumbnail_resources) ⇒ Object
create a resource for each portfolio project
191 192 193 |
# 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) ⇒ Object
generate thumbnail and resource for an image in a project
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# 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.( 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) ⇒ Object
Generate resource path to project thumbnail, eg: “portfolio/example-project/1-thumbnail.jpg”
225 226 227 |
# File 'lib/middleman-portfolio.rb', line 225 def project_thumbnail_resource_path(project, thumbnail) File.join(.portfolio_dir, project, thumbnail) end |
#project_thumbnail_resources(project) ⇒ Object
generate thumbnail resource for each image in project dir
215 216 217 218 219 220 221 222 |
# 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 ⇒ Object
168 169 170 171 |
# 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 ⇒ Object
93 94 95 96 97 98 |
# 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') ⇒ Object
Resize to fill target dims. Crop any excess. Will upscale.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/middleman-portfolio.rb', line 69 def resize_to_fill(img, width, height, gravity = 'Center') cols, rows = img[:dimensions] img. 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) ⇒ Object
get path to source file for page, use default if not set, freak out if missing
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/middleman-portfolio.rb', line 230 def source_file(page) # Load custom template or default opt = .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) ⇒ Object
100 101 102 103 104 |
# 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) ⇒ Object
thumbnail_name(“1.jpg”) => “1-200x150.jpg”
244 245 246 247 248 |
# File 'lib/middleman-portfolio.rb', line 244 def thumbnail_name(image) name = "#{File.basename(image, '.*')}-#{.thumbnail_width}x#{.thumbnail_height}#{File.extname(image)}" name.gsub!(/ /, "-") return name end |