Created
May 28, 2014 14:40
-
-
Save angusdev/4fc19f1655afd1e648c3 to your computer and use it in GitHub Desktop.
Jekyll plugin to convert Less files to CSS using node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# jekyll-less-nodejs.rb | |
# | |
# Jekyll plugin to convert Less files to CSS using node.js | |
# Modified from jekyll-less (https://github.com/zroger/jekyll-less, version 85d6e73dc7) | |
# | |
# Twitter: @angusdev | |
# Released under APL 2.0 | |
# | |
# | |
# In windows, there is not easy (or no solution) to install therubyracer, which is a dependency | |
# of less gem (see http://stackoverflow.com/questions/6356450/therubyracer-gem-on-windows). | |
# To solve this issue, it will be more simple to run the lessc command line (via node.js) | |
# to convert the .less to .css | |
# | |
# Installation | |
# ============ | |
# | |
# 1. Install node.js | |
# 2. Install less | |
# npm install less | |
# 3. Put this file in _plugins/ folder. | |
# | |
# Default Configuration | |
# ===================== | |
# in _config.yml: | |
# | |
# less: | |
# compress: true | |
# lessc: lessc | |
# | |
# With the default configuration, lessc should be in the PATH, otherwise you should specify the | |
# location of lessc in config. As the location varies among machine, it is suggested to create | |
# a _config_local.yml and put the config in this file, then run | |
# | |
# jekyll serve --config _config.yml,_config_local.yml | |
# | |
module Jekyll | |
module Less | |
class LessCssFile < Jekyll::StaticFile | |
attr_accessor :compress | |
attr_accessor :lessc | |
# Obtain destination path. | |
# +dest+ is the String path to the destination dir | |
# | |
# Returns destination file path. | |
def destination(dest) | |
File.join(dest, @dir, @name.sub(/less$/, 'css')) | |
end | |
# Convert the less file into a css file. | |
# +dest+ is the String path to the destination dir | |
# | |
# Returns false if the file was not modified since last time (no-op). | |
def write(dest) | |
dest_path = destination(dest) | |
return false if File.exist? dest_path and !modified? | |
@@mtimes[path] = mtime | |
FileUtils.mkdir_p(File.dirname(dest_path)) | |
begin | |
`#{lessc} #{path} #{dest_path} #{compress ? '--compress' : ''}` | |
#content = File.read(path) | |
#content = ::Less::Parser.new({:paths => [File.dirname(path)]}).parse(content).to_css :compress => compress | |
#File.open(dest_path, 'w') do |f| | |
# f.write(content) | |
#end | |
rescue => e | |
STDERR.puts "Less Exception: #{e.message}" | |
end | |
true | |
end | |
end | |
class LessCssGenerator < Jekyll::Generator | |
safe true | |
# Initialize options from site config. | |
def initialize(config = {}) | |
@options = config["less"] ||= {"compress" => true, "lessc" => "lessc"} | |
end | |
# Jekyll will have already added the *.less files as Jekyll::StaticFile | |
# objects to the static_files array. Here we replace those with a | |
# LessCssFile object. | |
def generate(site) | |
site.static_files.clone.each do |sf| | |
if sf.kind_of?(Jekyll::StaticFile) && sf.path =~ /\.less$/ | |
site.static_files.delete(sf) | |
name = File.basename(sf.path) | |
destination = File.dirname(sf.path).sub(site.source, '') | |
less_file = LessCssFile.new(site, site.source, destination, name) | |
less_file.compress = @options["compress"] | |
less_file.lessc = @options["lessc"] | |
site.static_files << less_file | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment