Skip to content

Instantly share code, notes, and snippets.

@thexavismith
Forked from ttscoff/Jekyll Filer.rb
Last active December 12, 2016 20:03
Show Gist options
  • Save thexavismith/41c572503c07f38681c7 to your computer and use it in GitHub Desktop.
Save thexavismith/41c572503c07f38681c7 to your computer and use it in GitHub Desktop.
OS X System Service script for filing Jekyll images and putting Markdown links on the clipboard
require 'fileutils'
# An OS X System Service for quickly filing image files to a Jekyll blog folder,
# putting Markdown links to the files on the clipboard.
# Copyright Brett Terpstra 2013
# Config
# ======
# Where to store the images
base_path = '~/Projects/theboldreport.net/uploads/'
# What to output as the base image path in the clipboard links
base_url = '/uploads' # or set "http://yoursite.com/uploads"
def e_sh(str)
str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''")
end
input = ARGF
urls = []
input.each {|file_path|
if File.basename(file_path) =~ /\.(jpeg|jpg|png|gif)$/
extension = File.basename(file_path).match(/\.(jpeg|jpg|png|gif)$/)[0]
target_file = File.basename(file_path).gsub(/#{extension}$/,'').strip
target_dir = File.expand_path(base_path).gsub(/\/$/,'') + "/" + Time.now.strftime('%Y/%m/').strip
FileUtils.mkdir_p target_dir unless (File.directory? target_dir)
while File.exists?(File.join(target_dir, target_file + extension)) do
target_file = target_file + "_00" unless target_file =~ /_\d+/
target_file.next!
end
target = File.join(target_dir, target_file + extension)
begin
FileUtils.cp(file_path.strip,target)
case File.extname(target)
when /jpe?g/
# log.info("Compressing JPEG")
if File.exists?('/usr/local/bin/jpegoptim')
res = %x{/usr/local/bin/jpegoptim -fopt --strip-all -m60 "#{target}"}
end
when /png/
# log.info("Compressing PNG")
if File.exists?('/usr/local/bin/pngcrush')
res = %x{/usr/local/bin/pngcrush -q -ow "#{target}"}
end
end
urls.push("{% figure classNameHere #{target.gsub(/^#{File.expand_path(base_path).gsub(/\/$/,'')}/,"#{base_url.gsub(/\/$/,'')}")} CaptionGoesHereInQuotes %}")
rescue => e
puts e
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -s -m "Couldn't file #{File.basename(file_path)}" -t "Jekyll Image Filer"} if File.exists?("/usr/local/bin/growlnotify")
end
end
}
%x{echo #{e_sh urls.join("\n")}|pbcopy}
%x{/usr/local/bin/growlnotify -a "Automator.app" -s -m "Filed #{urls.length.to_s} images, urls in clipboard"} if File.exists?("/usr/local/bin/growlnotify")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment