【问题标题】:How to access public directory from rakefile如何从 rakefile 访问公共目录
【发布时间】:2013-10-20 08:50:10
【问题描述】:

我在 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 中使用了它,但是每次我尝试使用其中任何一个变量时它都不起作用。

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

【问题讨论】:

    标签: ruby rake jekyll octopress rakefile


    【解决方案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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      相关资源
      最近更新 更多