Skip to content

Instantly share code, notes, and snippets.

@stephenmckinney
Created January 10, 2013 21:28
Show Gist options
  • Save stephenmckinney/4505950 to your computer and use it in GitHub Desktop.
Save stephenmckinney/4505950 to your computer and use it in GitHub Desktop.
Jekyll draft -> publish workflow + SASS compilation + rsync deploy View draft posts locally but not in production.
destination: ./_site
future: false
environments:
production:
url: http://foobar.com
remote:
connection: [email protected]
path: /var/www
{% for post in site.posts %}
{% unless post.draft %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endunless %}
{% endfor %}
require 'bundler/setup'
require 'date'
require 'yaml'
Bundler.require(:default)
class Jekyll < Thor
include Thor::Actions
def self.source_root
File.dirname(__FILE__)
end
no_tasks do
def config
YAML.load_file('_config.yml')
end
def env
ENV['env'] || 'production'
end
end
desc "deploy", "compile assets, genenerate site, and rsync deploy"
def deploy
run "compass clean; compass compile --force"
run "jekyll && rsync -avz --delete #{config['destination']}/ #{config['environments'][env]['remote']['connection']}:#{config['environments'][env]['remote']['path']}"
run "compass clean"
end
desc "launch", "open the produciton site in the browser"
def launch
run "open #{config['environments'][env]['url']}"
end
# create a 'future' file with filename 3000-month-day-slug.md
desc "draft TITLE", "create a new draft post"
def draft(title)
future = Date.today.strftime("3000-%m-%d")
slug = title.downcase.gsub(/ +/,'-').gsub(/[^-\w]/,'').sub(/-+$/,'')
file = File.join("_posts", "#{future}-#{slug}.md")
if File.exists?(file)
say_status("error", "#{file} already exists", :red)
if file_collision(file)
remove_file(file)
else
return
end
end
create_file file do
<<-EOS.gsub(/^ {6}/, '')
---
layout: post
title: #{title}
draft: true
---
EOS
end
end
# publish - remove draft flag, change file to year-month-day-slug.md
desc "publish [FILE]", "turn a draft into a post"
method_option :latest, aliases: "-l", type: :boolean, default: false, desc: "post latest draft"
def publish(file = nil)
if options.latest?
file = Dir.glob("_posts/3000*").max_by { |f| File.mtime(f) }
elsif file
if !File.exists?(file)
say_status("error", "#{file} doesn't exist", :red)
end
else
files = Dir["_posts/3000*"]
files_array = Array[]
files.each_with_index { |f, i| files_array << [i, f] }
print_table(files_array)
choice = ask("Choose a draft to post:", :blue)
end
# remove draft flag
draft = file || files_array[choice.to_i][1]
run "sed -i '' -e '/^draft:[[:space:]]*true$/d' #{draft}"
# rename file with today's date
now = Date.today.strftime("%Y-%m-%d")
draft_title = /\d{4}-\d{2}-\d{2}-(.*)/.match(File.basename(draft))[1]
copy_file draft, "_posts/#{now}-#{draft_title}"
remove_file draft
end
desc "drafts", "list all drafts"
def drafts
headers = [["Date", "Name"], ["----", "----"]]
print_table(headers + Dir["_posts/3000*"].map { |d| [File.mtime(d).strftime("%m/%d/%Y"), d] })
end
desc "posts", "list all posts"
def posts
headers = [["Date", "Name"], ["----", "----"]]
print_table(headers + Dir["_posts/20*"].map { |p| [File.mtime(p).strftime("%m/%d/%Y"), p] })
end
end
web: jekyll --server --auto --future
comapss: compass watch --output-style expanded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment