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 org.asciidoctor.Asciidoctor; |
|
22 |
import static org.asciidoctor.Asciidoctor.Factory.create; |
cf1737
|
23 |
import org.asciidoctor.Attributes; |
U |
24 |
import org.asciidoctor.Options; |
60d3cb
|
25 |
import org.asciidoctor.SafeMode; |
U |
26 |
|
|
27 |
/** |
|
28 |
* Objekte der Klasse AdocWorker wandeln Asciidoc-Quelldateien in HTML und PDF um. |
|
29 |
* |
|
30 |
* @author Ulrich Hilger |
|
31 |
*/ |
|
32 |
public class AdocWorker { |
|
33 |
|
|
34 |
/** |
|
35 |
* Nach HTML transformieren |
|
36 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
37 |
*/ |
|
38 |
public void transform(String fileName) { |
|
39 |
transform(fileName, null); |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* In ein Format transformieren, das von einem 'Backend' von Asciidoctor |
|
44 |
* unterstuetzt wird |
|
45 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
46 |
* @param backend das Kuerzel des Backends, z.B. der String 'pdf', wenn |
|
47 |
* nach PDF transformiert werden soll |
|
48 |
*/ |
|
49 |
public void transform(String fileName, String backend) { |
cf1737
|
50 |
Attributes attributes; |
60d3cb
|
51 |
File outFile = new File(fileName); |
U |
52 |
String thisDirName = outFile.getParent(); |
|
53 |
File pdfStyles = new File(outFile.getParentFile(), "custom-theme.yml"); |
|
54 |
if(pdfStyles.exists()) { |
cf1737
|
55 |
attributes = Attributes.builder() |
60d3cb
|
56 |
.attribute("pdf-themesdir", thisDirName) |
U |
57 |
.attribute("pdf-theme","custom") |
|
58 |
.attribute("pdf-fontsdir", thisDirName + "/fonts") |
|
59 |
.attribute("allow-uri-read") |
|
60 |
.sourceHighlighter("highlightjs") |
cf1737
|
61 |
.build(); |
60d3cb
|
62 |
} else { |
cf1737
|
63 |
attributes = Attributes.builder() |
60d3cb
|
64 |
.sourceHighlighter("highlightjs") |
cf1737
|
65 |
.build(); |
60d3cb
|
66 |
} |
cf1737
|
67 |
Options options; |
60d3cb
|
68 |
if(null != backend) { |
cf1737
|
69 |
options = Options.builder() |
U |
70 |
.inPlace(false) |
60d3cb
|
71 |
.safe(SafeMode.SERVER) |
cf1737
|
72 |
.backend(backend).attributes(attributes).build(); |
60d3cb
|
73 |
|
U |
74 |
} else { |
cf1737
|
75 |
options = Options.builder() |
U |
76 |
.inPlace(false) |
60d3cb
|
77 |
.safe(SafeMode.SERVER) |
cf1737
|
78 |
.attributes(attributes).build(); |
60d3cb
|
79 |
} |
U |
80 |
|
|
81 |
File adcf = new File(fileName); |
|
82 |
Asciidoctor asciidoctor = create(); |
|
83 |
asciidoctor.requireLibrary("asciidoctor-diagram"); |
|
84 |
asciidoctor.convertFile(adcf, options); |
|
85 |
} |
|
86 |
} |