| | |
| | | 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; |
| | |
| | | import static org.asciidoctor.OptionsBuilder.options; |
| | | import static org.asciidoctor.Asciidoctor.Factory.create; |
| | | import org.asciidoctor.Asciidoctor; |
| | | import org.asciidoctor.OptionsBuilder; |
| | | |
| | | |
| | | |
| | |
| | | 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"; |
| | | |
| | |
| | | // 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); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | 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(); |
| | | private void transform(String fileName) { |
| | | transform(fileName, null); |
| | | } |
| | | asciidoctor.convertFile(new File(absoluteFileName), options); |
| | | |
| | | 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); |
| | | } |
| | | |
| | | /** |