Debian Codegraph @

src/tools/org/h2/build/doc/MergeDocs.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.build.doc;
7
8 import java.io.PrintWriter;
9 import java.nio.charset.StandardCharsets;
10 import java.nio.file.Files;
11 import java.nio.file.Paths;
12
13 import org.h2.engine.Constants;
14 import org.h2.util.StringUtils;
15
16 /**
17 * This application merges the html documentation to one file
18 * (onePage.html), so that the PDF document can be created.
19 */
20 public class MergeDocs {
21
22 private static final String BASE_DIR = "docs/html";
23
24 /**
25 * This method is called when executing this application from the command
26 * line.
27 *
28 * @param args the command line parameters
29 */
30 public static void main(String... args) throws Exception {
31 // the order of pages is important here
32 String[] pages = { "quickstart.html", "installation.html",
33 "tutorial.html", "features.html", "security.html", "performance.html",
34 "advanced.html", "commands.html",
35 "functions.html", "functions-aggregate.html", "functions-window.html",
36 "datatypes.html", "grammar.html", "systemtables.html",
37 "build.html", "history.html", "faq.html" };
38 StringBuilder buff = new StringBuilder();
39 for (String fileName : pages) {
40 String text = getContent(fileName);
41 for (String page : pages) {
42 text = StringUtils.replaceAll(text, page + "#", "#");
43 }
44 text = disableRailroads(text);
45 text = removeHeaderFooter(fileName, text);
46 text = fixLinks(text);
47 text = fixTableBorders(text);
48 text = addLegacyFontTag(fileName, text);
49 buff.append(text);
50 }
51 String finalText = buff.toString();
52 PrintWriter writer = new PrintWriter(Files.newBufferedWriter(Paths.get(BASE_DIR, "onePage.html")));
53 writer.println("<html><head><meta http-equiv=\"Content-Type\" " +
54 "content=\"text/html;charset=utf-8\" /><title>");
55 writer.println("H2 Documentation");
56 writer.println("</title><link rel=\"stylesheet\" type=\"text/css\" " +
57 "href=\"stylesheetPdf.css\" /></head><body>");
58 writer.println("<p class=\"title\">H2 Database Engine</p>");
59 writer.println("<p>Version " + Constants.FULL_VERSION + "</p>");
60 writer.println(finalText);
61 writer.println("</body></html>");
62 writer.close();
63 }
64
65 private static String disableRailroads(String text) {
66 text = StringUtils.replaceAll(text,
67 "<!-- railroad-start -->",
68 "<!-- railroad-start ");
69 text = StringUtils.replaceAll(text,
70 "<!-- railroad-end -->",
71 " railroad-end -->");
72 text = StringUtils.replaceAll(text,
73 "<!-- syntax-start",
74 "<!-- syntax-start -->");
75 text = StringUtils.replaceAll(text,
76 "syntax-end -->",
77 "<!-- syntax-end -->");
78 return text;
79 }
80
81 private static String addLegacyFontTag(String fileName, String text) {
82 int idx1 = text.indexOf("<span class=\"rule");
83 if (idx1 < 0) {
84 return text;
85 }
86 int idx2 = 0, length = text.length();
87 StringBuilder builder = new StringBuilder(length + (length >> 4));
88 do {
89 builder.append(text, idx2, idx1);
90 boolean compat = text.regionMatches(idx1 + 17, "Compat\">", 0, 8);
91 boolean h2 = text.regionMatches(idx1 + 17, "H2\">", 0, 4);
92 if (compat == h2) {
93 throw new RuntimeException("Unknown BNF rule style in file " + fileName);
94 }
95 idx2 = text.indexOf("</span>", idx1 + (compat ? 8 : 4));
96 if (idx2 <= 0) {
97 throw new RuntimeException("</span> not found in file " + fileName);
98 }
99 idx2 += 7;
100 builder.append("<font color=\"").append(compat ? "darkred" : "green").append("\">")
101 .append(text, idx1, idx2).append("</font>");
102 idx1 = text.indexOf("<span class=\"rule", idx2);
103 } while (idx1 >= 0);
104 return builder.append(text, idx2, length).toString();
105 }
106
107 private static String removeHeaderFooter(String fileName, String text) {
108 // String start = "<body";
109 // String end = "</body>";
110
111 String start = "<!-- } -->";
112 String end = "<!-- [close] { --></div></td></tr></table>" +
113 "<!-- } --><!-- analytics --></body></html>";
114
115 int idx = text.indexOf(end);
116 if (idx < 0) {
117 throw new RuntimeException("Footer not found in file " + fileName);
118 }
119 text = text.substring(0, idx);
120 idx = text.indexOf(start) + start.length();
121 text = text.substring(idx + 1);
122 return text;
123 }
124
125 private static String fixLinks(String text) {
126 return text
127 .replaceAll("href=\"build.html\"", "href=\"#build_index\"")
128 .replaceAll("href=\"datatypes.html\"", "href=\"#datatypes_index\"")
129 .replaceAll("href=\"faq.html\"", "href=\"#faq_index\"")
130 .replaceAll("href=\"commands.html\"", "href=\"#commands_index\"")
131 .replaceAll("href=\"grammar.html\"", "href=\"#grammar_index\"")
132 .replaceAll("href=\"functions.html\"", "href=\"#functions_index\"")
133 .replaceAll("href=\"functions-aggregate.html\"", "href=\"#functions_aggregate_index\"")
134 .replaceAll("href=\"functions-window.html\"", "href=\"#functions_window_index\"")
135 .replaceAll("href=\"tutorial.html\"", "href=\"#tutorial_index\"");
136 }
137
138 private static String fixTableBorders(String text) {
139 return text
140 .replaceAll("<table class=\"main\">",
141 "<table class=\"main\" border=\"1\" cellpadding=\"5\" cellspacing=\"0\">");
142 }
143
144 private static String getContent(String fileName) throws Exception {
145 return new String(Files.readAllBytes(Paths.get(BASE_DIR, fileName)), StandardCharsets.UTF_8);
146 }
147 }