1 package org.simantics.scl.compiler.markdown.html;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.StringReader;
6 import java.nio.charset.Charset;
7 import java.util.Arrays;
10 import org.simantics.scl.compiler.errors.Failable;
11 import org.simantics.scl.compiler.markdown.internal.MarkdownParser;
12 import org.simantics.scl.compiler.markdown.nodes.HeaderNode;
13 import org.simantics.scl.compiler.markdown.nodes.Node;
14 import org.simantics.scl.compiler.module.Module;
15 import org.simantics.scl.compiler.module.repository.ModuleRepository;
17 public class HtmlDocumentationGeneration {
19 private static String STYLESHEET;
22 InputStream stream = HtmlDocumentationGeneration.class.getResourceAsStream("SclDoc.css");
24 byte[] buffer = new byte[2048];
27 int count = stream.read(buffer, pos, buffer.length-pos);
31 if(pos == buffer.length)
32 buffer = Arrays.copyOf(buffer, 2*buffer.length);
34 STYLESHEET = new String(buffer, Charset.forName("UTF-8"));
38 } catch(IOException e) {
43 public static String generate(ModuleRepository moduleRepository, String documentationName, String navigation) {
44 String documentation = moduleRepository.getDocumentation(documentationName);
45 if(documentation == null || documentation.isEmpty()) {
46 Failable<Module> module = moduleRepository.getModule(documentationName);
47 if(!module.didSucceed())
48 return "Didn't find documentation or module for " + documentationName + ".";
49 documentation = "# Module "+documentationName+"\n\nThis module is undocumented. This is a list of its definitions.\n\n::undocumented[]";
51 MarkdownParser parser = new MarkdownParser();
53 Node result = parser.parseDocument(new StringReader(documentation));
54 result.processExtensionNodes(new SCLDocumentationExtensionNodeHandler(moduleRepository, documentationName));
56 StringBuilder b = new StringBuilder();
59 if(navigation != null) {
60 b.append("<table class=\"pagestructure\"><tr><td class=\"navi\">\n");
62 b.append("</td><td class=\"content\">\n");
64 addContentsTree(b, result);
66 if(navigation != null)
67 b.append("</td></tr></table>\n");
72 } catch (IOException e) {
78 private static void addContentsTree(StringBuilder b, Node node) {
79 List<HeaderNode> headers = node.extractHeaders();
80 if(headers.size() > 1) {
81 int minLevel=Integer.MAX_VALUE, minLevelCount=0;
82 for(HeaderNode header : headers) {
83 if(header.level == minLevel)
85 else if(header.level < minLevel) {
86 minLevel = header.level;
90 if(minLevelCount == 1)
93 b.append("<div id=\"contentspanel\">");
94 b.append("<h2>Contents</h2>\n");
95 int level = minLevel-1;
96 for(HeaderNode header : headers) {
97 if(header.level < minLevel)
99 while(header.level != level) {
100 if(header.level > level) {
110 b.append("<li><a href=\"#");
111 header.toPlainText(b);
113 header.toPlainText(b);
114 b.append("</a></li>\n");
116 while(level >= minLevel) {
124 private static void addHTMLHeader(StringBuilder b) {
125 b.append("<!DOCTYPE html>\n");
126 b.append("<html>\n");
127 b.append("<head>\n");
128 b.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n");
129 b.append("<meta http-equiv=\"x-ua-compatible\" content=\"IE=Edge\" />\n");
130 /*b.append("<script type=\"text/javascript\"\n");
131 b.append(" src=\"https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\">\n");
132 b.append("</script>\n");*/
133 b.append("<style type=\"text/css\">\n");
134 b.append(STYLESHEET);
135 b.append("</style>\n");
136 b.append("</head>\n");
137 b.append("<body>\n");
140 private static void addHTMLFooter(StringBuilder b) {
141 b.append("</body>\n");
142 b.append("</html>\n");