/*
|
neon-adoc - Asciidoctor extensions to Neon
|
Copyright (C) 2024 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.neon.adoc;
|
|
import java.io.File;
|
import java.util.Map;
|
import org.asciidoctor.Asciidoctor;
|
import static org.asciidoctor.Asciidoctor.Factory.create;
|
import static org.asciidoctor.AttributesBuilder.attributes;
|
import static org.asciidoctor.OptionsBuilder.options;
|
import org.asciidoctor.SafeMode;
|
|
/**
|
* Objekte der Klasse AdocWorker wandeln Asciidoc-Quelldateien in HTML und PDF um.
|
*
|
* @author Ulrich Hilger
|
*/
|
public class AdocWorker {
|
|
/**
|
* Nach HTML transformieren
|
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe
|
*/
|
public void transform(String fileName) {
|
transform(fileName, null);
|
}
|
|
/**
|
* In ein Format transformieren, das von einem 'Backend' von Asciidoctor
|
* unterstuetzt wird
|
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe
|
* @param backend das Kuerzel des Backends, z.B. der String 'pdf', wenn
|
* nach PDF transformiert werden soll
|
*/
|
public void transform(String fileName, String backend) {
|
//logger.fine("fileName: " + fileName + ", backend: " + backend);
|
Map<String, Object> attributes;
|
File outFile = new File(fileName);
|
String thisDirName = outFile.getParent();
|
File pdfStyles = new File(outFile.getParentFile(), "custom-theme.yml");
|
if(pdfStyles.exists()) {
|
attributes = attributes()
|
.attribute("pdf-themesdir", thisDirName)
|
.attribute("pdf-theme","custom")
|
.attribute("pdf-fontsdir", thisDirName + "/fonts")
|
.attribute("allow-uri-read")
|
.sourceHighlighter("highlightjs")
|
.asMap();
|
} else {
|
attributes = attributes()
|
.sourceHighlighter("highlightjs")
|
.asMap();
|
}
|
Map<String, Object> options;
|
if(null != backend) {
|
options = options().inPlace(false)
|
.safe(SafeMode.SERVER)
|
.backend(backend).attributes(attributes).asMap();
|
|
} else {
|
options = options().inPlace(false)
|
.safe(SafeMode.SERVER)
|
.attributes(attributes).asMap();
|
}
|
|
File adcf = new File(fileName);
|
//logger.fine("before asciidoctor create, adcf: " + adcf.getAbsolutePath());
|
Asciidoctor asciidoctor = create();
|
//logger.fine("asciidoctor created.");
|
asciidoctor.requireLibrary("asciidoctor-diagram");
|
//logger.fine("asciidoctor requireLibrary diagram passed.");
|
//logger.fine("calling asciidoctor.convert for file " + adcf.getAbsolutePath());
|
asciidoctor.convertFile(adcf, options);
|
}
|
}
|