Skip to content

Instantly share code, notes, and snippets.

@codenameone
Created November 15, 2017 11:16
Show Gist options
  • Save codenameone/1e5c0796321619614ec0c0fb97ba9735 to your computer and use it in GitHub Desktop.
Save codenameone/1e5c0796321619614ec0c0fb97ba9735 to your computer and use it in GitHub Desktop.
public class DevGuideIndexGenerator {
/**
* Argument 0 should be the output asciidoc file
* Argument 1 should be the index file in the website
* Argument 2 onwards should be the files in the manual in the correct order
*/
public static void main(String[] args) throws Exception {
String version = args[0];
FileOutputStream asciidocFile = new FileOutputStream(args[1]);
FileWriter indexFile = new FileWriter(args[2]);
indexFile.write("<div id=\"toc\" class=\"toc2\">\n" +
"<div id=\"toctitle\">Table of Contents</div>\n" +
"<ul class=\"sectlevel1\">\n");
SimpleDateFormat sd = new SimpleDateFormat("MMM dd yyyy");
asciidocFile.write(("= Codename One Developer Guide\n" +
"Version " + version + ", " + sd.format(new Date()) + "\n" +
":doctype: book\n" +
"\n" +
":toc:\n" +
":toc-placement: manual\n\n" +
"toc::[]\n\n").getBytes());
for(int iter = 3 ; iter < args.length ; iter++) {
File f = new File(args[iter]);
byte[] currentFile = new byte[(int)f.length()];
DataInputStream di = new DataInputStream(new FileInputStream(f));
di.readFully(currentFile);
di.close();
String fileContent = new String(currentFile, "UTF-8");
int index = fileContent.indexOf("~~~~~~");
String header = fileContent.substring(0, index);
Properties props = new Properties();
props.load(new CharArrayReader(header.toCharArray()));
index = fileContent.indexOf("\n", index);
fileContent = fileContent.substring(index);
// remove all the HTML only content
fileContent = fileContent.replace("// HTML_ONLY_START", "////");
fileContent = fileContent.replace("// HTML_ONLY_END", "////");
// comment in "PDF_ONLY" sections
int pdfOnly = fileContent.indexOf("PDF_ONLY");
if(pdfOnly > -1) {
StringBuilder sb = new StringBuilder(fileContent);
while(pdfOnly > -1) {
// replace the block comment following the pdfOnly
int followingComment = fileContent.indexOf("////", pdfOnly);
sb.setCharAt(followingComment + 2, ' ');
sb.setCharAt(followingComment + 3, ' ');
int beforeComment = fileContent.lastIndexOf("////", pdfOnly);
sb.setCharAt(beforeComment + 2, ' ');
sb.setCharAt(beforeComment + 3, ' ');
pdfOnly = fileContent.indexOf("PDF_ONLY", followingComment + 2);
}
fileContent = sb.toString();
}
// first page is the preface
if(iter == 3) {
asciidocFile.write("\n[preface]\n== ".getBytes());
} else {
asciidocFile.write("\n\n== ".getBytes());
}
asciidocFile.write(props.getProperty("title").getBytes());
if(props.getProperty("subtitle") != null) {
asciidocFile.write("\n\n".getBytes());
asciidocFile.write(props.getProperty("subtitle").getBytes());
}
asciidocFile.write(fileContent.replace("image::/img/developer-guide/", "image::").getBytes("UTF-8"));
String url = f.getName().replace(".adoc", ".html");
indexFile.write("<li <#if content.uri?ends_with(\"");
indexFile.write(url);
indexFile.write("\")> class=\"current\"</#if> ><a href=\"");
indexFile.write(url);
indexFile.write("\">");
indexFile.write(props.getProperty("title"));
indexFile.write("</a>\n<ul class=\"sectlevel2\">\n");
for(String line : fileContent.split("\n")) {
if(line.startsWith("=== ")) {
line = line.substring(4);
indexFile.write("<li><a href=\"");
indexFile.write(url);
indexFile.write("#");
StringTokenizer tt = new StringTokenizer(line.toLowerCase(), " ;&:_");
while(tt.hasMoreTokens()) {
indexFile.write("_");
indexFile.write(tt.nextToken());
}
indexFile.write("\">");
indexFile.write(line);
indexFile.write("</a></li>\n");
}
}
indexFile.write("</ul></li>\n");
}
indexFile.write("</ul></div>\n");
indexFile.close();
asciidocFile.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment