diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f30a35 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +pkg/* +*.gem +.bundle diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..8a7d6bc --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in jekyll-minimagick.gemspec +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..0b319f5 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,34 @@ +PATH + remote: . + specs: + jekyll-minimagick (0.0.1) + jekyll (>= 0.10.0) + mini_magick (>= 3.3) + +GEM + remote: http://rubygems.org/ + specs: + classifier (1.3.3) + fast-stemmer (>= 1.0.0) + directory_watcher (1.4.0) + fast-stemmer (1.0.0) + jekyll (0.10.0) + classifier (>= 1.3.1) + directory_watcher (>= 1.1.1) + liquid (>= 1.9.0) + maruku (>= 0.5.9) + liquid (2.2.2) + maruku (0.6.0) + syntax (>= 1.0.0) + mini_magick (3.3) + subexec (~> 0.1.0) + subexec (0.1.0) + syntax (1.0.0) + +PLATFORMS + ruby + +DEPENDENCIES + jekyll (>= 0.10.0) + jekyll-minimagick! + mini_magick (>= 3.3) diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..14cfe0b --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require 'bundler' +Bundler::GemHelper.install_tasks diff --git a/jekyll-minimagick.gemspec b/jekyll-minimagick.gemspec new file mode 100644 index 0000000..1cb13a3 --- /dev/null +++ b/jekyll-minimagick.gemspec @@ -0,0 +1,24 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "jekyll-minimagick/version" + +Gem::Specification.new do |s| + s.name = "jekyll-minimagick" + s.version = Jekyll::Minimagick::VERSION + s.platform = Gem::Platform::RUBY + s.authors = ["Roger López"] + s.email = ["roger@zroger.com"] + s.homepage = "http://github.com/zroger/jekyll-minimagick" + s.summary = %q{MiniMagick integration for Jekyll} + s.description = %q{Use MiniMagick to crop and resize images in your Jekyll project.} + + s.rubyforge_project = "jekyll-minimagick" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + s.add_runtime_dependency('jekyll', [">= 0.10.0"]) + s.add_runtime_dependency('mini_magick', [">= 3.3"]) +end diff --git a/lib/jekyll-minimagick.rb b/lib/jekyll-minimagick.rb new file mode 100644 index 0000000..0e1be1e --- /dev/null +++ b/lib/jekyll-minimagick.rb @@ -0,0 +1,70 @@ +require 'mini_magick' + +module Jekyll + module JekyllMinimagick + + class GeneratedImageFile < Jekyll::StaticFile + # Initialize a new GeneratedImage. + # +site+ is the Site + # +base+ is the String path to the + # +dir+ is the String path between and the file + # +name+ is the String filename of the file + # +preset+ is the Preset hash from the config. + # + # Returns + def initialize(site, base, dir, name, preset) + @site = site + @base = base + @dir = dir + @name = name + @preset = preset + end + + # Obtains source file path by substituting the preset's source directory + # for the destination directory. + # + # Returns source file path. + def path + File.join(@base, @dir.sub(@preset['destination'], @preset['source']), @name) + end + + # Use MiniMagick to create a derivative image at the destination + # specified (if the original is modified). + # +dest+ is the String path to the destination dir + # + # Returns false if the file was not modified since last time (no-op). + def write(dest) + dest_path = destination(dest) + + return false if File.exist? dest_path and !modified? + @@mtimes[path] = mtime + + FileUtils.mkdir_p(File.dirname(dest_path)) + image = ::MiniMagick::Image.open(path) + image.resize @preset['resize'] + image.write dest_path + + true + end + + end + + class MiniMagickGenerator < Generator + safe true + + # Find all image files in the source directories of the presets specified + # in the site config. Add a GeneratedImageFile to the static_files stack + # for later processing. + def generate(site) + return unless site.config['mini_magick'] + + site.config['mini_magick'].each_pair do |name, preset| + Dir.glob(File.join(preset['source'], "*.{png,jpg,jpeg,gif}")) do |source| + site.static_files << GeneratedImageFile.new(site, site.source, preset['destination'], File.basename(source), preset) + end + end + end + end + + end +end diff --git a/lib/jekyll-minimagick/version.rb b/lib/jekyll-minimagick/version.rb new file mode 100644 index 0000000..29fa4a8 --- /dev/null +++ b/lib/jekyll-minimagick/version.rb @@ -0,0 +1,5 @@ +module Jekyll + module Minimagick + VERSION = "0.0.1" + end +end