Skip to content

Commit

Permalink
generate browsable site
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Feb 28, 2016
1 parent 1eb823d commit 43916d3
Show file tree
Hide file tree
Showing 11 changed files with 492 additions and 54 deletions.
177 changes: 177 additions & 0 deletions src/main/java/org/tipitaka/search/Builder.java
@@ -0,0 +1,177 @@
package org.tipitaka.search;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Created by cmeier on 2/28/16.
*/
class Builder
{

final DirectoryStructure directory;

final Script script;

final Writer writer;

final Map<String, String> breadCrumbs;

final String path;

final boolean isSubdir;

final String url;
Builder(DirectoryStructure directory, Script script, Writer writer, String path) throws IOException {
this(directory, script, writer, path, null);
}

Builder(DirectoryStructure directory, Script script, Writer writer, String path, String url) throws IOException {
this.url = url;
if (url == null) {
this.isSubdir = directory.fileOf(path) == null;
if (isSubdir) {
this.path = path.replaceFirst("/[^/]+$", "");
;
}
else {
this.path = path.replaceFirst("[^/]+$", "");
}
this.breadCrumbs = directory.breadCrumbs(script, this.path);
}
else {
this.isSubdir = false;
this.path = path;
this.breadCrumbs = directory.breadCrumbs(script, this.path);//.replaceFirst("[^/]+$", ""));
}
this.directory = directory;
this.script = script;
this.writer = writer;
}

public Builder buildDir() throws IOException {
if (isSubdir) {
return buildSubdir();
}
return buildLeafdir();
}

public Builder buildSubdir() throws IOException {
startHtmlBody();
appendNavigation("/index.html");
endBodyHtml();
return this;
}

public Builder buildLeafdir() throws IOException {
startHtmlBody();
appendNavigation(".html");
endBodyHtml();
return this;
}

public Builder appendTitle() throws IOException {
writer.append("<title>");
boolean first = true;
List<String> parts = new LinkedList<String>(breadCrumbs.values());
Collections.reverse(parts);
for (String part : parts) {
if (part != null) {
if (first) {
first = false;
}
else {
writer.append(" - ");
}
writer.append(part);
}
}
writer.append("</title>");
return this;
}

public Builder startHtmlBody() throws IOException {
writer.append("<!doctype html>\n" +
"<html>\n" +
"<head>\n" +
"<link rel=\"stylesheet\" href=\"/nav.css\">\n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
if (this.url != null) {
writer.append("<meta name=\"normative-source\" content=\"").append(url).append("\" />\n");
}
writer.append("<meta name=\"archive-path\" content=\"").append(this.path).append(".html\" />\n");

appendTitle();

writer.append("</head>\n<body>\n");

return this;
}

public Builder endBodyHtml() throws IOException {
writer.append("</body>\n</html>\n");
return this;
}

public Builder appendNavigation(String postfix) throws IOException {
writer.append("<div class=\"navigation-container\">\n" +
"<div class=\"navigation\">\n");
writer.append("<span>");
if ("/".equals(path)) {
writer.append("ROOT");
}
else {
writer.append("<a href=\"").append("/index.html\">ROOT</a>");
}
writer.append("</span>");
List<Map.Entry<String, String>> crumbs = new LinkedList<Map.Entry<String, String>>(breadCrumbs.entrySet());
for (Map.Entry<String, String> part : crumbs) {
if (part.getValue() != null) {
boolean hilight = path.length() == part.getKey().length() || path.length() == part.getKey().length() +1;
if ((!hilight && url != null) || url == null) {
writer.append("<span> - </span>\n");
writer.append("<span>");
if (hilight) {
writer.append(part.getValue());
}
else {
writer.append("<a href=\"").append(part.getKey()).append("/index.html\">")
.append(part.getValue()).append("</a>");
}
writer.append("</span>");
}
}
}
writer.append("<span> - </span>\n");
writer.append("<span class=\"context-toc\">\n");

for (Map.Entry<String, String> entry : this.directory.list(this.script, this.path).entrySet()) {

writer.append("<div>");
if (path.equals(entry.getKey())) {
writer.append(entry.getValue());
}
else {
writer.append("<a href=\"").append(entry.getKey()).append(postfix).append("\">")
.append(entry.getValue()).append("</a>");

}
writer.append("</div>\n");
}

writer.append("</span>\n");


writer.append("\n</div>\n</div>\n");

return this;
}

public void flush() throws IOException {
writer.flush();
}
}
39 changes: 27 additions & 12 deletions src/main/java/org/tipitaka/search/DirectoryStructure.java
Expand Up @@ -12,6 +12,7 @@
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -89,7 +90,11 @@ public static void main(String... args) throws Exception {
System.err.println(System.currentTimeMillis() - start2);
factory.newHtmlBuilder(new FileWriter("/home/kristian/deva-test-mini.html"), new TipitakaPath("", path)).buildMinimal();
}


public Collection<String> allPaths() {
return map.keySet();
}

public Map<String, String> list(Script words, String path) {
if(map.containsKey(path)){
return listFiles(words, path);
Expand Down Expand Up @@ -128,19 +133,24 @@ public Map<String, String> listFiles(Script words, String path) {

public Map<String, String> breadCrumbs(Script trans, String path) {
Map<String, String> result = new LinkedHashMap<String, String>();
if(path != null && !"/".equals(path)){
if(path != null && !"/".equals(path)) {
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
int from = 1;
int next = path.indexOf("/", from);
while (next > -1) {
result.put(path.substring(0, next), trans.get(path.substring(
from, next)));
from, next)));
from = next + 1;
next = path.indexOf("/", from);
}
result.put(path, trans.get(path.substring(from)));
if (path.length() == 0) {
result.put(path, "ROOT");
}
else {
result.put(path, trans.get(path.substring(from)));
}
}
return result;
}
Expand All @@ -165,13 +175,15 @@ void reload() throws XmlPullParserException, IOException{
parts.clear();
for(String part: entry.getValue()){
if(part != null){
parts.add(RomanScriptHelper.removeDiacritcals(part.replaceFirst("[(][0-9]+[)]", "").replaceFirst("[0-9]+\\.\\ ", "")
.replaceFirst("(bhikkhunīvibhaṅgo)", "- bhikkhunīvibhaṅgo")//.replaceFirst("\\ [(].*[)]", "")
.replaceAll("[()]","").replace("’", " "))
.toLowerCase().trim().replaceAll(" ", "_"));
parts.add(RomanScriptHelper.removeDiacritcals(part//.replaceFirst("[(][0-9]+[)]", "").replaceFirst("[0-9]+\\.\\ ", "")
//.replaceFirst("(bhikkhunīvibhaṅgo)", "- bhikkhunīvibhaṅgo")//.replaceFirst("\\ [(].*[)]", "")
//.replaceAll("[()]","").replace("’", " "))
.replaceFirst("\\.$", "")
.toLowerCase().trim()));//.replaceAll(" ", "_"));
}
}
String path = root.addLeaf(entry.getKey(), parts);
while (map.containsKey(path)) path += "_";
map.put(path, entry.getKey());
rmap.put(entry.getKey(), path);
}
Expand All @@ -195,11 +207,12 @@ Script transcribe(String script) throws XmlPullParserException, IOException{
if(rmap.containsKey(entry.getKey())){
String[] paths = rmap.get(entry.getKey()).substring(1).split("/");
for(int i = 0; i < paths.length; i++){
System.err.println(paths[i] + " <> " + parts.get(i));
words.put(paths[i], parts.get(i));
}
}
else {
System.err.println("---" + entry.getKey());
//System.err.println("---" + entry.getKey());
}
}
return words;
Expand Down Expand Up @@ -240,7 +253,10 @@ public void load(Reader reader) throws IOException{
}
}
}


public String pathOfOrignalFile(String path) {
return rmap.get(path);
}
public String fileOf(String path){
return map.get(path);
}
Expand All @@ -253,7 +269,6 @@ Set<String> subdirs(String path){
return EMPTY;
}
path = path.replaceAll("^/|/$", "");
System.err.println("--- " + path);
return root.getNode(path.split("/")).children.keySet();
}

Expand Down Expand Up @@ -316,4 +331,4 @@ Node getNode(String... parts){
return n;
}
}
}
}
8 changes: 2 additions & 6 deletions src/main/java/org/tipitaka/search/HtmlBuilder.java
Expand Up @@ -30,7 +30,7 @@ public HtmlBuilder(Writer writer, String prefix, Script script, String path,
this.factory = factory;
try {
this.tipitaka = new TipitakaOrgVisitorHtml(urlFactory);
} catch (XmlPullParserException e) {
} catch (Exception e) {
throw new RuntimeException("error creating html visitor", e);
}
}
Expand Down Expand Up @@ -58,11 +58,7 @@ public void buildPage() throws IOException {
String file = structure.fileOf(path.replaceFirst("\\.[a-z]+$", ""));
if(file != null){
writer.append(" <div class='page'>\n");
try {
tipitaka.accept(writer, script.tipitakaOrgName, file);
} catch (XmlPullParserException e) {
throw new RuntimeException("parse error", e);
}
tipitaka.accept(writer, script, file);
writer.append(" </div>\n");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/tipitaka/search/Main.java
Expand Up @@ -9,7 +9,7 @@ public class Main {
private static final int DONE = 1000;

enum Command {
MIRROR, INDEX, DIRECTORY_STRUCTURE, DIRECTORY_TRANSLATION
MIRROR, INDEX, DIRECTORY_STRUCTURE, DIRECTORY_TRANSCRIBE
}
public static void main(String... args) throws Exception {
if( args.length == 0){
Expand Down Expand Up @@ -52,15 +52,15 @@ public static int run(int offset, String... args) throws Exception {
dir.reload();
dir.save(new File(basedir, "directory.map"));
return 2;
case DIRECTORY_TRANSLATION :
case DIRECTORY_TRANSCRIBE:
if (args.length == offset + 2) {
throw new RuntimeException(
"no <script> or <script,script> or ... given");
} else {
dir = new DirectoryStructure(urlFactory);
dir.load(new File(basedir, "directory.map"));
Script script = dir.transcribe(args[offset + 2]);
script.save(new File(basedir, script.name + ".map"));
script.save(new File(basedir, script.name + ".script"));
return 3;
}
default :
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tipitaka/search/Script.java
Expand Up @@ -31,7 +31,7 @@ void put(String key, String value){
String get(String key){
return words.getProperty(key);
}

public void save(File file) throws IOException{
save(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tipitaka/search/ScriptFactory.java
Expand Up @@ -45,6 +45,6 @@ public Script script(String scriptName) throws IOException{
}

public Script newScript(String script) {
return new Script(script, INVERSE.get(script));
return new Script(MAP.get(script), script);
}
}

0 comments on commit 43916d3

Please sign in to comment.