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