Created
June 1, 2013 13:35
-
-
Save RobertWHurst/5690400 to your computer and use it in GitHub Desktop.
Convert less to stylus files. Note that less scoping will not work in stylus as stylus does not have scoping.
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
var fs = require('fs'); | |
function less2stylus(source, mixinNames) { | |
return source | |
// @var => $var | |
.replace(/@([a-zA-Z0-9-_]+)[\s\t]*(:?)[\s\t]*/g, function(str, key, sep) { | |
// do not convert import or media keywords | |
if(['import', 'media'].indexOf(key) > -1) { return str; } | |
// replace @ with $ | |
if(sep) { | |
return '$' + key + '='; | |
} else { | |
return '$' + key; | |
} | |
}) | |
// @import "foo.less" => @import "foo.styl" | |
.replace(/@import[\s]*["']?([^"']+)\.less["']?/g, function(str, fileName) { | |
return '@import "' + fileName + '.styl"'; | |
}) | |
// .mixin() => mixin() | |
.replace(/\.([a-zA-Z0-9_-]+)[\s\t]*(\(?)/g, function(str, mixinName, paren) { | |
if(mixinNames.indexOf(mixinName) > -1) { | |
if(paren) { | |
return mixinName + paren; | |
} else { | |
return mixinName + '()'; | |
} | |
} | |
return str; | |
}) | |
// ; => | |
// : => | |
// { => | |
// } => | |
.replace(/;[^\n\/]*([\n]?\/?\/?)/g, function(str, trail) { | |
return trail; | |
}) | |
.replace(/:/g, '') | |
.replace(/\{/g, '') | |
.replace(/\}/g, '') | |
.replace(/[\s\t]*\n/g, '\n'); | |
} | |
function catalogLessMixins(source, mixinNames) { | |
var mixinName = true; | |
var regex = /\.([a-zA-Z0-9_-]+)\(/g; | |
while(mixinName) { | |
var mixinName = regex.exec(source); | |
if(mixinName && mixinName[1] && mixinNames.indexOf(mixinName[1]) == -1) { | |
console.log('Mixin "' + mixinName[1] + '"'); | |
mixinNames.push(mixinName[1]); | |
} | |
} | |
} | |
function catalog(src, mixinNames) { | |
var srcStat = fs.statSync(src); | |
// directory | |
if(srcStat.isDirectory()) { | |
// iterate | |
var srcDir = fs.readdirSync(src); | |
for(var iD = 0; iD < srcDir.length; iD += 1) { | |
catalog(src + '/' + srcDir[iD], mixinNames); | |
} | |
} | |
// file | |
else if (srcStat.isFile() && src.match(/\.less$/)) { | |
// convert to stylus | |
var source = fs.readFileSync(src, "utf-8"); | |
catalogLessMixins(source, mixinNames); | |
} | |
} | |
function convert(src, dest, mixinNames) { | |
mixinNames = mixinNames || []; | |
var srcStat = fs.statSync(src); | |
// directory | |
if(srcStat.isDirectory()) { | |
// check for the dest directory | |
if(!fs.existsSync(dest)) { | |
fs.mkdirSync(dest); | |
} | |
// iterate | |
var srcDir = fs.readdirSync(src); | |
for(var iD = 0; iD < srcDir.length; iD += 1) { | |
catalog(src + '/' + srcDir[iD], mixinNames); | |
} | |
for(var iD = 0; iD < srcDir.length; iD += 1) { | |
convert(src + '/' + srcDir[iD], dest + '/' + srcDir[iD].replace(/\.less$/, ".styl"), mixinNames); | |
} | |
} | |
// file | |
else if (srcStat.isFile() && src.match(/\.less$/)) { | |
console.log(src + ' => ' + dest); | |
// convert to stylus | |
var source = fs.readFileSync(src, "utf-8"); | |
var output = less2stylus(source, mixinNames); | |
fs.writeFileSync(dest, output); | |
} | |
} | |
convert('./less', './styl', [/* any mixins that are never used with () */]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment