-
-
Save LabiKyo/3848563 to your computer and use it in GitHub Desktop.
Convert Less to Stylus for general purpose
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
less2stylus = (string) -> | |
string = string | |
.replace(/^(\ *)(.+)\ +\{\ *\n?\ */mg, "$1$2\n$1 ") # remove opening brackets | |
.replace(/^(\ *)([^\ \n]+)\ +\{\ *\n?\ */mg, "$1$2\n$1 ") # remove opening brackets | |
.replace(/\ *\{\ *\n*/g, "\n") # remove opening brackets again (some random cases I'm too lazy to think through) | |
.replace(/\ *\}\ *\n*/g, "\n") # remove closing brackets | |
.replace(/\;\ *?$/gm, "") # remove semicolons | |
.replace(/@(\w+):(\ *)\ /g, (_, $1, $2) -> # replace @variable: with $variable = | |
"$#{$1}#{$2} = " | |
) | |
.replace(/\@(\w+)/g, (_, $1) -> (if $1 == "import" then _ else "$#{$1}")) | |
.replace(/\.([\w-]+)\(/g, "$1(") # replace mixins from .border-radius(4px) to border-radius(4px) | |
.replace(/,\ */g, ", ") # make all commas have 1 space after them | |
.replace(/\.less/g, ".styl") | |
.replace(/(\ *)(.+)>\ *([\w-]+)\(/g, "$1$2>\n$1 $3(") | |
# need to make sure things like -webkit-background-size are left-aligned | |
# need to handle expressions | |
.replace(/\/\/.*$/mg, "") # remove inline comments | |
.replace(/\n+/g, "\n") # remove multiple EOL | |
.replace(/\ +$/mg, "") # remove trailing whitespace | |
lines = string.split("\n") | |
indent = 0 | |
for line, i in lines | |
if line.match(/^[^\/]?\ *[\w-\*]+: /) # property name | |
lines[i] = line.replace(/^\ */, Array(indent + 1).join(" ")) | |
else if line.match(/^\ +[\w-]+\([^\$\)]/) # mixin | |
line | |
else if !line.match(/\/{2}/) | |
indent = line.match(/^(\ *)/)[1].length + 2 | |
if line.match(/e\(\%\(/) | |
lines[i] = "#{Array(indent + 1).join(" ")}// #{line}" | |
lines.join("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment