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 |
//String path = uri.getPath(); |
U |
42 |
try { |
|
43 |
String path = new HttpHelper().getFileName(exchange); |
|
44 |
if(path.endsWith(".adoc")) { |
|
45 |
HttpContext ctx = exchange.getHttpContext(); |
|
46 |
String fileBase = ctx.getAttributes().get(FileServer.ATTR_FILE_BASE).toString(); |
|
47 |
String fileName = path.substring(ctx.getPath().length()); |
|
48 |
String query = uri.getQuery(); |
|
49 |
if (query instanceof String && query.contains("pdf=true")) { |
|
50 |
processAdocFile(new File(fileBase, fileName), true); |
|
51 |
} else { |
|
52 |
processAdocFile(new File(fileBase, fileName), false); |
|
53 |
} |
60d3cb
|
54 |
} |
6700d1
|
55 |
} catch (IllegalArgumentException ex) { |
U |
56 |
// ungueltiger Dateiname, keine Transformation noetig, also tue nichts |
|
57 |
} finally { |
|
58 |
chain.doFilter(exchange); |
60d3cb
|
59 |
} |
6700d1
|
60 |
|
60d3cb
|
61 |
} |
U |
62 |
|
|
63 |
/** |
|
64 |
* Eine Asciidoc-Quelldatei nach HTML und optional PDF transformieren, sofern |
|
65 |
* eine HTML-Datei noch nicht existiert oder die HTML-Datei aelter ist, als |
|
66 |
* die Asciidoc-Quelldatei |
|
67 |
* |
|
68 |
* @param adocfile das Objekt, das auf die Quelldatei im Asciidoc-Format verweist |
|
69 |
* @param createPdf true, wenn auch nach PDF transformiert werden soll, |
|
70 |
* sonst wird nur nach HTML transformiert |
|
71 |
*/ |
|
72 |
private void processAdocFile(File adocfile, boolean createPdf) { |
|
73 |
String absname = adocfile.getAbsolutePath(); |
|
74 |
File htmlfile = getTargetFile(adocfile, "html"); |
|
75 |
AdocWorker worker = null; |
|
76 |
if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) { |
|
77 |
worker = new AdocWorker(); |
|
78 |
worker.transform(absname); |
|
79 |
} |
|
80 |
if(createPdf) { |
|
81 |
String pdfStr = "pdf"; |
|
82 |
File pdffile = getTargetFile(adocfile, pdfStr); |
|
83 |
if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { |
|
84 |
if(!(worker instanceof AdocWorker)) { |
|
85 |
worker = new AdocWorker(); |
|
86 |
} |
|
87 |
worker.transform(absname, pdfStr); |
|
88 |
} |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
public File getTargetFile(File adocfile, String ext) { |
|
93 |
String dot = "."; |
|
94 |
String nameext = adocfile.getName(); |
|
95 |
String fname = nameext.substring(0, nameext.lastIndexOf(dot)); |
|
96 |
File outfile = new File(adocfile.getParentFile(), fname + dot + ext); |
|
97 |
return outfile; |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public String description() { |
|
102 |
return "Asciidoctor Filter"; |
|
103 |
} |
|
104 |
} |