commit | author | age
|
9c5db9
|
1 |
/* |
U |
2 |
http-adoc - Asciidoctor extensions to jdk.httpserver |
|
3 |
Copyright (C) 2021 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.httpserver.adoc; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.util.Map; |
|
22 |
import java.util.logging.Logger; |
|
23 |
|
|
24 |
import static org.asciidoctor.Asciidoctor.Factory.create; |
|
25 |
import org.asciidoctor.Asciidoctor; |
|
26 |
import static org.asciidoctor.AttributesBuilder.attributes; |
|
27 |
import static org.asciidoctor.OptionsBuilder.options; |
|
28 |
import org.asciidoctor.SafeMode; |
|
29 |
|
|
30 |
/** |
|
31 |
* Der AdocActor transformiert den Asciidoctor-Quellcode aus einer |
|
32 |
* gegebenen Datei zu HTML oder PDF und legt das Ergebnis als HTML- oder |
|
33 |
* PDF-Datei ab. |
|
34 |
* |
|
35 |
* Der AdocActor benötigt AsciidoctorJ im Classpath. |
|
36 |
* |
|
37 |
* @author Ulrich Hilger |
|
38 |
* @version 1, 16.06.2021 |
|
39 |
*/ |
|
40 |
public class AdocActor { |
|
41 |
|
|
42 |
private static final Logger logger = Logger.getLogger(AdocActor.class.getName()); |
|
43 |
|
|
44 |
private static final String DOT = "."; |
|
45 |
private static final String HTML = "html"; |
|
46 |
private static final String PDF = "pdf"; |
|
47 |
|
|
48 |
|
|
49 |
public void processAdocFile(File adocfile, String pdf) { |
|
50 |
|
|
51 |
String absname = adocfile.getAbsolutePath(); |
|
52 |
logger.fine("in: " + absname); |
|
53 |
|
|
54 |
// HTML-Datei ermitteln |
|
55 |
String nameext = adocfile.getName(); |
|
56 |
String fname = nameext.substring(0, nameext.lastIndexOf(DOT)); |
|
57 |
File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML); |
|
58 |
File outfile = htmlfile; // Standardmaessig wird HTML zurueckgegeben |
|
59 |
logger.fine("out: " + outfile.getAbsolutePath()); |
|
60 |
//response.setCharacterEncoding("UTF-8"); |
|
61 |
|
|
62 |
/* |
|
63 |
nach HTML transformieren, wenn die Quelle sich geandert hat oder |
|
64 |
die HTML-Datei noch nicht existiert |
|
65 |
*/ |
|
66 |
if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) { |
|
67 |
transform(absname); |
|
68 |
} |
|
69 |
|
|
70 |
/* |
|
71 |
nach PDF transformieren, wenn der Parameter pdf=true existiert und |
|
72 |
wenn die Quelle sich geandert hat oder |
|
73 |
die PDF-Datei noch nicht existiert |
|
74 |
*/ |
|
75 |
|
|
76 |
if(null != pdf && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) { |
|
77 |
File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF); |
|
78 |
outfile = pdffile; // PDF soll zurueckgegeben werden |
|
79 |
if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { |
|
80 |
//response.setContentType("application/pdf"); |
|
81 |
transform(absname, PDF); |
|
82 |
} |
|
83 |
//ServletOutputStream os = response.getOutputStream(); |
|
84 |
//InputStream bytes = new FileInputStream(outfile); |
|
85 |
//int b = bytes.read(); |
|
86 |
//while(b > -1 ) { |
|
87 |
// os.write(b); |
|
88 |
// b = bytes.read(); |
|
89 |
//} |
|
90 |
} else { |
|
91 |
//PrintWriter out = response.getWriter(); |
|
92 |
//InputStreamReader in = new InputStreamReader(new FileInputStream(outfile), "UTF-8"); |
|
93 |
//in.transferTo(out); |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* Nach HTML transformieren |
|
99 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
100 |
*/ |
|
101 |
private void transform(String fileName) { |
|
102 |
transform(fileName, null); |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* In ein Format transformieren, das von einem 'Backend' von Asciidoctor |
|
107 |
* unterstuetzt wird |
|
108 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
109 |
* @param backend das Kuerzel des Backends, z.B. der String 'pdf', wenn |
|
110 |
* nach PDF transformiert werden soll |
|
111 |
*/ |
|
112 |
private void transform(String fileName, String backend) { |
|
113 |
Map<String, Object> attributes; |
|
114 |
File outFile = new File(fileName); |
|
115 |
String thisDirName = outFile.getParent(); |
|
116 |
File pdfStyles = new File(outFile.getParentFile(), "custom-theme.yml"); |
|
117 |
if(pdfStyles.exists()) { |
|
118 |
attributes = attributes() |
|
119 |
.attribute("pdf-themesdir", thisDirName) |
|
120 |
.attribute("pdf-theme","custom") |
|
121 |
.attribute("pdf-fontsdir", thisDirName + "/fonts") |
|
122 |
.attribute("allow-uri-read") |
|
123 |
.sourceHighlighter("highlightjs") |
|
124 |
.asMap(); |
|
125 |
} else { |
|
126 |
attributes = attributes() |
|
127 |
.sourceHighlighter("highlightjs") |
|
128 |
.asMap(); |
|
129 |
} |
|
130 |
Map<String, Object> options; |
|
131 |
if(null != backend) { |
|
132 |
options = options().inPlace(false) |
|
133 |
.safe(SafeMode.SERVER) |
|
134 |
.backend(backend).attributes(attributes).asMap(); |
|
135 |
|
|
136 |
} else { |
|
137 |
options = options().inPlace(false) |
|
138 |
.safe(SafeMode.SERVER) |
|
139 |
.attributes(attributes).asMap(); |
|
140 |
} |
|
141 |
|
|
142 |
Asciidoctor asciidoctor = create(); |
|
143 |
asciidoctor.requireLibrary("asciidoctor-diagram"); |
|
144 |
asciidoctor.convertFile(new File(fileName), options); |
|
145 |
} |
|
146 |
|
|
147 |
} |