Add formatter for plain text project documentation This adds a formatter that can display project documentation that is written in plain "*.txt" files. While the preview functionality for such files is of no use (you already see the plain text in the code mirror editor) being able to browse plain text files has a value. E.g it makes it possible to link to plain text files (or other resources which could be viewed as plain text, e.g. script files) from within project documentation that is written in Markdown. Also projects may decide to have their project documentation completely as plain text files. In this case the index file for the project could e.g. be configured to "readme.txt" and a link to it would appear in the project list. This is also an example of how new formatters can be added. Change-Id: I06b962c95f5d763b6009fe42534cf8ae73f369e5 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java index 062a683..6fbce26 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
@@ -27,7 +27,7 @@ private static final String KEY_MIME_TYPE = "mimeType"; enum Formatter { - MARKDOWN; + MARKDOWN, PLAIN_TEXT; } private final Config cfg; @@ -55,5 +55,7 @@ static void initialize(Config cfg) { cfg.setString(SECTION_FORMATTER, Formatter.MARKDOWN.name(), KEY_MIME_TYPE, "text/x-markdown"); + cfg.setString(SECTION_FORMATTER, Formatter.PLAIN_TEXT.name(), KEY_MIME_TYPE, + "text/plain"); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java index 1461186..ebfb54d 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
@@ -15,6 +15,7 @@ package com.googlesource.gerrit.plugins.xdocs; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; import com.google.common.base.Strings; import com.google.common.cache.CacheLoader; @@ -136,6 +137,8 @@ switch (formatter) { case MARKDOWN: return formatMarkdownAsHtml(cfg, raw); + case PLAIN_TEXT: + return formatTxtAsHtml(raw); default: throw new IllegalStateException("Unsupported formatter: " + formatter.name()); @@ -151,6 +154,11 @@ return f.markdownToDocHtml(md, UTF_8.name()); } + private byte[] formatTxtAsHtml(String txt) { + String html = "<pre>" + escapeHtml(txt) + "</pre>"; + return html.getBytes(UTF_8); + } + private Resource getAsHtmlResource(byte[] html, int lastModified) { return new SmallResource(html) .setContentType("text/html")
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md index 3ec161f..24d6027 100644 --- a/src/main/resources/Documentation/config.md +++ b/src/main/resources/Documentation/config.md
@@ -36,8 +36,16 @@ ``` [formatter "MARKDOWN"] mimeType = text/x-markdown + [formatter "PLAIN_TEXT"] + mimeType = text/plain ``` +Supported formatters: + +* `MARKDOWN` +* `PLAIN_TEXT` + + <a id="formatterMimeType"> formatter.<formatter>.mimeType : The mime type of files that you be rendered by this formatter.