Erweiterung von neon zur Transformation von Asciidoc
ulrich
2024-02-25 e76d49eb05e0ab3280c9cac285c274b5b6f38fab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
  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 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();
    String path = uri.getPath();
    if(path.endsWith(".adoc")) {
      HttpContext ctx = exchange.getHttpContext();
      String fileBase = ctx.getAttributes().get(FileServer.ATTR_FILE_BASE).toString();
      String fileName = path.substring(ctx.getPath().length());
      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);
      }
    }
    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";
  }  
}