summaryrefslogtreecommitdiffstats
path: root/docs/user-guide/scripts/Program.cs
blob: 4ed2d18a6db595796d608156b37e0e8f325d7c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace toc
{
    public class Builder
    {
        public static string getAnchorId(string title)
        {
            StringBuilder sb = new StringBuilder();
            title = title.Trim().ToLower();
            foreach (var ch in title)
            {
                if (ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9'
                    || ch == '-')
                    sb.Append(ch);
                else if (ch==' ' || ch =='_')
                    sb.Append('-');
            }
            return sb.ToString();
        }

        public class Node
        {
            public List<string> fileNamePrefix = new List<string>();
            public string title;
            public string fileID;
            public List<string> sections = new List<string>();
            public List<Node> children = new List<Node>();
        }

        public static void buildTOC(StringBuilder sb, Node n)
        {
            sb.AppendFormat("<li data-link=\"{0}\"><span>{1}</span>\n", n.fileID, n.title);
            if (n.children.Count != 0)
            {
                sb.AppendLine("<ul class=\"toc_list\">");
                foreach(var c in n.children)
                    buildTOC(sb, c);
                sb.AppendLine("</ul>");
            }
            else if (n.sections.Count != 0)
            {
                sb.AppendLine("<ul class=\"toc_list\">");
                foreach (var s in n.sections)
                {
                    sb.AppendFormat("<li data-link=\"{0}#{1}\"><span>{2}</span></li>\n", n.fileID, getAnchorId(s), s);
                }
                sb.AppendLine("</ul>");
            }
            sb.AppendLine("</li>");
        }
        public static string buildTOC(Node n)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"<ul class=""toc_root_list"">");
            buildTOC(sb, n);
            sb.Append(@"</ul>");
            return sb.ToString();
        }

        public static bool isChild(Node parent, Node child)
        {
            if (parent.fileNamePrefix.Count < child.fileNamePrefix.Count)
            {
                bool equal = true;
                for (int k = 0; k < parent.fileNamePrefix.Count; k++)
                {
                    if (parent.fileNamePrefix[k] != child.fileNamePrefix[k])
                    {
                        equal = false;
                        break;
                    }
                }
                return equal;
            }
            return false;
        }
        public static string Run(string path)
        {
            StringBuilder outputSB = new StringBuilder();
            outputSB.AppendFormat("Building table of contents from {0}...\n", path);
            var files = System.IO.Directory.EnumerateFiles(path, "*.md");
            List<Node> nodes = new List<Node>();
            foreach (var f in files)
            {
                var content = File.ReadAllLines(f);
                Node node = new Node();
                node.fileID = Path.GetFileNameWithoutExtension(f);
                outputSB.AppendFormat("  {0}.md\n", node.fileID);
                for (int i = 1; i < content.Length; i++)
                {
                    if (content[i-1].StartsWith("layout: "))
                        continue;
                    if (content[i].StartsWith("==="))
                        node.title = content[i-1];
                    if (content[i].StartsWith("---"))
                        node.sections.Add(content[i-1]);
                    if (content[i].StartsWith("#") && !content[i].StartsWith("##") && node.title == null)
                        node.title = content[i].Substring(1, content[i].Length - 1).Trim();
                    if (content[i].StartsWith("##") && !content[i].StartsWith("###"))
                        node.sections.Add(content[i].Substring(2, content[i].Length - 2).Trim());
                }
                if (node.title == null)
                {
                    outputSB.AppendFormat("Error: {0} does not define a title.", f);
                    node.title = "Untitiled";
                }
                var titleSecs = Path.GetFileName(f).Split('-');
                foreach (var s in titleSecs)
                {
                    if (s.Length == 2 && s[1]>='0' && s[1] <= '9')
                    {
                        node.fileNamePrefix.Add(s);
                    }
                    else
                    {
                        break;
                    }
                }
                // Find parent node.
                Node parent=null;
                for (int l = nodes.Count-1; l>=0; l--)
                {
                    var n = nodes[l];
                    if (isChild(n, node))
                    {
                       parent = n;
                       break;
                    }
                }
                if (parent != null)
                    parent.children.Add(node);
                else
                {
                    // find child
                    foreach (var other in nodes)
                    {
                        if (isChild(node, other))
                        {
                            node.children.Add(other);
                        }
                    }
                    foreach (var c in node.children)
                    {
                        nodes.Remove(c);

                    }
                    nodes.Add(node);
                }
            }
            var root = nodes.Find(x=>x.fileID=="index");
            if (root != null)
            {
                var html = buildTOC(root);
                var outPath = Path.Combine(path, "user-guide-toc.html");
                File.WriteAllText(outPath, html);
                outputSB.AppendFormat("Output written to: {0}\n", outPath);
            }
            return outputSB.ToString();
        }
    }
}