mirror of
https://github.com/seigler/dash-docs
synced 2025-07-27 01:36:13 +00:00
generate css from less using lessc
This commit is contained in:
parent
c4a8e4e904
commit
1b6b0b245f
12 changed files with 62 additions and 18 deletions
61
_plugins/less.rb
Normal file
61
_plugins/less.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
require 'digest/md5'
|
||||
|
||||
module Jekyll
|
||||
|
||||
class LessCSSFile < StaticFile
|
||||
def write(dest)
|
||||
# do nothing
|
||||
end
|
||||
end
|
||||
|
||||
class LessCSS < Liquid::Tag
|
||||
def initialize(tag_name, file, tokens)
|
||||
super
|
||||
@file = file.strip
|
||||
end
|
||||
|
||||
def render(context)
|
||||
site = context.registers[:site]
|
||||
|
||||
less_dir = File.join(site.source, '_less')
|
||||
|
||||
if File.symlink?(less_dir)
|
||||
return "LessCSS directory '#{less_dir}' cannot be a symlink"
|
||||
end
|
||||
|
||||
if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
|
||||
return "LessCSS file '#{@file}' contains invalid characters or sequences"
|
||||
end
|
||||
|
||||
Dir.chdir(less_dir) do
|
||||
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
|
||||
if choices.include?(@file)
|
||||
source = File.read(@file)
|
||||
f = IO.popen("lessc", "w+")
|
||||
f.write(source)
|
||||
f.close_write()
|
||||
|
||||
css = f.readlines().join()
|
||||
|
||||
digest = Digest::MD5.hexdigest(css)
|
||||
|
||||
css_file = digest + ".css"
|
||||
css_path = File.join(site.dest, css_file)
|
||||
|
||||
File.open(css_path, "w") do |f|
|
||||
f.write(css)
|
||||
end
|
||||
|
||||
site.static_files << LessCSSFile.new(site, site.source, '', css_file)
|
||||
|
||||
'<link rel="stylesheet" type="text/css" href="/' + css_file + '" />'
|
||||
else
|
||||
return "LessCSS file '#{@file}' not found in '#{less_dir}'"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Liquid::Template.register_tag('lesscss', Jekyll::LessCSS)
|
Loading…
Add table
Add a link
Reference in a new issue