:date: 2012-10-25 10:03
.. index:: tech, blogg, ruby, octopress
Octopress: Rake modifications
=============================
.. image:: /_images/images//gallery/2012/2012-placeholder/20120907-octopress-logo.png
:alt: Octopress
:align: right
:scale: 50
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 :program:`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 :file:`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.
.. code:: bash
# 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 :file:`Rakefile` - file in the way that I've got now the optional parameter :command:`:open` which will trigger opening my editor of choice (I'm still using `Scribes `__.
So I've patched the :file:`Rakefile`-file for creating new Posts and Pages, put in my editor, changed the file-extension from :file:`.markdown` to :file:`.mkd` and extended the yaml-metainformation as well (since I were already in that file):
.. code:: bash
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
.. code:: bash
rake new_post["Posttitle",:open]
magically will open my editor as well.
So: today's lesson learnt: :command:`rake` is basically doing for Ruby what :command:`make` is doing for Unix.