Skip to content

Instantly share code, notes, and snippets.

@fmartingr
Forked from jarrettmeyer/Rakefile
Created December 5, 2012 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmartingr/4216825 to your computer and use it in GitHub Desktop.
Save fmartingr/4216825 to your computer and use it in GitHub Desktop.
Octopress publish drafts
require "rubygems"
require "date"
# Configuration
base_dir = Dir.pwd
blog_dir = "#{base_dir}/blog"
source_dir = "#{blog_dir}/source"
posts_dir = "#{source_dir}/_posts"
git_remote = "origin"
git_branch = "master"
pull_command = "git pull #{git_remote} #{git_branch}"
push_command = "git push #{git_remote} #{git_branch}"
log_path = "#{base_dir}/log-#{Date.today}.txt"
log = File.open log_path, "a"
drafts = []
updated = false
task :test_update_blog => [:pull, :find_drafts, :update_drafts]
task :test_update_drafts => [:find_drafts, :update_drafts]
task :update_blog => [:pull, :find_drafts, :update_drafts, :regenerate_blog, :push]
desc "Check out latest version from source control."
task :pull do
abort("No blog found!") unless File.directory? blog_dir
log << "#{Time.now} Pull latest from #{git_remote}/#{git_branch}.\n"
Dir.chdir blog_dir
system pull_command
end
desc "Push all changes back to source control."
task :push do
abort("No changes to push") unless updated
log << "#{Time.now} Pushing updates to #{git_remote}/#{git_branch}."
Dir.chdir blog_dir
system "git add --all"
system "git commit -m \"publishing drafts with Rakefile\""
system push_command
log << "#{Time.now} Done.\n\n"
end
desc "Find all files marked as 'published: false'. Save them in an array."
task :find_drafts do
Dir.chdir posts_dir
files = Dir.glob "*.*"
log << "#{Time.now} Found #{files.length} files in #{posts_dir}.\n"
files.each do |file|
yaml_block = 0
File.readlines(file).each do |line|
next if yaml_block >= 2
line.chomp!
yaml_block += 1 if line =~ /^---/
if line =~ /^published:\s?false/
drafts.push file
next
end
end
end
log << "#{Time.now} Found #{drafts.length} drafts in #{posts_dir}.\n"
end
desc "If the draft is scheduled to publish, then change the appropriate yaml line to 'published: true'."
task :update_drafts do
if drafts.empty?
log << "#{Time.now} No drafts found. Quitting.\n\n"
abort
end
drafts.each do |draft|
yaml_block = 0
File.readlines(draft).each do |line|
next if yaml_block >= 2
line.chomp!
yaml_block += 1 if line =~ /^---/
if line =~ /^date:(.*)/
publish_at = DateTime.parse $1
log << "#{Time.now} Draft #{draft} is scheduled to be published #{publish_at}.\n"
should_publish = DateTime.now >= publish_at
if should_publish
publish_draft draft if should_publish
updated = true
end
end
end
end
end
desc "Generate blog by running the rake task 'generate' built into Octopress."
task :regenerate_blog do
abort("No changes to regenerate") unless updated
Dir.chdir blog_dir
log << "#{Time.now} Regenerating blog.\n"
system "rake generate"
end
def publish_draft(file)
text = ""
yaml_block = 0
File.readlines(file).each do |line|
yaml_block += 1 if line =~ /^---/
if yaml_block < 2 and line =~ /^published: false/
text << "published: true \n"
next
end
text << line
end
#puts text
File.open(file, "w") do |file|
file.write(text)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment