commit | author | age
|
60d3cb
|
1 |
/* |
U |
2 |
neon-adoc - Asciidoctor extensions to Neon |
|
3 |
Copyright (C) 2024 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.neon.adoc; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.Filter; |
e76d49
|
21 |
import com.sun.net.httpserver.HttpContext; |
60d3cb
|
22 |
import com.sun.net.httpserver.HttpExchange; |
U |
23 |
import de.uhilger.neon.FileServer; |
6700d1
|
24 |
import de.uhilger.neon.HttpHelper; |
60d3cb
|
25 |
import java.io.File; |
U |
26 |
import java.io.IOException; |
e76d49
|
27 |
import java.net.URI; |
60d3cb
|
28 |
|
U |
29 |
/** |
|
30 |
* AdocFilter prueft, ob fuer eine Asciidoctor-Quelldatei bereits eine HTML-Version vorliegt. Wenn |
|
31 |
* nicht, verwendet der AdocFilter den AdocActor, um eine HTML-Version zu erzeugen. |
|
32 |
* |
|
33 |
* @author Ulrich Hilger |
|
34 |
* @version 1, 16.06.2021 |
|
35 |
*/ |
|
36 |
public class AdocFilter extends Filter { |
|
37 |
|
|
38 |
@Override |
|
39 |
public void doFilter(HttpExchange exchange, Chain chain) throws IOException { |
e76d49
|
40 |
URI uri = exchange.getRequestURI(); |
6700d1
|
41 |
try { |
cf1737
|
42 |
String fileName = new HttpHelper().getFileName(exchange); |
U |
43 |
if(fileName.endsWith(".adoc")) { |
6700d1
|
44 |
HttpContext ctx = exchange.getHttpContext(); |
U |
45 |
String fileBase = ctx.getAttributes().get(FileServer.ATTR_FILE_BASE).toString(); |
|
46 |
String query = uri.getQuery(); |
|
47 |
if (query instanceof String && query.contains("pdf=true")) { |
|
48 |
processAdocFile(new File(fileBase, fileName), true); |
|
49 |
} else { |
|
50 |
processAdocFile(new File(fileBase, fileName), false); |
|
51 |
} |
60d3cb
|
52 |
} |
6700d1
|
53 |
} catch (IllegalArgumentException ex) { |
U |
54 |
// ungueltiger Dateiname, keine Transformation noetig, also tue nichts |
|
55 |
} finally { |
|
56 |
chain.doFilter(exchange); |
60d3cb
|
57 |
} |
6700d1
|
58 |
|
60d3cb
|
59 |
} |
U |
60 |
|
|
61 |
/** |
|
62 |
* Eine Asciidoc-Quelldatei nach HTML und optional PDF transformieren, sofern |
|
63 |
* eine HTML-Datei noch nicht existiert oder die HTML-Datei aelter ist, als |
|
64 |
* die Asciidoc-Quelldatei |
|
65 |
* |
|
66 |
* @param adocfile das Objekt, das auf die Quelldatei im Asciidoc-Format verweist |
|
67 |
* @param createPdf true, wenn auch nach PDF transformiert werden soll, |
|
68 |
* sonst wird nur nach HTML transformiert |
|
69 |
*/ |
|
70 |
private void processAdocFile(File adocfile, boolean createPdf) { |
|
71 |
String absname = adocfile.getAbsolutePath(); |
|
72 |
File htmlfile = getTargetFile(adocfile, "html"); |
|
73 |
AdocWorker worker = null; |
|
74 |
if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) { |
|
75 |
worker = new AdocWorker(); |
|
76 |
worker.transform(absname); |
|
77 |
} |
|
78 |
if(createPdf) { |
|
79 |
String pdfStr = "pdf"; |
|
80 |
File pdffile = getTargetFile(adocfile, pdfStr); |
|
81 |
if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { |
|
82 |
if(!(worker instanceof AdocWorker)) { |
|
83 |
worker = new AdocWorker(); |
|
84 |
} |
|
85 |
worker.transform(absname, pdfStr); |
|
86 |
} |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
public File getTargetFile(File adocfile, String ext) { |
|
91 |
String dot = "."; |
|
92 |
String nameext = adocfile.getName(); |
|
93 |
String fname = nameext.substring(0, nameext.lastIndexOf(dot)); |
|
94 |
File outfile = new File(adocfile.getParentFile(), fname + dot + ext); |
|
95 |
return outfile; |
|
96 |
} |
|
97 |
|
|
98 |
@Override |
|
99 |
public String description() { |
|
100 |
return "Asciidoctor Filter"; |
|
101 |
} |
|
102 |
} |