-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathciigo.go
353 lines (295 loc) · 8.35 KB
/
ciigo.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// SPDX-FileCopyrightText: 2019 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
// Package ciigo is a program to write static web server with embedded files
// using the asciidoc markup languages.
//
// For more information see the README file at the page repository
// https://sr.ht/~shulhan/ciigo.
package ciigo
import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"strings"
libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http"
"git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
)
const (
extAsciidoc = `.adoc`
extMarkdown = `.md`
internalTemplatePath = `_internal/.template`
)
// Version define the latest tagged release of this module.
var Version = `0.15.1`
// defExcludes define default files to be excludes on GoEmbed.
var defExcludes = []string{
`.*\.adoc$`,
`.*\.md$`,
`^\..*`,
}
// Ciigo provides customizable and reusable instance of ciigo for embedding,
// converting, and/or serving HTTP server.
// This type is introduced so one can add HTTP handler or endpoint along
// with serving the files.
type Ciigo struct {
HTTPServer *libhttp.Server
converter *Converter
watcher *watcher
serveOpts ServeOptions
}
// Convert all markup files inside directory "dir" recursively into HTML
// files using ConvertOptions HTMLTemplate file as base template.
// If HTMLTemplate is empty it will default to use embedded HTML template.
// See template_index_html.go for template format.
func Convert(opts ConvertOptions) (err error) {
var ciigo = &Ciigo{}
return ciigo.Convert(opts)
}
// GoEmbed generate a static Go file that embed all files inside Root except
// the one that being excluded explicitly by ConvertOptions Exclude.
//
// It convert all markup files inside directory "dir" into HTML files,
// recursively, and then embed them into Go file defined by
// EmbedOptions.GoFileName.
//
// If HTMLTemplate option is empty it default to use embedded HTML
// template.
// See template_index_html.go for template format.
func GoEmbed(opts EmbedOptions) (err error) {
var ciigo = &Ciigo{}
return ciigo.GoEmbed(opts)
}
// Serve the content under directory "[ServeOptions].ConvertOptions.Root"
// using HTTP server at specific "[ServeOptions].Address".
func Serve(opts ServeOptions) (err error) {
var ciigo = &Ciigo{}
err = ciigo.InitHTTPServer(opts)
if err != nil {
return err
}
return ciigo.Serve()
}
// Watch any changes on markup files on directory Root recursively and
// changes on the HTML template file.
// If there is new or modified markup files it will convert them into HTML
// files using HTML template automatically.
//
// If the HTML template file modified, it will re-convert all markup files.
// If the HTML template file deleted, it will replace them with internal,
// default HTML template.
func Watch(opts ConvertOptions) (err error) {
var ciigo = &Ciigo{}
return ciigo.Watch(opts)
}
// isHTMLTemplateNewer will return true if HTMLTemplate is not defined or
// newer than embedded GoFileName.
func isHTMLTemplateNewer(opts EmbedOptions) bool {
var (
logp = `isHTMLTemplateNewer`
fiHTMLTmpl fs.FileInfo
fiGoEmbed fs.FileInfo
err error
)
if len(opts.HTMLTemplate) == 0 {
return false
}
fiHTMLTmpl, err = os.Stat(opts.HTMLTemplate)
if err != nil {
log.Fatalf(`%s: %s`, logp, err)
}
if len(opts.EmbedOptions.GoFileName) == 0 {
// No output file for GoEmbed.
return false
}
fiGoEmbed, err = os.Stat(opts.EmbedOptions.GoFileName)
if err != nil {
if os.IsNotExist(err) {
return false
}
log.Fatalf(`%s: %s`, logp, err)
}
return fiHTMLTmpl.ModTime().After(fiGoEmbed.ModTime())
}
// isExtensionMarkup return true if the file extension ext match with one of
// supported markup format.
func isExtensionMarkup(ext string) bool {
if ext == extAsciidoc {
return true
}
return ext == extMarkdown
}
// listFileMarkups find any markup files inside the content directory,
// recursively.
func listFileMarkups(dir string, excRE []*regexp.Regexp) (
fileMarkups map[string]*FileMarkup, err error,
) {
var (
logp = `listFileMarkups`
d *os.File
fi os.FileInfo
fmarkup *FileMarkup
name string
filePath string
k string
ext string
fis []os.FileInfo
fmarkups map[string]*FileMarkup
)
d, err = os.Open(dir)
if err != nil {
log.Printf(`%s: %s`, logp, err)
return nil, nil
}
fis, err = d.Readdir(0)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
fileMarkups = make(map[string]*FileMarkup)
for _, fi = range fis {
name = fi.Name()
filePath = filepath.Join(dir, name)
if isExcluded(filePath, excRE) {
continue
}
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
fi, err = os.Stat(filePath)
if err != nil {
// Broken symlink.
log.Printf(`%s: %s`, logp, err)
continue
}
}
if fi.IsDir() {
if name[0] == '.' {
// Skip any directory start with '.'.
continue
}
fmarkups, err = listFileMarkups(filePath, excRE)
if err != nil {
return nil, fmt.Errorf(`%s: %s: %w`, logp, filePath, err)
}
for k, fmarkup = range fmarkups {
fileMarkups[k] = fmarkup
}
continue
}
ext = strings.ToLower(filepath.Ext(name))
if !isExtensionMarkup(ext) {
continue
}
if fi.Size() == 0 {
continue
}
fmarkup, err = NewFileMarkup(filePath, fi)
if err != nil {
return nil, fmt.Errorf(`%s: %s: %w`, logp, filePath, err)
}
fileMarkups[filePath] = fmarkup
}
return fileMarkups, nil
}
func isExcluded(path string, excs []*regexp.Regexp) bool {
if len(excs) == 0 {
return false
}
var re *regexp.Regexp
for _, re = range excs {
if re.MatchString(path) {
return true
}
}
return false
}
// Convert all markup files inside directory [ConvertOptions.Root]
// recursively into HTML files using [ConvertOptions.HTMLTemplate] file as
// base template.
// If HTMLTemplate is empty it use the default embedded HTML template.
// See template_index_html.go for template format.
func (ciigo *Ciigo) Convert(opts ConvertOptions) (err error) {
var logp = `Convert`
err = opts.init()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
ciigo.serveOpts.ConvertOptions = opts
ciigo.converter, err = NewConverter(opts.HTMLTemplate)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
var fileMarkups map[string]*FileMarkup
fileMarkups, err = listFileMarkups(opts.Root, opts.excRE)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
ciigo.converter.convertFileMarkups(fileMarkups, false)
return nil
}
// GoEmbed embed the file system (directories and files) inside the
// [ConvertOptions.Root] into a Go code.
// One can exclude files by writing regular expression in
// [ConvertOptions.Exclude].
func (ciigo *Ciigo) GoEmbed(embedOpts EmbedOptions) (err error) {
var logp = `GoEmbed`
err = embedOpts.init()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
ciigo.converter, err = NewConverter(embedOpts.HTMLTemplate)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
var fileMarkups map[string]*FileMarkup
fileMarkups, err = listFileMarkups(embedOpts.Root, embedOpts.excRE)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
var convertForce = isHTMLTemplateNewer(embedOpts)
ciigo.converter.convertFileMarkups(fileMarkups, convertForce)
var excludes = append(defExcludes, embedOpts.Exclude...)
var mfsOpts = &memfs.Options{
Root: embedOpts.Root,
Excludes: excludes,
Embed: embedOpts.EmbedOptions,
}
var mfs *memfs.MemFS
mfs, err = memfs.New(mfsOpts)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
if len(embedOpts.HTMLTemplate) > 0 {
_, err = mfs.AddFile(internalTemplatePath, embedOpts.HTMLTemplate)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
}
err = mfs.GoEmbed()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
return nil
}
// Watch start a watcher on [ConvertOptions.Root] directory that monitor any
// changes to markup files and convert them to HTML files.
func (ciigo *Ciigo) Watch(convertOpts ConvertOptions) (err error) {
var logp = `Watch`
err = convertOpts.init()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
ciigo.converter, err = NewConverter(convertOpts.HTMLTemplate)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
ciigo.watcher, err = newWatcher(ciigo.converter, convertOpts)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
err = ciigo.watcher.start()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
return nil
}