Question

I have a task in my Rakefile for creating a new post in Jekyll. (I borrowed it from Octopress).

I've tried to hack it so that it will create a new markdown file, add the default header info and then open it in IA Writer. It creates the file and adds the info, but what it opens isn't the file. It looks like the filename variable isn't being passed through to the system command, but I have no idea how I should be writing it.

Here's the code:

# Usage rake new_post['Title of the Post']
desc "Beginning a new post in _posts"
task :new_post, :title do |t, args|
  args.with_defaults(:title => 'new-post')
  title = args.title
  filename = "_posts/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.markdown"
  open_filename = "#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.markdown"
  if File.exist?(filename)
    abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
  end
  puts "Creating new post: #{filename}"
  open(filename, 'w') do |post|
    post.puts "---"
    post.puts "layout: post"
    post.puts "title: \"#{title.gsub(/&/,'&')}\""
    post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
    post.puts "categories: "
    post.puts "---"
  end
  puts "Opening: #{filename}"
  system('open -a IA\ Writer ~/Dropbox/Archive/jdb/#{filename}')
end
Was it helpful?

Solution

In line system... change ' to ". In ' ruby doesn't use variables, example:

001 > filename = "test"
=> "test"
002 > puts '#{filename}'
=> #{filename}
003 > puts "#{filename}"
=> test
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top