public
Last active

Jekyll plugin to render less.js - fixed for new versions of jekyll

  • Download Gist
lessjs.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
module Jekyll
class LessCssFile < StaticFile
def write(dest)
# do nothing
end
end
# Expects a lessc: key in your _config.yml file with the path to a local less.js/bin/lessc
# Less.js will require node.js to be installed
class LessJsGenerator < Generator
safe true
priority :low
def generate(site)
src_root = site.config['source']
dest_root = site.config['destination']
less_ext = /\.less$/i
raise "Missing 'lessc' path in site configuration" if !site.config['lessc']
# static_files have already been filtered against excludes, etc.
site.static_files.each do |sf|
next if not sf.path =~ less_ext
less_path = sf.path
css_path = less_path.gsub(less_ext, '.css').gsub(src_root, dest_root)
css_dir = File.dirname(css_path)
css_dir_relative = css_dir.gsub(dest_root, '')
css_name = File.basename(css_path)
FileUtils.mkdir_p(css_dir)
 
begin
command = [site.config['lessc'],
less_path,
css_path
].join(' ')
puts 'Compiling LESS: ' + command
`#{command}`
raise "LESS compilation error" if $?.to_i != 0
end
# Add this output file so it won't be cleaned
site.static_files << LessCssFile.new(site, site.source, css_dir_relative, css_name)
end
end
end
end

I've modified this script slightly—I had issues with Jekyll "cleaning" the CSS file generated by LESS out of my output directory moments after it was created. I fixed it in a crufty way but it works.

I added a line to remove the LESS files from the site.static_files array so they automatically get cleaned. https://gist.github.com/2391969

Found a bug with generating css with lessc. I guess new lessc generator doesn't take OUTPUT file argument but just spits itout to STDOUT, so the above generator will fail, I've fixed the bug by adding > to the command. Here's the gist https://gist.github.com/tdak/6496935

The line that adds the css file to site.static_files isn't working for me. There's no error, but the css file still gets cleaned. I can see that it's being created and then deleted within a fraction of a second.

If I create /css/test.css alongside /css/test.less, then /_site/css/test.css will not get cleaned by Jekyll. It's not pretty, but it works for now until someone figures out why this plugin isn't preventing the css file from being cleaned.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.