Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created March 3, 2014 02:50
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 james2doyle/9317691 to your computer and use it in GitHub Desktop.
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.
// 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