Skip to content

Instantly share code, notes, and snippets.

@davethegr8
Forked from andyfowler/lessjs.rb
Created April 25, 2014 05:05
Show Gist options
  • Save davethegr8/11278149 to your computer and use it in GitHub Desktop.
Save davethegr8/11278149 to your computer and use it in GitHub Desktop.
module Jekyll
class LessCssFile < StaticFile
attr_accessor :styles
def destination(dest)
File.join(dest, @dir, @name.sub(/less$/, 'css'))
end
def write(dest)
dest_path = destination(dest)
FileUtils.mkdir_p(File.dirname(dest_path))
begin
File.open(dest_path, 'w') do |f|
f.write(@styles)
end
end
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_index do |i|
sf = site.static_files[i]
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
].join(' ')
puts "\nCompiling LESS: " + command
output = `#{command}`
raise "LESS compilation error" if $?.to_i != 0
end
less_file = LessCssFile.new(site, site.source, css_dir_relative, css_name)
less_file.styles = output
# Add this output file so it won't be cleaned
site.static_files[i] = less_file
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment