Search in sources :

Example 1 with CombineConsecutiveRawTextNodesPass

use of com.google.template.soy.passes.CombineConsecutiveRawTextNodesPass in project closure-templates by google.

the class JsSrcMain method genJsSrc.

/**
 * Generates JS source code given a Soy parse tree, an options object, and an optional bundle of
 * translated messages.
 *
 * @param soyTree The Soy parse tree to generate JS source code for.
 * @param templateRegistry The template registry that contains all the template information.
 * @param jsSrcOptions The compilation options relevant to this backend.
 * @param msgBundle The bundle of translated messages, or null to use the messages from the Soy
 *     source.
 * @param errorReporter The Soy error reporter that collects errors during code generation.
 * @return A list of strings where each string represents the JS source code that belongs in one
 *     JS file. The generated JS files correspond one-to-one to the original Soy source files.
 */
public List<String> genJsSrc(SoyFileSetNode soyTree, TemplateRegistry templateRegistry, SoyJsSrcOptions jsSrcOptions, @Nullable SoyMsgBundle msgBundle, ErrorReporter errorReporter) {
    // VeLogInstrumentationVisitor add html attributes for {velog} commands and also run desugaring
    // pass since code generator does not understand html nodes (yet).
    new VeLogInstrumentationVisitor(templateRegistry).exec(soyTree);
    try (GuiceSimpleScope.InScope inScope = apiCallScope.enter()) {
        // Seed the scoped parameters.
        BidiGlobalDir bidiGlobalDir = SoyBidiUtils.decodeBidiGlobalDirFromJsOptions(jsSrcOptions.getBidiGlobalDir(), jsSrcOptions.getUseGoogIsRtlForBidiGlobalDir());
        ApiCallScopeUtils.seedSharedParams(inScope, msgBundle, bidiGlobalDir);
        // Replace MsgNodes.
        if (jsSrcOptions.shouldGenerateGoogMsgDefs()) {
            Preconditions.checkState(bidiGlobalDir != null, "If enabling shouldGenerateGoogMsgDefs, must also set bidi global directionality.");
        } else {
            Preconditions.checkState(bidiGlobalDir == null || bidiGlobalDir.isStaticValue(), "If using bidiGlobalIsRtlCodeSnippet, must also enable shouldGenerateGoogMsgDefs.");
            new InsertMsgsVisitor(msgBundle, errorReporter).insertMsgs(soyTree);
        }
        // Combine raw text nodes before codegen.
        new CombineConsecutiveRawTextNodesPass().run(soyTree);
        return createVisitor(jsSrcOptions, typeRegistry).gen(soyTree, templateRegistry, errorReporter);
    }
}
Also used : BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) GuiceSimpleScope(com.google.template.soy.shared.internal.GuiceSimpleScope) InsertMsgsVisitor(com.google.template.soy.msgs.internal.InsertMsgsVisitor) CombineConsecutiveRawTextNodesPass(com.google.template.soy.passes.CombineConsecutiveRawTextNodesPass)

Example 2 with CombineConsecutiveRawTextNodesPass

use of com.google.template.soy.passes.CombineConsecutiveRawTextNodesPass in project closure-templates by google.

the class IncrementalDomSrcMain method genJsSrc.

/**
 * Generates Incremental DOM JS source code given a Soy parse tree, an options object, and an
 * optional bundle of translated messages.
 *
 * @param soyTree The Soy parse tree to generate JS source code for.
 * @param registry The template registry that contains all the template information.
 * @param options The compilation options relevant to this backend.
 * @param errorReporter The Soy error reporter that collects errors during code generation.
 * @return A list of strings where each string represents the JS source code that belongs in one
 *     JS file. The generated JS files correspond one-to-one to the original Soy source files.
 */
public List<String> genJsSrc(SoyFileSetNode soyTree, TemplateRegistry registry, SoyIncrementalDomSrcOptions options, ErrorReporter errorReporter) {
    SoyJsSrcOptions incrementalJSSrcOptions = options.toJsSrcOptions();
    try (GuiceSimpleScope.InScope inScope = apiCallScope.enter()) {
        // Seed the scoped parameters.
        BidiGlobalDir bidiGlobalDir = SoyBidiUtils.decodeBidiGlobalDirFromJsOptions(incrementalJSSrcOptions.getBidiGlobalDir(), incrementalJSSrcOptions.getUseGoogIsRtlForBidiGlobalDir());
        ApiCallScopeUtils.seedSharedParams(inScope, null, /* msgBundle */
        bidiGlobalDir);
        // Do the code generation.
        new HtmlContextVisitor(errorReporter).exec(soyTree);
        // Return an empty list here, {@code SoyFileSet} will throw an exception.
        if (errorReporter.hasErrors()) {
            return Collections.emptyList();
        }
        new UnescapingVisitor().exec(soyTree);
        new RemoveUnnecessaryEscapingDirectives().run(soyTree);
        // some of the above passes may slice up raw text nodes, recombine them.
        new CombineConsecutiveRawTextNodesPass().run(soyTree);
        return createVisitor(incrementalJSSrcOptions, typeRegistry).gen(soyTree, registry, errorReporter);
    }
}
Also used : BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) GuiceSimpleScope(com.google.template.soy.shared.internal.GuiceSimpleScope) SoyJsSrcOptions(com.google.template.soy.jssrc.SoyJsSrcOptions) CombineConsecutiveRawTextNodesPass(com.google.template.soy.passes.CombineConsecutiveRawTextNodesPass)

Aggregations

BidiGlobalDir (com.google.template.soy.internal.i18n.BidiGlobalDir)2 CombineConsecutiveRawTextNodesPass (com.google.template.soy.passes.CombineConsecutiveRawTextNodesPass)2 GuiceSimpleScope (com.google.template.soy.shared.internal.GuiceSimpleScope)2 SoyJsSrcOptions (com.google.template.soy.jssrc.SoyJsSrcOptions)1 InsertMsgsVisitor (com.google.template.soy.msgs.internal.InsertMsgsVisitor)1