Octopress Tips and Tricks

I have been using Octopress for a couple of years now. I don’t blog that frequently and Octopress somehow just feels right for my work style. I run 4 websites using it and have learned a few tricks over the last few years. Fyi, I used to use an Ubuntu VM locally for my Octopress stuff but have shifted to Windows for everything over the last year. For hosting purposes, I use Heroku.

  1. Use ConEmu on Windows
    I am a big fan of ConEmu and have created a task inside it to setup my local blog environment with a single click. It launches 2 split console windows in a tab with rake preview running in one, and my git status in the other one. I compose the blog posts in a standalone gvim intance.



2. Use LiveReload
I launch LiveReload and have it watch the public folder for my blog. This automatically refreshes my browser as I am editing and working on a blog post. In my experience, LiveReload seems to play nicer with Firefox (than Chrome).

3. Speed up site generation using Isolate task If you have a lot of posts in your blog, the HTML output generation can take a few seconds. To speed it up when you are focusing on one or more blog posts, use the rake isolate task. Doing this puts all the posts except the ones you are working on in the source/_stash folder. For example, while working on this post, I ran rake isolate[octopress]. When you are done, just run rake integrate and you’ll be back to normal.

4. Use 3rd Party Octopress themes If you want to change the look and feel of your octopress site, there are several other options.

5. Schedule posts with Windows Tasks I don’t remember the original source for this ruby code, but I found a bunch of rake tasks on the interwebs that check for the published flag on all posts. If it is set to false and the current date is greater than the date on the blog post, it sets the published flag to true and regenerates and pushes the blog updates. I have a Windows task that runs a batch file with this rake task once a day. This lets me write blog posts in advance and schedule them for specific days.

	task :update_blog => [:find_drafts, :update_drafts, :regenerate_blog, :push]
	 
	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 "#{source_dir}/#{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  

If you have any other tips, please do mention them in the comments below.