-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhtml.go
124 lines (112 loc) · 3.73 KB
/
html.go
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
package main
import (
"fmt"
"github.com/russross/blackfriday"
"html/template"
"io/ioutil"
"os"
"strings"
)
var fns = template.FuncMap{
"last": func(index int, length int) bool {
return index == length-1
},
}
func generateIndexFile(classes Classes) {
indexFile, err := os.OpenFile("./docs/index.html", os.O_CREATE|os.O_WRONLY, 0777)
panicIf(err)
indexTemplate, err := template.New("index").ParseFiles(
"./templates/html/index.html",
"./templates/html/layout.html",
"./templates/html/sidebar.html",
"./templates/html/navbar.html",
)
panicIf(err)
variables := map[string]interface{}{
"classes": classes,
"class": nil,
"readme": template.HTML(readmeHTML("./README.md")),
}
err = indexTemplate.ExecuteTemplate(indexFile, "layout", variables)
panicIf(err)
fmt.Println("Generated: ./docs/index.html")
}
func generateClassFile(classes Classes, class Class) {
classFile, err := os.OpenFile("./docs/"+class.Filename+".html", os.O_CREATE|os.O_WRONLY, 0777)
panicIf(err)
classTemplate, err := template.New(class.Filename).Funcs(fns).ParseFiles(
"./templates/html/class.html",
"./templates/html/layout.html",
"./templates/html/sidebar.html",
"./templates/html/navbar.html",
)
panicIf(err)
classComment := blackfriday.MarkdownCommon([]byte(class.Comment))
class.Comment = template.HTML(string(classComment))
class.InstanceMethods = renderMethodCommentToMarkdown(class.InstanceMethods)
class.ClassMethods = renderMethodCommentToMarkdown(class.ClassMethods)
variables := map[string]interface{}{
"classes": classes,
"class": class,
}
err = classTemplate.ExecuteTemplate(classFile, "layout", variables)
panicIf(err)
fmt.Println("Generated: ./docs/" + class.Filename + ".html")
}
func renderMethodCommentToMarkdown(methods Methods) Methods {
for i := 0; i < len(methods); i++ {
methodComment := blackfriday.MarkdownCommon([]byte(methods[i].Comment))
methods[i].Comment = template.HTML(methodComment)
for j := 0; j < len(methods[i].Params); j++ {
p := methods[i].Params[j]
paramClass := blackfriday.MarkdownCommon([]byte(p.Class))
paramDesc := blackfriday.MarkdownCommon([]byte(p.Description))
paramClass = stripTag(paramClass)
paramDesc = stripTag(paramDesc)
methods[i].Params[j].Class = template.HTML(paramClass)
methods[i].Params[j].Description = template.HTML(paramDesc)
}
for j := 0; j < len(methods[i].Returns); j++ {
r := methods[i].Returns[j]
returnClass := blackfriday.MarkdownCommon([]byte(r.Class))
returnDesc := blackfriday.MarkdownCommon([]byte(r.Description))
returnClass = stripTag(returnClass)
returnDesc = stripTag(returnDesc)
methods[i].Returns[j].Class = template.HTML(returnClass)
methods[i].Returns[j].Description = template.HTML(returnDesc)
}
}
return methods
}
func copyAsset(filename string) {
bytes, err := ioutil.ReadFile("./assets/" + filename)
panicIf(err)
_, err = os.OpenFile("./docs/assets/"+filename, os.O_CREATE|os.O_WRONLY, 0644)
panicIf(err)
err = ioutil.WriteFile("./docs/assets/"+filename, bytes, 0644)
panicIf(err)
fmt.Println("Generated: ./docs/" + filename)
}
func readmeHTML(filepath string) string {
bytes, err := ioutil.ReadFile(filepath)
panicIf(err)
html := string(blackfriday.MarkdownCommon(bytes))
html = strings.Replace(html, "<code class=\"language-", "<code class=\"", -1)
return html
}
func GenerateHTML(classes Classes) {
os.Mkdir("./docs/assets", 0777)
generateIndexFile(classes)
for _, class := range classes {
generateClassFile(classes, class)
}
copyAsset("app.css")
copyAsset("app.js")
}
func stripTag(bytes []byte) []byte {
t := strings.Replace(string(bytes), "<p>", "", 1)
t = strings.Replace(t, "</p>", "", 1)
t = strings.Replace(t, "\n", "", 1)
t = strings.Replace(t, "<a ", "<a class=\"class-link\"", 1)
return []byte(t)
}