-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_doc.go
More file actions
137 lines (116 loc) · 2.79 KB
/
generate_doc.go
File metadata and controls
137 lines (116 loc) · 2.79 KB
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
package main
import (
"fmt"
"io/ioutil"
"github.com/russross/blackfriday"
"github.com/simpleelegant/project-doc/list"
)
const tmpl = `
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Project Documents</title>
<style media="screen">
body {
font-family: "Fira Sans", "Trebuchet MS", Arial, "Microsoft Yahei", sans-serif;
}
code {
font-family: Consolas, "Fira Mono", "Liberation Mono", Courier, "Microsoft Yahei", monospace;
color: red;
border: 1px solid #CECECE;
border-radius: 4px;
padding: 2px 4px;
font-size: 0.9em;
}
pre {
border: 1px solid #CACACA;
padding: 10px;
overflow: auto;
border-radius: 3px;
background-color: #FAFAFB;
color: #393939;
margin: 2em 0px;
}
pre code {
border: 0;
padding: 0;
}
.body {
max-width: 980px;
margin: 0px auto;
}
.left {
margin: 30px 250px 20px 0;
}
.right {
margin: 20px 0;
border: 1px solid #DDD;
border-radius: 4px;
overflow: hidden;
width: 220px;
background-color: #F5F5F5;
font-size: 0.9em;
float: right;
}
.right ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.right a {
display: block;
text-decoration: none;
color: #4183C4;
line-height: 32px;
padding: 0 10px 0 2em;
border-bottom: 1px solid #E8E8E8;
}
.right a.h {
color: #555;
padding-left: 10px;
background-color: #FFF;
font-size: 1.2em;
}
</style>
</head>
<body>
<div class="body">
<div class="right">%s</div>
<div class="left">%s</div>
</div>
</body>
</html>
`
func generateDoc(base, src, out string) error {
fileInfos, err := ioutil.ReadDir(src)
if err != nil {
return err
}
docs := list.New(fileInfos)
docs.SetHomePage()
docs.SetTopDoc()
contents := docs.ToHTML()
for _, v := range docs {
if v.SrcName != "" {
if err := md2HTML(src+v.SrcName, out+v.OutName, contents); err != nil {
return err
}
}
for _, w := range v.Sub {
if err := md2HTML(src+w.SrcName, out+w.OutName, contents); err != nil {
return err
}
}
}
return nil
}
func md2HTML(srcName, outName, contents string) error {
data, err := ioutil.ReadFile(srcName)
if err != nil {
return err
}
// markdown to html
html := blackfriday.MarkdownCommon(data)
// write file
return ioutil.WriteFile(outName, []byte(fmt.Sprintf(tmpl, contents, html)), 0644)
}