-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
253 lines (205 loc) · 4.81 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
_ "embed"
"flag"
"fmt"
"html/template"
"io/fs"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"time"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"golang.org/x/sync/errgroup"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// TemplateData contains the data for the template
type TemplateData struct {
Title string
Content template.HTML
}
var (
//go:embed template.html
htmlTemplateFile string
htmlTemplate *template.Template
wikiDir string
srcDir = flag.String("f", ".", "markdown src dir")
)
func md2html(path string) error {
contents, err := os.ReadFile(path)
if err != nil {
return err
}
// Create markdown parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(contents)
// create HTML renderer with extensions
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
// Compile markdown to html
bytes := markdown.Render(doc, renderer)
// Create folder if needed
dirPath := filepath.Dir(filepath.Join(wikiDir, path))
if _, err = os.Stat(dirPath); os.IsNotExist(err) {
err := os.MkdirAll(dirPath, 0755)
if err != nil {
return err
}
}
withoutExt := strings.TrimSuffix(path, filepath.Ext(path))
withDir := filepath.Join(wikiDir, fmt.Sprintf("%s.html", withoutExt))
// Create file
file, err := os.Create(withDir)
if err != nil {
return err
}
defer file.Close()
data := TemplateData{
Title: cases.Title(language.English).String(filepath.Base(withoutExt)),
Content: template.HTML(bytes),
}
// Write HTML content into file
err = htmlTemplate.Execute(file, data)
if err != nil {
return err
}
return nil
}
func buildWiki() error {
g := new(errgroup.Group)
walkFunc := func(path string, _ fs.DirEntry, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) != ".md" {
return nil
}
// Launch a goroutine for each the building of each markdown file
g.Go(func() error {
err := md2html(path)
if err != nil {
return err
}
return nil
})
return nil
}
err := filepath.WalkDir(*srcDir, walkFunc)
if err != nil {
return err
}
// Wait for all files to build
return g.Wait()
}
func fileWatcher(path string, fileChanged chan<- string) {
// Fetch initial stat as a comparison value
initialStat, err := os.Stat(path)
if err != nil {
fmt.Fprintf(os.Stderr, "wiki: could not get file info: %v", err)
}
for {
time.Sleep(time.Second)
// Every one second, fetch stat again
stat, err := os.Stat(path)
if err != nil {
fmt.Fprintf(os.Stderr, "wiki: could not get file info: %v", err)
}
// If file has changed, send path to channel and replace initialStat
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
fileChanged <- path
initialStat = stat
}
}
}
func launchFileWatchers() error {
fileChanged := make(chan string)
walkFunc := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) != ".md" {
return nil
}
// Launch a goroutine to watch each markdown file
go fileWatcher(path, fileChanged)
return nil
}
err := filepath.WalkDir(*srcDir, walkFunc)
if err != nil {
return err
}
// Poll for events: recompile if file changed
go func() {
for {
path := <-fileChanged
fmt.Println("Recompiling ", path)
err := md2html(path)
if err != nil {
fmt.Println(err)
}
}
}()
return nil
}
func serveAndWatchFiles() {
// Launch file server
go func() {
fmt.Println("Starting server on :1234")
err := http.ListenAndServe(":1234", http.FileServer(http.Dir(wikiDir)))
if err != nil {
fmt.Println(err)
}
}()
// Launch file watcher
go func() {
fmt.Println("Starting file watcher")
err := launchFileWatchers()
if err != nil {
fmt.Println(err)
}
}()
}
func run() error {
// Create temporary dir
tempDir, err := os.MkdirTemp("", *srcDir)
if err != nil {
return err
}
wikiDir = tempDir
defer os.RemoveAll(wikiDir)
// Parse template for markdown
htmlTemplate, err = template.New("markdown").Parse(htmlTemplateFile)
if err != nil {
return err
}
// Go through each md file in srcDir, build it and create an HTML file in
// wikiDir
err = buildWiki()
if err != nil {
return err
}
fmt.Println("wiki built!")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Launch file server and file watcher
serveAndWatchFiles()
// Block here until Interrupt is received
<-c
fmt.Println()
fmt.Println("Exiting")
return nil
}
func main() {
flag.Parse()
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "wiki: %v\n", err)
os.Exit(1)
}
}