commit | author | age
|
60d3cb
|
1 |
/* |
U |
2 |
neon-adoc - Asciidoctor extensions to Neon |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.neon.adoc; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.util.Map; |
|
22 |
import org.asciidoctor.Asciidoctor; |
|
23 |
import static org.asciidoctor.Asciidoctor.Factory.create; |
|
24 |
import static org.asciidoctor.AttributesBuilder.attributes; |
|
25 |
import static org.asciidoctor.OptionsBuilder.options; |
|
26 |
import org.asciidoctor.SafeMode; |
|
27 |
|
|
28 |
/** |
|
29 |
* Objekte der Klasse AdocWorker wandeln Asciidoc-Quelldateien in HTML und PDF um. |
|
30 |
* |
|
31 |
* @author Ulrich Hilger |
|
32 |
*/ |
|
33 |
public class AdocWorker { |
|
34 |
|
|
35 |
/** |
|
36 |
* Nach HTML transformieren |
|
37 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
38 |
*/ |
|
39 |
public void transform(String fileName) { |
|
40 |
transform(fileName, null); |
|
41 |
} |
|
42 |
|
|
43 |
/** |
|
44 |
* In ein Format transformieren, das von einem 'Backend' von Asciidoctor |
|
45 |
* unterstuetzt wird |
|
46 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
47 |
* @param backend das Kuerzel des Backends, z.B. der String 'pdf', wenn |
|
48 |
* nach PDF transformiert werden soll |
|
49 |
*/ |
|
50 |
public void transform(String fileName, String backend) { |
|
51 |
//logger.fine("fileName: " + fileName + ", backend: " + backend); |
|
52 |
Map<String, Object> attributes; |
|
53 |
File outFile = new File(fileName); |
|
54 |
String thisDirName = outFile.getParent(); |
|
55 |
File pdfStyles = new File(outFile.getParentFile(), "custom-theme.yml"); |
|
56 |
if(pdfStyles.exists()) { |
|
57 |
attributes = attributes() |
|
58 |
.attribute("pdf-themesdir", thisDirName) |
|
59 |
.attribute("pdf-theme","custom") |
|
60 |
.attribute("pdf-fontsdir", thisDirName + "/fonts") |
|
61 |
.attribute("allow-uri-read") |
|
62 |
.sourceHighlighter("highlightjs") |
|
63 |
.asMap(); |
|
64 |
} else { |
|
65 |
attributes = attributes() |
|
66 |
.sourceHighlighter("highlightjs") |
|
67 |
.asMap(); |
|
68 |
} |
|
69 |
Map<String, Object> options; |
|
70 |
if(null != backend) { |
|
71 |
options = options().inPlace(false) |
|
72 |
.safe(SafeMode.SERVER) |
|
73 |
.backend(backend).attributes(attributes).asMap(); |
|
74 |
|
|
75 |
} else { |
|
76 |
options = options().inPlace(false) |
|
77 |
.safe(SafeMode.SERVER) |
|
78 |
.attributes(attributes).asMap(); |
|
79 |
} |
|
80 |
|
|
81 |
File adcf = new File(fileName); |
|
82 |
//logger.fine("before asciidoctor create, adcf: " + adcf.getAbsolutePath()); |
|
83 |
Asciidoctor asciidoctor = create(); |
|
84 |
//logger.fine("asciidoctor created."); |
|
85 |
asciidoctor.requireLibrary("asciidoctor-diagram"); |
|
86 |
//logger.fine("asciidoctor requireLibrary diagram passed."); |
|
87 |
//logger.fine("calling asciidoctor.convert for file " + adcf.getAbsolutePath()); |
|
88 |
asciidoctor.convertFile(adcf, options); |
|
89 |
} |
|
90 |
} |