Search in sources :

Example 6 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project gerrit by GerritCodeReview.

the class IndexServlet method getTemplateData.

static SoyMapData getTemplateData(String canonicalURL, String cdnPath) throws URISyntaxException {
    String canonicalPath = computeCanonicalPath(canonicalURL);
    String staticPath = "";
    if (cdnPath != null) {
        staticPath = cdnPath;
    } else if (canonicalPath != null) {
        staticPath = canonicalPath;
    }
    // The resource path must be typed as safe for use in a script src.
    // TODO(wyatta): Upgrade this to use an appropriate safe URL type.
    SanitizedContent sanitizedStaticPath = UnsafeSanitizedContentOrdainer.ordainAsSafe(staticPath, SanitizedContent.ContentKind.TRUSTED_RESOURCE_URI);
    return new SoyMapData("canonicalPath", canonicalPath, "staticResourcePath", sanitizedStaticPath);
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) SanitizedContent(com.google.template.soy.data.SanitizedContent)

Example 7 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project gerrit by GerritCodeReview.

the class IndexServletTest method noPathAndNoCDN.

@Test
public void noPathAndNoCDN() throws URISyntaxException {
    SoyMapData data = IndexServlet.getTemplateData("http://example.com/", null);
    assertThat(data.getSingle("canonicalPath").stringValue()).isEqualTo("");
    assertThat(data.getSingle("staticResourcePath").stringValue()).isEqualTo("");
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) Test(org.junit.Test)

Example 8 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project gerrit by GerritCodeReview.

the class IndexServletTest method pathAndCDN.

@Test
public void pathAndCDN() throws URISyntaxException {
    SoyMapData data = IndexServlet.getTemplateData("http://example.com/gerrit", "http://my-cdn.com/foo/bar/");
    assertThat(data.getSingle("canonicalPath").stringValue()).isEqualTo("/gerrit");
    assertThat(data.getSingle("staticResourcePath").stringValue()).isEqualTo("http://my-cdn.com/foo/bar/");
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) Test(org.junit.Test)

Example 9 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project gerrit by GerritCodeReview.

the class ChangeEmail method getDiffTemplateData.

/**
   * Generate a Soy list of maps representing each line of the unified diff. The line maps will have
   * a 'type' key which maps to one of 'common', 'add' or 'remove' and a 'text' key which maps to
   * the line's content.
   */
private SoyListData getDiffTemplateData() {
    SoyListData result = new SoyListData();
    Splitter lineSplitter = Splitter.on(System.getProperty("line.separator"));
    for (String diffLine : lineSplitter.split(getUnifiedDiff())) {
        SoyMapData lineData = new SoyMapData();
        lineData.put("text", diffLine);
        // Skip empty lines and lines that look like diff headers.
        if (diffLine.isEmpty() || diffLine.startsWith("---") || diffLine.startsWith("+++")) {
            lineData.put("type", "common");
        } else {
            switch(diffLine.charAt(0)) {
                case '+':
                    lineData.put("type", "add");
                    break;
                case '-':
                    lineData.put("type", "remove");
                    break;
                default:
                    lineData.put("type", "common");
                    break;
            }
        }
        result.add(lineData);
    }
    return result;
}
Also used : Splitter(com.google.common.base.Splitter) SoyMapData(com.google.template.soy.data.SoyMapData) SoyListData(com.google.template.soy.data.SoyListData)

Example 10 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project buck by facebook.

the class TemplateHandler method createHtmlForResponse.

@Nullable
String createHtmlForResponse(Request baseRequest) throws IOException {
    String template = delegate.getTemplateForRequest(baseRequest);
    SoyTofu.Renderer renderer = createAndMaybeCacheSoyTofu().newRenderer(template);
    SoyMapData data = delegate.getDataForRequest(baseRequest);
    if (data != null) {
        renderer.setData(data);
        return renderer.render();
    } else {
        return null;
    }
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) SoyTofu(com.google.template.soy.tofu.SoyTofu) Nullable(javax.annotation.Nullable)

Aggregations

SoyMapData (com.google.template.soy.data.SoyMapData)14 Test (org.junit.Test)9 SoyListData (com.google.template.soy.data.SoyListData)5 TraceAttributes (com.facebook.buck.util.trace.BuildTraces.TraceAttributes)2 Matcher (java.util.regex.Matcher)2 Nullable (javax.annotation.Nullable)2 Request (org.eclipse.jetty.server.Request)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Splitter (com.google.common.base.Splitter)1 SanitizedContent (com.google.template.soy.data.SanitizedContent)1 SoyTofu (com.google.template.soy.tofu.SoyTofu)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Path (java.nio.file.Path)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1