Debian Codegraph @

src/tools/org/h2/build/doc/WebSite.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.IOException;
9 import java.nio.charset.StandardCharsets;
10 import java.nio.file.DirectoryStream;
11 import java.nio.file.FileVisitResult;
12 import java.nio.file.Files;
13 import java.nio.file.Path;
14 import java.nio.file.Paths;
15 import java.nio.file.SimpleFileVisitor;
16 import java.nio.file.attribute.BasicFileAttributes;
17 import java.util.HashMap;
18
19 import org.h2.build.BuildBase;
20 import org.h2.samples.Newsfeed;
21 import org.h2.util.StringUtils;
22
23 /**
24 * Create the web site, mainly by copying the regular docs. A few items are
25 * different in the web site, for example it calls web site analytics.
26 * Also, the main entry point page is different.
27 * The newsfeeds are generated here as well.
28 */
29 public class WebSite {
30
31 private static final String ANALYTICS_TAG = "<!-- analytics -->";
32 private static final String ANALYTICS_SCRIPT = "";
33 private static final String TRANSLATE_START = "<!-- translate";
34 private static final String TRANSLATE_END = "translate -->";
35
36 private static final Path SOURCE_DIR = Paths.get("docs");
37 private static final Path WEB_DIR = Paths.get("../h2web");
38 private final HashMap<String, String> fragments = new HashMap<>();
39
40 /**
41 * This method is called when executing this application from the command
42 * line.
43 *
44 * @param args the command line parameters
45 */
46 public static void main(String... args) throws Exception {
47 new WebSite().run();
48 }
49
50 private void run() throws Exception {
51 // create the web site
52 BuildBase.deleteRecursive(WEB_DIR);
53 loadFragments();
54 copy(SOURCE_DIR, WEB_DIR, true, true);
55 Newsfeed.main(WEB_DIR + "/html");
56
57 // create the internal documentation
58 copy(SOURCE_DIR, SOURCE_DIR, true, false);
59 }
60
61 private void loadFragments() throws IOException {
62 try (DirectoryStream<Path> stream = Files.newDirectoryStream(SOURCE_DIR.resolve("html"), "fragments*")) {
63 for (Path f : stream) {
64 fragments.put(f.getFileName().toString(), new String(Files.readAllBytes(f), StandardCharsets.UTF_8));
65 }
66 }
67 }
68
69 private String replaceFragments(String fileName, String page) {
70 if (fragments.size() == 0) {
71 return page;
72 }
73 String language = "";
74 int index = fileName.indexOf('_');
75 if (index >= 0) {
76 int end = fileName.indexOf('.');
77 language = fileName.substring(index, end);
78 }
79 String fragment = fragments.get("fragments" + language + ".html");
80 int start = 0;
81 while (true) {
82 start = fragment.indexOf("<!-- [", start);
83 if (start < 0) {
84 break;
85 }
86 int endTag = fragment.indexOf("] { -->", start);
87 int endBlock = fragment.indexOf("<!-- } -->", start);
88 String tag = fragment.substring(start, endTag);
89 String replacement = fragment.substring(start, endBlock);
90 int pageStart = 0;
91 while (true) {
92 pageStart = page.indexOf(tag, pageStart);
93 if (pageStart < 0) {
94 break;
95 }
96 int pageEnd = page.indexOf("<!-- } -->", pageStart);
97 page = page.substring(0, pageStart) + replacement + page.substring(pageEnd);
98 pageStart += replacement.length();
99 }
100 start = endBlock;
101 }
102 return page;
103 }
104
105 private void copy(Path source, Path target, boolean replaceFragments, boolean web) throws IOException {
106 Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
107 @Override
108 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
109 Files.createDirectories(target.resolve(source.relativize(dir)));
110 return FileVisitResult.CONTINUE;
111 }
112 @Override
113 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
114 copyFile(file, target.resolve(source.relativize(file)), replaceFragments, web);
115 return super.visitFile(file, attrs);
116 }
117 });
118 }
119
120 /**
121 * Copy a file.
122 *
123 * @param source the source file
124 * @param target the target file
125 * @param replaceFragments whether to replace fragments
126 * @param web whether the target is a public web site (false for local documentation)
127 */
128 void copyFile(Path source, Path target, boolean replaceFragments, boolean web) throws IOException {
129 String name = source.getFileName().toString();
130 if (name.endsWith("onePage.html") || name.startsWith("fragments")) {
131 return;
132 }
133 if (web) {
134 if (name.endsWith("main.html")) {
135 return;
136 }
137 } else {
138 if (name.endsWith("mainWeb.html")) {
139 return;
140 }
141 }
142 byte[] bytes = Files.readAllBytes(source);
143 if (name.endsWith(".html")) {
144 String page = new String(bytes, StandardCharsets.UTF_8);
145 if (web) {
146 page = StringUtils.replaceAll(page, ANALYTICS_TAG, ANALYTICS_SCRIPT);
147 }
148 if (replaceFragments) {
149 page = replaceFragments(name, page);
150 page = StringUtils.replaceAll(page, "<a href=\"frame", "<a href=\"main");
151 page = StringUtils.replaceAll(page, "html/frame.html", "html/main.html");
152 }
153 if (web) {
154 page = StringUtils.replaceAll(page, TRANSLATE_START, "");
155 page = StringUtils.replaceAll(page, TRANSLATE_END, "");
156 page = StringUtils.replaceAll(page, "<pre>", "<pre class=\"notranslate\">");
157 page = StringUtils.replaceAll(page, "<code>", "<code class=\"notranslate\">");
158 }
159 if (name.endsWith("changelog.html")) {
160 page = page.replaceAll("Issue\\s+#?(\\d+)",
161 "<a href=\"https://github.com/h2database/h2database/issues/$1\">Issue #$1</a>");
162 page = page.replaceAll("PR\\s+#?(\\d+)",
163 "<a href=\"https://github.com/h2database/h2database/pull/$1\">PR #$1</a>");
164 }
165 bytes = page.getBytes(StandardCharsets.UTF_8);
166 }
167 Files.write(target, bytes);
168 if (web) {
169 if (name.endsWith("mainWeb.html")) {
170 Files.move(target, target.getParent().resolve("main.html"));
171 }
172 }
173 }
174
175 }