-
Notifications
You must be signed in to change notification settings - Fork 1
/
HtmlBuilder.java
155 lines (139 loc) · 5.79 KB
/
HtmlBuilder.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
*
*/
package org.tipitaka.search;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Map;
import org.xmlpull.v1.XmlPullParserException;
public class HtmlBuilder {
final Writer writer;
private final Script script;
private final DirectoryStructure structure;
private final TipitakaOrgVisitorHtml tipitaka;
private final String path;
private final String prefix;
private final ScriptFactory factory;
public HtmlBuilder(Writer writer, String prefix, Script script, String path,
DirectoryStructure structure, ScriptFactory factory, TipitakaUrlFactory urlFactory) {
this.writer = writer;
this.path = path;
this.prefix = prefix;
this.script = script;
this.structure = structure;
this.factory = factory;
try {
this.tipitaka = new TipitakaOrgVisitorHtml(urlFactory);
} catch (Exception e) {
throw new RuntimeException("error creating html visitor", e);
}
}
public void buildAll() throws IOException {
buildHeader();
buildScriptNavigation();
buildBreadCrumbs();
buildViewLink();
buildSubmenu();
buildPage();
buildFooter();
}
public void buildMinimal() throws IOException {
buildHeader(null);
buildPage();
buildFooter();
}
public void buildPage() throws IOException {
if(path == null || "/".equals(path)){
return;
}
String file = structure.fileOf(path.replaceFirst("\\.[a-z]+$", ""));
if(file != null){
writer.append(" <div class='page'>\n");
tipitaka.accept(writer, script, file);
writer.append(" </div>\n");
}
}
void buildHeader() throws IOException {
buildHeader("/main.css");
}
void buildHeader(String css) throws IOException {
writer.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
" <head>\n" +
" <title>");
Collection<String> crumbs = structure.breadCrumbs(script, path).values();
for(String crumb : crumbs){
writer.append(crumb).append("/");
}
writer.append("</title>\n");
writer.append(" <meta name='tipitaka-script' content='").append(script.name).append("' />\n");
writer.append(" <meta name='tipitaka-path' content='").append(path).append("' />\n");
writer.append(" <meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />\n");
if(css != null){
writer.append(" <link rel=\"stylesheet\" href=\"").append(css).append("\" />\n");
}
writer.append(" <link rel=\"stylesheet\" href=\"http://tipitaka.org/")
.append(script.tipitakaOrgName).append("/cscd/tipitaka-").append("romn".equals(script.tipitakaOrgName)? "latn" : script.tipitakaOrgName).append(".css\" />\n");
writer.append(" <link rel=\"icon\" type=\"image/ico\" href=\"http://tipitaka.org/favicon.ico\" />\n");
writer.append(" </head>\n" +
" <body>\n");
writer.flush();
}
void buildScriptNavigation() throws IOException {
writer.append(" <div class='scripts'>\n");
for(String script : factory.allScriptNames()){
writer.append(" <a href='").append(this.prefix).append("/").append(script).append(path)
.append("'>")
.append(script)
.append("</a>\n");
}
writer.append(" </div>\n");
}
void buildBreadCrumbs() throws IOException {
writer.append(" <div class='breadcrumbs'>\n" +
" <a href='").append(this.prefix).append("/").append(script.name).append("\'>ALL</a>\n");
Map<String,String> crumbs = structure.breadCrumbs(script, path);
for(Map.Entry<String, String> crumb : crumbs.entrySet()){
writer.append(" >\n" +
" <a href='").append(this.prefix).append("/").append(script.name)
.append(crumb.getKey())
.append("'>")
.append(crumb.getValue())
.append("</a>\n");
}
writer.append(" </div>\n");
}
void buildViewLink() throws IOException {
String file = structure.fileOf(path);
if( file != null){
writer.append(" <div class='views'>\n" +
" <a href='").append(this.prefix).append("/").append(script.name).append(path).append(".html'>full page</a>\n" +
" |\n" +
" <a href='").append(this.prefix).append("/").append(script.name).append(path).append(".tei.xml'>TEI format</a>\n" +
" |\n" +
" <a href='http://tipitaka.org/").append(script.tipitakaOrgName).append("/").append(file).append("'>page on tipitaka.org</a>\n" +
" </div>\n");
}
}
void buildSubmenu() throws IOException {
writer.append(" <div class='sidemenu'>\n");
Map<String, String> list = structure.list(script, path);
for (Map.Entry<String, String> item : list.entrySet()) {
writer.append(" <a href='")
.append(this.prefix)
.append("/")
.append(script.name)
.append(item.getKey())
.append("'>")
.append(item.getValue())
.append("</a>\n");
}
writer.append(" </div>\n");
}
void buildFooter() throws IOException {
writer.append(" </body>\n" +
"</html>\n");
writer.flush();
}
}