Pregunta

I have created a custom deploy method in the rakefile for octopress. The function is simple,

I want to be able to:

rake generate and put all the files in my public folder (ie: /home/jeremy/website/wwwdata) Then I want to be able to "rake compress" and remove all the excess spaces and blank lines from the files in the output folder (which I would upload to my server).

So far I have this:

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

The issue I'm having is finding that output directory. I know it's specified in _config.yml and it's obviously used in the rakefile but every time I try to use any of any of those variables it doesn't work.

I googled around and read through the docs and couldn't find much. Is there a way to get that information? I don't want a hardcoded file path because I'd like to submit this as a plugin or pull request when I get it done so others can use it.

¿Fue útil?

Solución

You can access the Jekyll configuration this way:

require 'jekyll'

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

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

You can use this in your Rakefile this way:

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

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

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

    # ...

  end
end

Note that I have added #{} around public_dir in the argument to Dir.glob, otherwise this would be looking for the literal directory public_dir/ instead of the actual destination directory.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top