]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/GenerateAllHtmlDocumentation.java
Merge "(refs #7621) Fixed handling of $ in application ignoring return value"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / markdown / html / GenerateAllHtmlDocumentation.java
1 package org.simantics.scl.compiler.markdown.html;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.List;
7
8 import org.simantics.scl.compiler.module.repository.ModuleRepository;
9
10 public class GenerateAllHtmlDocumentation {
11     public static void generate(ModuleRepository moduleRepository, Path directory) throws IOException {
12         HierarchicalDocumentationRef root = HierarchicalDocumentationRef.generateTree(moduleRepository.getSourceRepository());
13         generate(moduleRepository, directory, root.getChildren(), 0, root);
14         generateIndex(directory, 0, "StandardLibrary/Prelude.html");
15     }
16
17     private static void generateIndex(Path directory, int level, String url) throws IOException {
18         Path documentationFile = directory.resolve("index.html");
19         for(int i=0;i<level;++i)
20             url = "../" + url;
21         String content = "<html><head><meta http-equiv=\"refresh\" content=\"1;url="+url+"\">" +
22                          "<script type=\"text/javascript\">window.location.href = \""+url+"\"</script>" +
23                          "</head><body>If you are not redirected automatically, <a href=\""+url+"\">follow the link</a>.</body></html>";
24         Files.write(documentationFile, content.getBytes());
25     }
26
27     private static void generate(ModuleRepository moduleRepository, Path directory, List<HierarchicalDocumentationRef> refs, int level, HierarchicalDocumentationRef root) throws IOException {
28         String navigation = generateNavigation(level, root);
29         for(HierarchicalDocumentationRef ref : refs) {
30             String name = ref.getName();
31             
32             // Documentation for this ref
33             String documentationName = ref.getDocumentationName();
34             if(documentationName != null) {
35                 String documentation = HtmlDocumentationGeneration.generate(moduleRepository, documentationName, navigation);
36                 Path documentationFile = directory.resolve(name + ".html"); 
37                 Files.write(documentationFile, documentation.getBytes());
38             }
39             
40             // Children of the ref
41             List<HierarchicalDocumentationRef> children = ref.getChildren();
42             if(!children.isEmpty()) {
43                 Path childDirectory = directory.resolve(name);
44                 if(!Files.exists(childDirectory))
45                     Files.createDirectory(childDirectory);
46                 generate(moduleRepository, childDirectory, children, level+1, root);
47                 if(documentationName != null)
48                     generateIndex(childDirectory, 1, ref.getName() + ".html");
49                 else
50                     generateIndex(childDirectory, level+1, "StandardLibrary/Prelude.html");
51             }
52         }
53     }
54
55     private static String generateNavigation(int level,
56             HierarchicalDocumentationRef root) {
57         StringBuilder b = new StringBuilder();
58         generateNavigation(b, level, root.getChildren());
59         return b.toString();
60     }
61
62     private static void generateNavigation(StringBuilder b, int level,
63             List<HierarchicalDocumentationRef> refs) {
64         b.append("<ul>");
65         for(HierarchicalDocumentationRef ref : refs) {
66             b.append("<li>");
67             String documentationName = ref.getDocumentationName();
68             if(documentationName != null) {
69                 if(!documentationName.contains("/"))
70                     documentationName = "StandardLibrary/" + documentationName;
71                 b.append("<a href=\"");
72                 for(int i=0;i<level;++i)
73                     b.append("../");
74                 b.append(documentationName);
75                 b.append(".html\">");
76             }
77             b.append(ref.getName());
78             if(documentationName != null)
79                 b.append("</a>");
80             List<HierarchicalDocumentationRef> children = ref.getChildren();
81             if(children != null)
82                 generateNavigation(b, level, children);
83             b.append("</li>");
84         }
85         b.append("</ul>");
86     }
87 }