-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lennart Melzer
committed
Nov 18, 2011
0 parents
commit d058801
Showing
10 changed files
with
758 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tmp |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
fs = require 'fs' | ||
spawn = require('child_process').spawn | ||
|
||
# Run a CoffeeScript through our node/coffee interpreter. | ||
run = (args, cb) -> | ||
proc = spawn 'coffee', args | ||
proc.stderr.on 'data', (buffer) -> console.log buffer.toString() | ||
proc.on 'exit', (status) -> | ||
process.exit(1) if status != 0 | ||
cb() if typeof cb is 'function' | ||
|
||
task 'build', 'build the CoffeeScript language from source', build = (cb) -> | ||
files = fs.readdirSync 'src' | ||
files = ('src/' + file for file in files when file.match(/\.coffee$/)) | ||
run ['-c', '-o', 'lib/style-less'].concat(files), cb |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# styleLess | ||
|
||
Parses LESS, beautifies LESS then writes LESS all using LESS.js | ||
|
||
## Synopsis | ||
|
||
__Warning__ this is opionionated formatting, as it compresses lines that take up | ||
less than 81 chars into one line to be more concise. | ||
|
||
```less | ||
|
||
/* Comments before rule */ | ||
.navbar { | ||
background-color: black; | ||
color: #fff; | ||
height: 23px; | ||
-moz-transition-duration: 1337ms; | ||
width: ~`@{document.body.clientWidth}`; | ||
@desired-menu-width: 950px; | ||
|
||
a:link, a:visited { color: #dadada; } | ||
|
||
.nav-main { | ||
/* calls to mixins */ | ||
.grid(24, 14); | ||
|
||
/* Inline mixin declarations */ | ||
.right-separator() { border-right: 1px solid #949494; } | ||
|
||
/* Literal escaped values */ | ||
filter: ~"progid:DXImageTransform.Microsoft.Alpha(Opacity=89)"; | ||
|
||
/* Literal quoted values */ | ||
font-family: "Trebuchet MS"; | ||
|
||
li { &:first-child { margin: 0; } } | ||
|
||
> li.submenu > a:link, > li.submenu > a:visited { | ||
background: url('icons/arrow-down.png') 23px 23px no-repeat transparent; | ||
.right-separator; | ||
} | ||
|
||
// Keep Me | ||
.preview-label { width: 100px; display: block; font-size: small; } | ||
|
||
li a:hover, li a:active, li.current a:link, | ||
li.current a:visited, li.submenu a:link, li.submenu a:visited { | ||
.menublock { | ||
opacity: 1; | ||
padding: 10px ((@desired-menu-width - 100px) / 2); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Comments after rule | ||
*/ | ||
} | ||
|
||
/* Comments between root rules */ | ||
#main { .link { color: white; font-weight: bold; text-decoration: underline; } } | ||
|
||
``` | ||
|
||
## Installation | ||
|
||
npm install style-less | ||
|
||
## Usage | ||
|
||
style-less ugly.less > pretty.less | ||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
|
||
module.exports = function(tree) { | ||
var _compact, _filter, _isLogicalSection, _multi, _selectors, _shortened, _single; | ||
tree.Import.prototype.toLess = function() { | ||
if (this.css) { | ||
return this.toCSS(); | ||
} else { | ||
return "@import " + this._path.toCSS() + ";"; | ||
} | ||
}; | ||
tree.mixin.Definition.prototype.toLess = function(ctx, env) { | ||
var merged, params, rules, sel; | ||
params = this.params.map(function(p) { | ||
if (p.value != null) { | ||
return p.name + ": " + p.value.toLess(env); | ||
} else { | ||
return p.name; | ||
} | ||
}).join(", ").trim(); | ||
sel = this.selectors[0].toLess(env).trim() + "(" + (params || "") + ")"; | ||
rules = this.rules.reduce(function(_css, r) { | ||
_css += ctx + " " + r.toLess(ctx + " ", env).trim(); | ||
return _css; | ||
}, ""); | ||
merged = _multi(sel, ctx, env, rules); | ||
if (merged.length > 80) { | ||
return ctx + merged; | ||
} else { | ||
return ctx + _single(sel, ctx, { | ||
compress: true | ||
}, rules); | ||
} | ||
}; | ||
tree.Selector.prototype.toLess = function(env) { | ||
if (this._css) { | ||
console.log(this._css); | ||
this._css; | ||
} | ||
return this.elements.map(function(e) { | ||
if (typeof e === 'string') { | ||
return ' ' + e.trim(); | ||
} else { | ||
return e.toLess(env); | ||
} | ||
}).join(""); | ||
}; | ||
tree.Color.prototype.toLess = function() { | ||
return this.toCSS(); | ||
}; | ||
tree.Call.prototype.toLess = function(env) { | ||
var args, _ref; | ||
args = (_ref = this.args) != null ? _ref.map(function(a) { | ||
return a.toLess(env); | ||
}).join(", ").trim() : void 0; | ||
return this.name + (args != null ? "(" + args + ")" : ""); | ||
}; | ||
tree.mixin.Call.prototype.toLess = function(env) { | ||
var args, _ref; | ||
args = (_ref = this.arguments) != null ? _ref.map(function(a) { | ||
return a.toLess(env); | ||
}).join(", ").trim() : void 0; | ||
return this.selector.toLess(env).trim() + (args != null ? "(" + args + ");" : ";"); | ||
}; | ||
tree.Directive.prototype.toLess = function(ctx, env) { | ||
var merged, params, rules, sel; | ||
params = this.params.map(function(p) { | ||
return p.toLess(env); | ||
}).join(", ").trim(); | ||
sel = this.selectors[0].toLess(env).trim() + "(" + (params || "") + ")"; | ||
rules = this.rules.map(function(r) { | ||
return r.toLess(env).trim(); | ||
}); | ||
merged = _multi(sel, ctx, env, rules); | ||
if (merged.length > 80) { | ||
return merged; | ||
} else { | ||
return _single(sel, ctx, { | ||
compress: true | ||
}, rules); | ||
} | ||
}; | ||
tree.Dimension.prototype.toLess = function() { | ||
return this.toCSS(); | ||
}; | ||
tree.Anonymous.prototype.toLess = function() { | ||
return this.toCSS(); | ||
}; | ||
tree.Alpha.prototype.toLess = function() { | ||
return this.toCSS(); | ||
}; | ||
tree.Element.prototype.toLess = function(env) { | ||
return this.combinator.toLess(env || {}) + this.value; | ||
}; | ||
tree.Combinator.prototype.toLess = function(env) { | ||
return { | ||
'': '', | ||
' ': ' ', | ||
'&': '&', | ||
'& ': '& ', | ||
':': ' :', | ||
'::': '::', | ||
'+': ' + ', | ||
'~': ' ~ ', | ||
'>': ' > ' | ||
}[this.value]; | ||
}; | ||
tree.Expression.prototype.toLess = function(env) { | ||
return this.value.map(function(e) { | ||
return e.toLess(env); | ||
}).join(' '); | ||
}; | ||
tree.JavaScript.prototype.toLess = function() { | ||
return "" + (this.escaped && "~" || "") + "`" + this.expression + "`"; | ||
}; | ||
tree.Keyword.prototype.toLess = function() { | ||
return this.toCSS(); | ||
}; | ||
tree.Quoted.prototype.toLess = function() { | ||
return (this.escaped && "~" || "") + this.quote + this.value + this.quote; | ||
}; | ||
tree.URL.prototype.toLess = function(env) { | ||
return "url(" + this.value.toLess(env) + ")"; | ||
}; | ||
tree.Variable.prototype.toLess = function() { | ||
return this.name; | ||
}; | ||
tree.Value.prototype.toLess = function(env) { | ||
return this.value.map(function(e) { | ||
return e.toLess(env); | ||
}).join(env.compress ? ',' : ', '); | ||
}; | ||
tree.Shorthand.prototype.toLess = function(env) { | ||
return this.a.toLess(env) + "/" + this.b.toLess(env); | ||
}; | ||
tree.Comment.prototype.toLess = function(env) { | ||
return this.toCSS(env); | ||
}; | ||
tree.Rule.prototype.toLess = function(env) { | ||
return this.name + (env.compress ? ':' : ': ') + this.value.toLess(env) + this.important + ";"; | ||
}; | ||
tree.Operation.prototype.toLess = function(env) { | ||
return "(" + [this.operands[0].toLess(env), this.op, this.operands[1].toLess(env)].join(" ") + ")"; | ||
}; | ||
_multi = function(selector, context, env, rules) { | ||
return "" + selector + " {\n" + rules + "\n" + context + "}"; | ||
}; | ||
_single = function(selector, context, env, rules) { | ||
return selector + ' { ' + rules.trim() + ' }'; | ||
}; | ||
_selectors = function(paths, env, padding) { | ||
var i, mapped, part, results; | ||
mapped = paths.map(function(p) { | ||
return p.map(function(s) { | ||
return s.toLess(env); | ||
}).join('').trim(); | ||
}); | ||
results = [padding + mapped.shift()]; | ||
if (mapped.length !== 0) { | ||
for (i in mapped) { | ||
part = mapped[i]; | ||
if (env.compress) { | ||
results.push(',' + part); | ||
} else { | ||
if (paths.length > 3 && i % 3 === 2) { | ||
results.push(',\n' + padding + part); | ||
} else { | ||
results.push(', ' + part); | ||
} | ||
} | ||
} | ||
} | ||
return results.join(""); | ||
}; | ||
_filter = function(obj, iterator, context) { | ||
var results; | ||
results = []; | ||
if (obj === null) return results; | ||
return obj.filter(iterator, context); | ||
}; | ||
_compact = function(array) { | ||
return _filter(array, function(value) { | ||
return !!value; | ||
}); | ||
}; | ||
_isLogicalSection = function(content) { | ||
var lastChar; | ||
lastChar = content.charAt(content.length - 2); | ||
return lastChar === ";" || lastChar === "}"; | ||
}; | ||
_shortened = function(content) { | ||
return content.replace(/\s+/g, " ").trim(); | ||
}; | ||
tree.Ruleset.prototype.toLess = function(context, env) { | ||
var css, merged, padding, paths, rules, rulesets, sel; | ||
var _this = this; | ||
css = ""; | ||
paths = []; | ||
rulesets = []; | ||
if (!this.root) { | ||
paths = this.selectors.map(function(s) { | ||
return [s]; | ||
}); | ||
} | ||
padding = this.root ? "" : context + " "; | ||
rules = this.rules.reduce(function(_css, rule, r, list) { | ||
var comment, content, sel; | ||
if (rule.rules || (rule instanceof tree.Directive)) { | ||
sel = rule.selectors.map(function(s) { | ||
return s.toLess(env); | ||
}).join(", ").trim(); | ||
if (_isLogicalSection(_css)) _css += "\n"; | ||
content = rule.toLess(padding, env); | ||
if (_shortened(content).length < 90) { | ||
content = padding + _shortened(content); | ||
} | ||
_css += content; | ||
} else { | ||
if (rule instanceof tree.Comment) { | ||
if (_isLogicalSection(_css)) _css += "\n"; | ||
if (_this.root) { | ||
_css += rule.toLess(env).trim(); | ||
} else { | ||
comment = rule.value.toString().trim(); | ||
_css += padding + comment; | ||
} | ||
} else { | ||
if (rule.toLess && !rule.variable) { | ||
_css += padding + rule.toLess(env); | ||
} else if (rule.value) { | ||
if (rule.variable) { | ||
_css += context + padding + rule.name + ": " + rule.value.toLess(env) + ";"; | ||
} else { | ||
_css += context + rule.value.toString(); | ||
} | ||
} | ||
} | ||
} | ||
if (list.length !== r + 1) _css += '\n'; | ||
return _css; | ||
}, ""); | ||
if (this.root) { | ||
css += rules; | ||
} else { | ||
sel = _selectors(paths, env, context); | ||
merged = _multi(sel, context, env, rules); | ||
if (merged.length > 80) { | ||
css += merged; | ||
} else { | ||
sel = _selectors(paths, { | ||
compress: false | ||
}, context); | ||
css += _single(sel, context, env, rules); | ||
} | ||
} | ||
return css; | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(function() { | ||
var Beautifier, fs, less, munge, parse, parser; | ||
|
||
fs = require("fs"); | ||
|
||
less = require("less"); | ||
|
||
Beautifier = require(__dirname + '/beautifier'); | ||
|
||
less.tree = Beautifier(require(require('path').dirname(require.resolve('less')) + "/tree")); | ||
|
||
parser = function(options) { | ||
return new less.Parser({ | ||
paths: options.paths || [] | ||
}); | ||
}; | ||
|
||
parse = function(lessSrc, parser, cb) { | ||
return parser.parse(lessSrc, function(e, root) { | ||
return cb(e, root); | ||
}); | ||
}; | ||
|
||
munge = function(lessSrc, parser, cb) { | ||
return parse(lessSrc, parser, function(e, root) { | ||
if (e) { | ||
return cb(e); | ||
} else { | ||
return cb(null, root.toLess("", "production")); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
parse: parse, | ||
munge: munge | ||
}; | ||
|
||
}).call(this); |
Oops, something went wrong.