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