Skip to content

Instantly share code, notes, and snippets.

@hara
Created April 13, 2013 03:13
Show Gist options
  • Save hara/5376738 to your computer and use it in GitHub Desktop.
Save hara/5376738 to your computer and use it in GitHub Desktop.
A Jekyll plugin to compile Sass with Compass when building pages.
# -*- coding: utf-8 -*-
#
# Generates css files from Sass source files with Compass.
#
require 'pathname'
require 'sass'
require 'compass'
module Jekyll
module Sass
# Sass Static File# {{{
class SassStaticFile < ::Jekyll::StaticFile
# Initialize a new SassStaticFile.
#
# site - The Site.
# base - The String path to the <source>.
# dir - The String path between <source> and the file.
# name - The String filename of the file.
# syntax - The Symbol syntax of the file (:scss or :sass).
def initialize(site, base, dir, name, syntax)
super(site, base, dir, name)
@syntax = syntax
end
# Obtain destination path.
#
# dest - The String path to the destination dir.
def destination(dest)
Pathname.new(super(dest)).sub_ext('.css').to_s
end
# Write the converted css file to the destination directory (if modified).
#
# dest - 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))
sass = ::Sass::Engine.new(
File.read(path, encoding: 'UTF-8'),
syntax: @syntax,
load_paths: Compass.configuration.sass_load_paths)
File.write(dest_path, sass.render, encoding: 'UTF-8')
true
end
end
# }}}
# StaticFile class extension.# {{{
class ::Jekyll::StaticFile
# Returns whether the file is a sass source file.
def sass_file?
File.extname(@name) == '.sass' || File.extname(@name) == '.scss'
end
# Returns the Symbol syntax of sass source file (:sass or :scss).
def sass_syntax
File.extname(@name)[1 .. -1].to_sym
end
# Returns the SassStaticFile of this file.
def to_sass
return nil unless sass_file?
SassStaticFile.new(@site, @base, @dir, @name, sass_syntax)
end
end
# }}}
# Sass CSS Generator# {{{
class SassCssGenerator < ::Jekyll::Generator
# Generate Sass CSS file.
#
# site - The Site.
def generate(site)
site.static_files.map! { |file| file.sass_file? ? file.to_sass : file}
end
end
# }}}
end
end
if __FILE__ == $0
require 'minitest/autorun'
end
# vim: foldmethod=marker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment