Search in sources :

Example 6 with SoyPrintDirective

use of com.google.template.soy.shared.restricted.SoyPrintDirective in project closure-templates by google.

the class PluginResolver method lookupPrintDirective.

/**
 * Returns a print directive with the given name and arity.
 *
 * <p>An error will be reported according to the current {@link Mode} and a placeholder function
 * will be returned if it cannot be found.
 */
public SoyPrintDirective lookupPrintDirective(String name, int numArgs, SourceLocation location) {
    SoyPrintDirective soyPrintDirective = printDirectives.get(name);
    if (soyPrintDirective == null) {
        if (mode == Mode.REQUIRE_DEFINITIONS || mode == Mode.ALLOW_UNDEFINED_FUNCTIONS_FOR_V1_SUPPORT) {
            reporter.report(location, UNKNOWN_PLUGIN, "print directive", name, SoyErrors.getDidYouMeanMessage(printDirectives.keySet(), name));
        }
        soyPrintDirective = createPlaceholderPrintDirective(name, numArgs);
    }
    checkNumArgs("print directive", soyPrintDirective.getValidArgsSizes(), numArgs, location);
    warnIfDeprecated(name, soyPrintDirective, location);
    return soyPrintDirective;
}
Also used : SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective)

Example 7 with SoyPrintDirective

use of com.google.template.soy.shared.restricted.SoyPrintDirective in project closure-templates by google.

the class SoyFileSetParserBuilder method addPrintDirectives.

public SoyFileSetParserBuilder addPrintDirectives(Iterable<? extends SoyPrintDirective> soyPrintDirectives) {
    Map<String, SoyPrintDirective> directives = new LinkedHashMap<>();
    directives.putAll(soyPrintDirectiveMap);
    for (SoyPrintDirective printDirective : soyPrintDirectives) {
        directives.put(printDirective.getName(), printDirective);
    }
    this.soyPrintDirectiveMap = ImmutableMap.copyOf(directives);
    return this;
}
Also used : SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with SoyPrintDirective

use of com.google.template.soy.shared.restricted.SoyPrintDirective in project closure-templates by google.

the class SharedModuleTest method testBuiltinPluginsSupportAllBackends.

@Test
public void testBuiltinPluginsSupportAllBackends() throws Exception {
    for (SoyPrintDirective directive : injector.getInstance(new Key<Set<SoyPrintDirective>>() {
    })) {
        assertThat(directive).isInstanceOf(SoyJsSrcPrintDirective.class);
        assertThat(directive).isInstanceOf(SoyJavaPrintDirective.class);
        assertThat(directive).isInstanceOf(SoyJbcSrcPrintDirective.class);
        if (!PYSRC_DIRECTIVE_BLACKLIST.contains(directive.getName())) {
            assertThat(directive).isInstanceOf(SoyPySrcPrintDirective.class);
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) Test(org.junit.Test)

Example 9 with SoyPrintDirective

use of com.google.template.soy.shared.restricted.SoyPrintDirective in project closure-templates by google.

the class GenJsExprsVisitor method visitPrintNode.

/**
 * Example:
 * <pre>
 *   {$boo.foo}
 *   {$goo.moo + 5}
 * </pre>
 * might generate
 * <pre>
 *   opt_data.boo.foo
 *   gooData4.moo + 5
 * </pre>
 */
@Override
protected void visitPrintNode(PrintNode node) {
    CodeChunk.WithValue expr = translateExpr(node.getExpr());
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Get directive.
        SoyPrintDirective directive = directiveNode.getPrintDirective();
        if (!(directive instanceof SoyJsSrcPrintDirective)) {
            errorReporter.report(node.getSourceLocation(), UNKNOWN_SOY_JS_SRC_PRINT_DIRECTIVE, directiveNode.getName());
            return;
        }
        // Get directive args.
        List<ExprRootNode> argNodes = directiveNode.getArgs();
        // Convert args to CodeChunks.
        List<CodeChunk.WithValue> argChunks = new ArrayList<>(argNodes.size());
        for (ExprRootNode argNode : argNodes) {
            argChunks.add(translateExpr(argNode));
        }
        // Apply directive.
        expr = SoyJsPluginUtils.applyDirective(translationContext.codeGenerator(), expr, (SoyJsSrcPrintDirective) directive, argChunks);
    }
    chunks.add(expr);
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) ArrayList(java.util.ArrayList) SoyJsSrcPrintDirective(com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 10 with SoyPrintDirective

use of com.google.template.soy.shared.restricted.SoyPrintDirective in project closure-templates by google.

the class GenJsCodeVisitorAssistantForMsgs method generateMsgGroupVariable.

/**
 * Returns a code chunk representing a translated variable.
 *
 * <p>Example:
 *
 * <pre>
 *   {msg desc="Link to help content."}Learn more{/msg}
 *   {msg desc="Tells user how to access a product." hidden="true"}
 *     Click &lt;a href="}{$url}"&gt;here&lt;/a&gt; to access {$productName}.
 *   {/msg}
 * </pre>
 *
 * might return the following code chunk:
 *
 * <pre>
 *   /** @desc Link to help content. *{@literal /}
 *   var MSG_UNNAMED_9 = goog.getMsg('Learn more');
 *   var msg_s9 = MSG_UNNAMED_9;
 *   /** @desc Tells user how to access a product.
 *    *  @hidden *{@literal /}
 *   var MSG_UNNAMED_10 = goog.getMsg(
 *       'Click {$startLink}here{$endLink} to access {$productName}.',
 *       {startLink: '&lt;a href="' + opt_data.url + '"&gt;',
 *        endLink: '&lt;/a&gt;',
 *        productName: opt_data.productName});
 * </pre>
 */
public CodeChunk.WithValue generateMsgGroupVariable(MsgFallbackGroupNode node) {
    String tmpVarName = translationContext.nameGenerator().generateName("msg_s");
    CodeChunk.WithValue msg;
    if (node.numChildren() == 1) {
        translationContext.soyToJsVariableMappings().setIsPrimaryMsgInUse(node, CodeChunk.WithValue.LITERAL_TRUE);
        msg = generateSingleMsgVariable(node.getChild(0), tmpVarName);
    } else {
        // has fallbackmsg children
        msg = generateMsgGroupVariable(node, tmpVarName);
    }
    // handle escaping
    for (SoyPrintDirective printDirective : node.getEscapingDirectives()) {
        msg = SoyJsPluginUtils.applyDirective(translationContext.codeGenerator(), msg, (SoyJsSrcPrintDirective) printDirective, /* args= */
        ImmutableList.<CodeChunk.WithValue>of());
    }
    return msg;
}
Also used : CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) SoyJsSrcPrintDirective(com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)

Aggregations

SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)18 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)4 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)3 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)3 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)3 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)3 PrintDirectiveNode (com.google.template.soy.soytree.PrintDirectiveNode)3 ArrayList (java.util.ArrayList)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 SoyValue (com.google.template.soy.data.SoyValue)2 Expression (com.google.template.soy.jbcsrc.restricted.Expression)2 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 Statement (com.google.template.soy.jbcsrc.restricted.Statement)2 LinkedHashMap (java.util.LinkedHashMap)2 Set (java.util.Set)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)1 UniqueNameGenerator (com.google.template.soy.base.internal.UniqueNameGenerator)1