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 |
|
f5adf5
|
52 |
public void handle(HttpExchange e, String fileBase, String fileName) throws IOException { |
U |
53 |
File adocfile = new File(fileBase, fileName); |
|
54 |
logger.fine("adocfile: " + adocfile.getAbsolutePath()); |
|
55 |
AdocActor actor = new AdocActor(); |
|
56 |
File outfile = actor.getTargetFile(adocfile, AdocActor.HTML); |
|
57 |
logger.fine("outfile: " + outfile.getAbsolutePath()); |
|
58 |
HttpResponder fs = new HttpResponder(); |
|
59 |
fs.serveFile(e, outfile); |
|
60 |
} |
|
61 |
|
5ca813
|
62 |
public File getTargetFile(File adocfile, String ext) { |
U |
63 |
String nameext = adocfile.getName(); |
|
64 |
String fname = nameext.substring(0, nameext.lastIndexOf(DOT)); |
|
65 |
File outfile = new File(adocfile.getParentFile(), fname + DOT + ext); |
|
66 |
logger.log(Level.FINE, "out: {0}", outfile.getAbsolutePath()); |
|
67 |
return outfile; |
|
68 |
} |
9c5db9
|
69 |
|
U |
70 |
public void processAdocFile(File adocfile, String pdf) { |
|
71 |
|
|
72 |
String absname = adocfile.getAbsolutePath(); |
5ca813
|
73 |
logger.log(Level.FINE, "in: {0}", absname); |
9c5db9
|
74 |
|
U |
75 |
// HTML-Datei ermitteln |
5ca813
|
76 |
//String nameext = adocfile.getName(); |
U |
77 |
//String fname = nameext.substring(0, nameext.lastIndexOf(DOT)); |
|
78 |
//File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML); |
|
79 |
//File outfile = htmlfile; // Standardmaessig wird HTML zurueckgegeben |
|
80 |
//logger.fine("out: " + outfile.getAbsolutePath()); |
9c5db9
|
81 |
//response.setCharacterEncoding("UTF-8"); |
5ca813
|
82 |
File outfile = getTargetFile(adocfile, HTML); |
U |
83 |
File htmlfile = outfile; |
9c5db9
|
84 |
|
U |
85 |
/* |
|
86 |
nach HTML transformieren, wenn die Quelle sich geandert hat oder |
|
87 |
die HTML-Datei noch nicht existiert |
|
88 |
*/ |
|
89 |
if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) { |
4ed0b2
|
90 |
logger.fine("calling transform for " + absname); |
9c5db9
|
91 |
transform(absname); |
U |
92 |
} |
|
93 |
|
|
94 |
/* |
|
95 |
nach PDF transformieren, wenn der Parameter pdf=true existiert und |
|
96 |
wenn die Quelle sich geandert hat oder |
|
97 |
die PDF-Datei noch nicht existiert |
|
98 |
*/ |
|
99 |
|
|
100 |
if(null != pdf && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) { |
5ca813
|
101 |
//File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF); |
U |
102 |
File pdffile = getTargetFile(adocfile, PDF); |
9c5db9
|
103 |
outfile = pdffile; // PDF soll zurueckgegeben werden |
U |
104 |
if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { |
|
105 |
//response.setContentType("application/pdf"); |
|
106 |
transform(absname, PDF); |
|
107 |
} |
|
108 |
//ServletOutputStream os = response.getOutputStream(); |
|
109 |
//InputStream bytes = new FileInputStream(outfile); |
|
110 |
//int b = bytes.read(); |
|
111 |
//while(b > -1 ) { |
|
112 |
// os.write(b); |
|
113 |
// b = bytes.read(); |
|
114 |
//} |
|
115 |
} else { |
|
116 |
//PrintWriter out = response.getWriter(); |
|
117 |
//InputStreamReader in = new InputStreamReader(new FileInputStream(outfile), "UTF-8"); |
|
118 |
//in.transferTo(out); |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Nach HTML transformieren |
|
124 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
125 |
*/ |
|
126 |
private void transform(String fileName) { |
|
127 |
transform(fileName, null); |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* In ein Format transformieren, das von einem 'Backend' von Asciidoctor |
|
132 |
* unterstuetzt wird |
|
133 |
* @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe |
|
134 |
* @param backend das Kuerzel des Backends, z.B. der String 'pdf', wenn |
|
135 |
* nach PDF transformiert werden soll |
|
136 |
*/ |
|
137 |
private void transform(String fileName, String backend) { |
4ed0b2
|
138 |
logger.fine("fileName: " + fileName + ", backend: " + backend); |
9c5db9
|
139 |
Map<String, Object> attributes; |
U |
140 |
File outFile = new File(fileName); |
|
141 |
String thisDirName = outFile.getParent(); |
|
142 |
File pdfStyles = new File(outFile.getParentFile(), "custom-theme.yml"); |
|
143 |
if(pdfStyles.exists()) { |
|
144 |
attributes = attributes() |
|
145 |
.attribute("pdf-themesdir", thisDirName) |
|
146 |
.attribute("pdf-theme","custom") |
|
147 |
.attribute("pdf-fontsdir", thisDirName + "/fonts") |
|
148 |
.attribute("allow-uri-read") |
|
149 |
.sourceHighlighter("highlightjs") |
|
150 |
.asMap(); |
|
151 |
} else { |
|
152 |
attributes = attributes() |
|
153 |
.sourceHighlighter("highlightjs") |
|
154 |
.asMap(); |
|
155 |
} |
|
156 |
Map<String, Object> options; |
|
157 |
if(null != backend) { |
|
158 |
options = options().inPlace(false) |
|
159 |
.safe(SafeMode.SERVER) |
|
160 |
.backend(backend).attributes(attributes).asMap(); |
|
161 |
|
|
162 |
} else { |
|
163 |
options = options().inPlace(false) |
|
164 |
.safe(SafeMode.SERVER) |
|
165 |
.attributes(attributes).asMap(); |
|
166 |
} |
|
167 |
|
4ed0b2
|
168 |
File adcf = new File(fileName); |
U |
169 |
logger.fine("before asciidoctor create, adcf: " + adcf.getAbsolutePath()); |
9c5db9
|
170 |
Asciidoctor asciidoctor = create(); |
4ed0b2
|
171 |
logger.fine("asciidoctor created."); |
9c5db9
|
172 |
asciidoctor.requireLibrary("asciidoctor-diagram"); |
4ed0b2
|
173 |
logger.fine("asciidoctor requireLibrary diagram passed."); |
U |
174 |
logger.fine("calling asciidoctor.convert for file " + adcf.getAbsolutePath()); |
|
175 |
asciidoctor.convertFile(adcf, options); |
9c5db9
|
176 |
} |
U |
177 |
|
|
178 |
} |