AsciidoctorFormatter: Simplify code by using the Asciidoctor >= 1.5 API
It was checked that attributes, like toggling tableOfContents(), are
indeed taken into account even without an output file thanks to the new
version of Asciidoctor.
Change-Id: I06b669d399c2ee0c9e5b314531e917396cf2620e
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 caa31c4..2da5f69 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
@@ -16,27 +16,18 @@
import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_ALLOW_HTML;
import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_INCLUDE_TOC;
-import static java.nio.charset.StandardCharsets.UTF_8;
-import com.google.common.io.ByteStreams;
-import com.google.gerrit.common.TimeUtil;
-import com.google.gerrit.extensions.annotations.PluginData;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.googlesource.gerrit.plugins.xdocs.ConfigSection;
import org.asciidoctor.Asciidoctor;
-import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
-import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import java.io.BufferedReader;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
@@ -50,15 +41,14 @@
private static final String DOCTYPE = "article";
private static final String ERUBY = "erb";
- private final File baseDir;
private final Properties attributes;
private final FormatterUtil util;
private final Formatters formatters;
@Inject
- public AsciidoctorFormatter(@PluginData File baseDir,
- FormatterUtil formatterUtil, Formatters formatters) throws IOException {
- this.baseDir = baseDir;
+ public AsciidoctorFormatter(
+ FormatterUtil formatterUtil,
+ Formatters formatters) throws IOException {
this.attributes = readAttributes();
this.util = formatterUtil;
this.formatters = formatters;
@@ -73,25 +63,9 @@
ConfigSection projectCfg =
formatters.getFormatterConfig(NAME, projectName);
- // asciidoctor ignores all attributes if no output file is specified,
- // this is why we must specify an output file and then read its content
- File tmpDir = new File(baseDir, "tmp");
- tmpDir.mkdirs();
- File tmpFile = File.createTempFile("asciidoctor-", null, tmpDir);
- try {
- Asciidoctor.Factory.create(AsciidoctorFormatter.class.getClassLoader())
- .render(raw, createOptions(projectCfg, abbrRev, tmpFile));
- try (FileInputStream input = new FileInputStream(tmpFile)) {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ByteStreams.copy(input, out);
- String html = out.toString(UTF_8.name());
- return util.applyCss(html, NAME, projectName);
- }
- } finally {
- if (!tmpFile.delete()) {
- tmpFile.deleteOnExit();
- }
- }
+ String html = Asciidoctor.Factory.create(AsciidoctorFormatter.class.getClassLoader())
+ .convert(raw, createOptions(projectCfg, abbrRev));
+ return util.applyCss(html, NAME, projectName);
}
private String suppressHtml(String raw) throws IOException {
@@ -112,19 +86,17 @@
}
}
- private Options createOptions(ConfigSection cfg, String revision, File out) {
+ private OptionsBuilder createOptions(ConfigSection cfg, String revision) {
return OptionsBuilder.options()
.backend(BACKEND)
.docType(DOCTYPE)
.eruby(ERUBY)
.safe(SafeMode.SECURE)
.attributes(getAttributes(cfg, revision))
- .mkDirs(true)
- .toFile(out)
- .get();
+ .mkDirs(true);
}
- private Attributes getAttributes(ConfigSection cfg, String revision) {
+ private AttributesBuilder getAttributes(ConfigSection cfg, String revision) {
AttributesBuilder ab = AttributesBuilder.attributes()
.tableOfContents(cfg.getBoolean(KEY_INCLUDE_TOC, true))
.sourceHighlighter("prettify");
@@ -133,7 +105,7 @@
}
ab.attribute("last-update-label!");
ab.attribute("revnumber", revision);
- return ab.get();
+ return ab;
}
private static Properties readAttributes() throws IOException {