use of com.google.gerrit.server.documentation.MarkdownFormatter in project gerrit by GerritCodeReview.
the class HttpPluginServlet method extractTitleFromMarkdown.
private static String extractTitleFromMarkdown(PluginContentScanner scanner, PluginEntry entry) throws IOException {
String charEnc = null;
Map<Object, String> atts = entry.getAttrs();
if (atts != null) {
charEnc = Strings.emptyToNull(atts.get(ATTR_CHARACTER_ENCODING));
}
if (charEnc == null) {
charEnc = UTF_8.name();
}
return new MarkdownFormatter().extractTitleFromMarkdown(readWholeEntry(scanner, entry), charEnc);
}
use of com.google.gerrit.server.documentation.MarkdownFormatter 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);
StringBuilder sb = new StringBuilder();
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);
}
Aggregations