Search in sources :

Example 1 with SmallResource

use of com.google.gerrit.httpd.resources.SmallResource in project gerrit by GerritCodeReview.

the class HttpPluginServlet method sendMarkdownAsHtml.

private void sendMarkdownAsHtml(String md, String pluginName, PluginResourceKey cacheKey, HttpServletResponse res, long lastModifiedTime) throws UnsupportedEncodingException, IOException {
    Map<String, String> macros = new HashMap<>();
    macros.put("PLUGIN", pluginName);
    macros.put("SSH_HOST", sshHost);
    macros.put("SSH_PORT", "" + sshPort);
    String url = webUrl.get();
    if (Strings.isNullOrEmpty(url)) {
        url = "http://review.example.com/";
    }
    macros.put("URL", url);
    Matcher m = Pattern.compile("(\\\\)?@([A-Z_]+)@").matcher(md);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String key = m.group(2);
        String val = macros.get(key);
        if (m.group(1) != null) {
            m.appendReplacement(sb, "@" + key + "@");
        } else if (val != null) {
            m.appendReplacement(sb, val);
        } else {
            m.appendReplacement(sb, "@" + key + "@");
        }
    }
    m.appendTail(sb);
    byte[] html = new MarkdownFormatter().markdownToDocHtml(sb.toString(), UTF_8.name());
    resourceCache.put(cacheKey, new SmallResource(html).setContentType("text/html").setCharacterEncoding(UTF_8.name()).setLastModified(lastModifiedTime));
    res.setContentType("text/html");
    res.setCharacterEncoding(UTF_8.name());
    res.setContentLength(html.length);
    res.setDateHeader("Last-Modified", lastModifiedTime);
    res.getOutputStream().write(html);
}
Also used : SmallResource(com.google.gerrit.httpd.resources.SmallResource) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) CharMatcher(com.google.common.base.CharMatcher) MarkdownFormatter(com.google.gerrit.server.documentation.MarkdownFormatter)

Aggregations

CharMatcher (com.google.common.base.CharMatcher)1 SmallResource (com.google.gerrit.httpd.resources.SmallResource)1 MarkdownFormatter (com.google.gerrit.server.documentation.MarkdownFormatter)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1