0

我在 octopress 的 rakefile 中创建了一个自定义部署方法。功能很简单,

我希望能够:

rake 生成并将所有文件放在我的公共文件夹中(即:/home/jeremy/website/wwwdata)然后我希望能够“rake compress”并从输出文件夹中的文件中删除所有多余的空格和空行(我会上传到我的服务器)。

到目前为止,我有这个:

desc "compress output files"
task :compress do
    puts "## Compressing html"
    # read every .htm and .html in a directory

#TODO: Find this directory! 

puts public_dir

    Dir.glob("public_dir/**/*.{htm,html}").each do|ourfile|
    linedata = ""
    #open the file and parse it
    File.open(ourfile).readlines.each do |line|
        #remove excess spaces and carraige returns
        line = line.gsub(/\s+/, " ").strip.delete("\n")
        #append to long string
        linedata << line
    end
    #reopen the file
    compfile = open(ourfile, "w")
    #write the compressed string to file
    compfile.write(linedata)
    #close the file
    compfile.close
    end 
end

我遇到的问题是找到那个输出目录。我知道它是在 _config.yml 中指定的,并且显然在 rakefile 中使用了它,但是每次我尝试使用其中任何一个变量时它都不起作用。

我四处搜索并阅读了文档,但找不到太多。有没有办法获取这些信息?我不想要硬编码的文件路径,因为我想在完成后将其作为插件提交或拉取请求,以便其他人可以使用它。

4

1 回答 1

1

您可以通过以下方式访问 Jekyll 配置:

require 'jekyll'

conf = Jekyll.configuration({})
#=> {
#     "source"      => "/Users/me/some_project",
#     "destination" => "/Users/me/some_project/_site",
#     ...
#   }

conf["destination"]
#=> "/Users/me/some_project/_site"

您可以通过这种方式在您的 Rakefile 中使用它:

require 'jekyll'
CONF = Jekyll.configuration({})

task :something do
  public_dir = CONF["destination"]

  Dir.glob("#{public_dir}/**/*.{htm,html}").each do |ourfile|

    # ...

  end
end

请注意,我已经在参数中添加#{}public_dirDir.glob否则这将寻找文字目录public_dir/而不是实际的目标目录。

于 2013-10-20T09:13:38.230 回答