Support configuring formatters for file extensions At the moment formatters can only be configured to handle files with a certain mime type. The problem is that some formats don't have an own standardized mime type. E.g. Asciidoc files often use the file extension "*.adoc" but the mime type "text/x-asciidoc" for it is not fully established and Gerrit doesn't recognize it. Change-Id: Ifedbe393a66f3e53ad3b3c67efcb98a1208e303c 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 b7fadf4..1bb0e2b 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
@@ -14,6 +14,7 @@ package com.googlesource.gerrit.plugins.xdocs; +import com.googlesource.gerrit.plugins.xdocs.formatter.AsciidoctorFormatter; import com.googlesource.gerrit.plugins.xdocs.formatter.MarkdownFormatter; import com.googlesource.gerrit.plugins.xdocs.formatter.PlainTextFormatter; @@ -23,6 +24,7 @@ private static final String SECTION_FORMATTER = "formatter"; public static final String KEY_ALLOW_HTML = "allowHtml"; + public static final String KEY_EXT = "ext"; public static final String KEY_MIME_TYPE = "mimeType"; private final Config cfg; @@ -36,6 +38,7 @@ } static void initialize(Config cfg) { + cfg.setString(SECTION_FORMATTER, AsciidoctorFormatter.NAME, KEY_EXT, "adoc"); cfg.setString(SECTION_FORMATTER, MarkdownFormatter.NAME, KEY_MIME_TYPE, "text/x-markdown"); cfg.setString(SECTION_FORMATTER, PlainTextFormatter.NAME, KEY_MIME_TYPE,
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java index b338027..64e9fc1 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java +++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
@@ -14,8 +14,11 @@ package com.googlesource.gerrit.plugins.xdocs.formatter; +import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_EXT; import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_MIME_TYPE; +import org.apache.commons.io.FilenameUtils; + import com.google.gerrit.extensions.annotations.PluginName; import com.google.gerrit.extensions.registration.DynamicMap; import com.google.gerrit.server.FileTypeRegistry; @@ -53,6 +56,7 @@ XDocGlobalConfig pluginCfg = new XDocGlobalConfig(pluginCfgFactory.getGlobalPluginConfig(pluginName)); MimeType mimeType = fileTypeRegistry.getMimeType(fileName, null); + String extension = FilenameUtils.getExtension(fileName); for (String pluginName : formatters.plugins()) { for (Entry<String, Provider<Formatter>> e : formatters.byPlugin(pluginName).entrySet()) { @@ -62,6 +66,12 @@ return new FormatterProvider(e.getKey(), e.getValue()); } } + for (String ext : + pluginCfg.getFormatterConfig(e.getKey()).getStringList(KEY_EXT)) { + if (extension.equals(ext)) { + return new FormatterProvider(e.getKey(), e.getValue()); + } + } } } return null;
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md index 6b0389e..ff50605 100644 --- a/src/main/resources/Documentation/config.md +++ b/src/main/resources/Documentation/config.md
@@ -34,6 +34,8 @@ configuration. ``` + [formatter "ASCIIDOCTOR"] + ext = adoc [formatter "MARKDOWN"] mimeType = text/x-markdown [formatter "PLAIN_TEXT"] @@ -46,10 +48,15 @@ * `MARKDOWN` * `PLAIN_TEXT` +<a id="ext"> +formatter.<formatter>.ext +: Extension of files that will be rendered by this formatter. + + Multiple extensions may be specified for a formatter. <a id="formatterMimeType"> formatter.<formatter>.mimeType -: The mime type of files that you be rendered by this formatter. +: The mime type of files that will be rendered by this formatter. Multiple mime types may be specified for a formatter.