Octopress: Rake modifications

Octopress

And the tweaking continues. While now posting a while in Octopress and I’m more and more getting used to it, there are new potential improvements at the horizon.

Creating the posts is quite easy, but I still need to open the editor manually so that I can start editing. Why? In about 99% of all cases I’m creating the post and start editing is right away afterwards. There shouldn’t be the need to explicitly open the post…

It seems that the command rake was the key and honestly: I don’t have a clue what it’s doing…

But while playing with Ruby, Octopress and Rake I’ve learned that there is a file called Rakefile in the main directory. There must be the key somewhere.

And indeed: in there is a section describing detailed what rake needs to do for creating a new post or a new page.

# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
desc "Begin a new post in #{source_dir}/#{posts_dir}"
 task :new_post, :title, :open_in_editor do |t, args|
  raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
  mkdir_p "#{source_dir}/#{posts_dir}"
  args.with_defaults(:title => 'new-post', :open_in_editor => false)
  title = args.title
  filename = "#{source_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
  ....

OK, OK. This looks like magic to me as well. But with the help of a searching engine I figured quickly out that I wasn’t the first one having this problem. Lucky me.

So I quickly altered the Rakefile - file in the way that I’ve got now the optional parameter :open which will trigger opening my editor of choice (I’m still using Scribes.

So I’ve patched the Rakefile-file for creating new Posts and Pages, put in my editor, changed the file-extension from .markdown to .mkd and extended the yaml-metainformation as well (since I were already in that file):

diff --git blogg/engine/Rakefile blogg/engine/Rakefile
index 1b5ece6..e63fa8f 100644
--- blogg/engine/Rakefile
+++ blogg/engine/Rakefile
@@ -22,8 +22,8 @@ deploy_dir      = "_deploy"   # deploy directory (for Github pages deployment)
 stash_dir       = "_stash"    # directory to stash posts for speedy generation
 posts_dir       = "_posts"    # directory for blog files
 themes_dir      = ".themes"   # directory for blog files
-new_post_ext    = "markdown"  # default new post file extension when using the new_post task
-new_page_ext    = "markdown"  # default new page file extension when using the new_page task
+new_post_ext    = "mkd"  # default new post file extension when using the new_post task
+new_page_ext    = "mkd"  # default new page file extension when using the new_page task
 server_port     = "4000"      # port for preview server eg. localhost:4000


@@ -90,10 +90,10 @@ end

 # usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
 desc "Begin a new post in #{source_dir}/#{posts_dir}"
-task :new_post, :title do |t, args|
+ task :new_post, :title, :open_in_editor do |t, args|
   raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
   mkdir_p "#{source_dir}/#{posts_dir}"
-  args.with_defaults(:title => 'new-post')
+  args.with_defaults(:title => 'new-post', :open_in_editor => false)
   title = args.title
   filename = "#{source_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
   if File.exist?(filename)
@@ -107,15 +107,21 @@ task :new_post, :title do |t, args|
     post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
     post.puts "comments: true"
     post.puts "categories: "
+    post.puts "tags: "
     post.puts "---"
   end
+  if args[:open_in_editor]
+    `scribes #{filename}`
+# Alternative open
+#   `$EDITOR #{filename}`
+  end
 end

 # usage rake new_page[my-new-page] or rake new_page[my-new-page.html] or rake new_page (defaults to "new-page.markdown")
 desc "Create a new page in #{source_dir}/(filename)/index.#{new_page_ext}"
-task :new_page, :filename do |t, args|
+task :new_page, :filename, :open_in_editor do |t, args|
   raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
-  args.with_defaults(:filename => 'new-page')
+  args.with_defaults(:filename => 'new-page', :open_in_editor => false)
   page_dir = [source_dir]
   if args.filename.downcase =~ /(^.+\/)?(.+)/
     filename, dot, extension = $2.rpartition('.').reject(&:empty?)         # Get filename and extension
@@ -145,6 +151,9 @@ task :new_page, :filename do |t, args|
       page.puts "footer: true"
       page.puts "---"
     end
+    if args[:open_in_editor]
+            `scribes #{file}`
+  end
   else
     puts "Syntax error: #{args.filename} contains unsupported characters"
   end

And now starting a new post or page with

rake new_post["Posttitle",:open]

magically will open my editor as well.

So: today’s lesson learnt: rake is basically doing for Ruby what make is doing for Unix.