Skip to content

Instantly share code, notes, and snippets.

@zaydek-owl
Last active August 21, 2017 19:58
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 zaydek-owl/20584728fc5e6cdd66628efb33279ad3 to your computer and use it in GitHub Desktop.
Save zaydek-owl/20584728fc5e6cdd66628efb33279ad3 to your computer and use it in GitHub Desktop.
not for the faint of heart
import std.file, std.regex, std.stdio, std.string;
void main(string[] args)
{
if (args.length != 2)
return ;
auto text = readText(args[1]);
/+
opening brace setup
+/
enum opening_brace_pattern = `((?:%-(%s|%))[^\n]*)\n\t*\{`;
enum opening_brace_with_newline_keywords = ["func", "for" ];
enum opening_brace_without_newline_keywords = ["if" , "else"];
enum opening_brace_with_newline_Regex = regex(format(opening_brace_pattern, opening_brace_with_newline_keywords ));
enum opening_brace_without_newline_Regex = regex(format(opening_brace_pattern, opening_brace_without_newline_keywords));
/+
opening brace regex
+/
text = replaceAll(text, opening_brace_with_newline_Regex, "$1 {\n");
text = replaceAll(text, opening_brace_without_newline_Regex, "$1 { ");
/+
one line setup
+/
enum one_line_if_regex = regex(`(if[^{\n]*)\n([^\n]+)(\n{2}\t+)else`);
enum one_line_else_regex = regex(`(else[^{\n]*)\n([^\n]+)\n(\t+)\}` );
/+
one line regex
+/
text = replaceAll(text, one_line_if_regex, "$1 {\n$2\n$3} else" );
text = replaceAll(text, one_line_else_regex, "$1 {\n$2\n$3\t}\n$3}");
write(text);
File(format("%s2.go", args[1][0 .. $ - 3]), "w").write(text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment