Skip to content

Instantly share code, notes, and snippets.

@skw
Forked from lancejpollard/less2stylus.coffee
Last active December 17, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skw/5659478 to your computer and use it in GitHub Desktop.
Save skw/5659478 to your computer and use it in GitHub Desktop.
made some changes to make it work for me
# Quick hack of regular expressions to convert twitter bootstrap from LESS to Stylus
fs = require 'fs'
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("./less")
for name in names
path = "./less/#{name}"
if fs.lstatSync(path).isDirectory()
path = fs.readFileSync path
else
return "error"
stylus = less2stylus path, 'utf-8'
fs.writeFileSync("./stylus/#{name.replace(/\.less$/, ".styl")}", stylus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment