0

我的 Rakefile 中有一个任务是在 Jekyll 中创建一个新帖子。(我从 Octopress 借来的)。

我试图破解它,以便它创建一个新的降价文件,添加默认标题信息,然后在 IA Writer 中打开它。它创建文件并添加信息,但它打开的不是文件。看起来文件名变量没有被传递给系统命令,但我不知道应该如何编写它。

这是代码:

# 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
4

1 回答 1

0

在线system...更改'". 在'ruby​​ 中不使用变量,例如:

001 > filename = "test"
=> "test"
002 > puts '#{filename}'
=> #{filename}
003 > puts "#{filename}"
=> test
于 2011-12-06T04:30:49.253 回答