Search in sources :

Example 1 with BodyNode

use of com.mitchellbosecke.pebble.node.BodyNode 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 BodyNode

use of com.mitchellbosecke.pebble.node.BodyNode in project Orchid by JavaEden.

the class BaseTagParser method parseBody.

protected Expression<?> parseBody(List<Expression<?>> filterInvocationExpressions, String bodyTagName, TokenStream stream, Parser parser) throws ParserException {
    BodyNode body = parser.subparse(token -> token.test(Token.Type.NAME, "end" + bodyTagName));
    stream.next();
    stream.expect(Token.Type.EXECUTE_END);
    Expression<?> lastExpression = new RenderableNodeExpression(body, stream.current().getLineNumber());
    for (Expression<?> filterInvocationExpression : filterInvocationExpressions) {
        FilterExpression filterExpression = new FilterExpression();
        filterExpression.setRight(filterInvocationExpression);
        filterExpression.setLeft(lastExpression);
        lastExpression = filterExpression;
    }
    return lastExpression;
}
Also used : BodyNode(com.mitchellbosecke.pebble.node.BodyNode) RenderableNodeExpression(com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression) FilterExpression(com.mitchellbosecke.pebble.node.expression.FilterExpression)

Aggregations

BodyNode (com.mitchellbosecke.pebble.node.BodyNode)2 PrintNode (com.mitchellbosecke.pebble.node.PrintNode)1 RenderableNode (com.mitchellbosecke.pebble.node.RenderableNode)1 RootNode (com.mitchellbosecke.pebble.node.RootNode)1 TextNode (com.mitchellbosecke.pebble.node.TextNode)1 ContextVariableExpression (com.mitchellbosecke.pebble.node.expression.ContextVariableExpression)1 FilterExpression (com.mitchellbosecke.pebble.node.expression.FilterExpression)1 RenderableNodeExpression (com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression)1 PebbleTemplateImpl (com.mitchellbosecke.pebble.template.PebbleTemplateImpl)1 ArrayList (java.util.ArrayList)1