/*
|
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.HttpExchange;
|
import de.uhilger.neon.FileServer;
|
import java.io.File;
|
import java.io.IOException;
|
|
/**
|
* 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 {
|
String fileBase = exchange.getHttpContext().getAttributes()
|
.get(FileServer.ATTR_FILE_BASE).toString();
|
String fileName = exchange
|
.getRequestURI()
|
.getPath()
|
.substring(exchange
|
.getHttpContext()
|
.getPath()
|
.length());
|
if(fileName.endsWith(".adoc")) {
|
String query = exchange.getRequestURI().getQuery();
|
if (query instanceof String && query.contains("pdf=true")) {
|
processAdocFile(new File(fileBase, fileName), true);
|
} else {
|
processAdocFile(new File(fileBase, fileName), false);
|
}
|
}
|
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";
|
}
|
}
|