Created
March 14, 2012 23:55
-
-
Save agius/2040500 to your computer and use it in GitHub Desktop.
Script to create & edit new jekyll post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Script to quickly generate a new post for Jekyll | |
# 2012 Andrew Evans - http://atevans.com | |
require "net/http" | |
require "uri" | |
require 'RMagick' | |
def titleize(str) | |
str.strip.split(' ').collect(&:capitalize).join(' ') | |
end | |
def linkify(str) | |
str.strip.downcase.gsub(/\s/, '-').gsub(/[^\w-]/, '') | |
end | |
def smart_truncate(str, len = 100) | |
str =~ /^([\w\s]*)[\W]/ | |
mini = $1 | |
if mini.length > len | |
index = mini.rindex(' ', len) | |
index = len if index.nil? | |
mini = mini[0..index] | |
end | |
mini.strip | |
end | |
def generate_thumbnail(path, filename = nil) | |
filename ||= path.split('/').last | |
mimage = Magick::Image.read(path).first | |
if mimage | |
mimage.change_geometry("600x600>") {|cols, rows, img| img } | |
thumbnail_path = File.dirname(__FILE__) << "/images/thumbnails/" | |
`mkdir -p #{thumbnail_path}` | |
thumbnail_path << filename | |
mimage.write(thumbnail_path) | |
$stderr.puts "Created thumbnail at #{thumbnail_path}" | |
return thumbnail_path | |
end | |
nil | |
end | |
unless $stdin.tty? | |
content = $stdin.read | |
else | |
content = "" | |
end | |
if ARGV.count > 0 | |
content = ARGV.first.dup | |
end | |
unless content.empty? | |
page_link = nil | |
# if we passed in a url | |
if content =~ /(http:|https:)\/\// | |
# if it's an image url | |
if content =~ /^(http:|https:)\/\/.*(\.png|\.gif|\.jpg|\.jpeg)$/ | |
filename = content.split('/').last.strip | |
path = File.dirname(__FILE__) << "/images/" << filename | |
`wget -O #{path} #{content}` | |
thumbnail = generate_thumbnail(path) | |
thumbnail_path = "/images/thumbnails/" << filename | |
link = "/images/" << filename | |
page_link = content.strip | |
imgtitle = filename.split('.').first | |
page_link = content.strip | |
content = thumbnail.nil? ? "![#{imgtitle}](#{link})" : "![#{imgtitle}](#{thumbnail_path})" | |
title = "Image: #{imgtitle}" | |
permalink = "image-#{imgtitle}" | |
# if it's a regular url | |
else | |
uri = URI.parse(content) | |
title = "" | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true if uri.scheme == 'https' | |
http.start do |h| | |
res = h.request Net::HTTP::Get.new(uri.request_uri) | |
if Net::HTTPSuccess | |
res.body =~ /<title>([\w\W]*)<\/title>/ | |
title = $1 | |
title ||= content | |
end | |
end | |
title.strip! | |
content.strip! | |
page_link = content | |
permalink = linkify(title) | |
content = "[#{title}](#{content})" | |
end | |
# if it's an image on the filesystem | |
elsif content =~ /(\.png|\.gif|\.jpg|\.jpeg)$/ | |
filename = content.split('/').last.strip | |
path = File.dirname(__FILE__) << "/images/" << filename | |
puts "cp #{content.strip} #{path}" | |
`cp #{content.strip} #{path}` | |
thumbnail = generate_thumbnail(path) | |
thumbnail_path = "/images/thumbnails/" << filename | |
link = "/images/" << filename | |
page_link = link | |
imgtitle = filename.split('.').first | |
content = thumbnail.nil? ? "![#{imgtitle}](#{link})" : "![#{imgtitle}](#{thumbnail_path})" | |
title = "Image: #{imgtitle}" | |
permalink = "image-#{imgtitle}" | |
# if it's regular text - probably a quote or snippet | |
else | |
title = smart_truncate(content) | |
permalink = linkify(title) | |
quote = "" | |
content = content.each_line{|s| quote << "> #{s}\n"} | |
content = quote | |
end | |
else | |
puts "Usage: ./new title " | |
exit | |
end | |
d = Time.now.strftime("%Y-%m-%d") | |
postname = File.dirname(__FILE__) << "/_posts/" << "#{d}-#{permalink}.md" | |
File.open("#{postname}", "w") do |f| | |
f.puts "---", "layout: default", "title: \"#{title}\"", "permalink: \"/#{permalink}.html\"", "date: \"#{d}\"" | |
f.puts "link: \"#{page_link}\"" unless page_link.nil? | |
f.puts "---", content | |
end | |
`mate #{postname}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment