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