malkomalko
4/19/2012 - 3:16 PM

Convert LESS to Stylus for Twitter Bootstrap

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)