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