Search in sources :

Example 1 with Expression

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

the class BaseTagParser method parseParams.

protected Map<String, Expression<?>> parseParams(String[] parameters, boolean enforceParamMap, Class<? extends OptionsHolder> paramsClass, TokenStream stream, Parser parser) throws ParserException {
    // Get list of available parameter names
    OptionsExtractor extractor = contextProvider.get().resolve(OptionsExtractor.class);
    List<String> remainingParameters = new ArrayList<>(extractor.getOptionNames(paramsClass));
    // parameter expressions will be added here
    Map<String, Expression<?>> paramExpressionMap = new HashMap<>();
    if (!isParameterEnd(stream)) {
        if (!EdenUtils.isEmpty(parameters) && !stream.current().test(Token.Type.NAME)) {
            // parse the parameters sequentially
            int i = 0;
            while (i < parameters.length && !isParameterEnd(stream) && !stream.current().test(Token.Type.NAME)) {
                Expression<?> parsedExpression = parser.getExpressionParser().parseExpression();
                paramExpressionMap.put(parameters[i], parsedExpression);
                i++;
            }
        }
        if (stream.current().test(Token.Type.NAME)) {
            // parse parameters as map of key=value pairs
            while (!isParameterEnd(stream)) {
                Token paramNameToken = stream.expect(Token.Type.NAME);
                final Optional<String> foundKey;
                if (enforceParamMap) {
                    foundKey = remainingParameters.stream().filter(key -> key.equalsIgnoreCase(paramNameToken.getValue())).findAny();
                } else {
                    foundKey = Optional.of(paramNameToken.getValue());
                }
                if (foundKey.isPresent()) {
                    String paramKey = foundKey.get();
                    if (enforceParamMap) {
                        remainingParameters.remove(paramKey);
                    }
                    stream.expect(Token.Type.PUNCTUATION, "=");
                    Expression<?> parsedExpression = parser.getExpressionParser().parseExpression();
                    paramExpressionMap.put(paramKey, parsedExpression);
                } else {
                    throw new ParserException(null, Clog.format("Could not parse parameter {}.", paramNameToken.getValue()), stream.current().getLineNumber(), "");
                }
            }
        }
    }
    return paramExpressionMap;
}
Also used : ParserException(com.mitchellbosecke.pebble.error.ParserException) RenderableNodeExpression(com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression) Expression(com.mitchellbosecke.pebble.node.expression.Expression) FilterExpression(com.mitchellbosecke.pebble.node.expression.FilterExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Token(com.mitchellbosecke.pebble.lexer.Token) OptionsExtractor(com.eden.orchid.api.options.OptionsExtractor)

Example 2 with Expression

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

the class ContentTagParser method parse.

public RenderableNode parse(Token token, Parser parser) throws ParserException {
    TokenStream stream = parser.getStream();
    int lineNumber = token.getLineNumber();
    // skip over the tag name token
    stream.next();
    // parameter expressions will be added here
    paramExpressionMap = parseParams(parameters, true, tagClass, stream, parser);
    List<Expression<?>> bodyFilters = parseBodyFilters(stream, parser);
    stream.expect(Token.Type.EXECUTE_END);
    tagBodyExpression = parseBody(bodyFilters, name, stream, parser);
    return new PebbleWrapperTemplateTag.TemplateTagNode(lineNumber, this);
}
Also used : TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream) Expression(com.mitchellbosecke.pebble.node.expression.Expression)

Example 3 with Expression

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

the class NonDynamicTabbedTagParser method parse.

public RenderableNode parse(Token token, Parser parser) throws ParserException {
    TokenStream stream = parser.getStream();
    int lineNumber = token.getLineNumber();
    tagBodyParams = new LinkedHashMap<>();
    tagBodyExpressions = new LinkedHashMap<>();
    // get Tag params
    paramExpressionMap = parseParams(tagParameters, true, tagClass, stream, parser);
    // get Tag filters, which are applied to all tabs
    bodyFilters = parseBodyFilters(stream, parser);
    stream.expect(Token.Type.EXECUTE_END);
    // parse tabs
    while (true) {
        // optionally allow line-breaks between tabs
        if (stream.current().test(Token.Type.TEXT)) {
            // do not allow text to be outside of a tab
            Token textToken = stream.expect(Token.Type.TEXT);
            if (!EdenUtils.isEmpty(textToken.getValue())) {
                throw new ParserException(null, "Error parsing " + name + " tabbed tag: Text must be included within a tab.", lineNumber, "");
            }
        }
        // start parsing tab
        stream.expect(Token.Type.EXECUTE_START);
        // this is actually the final Tag's closing tag, so exit the loop
        if (stream.current().test(Token.Type.NAME, "end" + name))
            break;
        // get tab name
        Token tabNameToken = stream.expect(Token.Type.NAME);
        String tabName = tabNameToken.getValue();
        // make sure we only have one of each tab key
        if (tagBodyExpressions.containsKey(tabName)) {
            throw new ParserException(null, "Error parsing " + name + " tabbed tag: Tab " + tabName + " already used.", lineNumber, "");
        }
        // get tab params
        Map<String, Expression<?>> tabParams = parseParams(tabParameters, true, tabClass, stream, parser);
        tagBodyParams.put(tabName, tabParams);
        // get tab filters
        List<Expression<?>> tabBodyFilters = parseBodyFilters(stream, parser);
        List<Expression<?>> fullTabFilters = Stream.concat(tabBodyFilters.stream(), bodyFilters.stream()).collect(Collectors.toList());
        // end opening tab
        stream.expect(Token.Type.EXECUTE_END);
        // subcompile until the end of the tab, which closes this tab
        Expression<?> tabBody = parseBody(fullTabFilters, tabName, stream, parser);
        tagBodyExpressions.put(tabName, tabBody);
    }
    stream.expect(Token.Type.NAME, "end" + name);
    stream.expect(Token.Type.EXECUTE_END);
    return new PebbleWrapperTemplateTag.TemplateTagNode(lineNumber, this);
}
Also used : ParserException(com.mitchellbosecke.pebble.error.ParserException) TokenStream(com.mitchellbosecke.pebble.lexer.TokenStream) Expression(com.mitchellbosecke.pebble.node.expression.Expression) Token(com.mitchellbosecke.pebble.lexer.Token)

Example 4 with Expression

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

the class NonDynamicTabbedTagParser method render.

@Override
public void render(PebbleTemplateImpl self, Writer writer, EvaluationContextImpl context) throws IOException {
    OrchidContext orchidContext = contextProvider.get();
    // create a new tag
    TemplateTag freshTag = orchidContext.resolve(tagClass);
    // evaluate its own params and populate the main Tag class with them
    Map<String, Object> evaluatedParamExpressionMap = evaluateParams(paramExpressionMap, self, context);
    Object pageVar = context.getVariable("page");
    final OrchidPage actualPage;
    if (pageVar instanceof OrchidPage) {
        actualPage = (OrchidPage) pageVar;
    } else {
        actualPage = null;
    }
    freshTag.extractOptions(orchidContext, evaluatedParamExpressionMap);
    // populate the content of each tab
    for (Map.Entry<String, Expression<?>> tagBodyExpression : tagBodyExpressions.entrySet()) {
        // get the tab body content
        String key = tagBodyExpression.getKey();
        String bodyContent = StringUtils.toString(tagBodyExpression.getValue().evaluate(self, context)).trim();
        // Get a new Tab instance, evaluate that tab's options, and then populate the Tab instance with them
        TemplateTag.Tab tab = freshTag.getNewTab(key, bodyContent);
        Map<String, Object> tabParams = evaluateParams(tagBodyParams.get(key), self, context);
        tab.extractOptions(orchidContext, tabParams);
        // add the tab to the Tag
        freshTag.setContent(key, tab);
    }
    freshTag.onRender(orchidContext, actualPage);
    if (freshTag.rendersContent()) {
        writer.append(freshTag.renderContent(orchidContext, actualPage));
    }
}
Also used : OrchidPage(com.eden.orchid.api.theme.pages.OrchidPage) OrchidContext(com.eden.orchid.api.OrchidContext) TemplateTag(com.eden.orchid.api.compilers.TemplateTag) PebbleWrapperTemplateTag(com.eden.orchid.impl.compilers.pebble.PebbleWrapperTemplateTag) Expression(com.mitchellbosecke.pebble.node.expression.Expression) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Expression (com.mitchellbosecke.pebble.node.expression.Expression)4 ParserException (com.mitchellbosecke.pebble.error.ParserException)2 Token (com.mitchellbosecke.pebble.lexer.Token)2 TokenStream (com.mitchellbosecke.pebble.lexer.TokenStream)2 OrchidContext (com.eden.orchid.api.OrchidContext)1 TemplateTag (com.eden.orchid.api.compilers.TemplateTag)1 OptionsExtractor (com.eden.orchid.api.options.OptionsExtractor)1 OrchidPage (com.eden.orchid.api.theme.pages.OrchidPage)1 PebbleWrapperTemplateTag (com.eden.orchid.impl.compilers.pebble.PebbleWrapperTemplateTag)1 FilterExpression (com.mitchellbosecke.pebble.node.expression.FilterExpression)1 RenderableNodeExpression (com.mitchellbosecke.pebble.node.expression.RenderableNodeExpression)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1