/* * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (https://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.build.doc; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import org.h2.engine.Constants; import org.h2.util.StringUtils; /** * This application merges the html documentation to one file * (onePage.html), so that the PDF document can be created. */ public class MergeDocs { private static final String BASE_DIR = "docs/html"; /** * This method is called when executing this application from the command * line. * * @param args the command line parameters */ public static void main(String... args) throws Exception { // the order of pages is important here String[] pages = { "quickstart.html", "installation.html", "tutorial.html", "features.html", "security.html", "performance.html", "advanced.html", "commands.html", "functions.html", "functions-aggregate.html", "functions-window.html", "datatypes.html", "grammar.html", "systemtables.html", "build.html", "history.html", "faq.html" }; StringBuilder buff = new StringBuilder(); for (String fileName : pages) { String text = getContent(fileName); for (String page : pages) { text = StringUtils.replaceAll(text, page + "#", "#"); } text = disableRailroads(text); text = removeHeaderFooter(fileName, text); text = fixLinks(text); text = fixTableBorders(text); text = addLegacyFontTag(fileName, text); buff.append(text); } String finalText = buff.toString(); PrintWriter writer = new PrintWriter(Files.newBufferedWriter(Paths.get(BASE_DIR, "onePage.html"))); writer.println(""); writer.println("H2 Documentation"); writer.println(""); writer.println("

H2 Database Engine

"); writer.println("

Version " + Constants.FULL_VERSION + "

"); writer.println(finalText); writer.println(""); writer.close(); } private static String disableRailroads(String text) { text = StringUtils.replaceAll(text, "", "", " railroad-end -->"); text = StringUtils.replaceAll(text, ""); text = StringUtils.replaceAll(text, "syntax-end -->", ""); return text; } private static String addLegacyFontTag(String fileName, String text) { int idx1 = text.indexOf("> 4)); do { builder.append(text, idx2, idx1); boolean compat = text.regionMatches(idx1 + 17, "Compat\">", 0, 8); boolean h2 = text.regionMatches(idx1 + 17, "H2\">", 0, 4); if (compat == h2) { throw new RuntimeException("Unknown BNF rule style in file " + fileName); } idx2 = text.indexOf("", idx1 + (compat ? 8 : 4)); if (idx2 <= 0) { throw new RuntimeException(" not found in file " + fileName); } idx2 += 7; builder.append("") .append(text, idx1, idx2).append(""); idx1 = text.indexOf("= 0); return builder.append(text, idx2, length).toString(); } private static String removeHeaderFooter(String fileName, String text) { // String start = ""; int idx = text.indexOf(end); if (idx < 0) { throw new RuntimeException("Footer not found in file " + fileName); } text = text.substring(0, idx); idx = text.indexOf(start) + start.length(); text = text.substring(idx + 1); return text; } private static String fixLinks(String text) { return text .replaceAll("href=\"build.html\"", "href=\"#build_index\"") .replaceAll("href=\"datatypes.html\"", "href=\"#datatypes_index\"") .replaceAll("href=\"faq.html\"", "href=\"#faq_index\"") .replaceAll("href=\"commands.html\"", "href=\"#commands_index\"") .replaceAll("href=\"grammar.html\"", "href=\"#grammar_index\"") .replaceAll("href=\"functions.html\"", "href=\"#functions_index\"") .replaceAll("href=\"functions-aggregate.html\"", "href=\"#functions_aggregate_index\"") .replaceAll("href=\"functions-window.html\"", "href=\"#functions_window_index\"") .replaceAll("href=\"tutorial.html\"", "href=\"#tutorial_index\""); } private static String fixTableBorders(String text) { return text .replaceAll("", "
"); } private static String getContent(String fileName) throws Exception { return new String(Files.readAllBytes(Paths.get(BASE_DIR, fileName)), StandardCharsets.UTF_8); } }