UNPKG

hig

Version:

HiG server

129 lines (121 loc) 4.31 kB
var fs = require('fs'), dirname = require('path').dirname, less = require('less'), stylus = require('stylus'), handlebars = require('handlebars'), fileutils = require('./fileutils'), higLess = require('./less'), compilers = {}; compilers.styl = function(path) { var content, result = ''; content = fs.readFileSync(path, 'utf8'); stylus(content).include(dirname(path)).render(function(err, css) { if (err) { throw err; } return result = css; }); return result; }; require.extensions['.styl'] = function(module, filename) { var source; source = JSON.stringify(compilers.styl(filename)); return module._compile("module.exports = " + source, filename); }; compilers.less = function(path) { var ee, parseFn, lessParser, content = fs.readFileSync(path, 'utf8'); //console.log(higLess); lessParser = new(less.Parser)({ paths: [, higLess.path, dirname(path) ] }); parseFn = function(callback, compress) { lessParser.parse(content + "//" + Math.random(), function (err, tree) { var css = !err && tree.toCSS({ compress: compress || false }), ext = []; if (css) { //exend: .test; css = css.replace(/((?:[\.#][\w\_][\w\d\-\_]+\s*)+)(\{[^\{]+)(?:extend|ext)\s*\:\s*["']*([\.#][\w\_][\w\d\-\_]+)["']*\s*\;*[\s\r\n\t]*([^\}\{\.]+\})/g, function($0, $1, $2, $3, $4) { //console.log(arguments); var classSelect = $1; ext.push([$1, $3]); $2 = $2.replace(/[\s\r\n\t]*(?:extend|ext)\s*\:\s*["']*([\.#][\w\_][\w\d\-\_]+)["']*\s*\;*/g, function($0, $1) { //console.log(arguments); ext.push([classSelect, $1]); return ""; }); return $1 + $2 + $4; }); if (ext.length) { ext.forEach(function(val, index) { var reg = new RegExp('(^|\\r|\\n|\\}|,)([\\s\\t]*' + val[1].replace(/(\.|\s|\-)/g, function($0, $1) { return '\\' + $1; }) + ')((?:(?:[^\\w\\d\\-_,\\{]*)|(?:\\s[^\\{\\,]+)))([\\{,])', "g"); //console.log(reg); css = css.replace(reg, function($0, $1, $2, $3, $4) { //console.log(arguments); return ($1 + $2 + $3 + "," + val[0] + $3 + $4).replace(/\s+,/g, ",").replace(/\s\s+/g, " "); }); }); } /* css = css.replace(/\.extendstart\s*\{[\s\r\n]*extend\:\s*1;[\s\r\n]*\}[\s\r\n]*((?:.|\n|\r)*?)[\s\r\n]*\s*\.extendend\s*\{[\s\r\n]*extend\:\s*1;[\s\r\n]*\}/g, function($0, $1) { //\.extendend\s*\{[\s\r\n]*extend\:\s*1;[\s\r\n]*\} //console.log(arguments); $1 = $1.replace(/[\r\n\}]+[^\{]+\,[^\{]+\s([^\{]+)/g, function($0, $1){ var path = $1.replace(/\s+$/, ''); return $0.replace(/([^\}\,]+)\,/g, "$1 " + path + ",");; }); return $1; });*/ css = css.replace(/\/\*[\s\r\n\{]*(extend|replace)\s*:\s*1\;[\s\r\n\}]*\*\//g, ""); } callback(err && JSON.stringify(err), css && css, path); }); } return parseFn; }; require.extensions['.less'] = function(module, filename) { var source, ee = compilers.less(filename); module.exports = ee; return module; }; compilers.hdb = function(path) { var content, template, dirPath = compilers.hdbPartialPath || path.replace(/[^\/]+$/, ""); content = fs.readFileSync(path, 'utf8'); template = handlebars.compile(content); content && content.replace(/\{\{>\s+([^\}]+)\}\}/g, function($0, $1) { var name = $1.replace(/\./, "/"), parPath = dirPath + name + ".hdb", partial = "\nPartial 404: " + name + " Not Found"; if (fileutils.isFile(parPath)) { partial = fs.readFileSync(parPath, 'utf8'); } handlebars.registerPartial(name, partial); }); return template; }; compilers.hdbPartialPath = ""; require.extensions['.hdb'] = function(module, filename) { var template = compilers.hdb(filename); module.exports = template; return module; }; /* var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " + "{{kids.length}} kids:</p>" + "<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>{{> per.son}}"; var template = handlebars.compile(source); handlebars.registerPartial("per.son", handlebars.compile("ssdsd {{name}} s")); var data = { "name": "Alan", "hometown": "Somewhere, TX", "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]}; var result = template(data); console.log(result); */ module.exports = compilers;