From 3bbfbc4952bf689a2f9024edc542ef609bc5c672 Mon Sep 17 00:00:00 2001 From: ulrich@undisclosed Date: Tue, 21 Apr 2020 15:41:33 +0000 Subject: [PATCH] Code fuer die Transformation ueberarbeitet --- src/de/uhilger/wbx/web/AdocServlet.java | 62 +++++++++++++++--------------- 1 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/de/uhilger/wbx/web/AdocServlet.java b/src/de/uhilger/wbx/web/AdocServlet.java index e0c43d2..73f459e 100644 --- a/src/de/uhilger/wbx/web/AdocServlet.java +++ b/src/de/uhilger/wbx/web/AdocServlet.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -34,6 +35,7 @@ import static org.asciidoctor.OptionsBuilder.options; import static org.asciidoctor.Asciidoctor.Factory.create; import org.asciidoctor.Asciidoctor; +import org.asciidoctor.OptionsBuilder; @@ -46,8 +48,7 @@ public class AdocServlet extends HttpServlet { private static final String DOT = "."; - private static final String HTMLEXT = ".html"; - private static final String PDFEXT = ".pdf"; + private static final String HTML = "html"; private static final String PDF = "pdf"; private static final String SERVLET_NAME = "AdocServlet"; @@ -74,21 +75,26 @@ // HTML-Datei ermitteln String nameext = adocfile.getName(); String fname = nameext.substring(0, nameext.lastIndexOf(DOT)); - File htmlfile = new File(adocfile.getParentFile(), fname + HTMLEXT); + File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML); /* - transformieren, wenn die Quelle sich geandert hat oder + nach HTML transformieren, wenn die Quelle sich geandert hat oder die HTML-Datei noch nicht existiert */ if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) { - transform(absname, htmlfile); + transform(absname); } + /* + nach PDF transformieren, wenn der Parameter pdf=true existiert und + wenn die Quelle sich geandert hat oder + die PDF-Datei noch nicht existiert + */ String pdf = request.getParameter(PDF); if(null != pdf && pdf.length() > 0 && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) { - File pdffile = new File(adocfile.getParentFile(), fname + PDFEXT); + File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF); if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { - transform(absname, pdffile); + transform(absname, PDF); } } @@ -102,31 +108,25 @@ } } - private void transform(String absoluteFileName, File target) { - Map<String, Object> options = null; - Map<String, Object> attributes; - Asciidoctor asciidoctor = create(); - String fname = target.getName().toLowerCase(); - - if(fname.endsWith(HTMLEXT)) { - attributes = attributes().noFooter(false) - .sourceHighlighter("highlightjs") - //.tableOfContents(true) - //.sectionNumbers(true) - .asMap(); - options = options().inPlace(false) - .attributes(attributes) - .asMap(); - } else if(fname.endsWith(PDFEXT)) { - attributes = attributes().noFooter(false) - .asMap(); - options = options().inPlace(false) - .attributes(attributes) - .backend("pdf") - .asMap(); - } - asciidoctor.convertFile(new File(absoluteFileName), options); + private void transform(String fileName) { + transform(fileName, null); } + + private void transform(String fileName, String backend) { + Map<String, Object> attributes = new HashMap<>(); + attributes.put("no_footer", false); + attributes.put("source_highlighter", "highlightjs"); + + Map<String, Object> options = new HashMap<>(); + options.put("attributes", attributes); + options.put("in_place", false); + if(null != backend) { + options.put("backend", backend); + } + + Asciidoctor asciidoctor = create(); + asciidoctor.convertFile(new File(fileName), options); + } /** * Handles the HTTP <code>GET</code> method. -- Gitblit v1.9.3