Skip to content

Instantly share code, notes, and snippets.

@agentgt
Created January 28, 2016 20:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agentgt/1bc5f14e62bce11e2ceb to your computer and use it in GitHub Desktop.
Save agentgt/1bc5f14e62bce11e2ceb to your computer and use it in GitHub Desktop.
A Groovy mustache(1) replacement
#!/usr/bin/env groovy
@Grab(group='com.github.jknack', module='handlebars', version='4.0.3')
@Grab(group='org.yaml', module='snakeyaml', version='1.16')
@Grab(group='org.slf4j', module='slf4j-simple', version='1.7.14')
import org.yaml.snakeyaml.Yaml;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
if (args.length == 2) {
String i = args[0];
String t = args[1];
if (i == t) return usage();
Yaml yaml = new Yaml()
InputStream input = i == '-' ? System.in : new File(args[0]).newInputStream();
String file = t == '-' ? System.in : new File(args[1]).text;
Object o = yaml.load(input)
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline(file);
Writer w = System.out.newWriter();
template.apply(o, w);
w.flush();
}
else {
return usage();
}
def usage() {
println "Usage: Mustache.groovy INPUT FILE"
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment