在 Mac 应用程序中使用 rake 文件打开文档

发布于 12-19 23:06 字数 1103 浏览 3 评论 0原文

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

我尝试破解它,以便它创建一个新的 markdown 文件,添加默认标题信息,然后在 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐2024-12-26 23:06:47

system... 行中,将 ' 更改为 "。在 ' 中,ruby 不使用变量,例如:

001 > filename = "test"
=> "test"
002 > puts '#{filename}'
=> #{filename}
003 > puts "#{filename}"
=> test

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

001 > filename = "test"
=> "test"
002 > puts '#{filename}'
=> #{filename}
003 > puts "#{filename}"
=> test
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文