Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Created February 9, 2012 19:33
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save lancejpollard/1782291 to your computer and use it in GitHub Desktop.
Save lancejpollard/1782291 to your computer and use it in GitHub Desktop.
Convert LESS to Stylus for Twitter Bootstrap
# Quick hack of regular expressions to convert twitter bootstrap from LESS to Stylus
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(/\ *$/g, "") # 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")
names = fs.readdirSync("lib/stylesheets/bootstrap")
for name in names
stylus = less2stylus(fs.readFileSync("lib/stylesheets/bootstrap/#{name}", "utf-8"))
fs.writeFileSync("lib/stylesheets/bootstrap/#{name.replace(/\.less$/, ".styl")}", stylus)
@adamjgrant
Copy link

This looks promising. Might try this out for Kickstrap 2.0 (http://getkickstrap.com)

I want to get away from LESS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment