Java Code Examples for com.samskivert.mustache.Mustache#Compiler

The following examples show how to use com.samskivert.mustache.Mustache#Compiler . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractScalaCodegen.java    From TypeScript-Microservices with MIT License 6 votes vote down vote up
@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
    Mustache.Escaper SCALA = new Mustache.Escaper() {
        @Override public String escape (String text) {
            // Fix included as suggested by akkie in #6393
            // The given text is a reserved word which is escaped by enclosing it with grave accents. If we would
            // escape that with the default Mustache `HTML` escaper, then the escaper would also escape our grave
            // accents. So we remove the grave accents before the escaping and add it back after the escaping.
            if (text.startsWith("`") && text.endsWith("`")) {
                String unescaped =  text.substring(1, text.length() - 1);
                return "`" + Escapers.HTML.escape(unescaped) + "`";
            }

            // All none reserved words will be escaped with the default Mustache `HTML` escaper
            return Escapers.HTML.escape(text);
        }
    };

    return compiler.withEscaper(SCALA);
}
 
Example 2
Source File: DefaultGenerator.java    From TypeScript-Microservices with MIT License 6 votes vote down vote up
private File processTemplateToFile(Map<String, Object> templateData, String templateName, String outputFilename) throws IOException {
    String adjustedOutputFilename = outputFilename.replaceAll("//", "/").replace('/', File.separatorChar);
    if (ignoreProcessor.allowsFile(new File(adjustedOutputFilename))) {
        String templateFile = getFullTemplateFile(config, templateName);
        String template = readTemplate(templateFile);
        Mustache.Compiler compiler = Mustache.compiler();
        compiler = config.processCompiler(compiler);
        Template tmpl = compiler
                .withLoader(new Mustache.TemplateLoader() {
                    @Override
                    public Reader getTemplate(String name) {
                        return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
                    }
                })
                .defaultValue("")
                .compile(template);

        writeToFile(adjustedOutputFilename, tmpl.execute(templateData));
        return new File(adjustedOutputFilename);
    }

    LOGGER.info("Skipped generation of " + adjustedOutputFilename + " due to rule in .swagger-codegen-ignore");
    return null;
}
 
Example 3
Source File: AbstractScalaCodegen.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
    Mustache.Escaper SCALA = new Mustache.Escaper() {
        @Override
        public String escape(String text) {
            // Fix included as suggested by akkie in #6393
            // The given text is a reserved word which is escaped by enclosing it with grave accents. If we would
            // escape that with the default Mustache `HTML` escaper, then the escaper would also escape our grave
            // accents. So we remove the grave accents before the escaping and add it back after the escaping.
            if (text.startsWith("`") && text.endsWith("`")) {
                String unescaped = text.substring(1, text.length() - 1);
                return "`" + Escapers.HTML.escape(unescaped) + "`";
            }

            // All none reserved words will be escaped with the default Mustache `HTML` escaper
            return Escapers.HTML.escape(text);
        }
    };

    return compiler.withEscaper(SCALA);
}
 
Example 4
Source File: SpringMustacheApplication.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public Mustache.Compiler mustacheCompiler(Mustache.TemplateLoader templateLoader, Environment environment) {

    MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector();
    collector.setEnvironment(environment);

    return Mustache.compiler()
      .defaultValue("Some Default Value")
      .withLoader(templateLoader)
      .withCollector(collector);

}
 
Example 5
Source File: TemplateContentCompiler.java    From Bastion with GNU General Public License v3.0 4 votes vote down vote up
private static Mustache.Compiler getCompiler() {
    Mustache.Compiler compiler = Mustache.compiler();
    compiler.escapeHTML(false);
    compiler.withDelims("{{ }}");
    return compiler;
}
 
Example 6
Source File: TemplateTest.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
private String renderTemplate(Map<String, Object> templateData, String templateLocation) throws IOException {
  String templateString = readResourceInClasspath(templateLocation);
  Mustache.Compiler compiler = Mustache.compiler();
  Template tmpl = compiler.compile(templateString);
  return tmpl.execute(templateData);
}
 
Example 7
Source File: TemplateRenderer.java    From intellij-idea-plugin with Apache License 2.0 4 votes vote down vote up
private static Mustache.Compiler mustacheCompiler() {
    return Mustache.compiler().withLoader(mustacheTemplateLoader());
}
 
Example 8
Source File: TemplateRenderer.java    From intellij-idea-plugin with Apache License 2.0 4 votes vote down vote up
public TemplateRenderer(Mustache.Compiler mustache) {
    this.mustache = mustache;
}
 
Example 9
Source File: LambdaTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
protected void test(Mustache.Compiler compiler, String expected,String template, Map<String, Object> ctx) {
    assertEquals(execute(compiler, template, ctx), expected);
}
 
Example 10
Source File: LambdaTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
protected String execute(Mustache.Compiler compiler, String template, Object ctx) {
    return compiler.compile(template).execute(ctx);
}
 
Example 11
Source File: JMustacheViewResolver.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public View getView(String viewName, final ResourceLoader resourceLoader) throws Exception {
	String filename = filename(viewName);

	byte[] bytes = resourceLoader.load(filename);
	if (bytes == null) return null;

	Mustache.TemplateLoader loader = loader(resourceLoader);

	String template = new String(bytes);

	Mustache.Compiler compiler = getViewFactory(resourceLoader);

	Template mustache = compiler.withLoader(loader).compile(template);

	return view(mustache);
}
 
Example 12
Source File: MustacheEngineAdapter.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public Mustache.Compiler getCompiler() {
    return compiler;
}
 
Example 13
Source File: JMustacheViewResolver.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
protected Mustache.Compiler createViewFactory(ResourceLoader templateLoader) {
	return Mustache.compiler().withLoader(loader(templateLoader));
}
 
Example 14
Source File: WebConfig.java    From eds-starter6-jpa with Apache License 2.0 4 votes vote down vote up
@Bean
public Mustache.Compiler mustacheCompiler() {
	return Mustache.compiler();
}
 
Example 15
Source File: CSharpNetCoreClientCodegen.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
    // To avoid unexpected behaviors when options are passed programmatically such as { "supportsAsync": "" }
    return super.processCompiler(compiler).emptyStringIsFalse(true);
}
 
Example 16
Source File: FsharpGiraffeServerCodegen.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
    // To avoid unexpected behaviors when options are passed programmatically such as { "useCollection": "" }
    return super.processCompiler(compiler).emptyStringIsFalse(true);
}
 
Example 17
Source File: CSharpClientCodegen.java    From TypeScript-Microservices with MIT License 4 votes vote down vote up
@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
    // To avoid unexpected behaviors when options are passed programmatically such as { "supportsAsync": "" }
    return super.processCompiler(compiler).emptyStringIsFalse(true);
}
 
Example 18
Source File: AbstractAdaCodegen.java    From openapi-generator with Apache License 2.0 3 votes vote down vote up
/**
 * Override the Mustache compiler configuration.
 * <p>
 * We don't want to have special characters escaped
 *
 * @param compiler the compiler.
 * @return the compiler to use.
 */
@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
    compiler = super.processCompiler(compiler).emptyStringIsFalse(true);

    return compiler.withEscaper(Escapers.NONE);
}
 
Example 19
Source File: AbstractAdaCodegen.java    From TypeScript-Microservices with MIT License 3 votes vote down vote up
/**
 * Override the Mustache compiler configuration.
 *
 * We don't want to have special characters escaped
 *
 * @param compiler the compiler.
 * @return the compiler to use.
 */
@Override
public Mustache.Compiler processCompiler(Mustache.Compiler compiler) {
    compiler = super.processCompiler(compiler).emptyStringIsFalse(true);

    return compiler.withEscaper(Escapers.NONE);
}
 
Example 20
Source File: MustacheConfig.java    From mojito with Apache License 2.0 2 votes vote down vote up
/**
 * Configure Mustache compiler to return empty string for null values.
 *
 * @return customized Mustache compiler
 */
@Bean
Mustache.Compiler getMustache() {
    return Mustache.compiler().nullValue("");
}