Search in sources :

Example 1 with PebbleTemplateImpl

use of com.mitchellbosecke.pebble.template.PebbleTemplateImpl in project symja_android_library by axkr.

the class IOFunctions method templateCompile.

/**
 * Compile the template into an object hierarchy representation for the
 * <a href="https://github.com/PebbleTemplates/pebble">Pebble template engine</a>.
 *
 * @param templateStr
 * @return
 */
private static PebbleTemplate templateCompile(String templateStr) {
    List<RenderableNode> nodes = new ArrayList<>();
    final int length = templateStr.length();
    int currentPosition = 0;
    int counter = 1;
    int lastLineNumber = 0;
    int lineNumber = 0;
    int lastPosition = 0;
    while (currentPosition < length) {
        char ch = templateStr.charAt(currentPosition++);
        if (ch == '\n') {
            lineNumber++;
            continue;
        }
        if (ch == '`') {
            if (lastPosition < currentPosition - 1) {
                nodes.add(new TextNode(templateStr.substring(lastPosition, currentPosition - 1), lastLineNumber));
                lastPosition = currentPosition;
                lastLineNumber = lineNumber;
            }
            int j = currentPosition;
            StringBuilder nameBuf = new StringBuilder();
            while (j < length) {
                char nextCh = templateStr.charAt(j++);
                if (nextCh == '`') {
                    if (j == currentPosition + 1) {
                        nameBuf.append(counter++);
                    }
                    currentPosition = j;
                    break;
                }
                nameBuf.append(nextCh);
            }
            Expression<?> expression = new ContextVariableExpression(nameBuf.toString(), lineNumber);
            nodes.add(new PrintNode(expression, lineNumber));
            lastPosition = currentPosition;
            lastLineNumber = lineNumber;
        }
    }
    if (lastPosition < length) {
        nodes.add(new TextNode(templateStr.substring(lastPosition, length), lineNumber));
        lastPosition = currentPosition;
    }
    BodyNode body = new BodyNode(0, nodes);
    RootNode rootNode = new RootNode(body);
    return new PebbleTemplateImpl(PEBBLE_ENGINE, rootNode, templateStr);
}
Also used : RootNode(com.mitchellbosecke.pebble.node.RootNode) ContextVariableExpression(com.mitchellbosecke.pebble.node.expression.ContextVariableExpression) PebbleTemplateImpl(com.mitchellbosecke.pebble.template.PebbleTemplateImpl) ArrayList(java.util.ArrayList) TextNode(com.mitchellbosecke.pebble.node.TextNode) BodyNode(com.mitchellbosecke.pebble.node.BodyNode) RenderableNode(com.mitchellbosecke.pebble.node.RenderableNode) PrintNode(com.mitchellbosecke.pebble.node.PrintNode)

Example 2 with PebbleTemplateImpl

use of com.mitchellbosecke.pebble.template.PebbleTemplateImpl in project Orchid by JavaEden.

the class PebbleCompiler method compile.

@Override
public void compile(OutputStream os, @Nullable OrchidResource resource, String extension, String input, Map<String, Object> data) {
    try {
        PebbleTemplateLoader loader = new PebbleTemplateLoader(contextProvider.get(), contextProvider.get().getTheme());
        PebbleEngine engine = new PebbleEngine.Builder().loader(loader).executorService(executor).extension(extensionArray).methodAccessValidator(new NoOpMethodAccessValidator()).newLineTrimming(false).tagCache(tagCache).templateCache(templateCache).build();
        engine.getExtensionRegistry().getAttributeResolver().add(new GetMethodAttributeResolver());
        LexerImpl lexer = new LexerImpl(engine.getSyntax(), engine.getExtensionRegistry().getUnaryOperators().values(), engine.getExtensionRegistry().getBinaryOperators().values());
        TokenStream tokenStream = lexer.tokenize(new StringReader(input), "");
        Parser parser = new ParserImpl(engine.getExtensionRegistry().getUnaryOperators(), engine.getExtensionRegistry().getBinaryOperators(), engine.getExtensionRegistry().getTokenParsers(), new ParserOptions());
        RootNode root = parser.parse(tokenStream);
        PebbleTemplateImpl compiledTemplate = new PebbleTemplateImpl(engine, root, "");
        for (NodeVisitorFactory visitorFactory : engine.getExtensionRegistry().getNodeVisitors()) {
            visitorFactory.createVisitor(compiledTemplate).visit(root);
        }
        ByteArrayOutputStream os1 = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(os1, StandardCharsets.UTF_8);
        compiledTemplate.evaluate(writer, data);
        writer.close();
        os.write(os1.toByteArray());
    } catch (PebbleException e) {
        OrchidExtensionsKt.logSyntaxError(input, extension, e.getLineNumber(), 0, e.getMessage(), e.getCause());
    } catch (Exception e) {
        Clog.e("Error rendering Pebble template (see template source below)", e);
        Clog.e(input);
        contextProvider.get().diagnosisMessage(() -> ExceptionUtils.getStackTrace(e));
    }
}
Also used : RootNode(com.mitchellbosecke.pebble.node.RootNode) TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream) NodeVisitorFactory(com.mitchellbosecke.pebble.extension.NodeVisitorFactory) ParserOptions(com.mitchellbosecke.pebble.parser.ParserOptions) PebbleTemplateImpl(com.mitchellbosecke.pebble.template.PebbleTemplateImpl) NoOpMethodAccessValidator(com.mitchellbosecke.pebble.attributes.methodaccess.NoOpMethodAccessValidator) ParserImpl(com.mitchellbosecke.pebble.parser.ParserImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PebbleException(com.mitchellbosecke.pebble.error.PebbleException) PebbleEngine(com.mitchellbosecke.pebble.PebbleEngine) Parser(com.mitchellbosecke.pebble.parser.Parser) PebbleException(com.mitchellbosecke.pebble.error.PebbleException) StringReader(java.io.StringReader) LexerImpl(com.mitchellbosecke.pebble.lexer.LexerImpl) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

RootNode (com.mitchellbosecke.pebble.node.RootNode)2 PebbleTemplateImpl (com.mitchellbosecke.pebble.template.PebbleTemplateImpl)2 PebbleEngine (com.mitchellbosecke.pebble.PebbleEngine)1 NoOpMethodAccessValidator (com.mitchellbosecke.pebble.attributes.methodaccess.NoOpMethodAccessValidator)1 PebbleException (com.mitchellbosecke.pebble.error.PebbleException)1 NodeVisitorFactory (com.mitchellbosecke.pebble.extension.NodeVisitorFactory)1 LexerImpl (com.mitchellbosecke.pebble.lexer.LexerImpl)1 TokenStream (com.mitchellbosecke.pebble.lexer.TokenStream)1 BodyNode (com.mitchellbosecke.pebble.node.BodyNode)1 PrintNode (com.mitchellbosecke.pebble.node.PrintNode)1 RenderableNode (com.mitchellbosecke.pebble.node.RenderableNode)1 TextNode (com.mitchellbosecke.pebble.node.TextNode)1 ContextVariableExpression (com.mitchellbosecke.pebble.node.expression.ContextVariableExpression)1 Parser (com.mitchellbosecke.pebble.parser.Parser)1 ParserImpl (com.mitchellbosecke.pebble.parser.ParserImpl)1 ParserOptions (com.mitchellbosecke.pebble.parser.ParserOptions)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1