/*
|
neon-adoc - Asciidoctor extensions to Neon
|
Copyright (C) 2024 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.neon.adoc;
|
|
import com.sun.net.httpserver.Filter;
|
import com.sun.net.httpserver.HttpContext;
|
import com.sun.net.httpserver.HttpExchange;
|
import de.uhilger.neon.FileServer;
|
import de.uhilger.neon.HttpHelper;
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.URI;
|
|
/**
|
* AdocFilter prueft, ob fuer eine Asciidoctor-Quelldatei bereits eine HTML-Version vorliegt. Wenn
|
* nicht, verwendet der AdocFilter den AdocActor, um eine HTML-Version zu erzeugen.
|
*
|
* @author Ulrich Hilger
|
* @version 1, 16.06.2021
|
*/
|
public class AdocFilter extends Filter {
|
|
@Override
|
public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
|
URI uri = exchange.getRequestURI();
|
try {
|
String fileName = new HttpHelper().getFileName(exchange);
|
if(fileName.endsWith(".adoc")) {
|
HttpContext ctx = exchange.getHttpContext();
|
String fileBase = ctx.getAttributes().get(FileServer.ATTR_FILE_BASE).toString();
|
String query = uri.getQuery();
|
if (query instanceof String && query.contains("pdf=true")) {
|
processAdocFile(new File(fileBase, fileName), true);
|
} else {
|
processAdocFile(new File(fileBase, fileName), false);
|
}
|
}
|
} catch (IllegalArgumentException ex) {
|
// ungueltiger Dateiname, keine Transformation noetig, also tue nichts
|
} finally {
|
chain.doFilter(exchange);
|
}
|
|
}
|
|
/**
|
* Eine Asciidoc-Quelldatei nach HTML und optional PDF transformieren, sofern
|
* eine HTML-Datei noch nicht existiert oder die HTML-Datei aelter ist, als
|
* die Asciidoc-Quelldatei
|
*
|
* @param adocfile das Objekt, das auf die Quelldatei im Asciidoc-Format verweist
|
* @param createPdf true, wenn auch nach PDF transformiert werden soll,
|
* sonst wird nur nach HTML transformiert
|
*/
|
private void processAdocFile(File adocfile, boolean createPdf) {
|
String absname = adocfile.getAbsolutePath();
|
File htmlfile = getTargetFile(adocfile, "html");
|
AdocWorker worker = null;
|
if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) {
|
worker = new AdocWorker();
|
worker.transform(absname);
|
}
|
if(createPdf) {
|
String pdfStr = "pdf";
|
File pdffile = getTargetFile(adocfile, pdfStr);
|
if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) {
|
if(!(worker instanceof AdocWorker)) {
|
worker = new AdocWorker();
|
}
|
worker.transform(absname, pdfStr);
|
}
|
}
|
}
|
|
public File getTargetFile(File adocfile, String ext) {
|
String dot = ".";
|
String nameext = adocfile.getName();
|
String fname = nameext.substring(0, nameext.lastIndexOf(dot));
|
File outfile = new File(adocfile.getParentFile(), fname + dot + ext);
|
return outfile;
|
}
|
|
@Override
|
public String description() {
|
return "Asciidoctor Filter";
|
}
|
}
|