Move CSS handling into FormatterUtil to make it reuseable
Which CSS is applied to a document depends on a number of
configuration paramters and on the availability of default CSS,
inherited CSS and project-specific CSS. Basically each CSS-aware
formatter would need to do the same computation to apply the correct
CSS. Having the code for it in a common place avoids reimplementation
of this logic again and again.
Please note that the CSS handling in the MarkdownFormatter is special
since it wants to reuse the default CSS that is packaged in Gerrit
core. This is why the MarkdownFormatter doesn't use the CSS function
from FormatterUtil.
Change-Id: I6fce8b6912ef8242c26a003438f1fe9038ba4150
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
index 42c0456..2f9ae4d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
@@ -15,12 +15,9 @@
package com.googlesource.gerrit.plugins.xdocs.formatter;
import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_ALLOW_HTML;
-import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_CSS_THEME;
import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_INCLUDE_TOC;
-import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_INHERIT_CSS;
import static java.nio.charset.StandardCharsets.UTF_8;
-import com.google.common.base.MoreObjects;
import com.google.common.io.ByteStreams;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.extensions.annotations.PluginData;
@@ -35,17 +32,14 @@
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
-import org.eclipse.jgit.util.TemporaryBuffer;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
-import java.net.URL;
import java.util.Properties;
@Singleton
@@ -57,7 +51,6 @@
private static final String ERUBY = "erb";
private final File baseDir;
- private final String defaultCss;
private final Properties attributes;
private final FormatterUtil util;
private final Formatters formatters;
@@ -66,7 +59,6 @@
public AsciidoctorFormatter(@PluginData File baseDir,
FormatterUtil formatterUtil, Formatters formatters) throws IOException {
this.baseDir = baseDir;
- this.defaultCss = readCss();
this.attributes = readAttributes();
this.util = formatterUtil;
this.formatters = formatters;
@@ -92,18 +84,7 @@
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteStreams.copy(input, out);
String html = out.toString(UTF_8.name());
- String cssTheme = projectCfg.getString(KEY_CSS_THEME);
- String inheritedCss =
- util.getInheritedCss(projectName, NAME, "asciidoctor", cssTheme);
- String projectCss = util.getCss(projectName, "asciidoctor", cssTheme);
- if (projectCfg.getBoolean(KEY_INHERIT_CSS, true)) {
- return util.insertCss(html,
- MoreObjects.firstNonNull(inheritedCss, defaultCss), projectCss);
- } else {
- return util.insertCss(html,
- MoreObjects.firstNonNull(projectCss,
- MoreObjects.firstNonNull(inheritedCss, defaultCss)));
- }
+ return util.applyCss(html, NAME, projectName);
}
} finally {
if (!tmpFile.delete()) {
@@ -154,19 +135,6 @@
return ab.get();
}
- private static String readCss() throws IOException {
- String name = "asciidoctor.css";
- URL url = AsciidoctorFormatter.class.getResource(name);
- if (url == null) {
- throw new FileNotFoundException("Resource " + name);
- }
- try (InputStream in = url.openStream();
- TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(128 * 1024)) {
- tmp.copy(in);
- return new String(tmp.toByteArray(), UTF_8);
- }
- }
-
private static Properties readAttributes() throws IOException {
Properties attributes = new Properties();
try (InputStream in = AsciidoctorFormatter.class
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/FormatterUtil.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/FormatterUtil.java
index 2c49704..5d62a7e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/FormatterUtil.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/FormatterUtil.java
@@ -14,11 +14,13 @@
package com.googlesource.gerrit.plugins.xdocs.formatter;
+import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_CSS_THEME;
import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_INHERIT_CSS;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
import com.google.common.base.Joiner;
+import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.gerrit.extensions.annotations.PluginData;
import com.google.gerrit.extensions.annotations.PluginName;
@@ -40,20 +42,31 @@
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
+import org.eclipse.jgit.util.TemporaryBuffer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
@Singleton
public class FormatterUtil {
+ private static final Logger log = LoggerFactory.getLogger(FormatterUtil.class);
+
private final String pluginName;
private final File baseDir;
private final GitRepositoryManager repoManager;
private final ProjectCache projectCache;
private final Formatters formatters;
+ private final Map<String, String> defaultCss;
@Inject
FormatterUtil(@PluginName String pluginName,
@@ -66,6 +79,7 @@
this.repoManager = repoManager;
this.projectCache = projectCache;
this.formatters = formatters;
+ this.defaultCss = new HashMap<>();
}
/**
@@ -191,6 +205,47 @@
return null;
}
+ public String applyCss(String html, String formatterName, String projectName)
+ throws IOException {
+ ConfigSection projectCfg =
+ formatters.getFormatterConfig(formatterName, projectName);
+ String cssName = formatterName.toLowerCase(Locale.US);
+ String cssTheme = projectCfg.getString(KEY_CSS_THEME);
+ String defaultCss = getDefaultCss(formatterName);
+ String inheritedCss =
+ getInheritedCss(projectName, formatterName, cssName, cssTheme);
+ String projectCss = getCss(projectName, cssName, cssTheme);
+ if (projectCfg.getBoolean(KEY_INHERIT_CSS, true)) {
+ return insertCss(html,
+ MoreObjects.firstNonNull(inheritedCss, defaultCss), projectCss);
+ } else {
+ return insertCss(html,
+ MoreObjects.firstNonNull(projectCss,
+ MoreObjects.firstNonNull(inheritedCss, defaultCss)));
+ }
+ }
+
+ private String getDefaultCss(String formatterName) throws IOException {
+ String css = defaultCss.get(formatterName) ;
+ if (css == null) {
+ URL url = FormatterUtil.class.getResource(
+ formatterName.toLowerCase(Locale.US) + ".css");
+ if (url != null) {
+ try (InputStream in = url.openStream();
+ TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(128 * 1024)) {
+ tmp.copy(in);
+ css = new String(tmp.toByteArray(), UTF_8);
+ }
+ } else {
+ log.info(String.format("No default CSS for formatter '%s' found.",
+ formatterName));
+ css = "";
+ }
+ defaultCss.put(formatterName, css);
+ }
+ return css;
+ }
+
/**
* Inserts the given CSS into the given HTML.
*