-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstaw.go
189 lines (172 loc) · 4.21 KB
/
staw.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// See LICENSE file for copyright and license details.
package main
import (
"bufio"
"flag"
"io"
"io/ioutil"
"log"
"os"
"strings"
"text/template"
"github.com/gomarkdown/markdown"
)
type Args struct {
siteDir string
srcPath string
dstPath string
tpl string
page Page
}
type Nav struct {
Path string
Name string
Sel bool
Items []Nav
}
type Page struct {
Site string
SiteTitle string
Title string
HtmlContent string
Items []Nav
}
func copyFile(src, dst string) {
in, err := os.Open(src)
dieOnError(err)
defer in.Close()
out, err := os.Create(dst)
dieOnError(err)
defer out.Close()
_, err = io.Copy(out, in)
dieOnError(err)
dieOnError(out.Close())
}
func dieIfEmpty(s *string, msg string) {
if *s == "" {
log.Fatal(msg)
}
}
func dieOnError(err error) {
if err != nil {
log.Fatal(err)
}
}
func getTitle(src string) string {
in, err := os.Open(src)
dieOnError(err)
defer in.Close()
reader := bufio.NewReader(in)
title, err := reader.ReadString('\n')
dieOnError(err)
return strings.TrimRight(title, "\n")
}
func isMdFile(f os.FileInfo) bool {
return strings.HasSuffix(f.Name(), ".md")
}
func mkDstPath(dstPath string, f os.FileInfo) string {
var dst, tmp string
if f == nil {
tmp = "index" // no index.md in directory case
} else {
tmp = strings.TrimSuffix(f.Name(), ".md")
}
if tmp != "index" {
dst = dstPath + "/" + tmp
os.Mkdir(dst, os.ModePerm)
dst += "/index.html"
} else {
dst = dstPath + "/index.html"
}
return dst
}
func mkNav(cwd, path, nbsp string, walk []string) []Nav {
files, err := ioutil.ReadDir(cwd)
dieOnError(err)
var nav []Nav
for _, f := range files {
sel := len(walk) > 0 && f.Name() == walk[0]
if f.IsDir() {
nav = mkNavNode(nav, cwd, path, nbsp, walk, sel, f)
} else if isMdFile(f) {
nav = mkNavLeaf(nav, cwd, path, nbsp, sel, f)
}
}
return nav
}
func mkNavLeaf(nav []Nav, cwd, path, nbsp string, sel bool, f os.FileInfo) []Nav {
title := getTitle(cwd + "/" + f.Name())
tmp := strings.TrimSuffix(f.Name(), ".md")
if tmp == "index" {
// prepend
return append([]Nav{Nav{path, nbsp + title, sel, nil}}, nav...)
} else {
return append(nav, Nav{path + tmp + "/", nbsp + title, sel, nil})
}
}
func mkNavNode(nav []Nav, cwd, path, nbsp string, walk []string, sel bool, f os.FileInfo) []Nav {
n := Nav{path + f.Name() + "/", nbsp + f.Name() + "/", sel, nil}
if sel {
n.Items = mkNav(cwd+"/"+f.Name(), path+f.Name()+"/", nbsp + " ", walk[1:])
}
return append(nav, n)
}
func processMdFile(a Args, walk []string, f os.FileInfo) {
if f != nil {
walk = append(walk, f.Name())
a.page.Title = getTitle(a.srcPath)
dat, err := ioutil.ReadFile(a.srcPath)
dieOnError(err)
a.page.HtmlContent = string(markdown.ToHTML(dat, nil, nil))
} else if len(walk) > 0 {
a.page.Title = walk[len(walk)-1] + "/"
}
a.page.Items = mkNav(a.siteDir, "", "", walk)
t, err := template.ParseFiles(a.tpl)
dieOnError(err)
dst := mkDstPath(a.dstPath, f)
out, err := os.Create(dst)
dieOnError(err)
defer out.Close()
dieOnError(t.Execute(out, a.page))
}
func processPath(a Args, walk []string) {
os.Mkdir(a.dstPath, os.ModePerm)
files, err := ioutil.ReadDir(a.srcPath)
dieOnError(err)
noIndexMd := true
for _, f := range files {
b := a
b.srcPath = a.srcPath + "/" + f.Name()
if f.IsDir() {
b.dstPath = a.dstPath + "/" + f.Name()
processPath(b, append(walk, f.Name()))
} else {
if f.Name() == "index.md" {
noIndexMd = false
}
if isMdFile(f) {
processMdFile(b, walk, f)
} else {
copyFile(b.srcPath, b.dstPath+"/"+f.Name())
}
}
}
if noIndexMd {
processMdFile(a, walk, nil)
}
}
func main() {
tpl := flag.String("tpl", "page.tpl", "template file to be used (required)")
src := flag.String("in", "", "input site directory (required)")
dst := flag.String("out", "", "output site directory (required)")
title := flag.String("t", "", "site title of the site (required)")
flag.Parse()
dieIfEmpty(tpl, "no template given")
dieIfEmpty(title, "no site title given")
dieIfEmpty(src, "no site input directory given")
dieIfEmpty(dst, "no output directory given")
site, err := os.Stat(*src)
dieOnError(err)
processPath(Args{*src, *src, *dst, *tpl, Page{site.Name(), *title, "", "", nil}}, []string{})
}