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