1 package org.simantics.scl.compiler.markdown.html;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
8 import org.simantics.scl.compiler.module.repository.ModuleRepository;
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");
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)
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());
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();
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());
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");
50 generateIndex(childDirectory, level+1, "StandardLibrary/Prelude.html");
55 private static String generateNavigation(int level,
56 HierarchicalDocumentationRef root) {
57 StringBuilder b = new StringBuilder();
58 generateNavigation(b, level, root.getChildren());
62 private static void generateNavigation(StringBuilder b, int level,
63 List<HierarchicalDocumentationRef> refs) {
65 for(HierarchicalDocumentationRef ref : refs) {
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)
74 b.append(documentationName);
77 b.append(ref.getName());
78 if(documentationName != null)
80 List<HierarchicalDocumentationRef> children = ref.getChildren();
82 generateNavigation(b, level, children);