Created
March 3, 2014 02:50
-
-
Save james2doyle/9317691 to your computer and use it in GitHub Desktop.
A small script written in D that replaces LESS style variables in a file.
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
// compile with: gdmd -O -inline -release -noboundscheck replace-css-vars.d | |
// usage: ./replace-css-vars text.less | |
import std.stdio, std.file, std.regex, std.string; | |
string FILE; | |
// empty a line | |
string clearLine(string content, string subject) { | |
auto com = regex(subject, "g"); | |
auto newcontent = replaceAll(content, com, ""); | |
return newcontent; | |
} | |
// replace the variables with their values | |
string replaceVars(string content, string[] parts) { | |
auto com = regex(parts[0], "g"); | |
auto newcontent = replaceAll(content, com, parts[1]); | |
return newcontent; | |
} | |
string findVars(Captures!(string) contents) { | |
// remove the variable declaration | |
FILE = clearLine(FILE, contents.hit); | |
// split the capture by the : | |
auto str = std.string.split(contents.captures[0], ':'); | |
// trim the trailing semi-colon on the declaration | |
str[1] = chomp(strip(str[1]), ";"); | |
//writeln(str); | |
FILE = replaceVars(FILE, str); | |
//return std.string.toUpper(contents.hit); | |
return FILE; | |
} | |
string readFile(char[] filename) { | |
if (exists(filename) != 0) { | |
// ulong size = getSize(filename); | |
return cast(string) read(filename); | |
} else { | |
writefln("File not found"); | |
assert(0); | |
} | |
} | |
int main(char[][] args) { | |
// first argument is the file | |
FILE = readFile(args[1]); | |
// main variable swap function | |
enum reg = ctRegex!(`^(?:@)(?:.*)(?:\:)(?: )?(?:.*)(?:;)$`, "m"); | |
auto s = replaceAll!(findVars)(FILE, reg); | |
FILE = strip(FILE); | |
writeln(FILE); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment