-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
93 lines (79 loc) · 2.5 KB
/
builder.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
package main
import (
"bufio"
"fmt"
"html/template"
"os"
"path/filepath"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"golang.org/x/sync/errgroup"
)
type builder struct {
inputFilePaths []string
outputDirPath string
template *template.Template
}
// readFile reads in a markdown file, converts it to HTML and returns the HTML string
func readFile(filePath string) (template.HTML, error) {
extensions := parser.CommonExtensions | parser.Attributes | parser.Mmark
parser := parser.NewWithExtensions(extensions)
data, err := os.ReadFile(filePath)
if err != nil {
return template.HTML(""), fmt.Errorf("error while reading file %s: %w", filePath, err)
}
html := markdown.ToHTML(data, parser, nil)
return template.HTML(html), nil
}
// build reads all the files in the inputFilePaths slice, then passes them to writeOutputFile to build an index.html
func (b *builder) build() error {
fmt.Println()
inputData, err := getInputData(b.inputFilePaths)
if err != nil {
return fmt.Errorf("failed to get input data: %w", err)
}
if err := writeOutputFile(inputData, b.outputDirPath, b.template); err != nil {
return fmt.Errorf("failed to write to output file: %w", err)
}
return nil
}
func getInputData(inputFilePaths []string) ([]template.HTML, error) {
fmt.Println("\033[0;34m[1/2]\033[0m converting source files to HTML")
var inputData []template.HTML
temporaryMap := make(map[string]template.HTML)
g := new(errgroup.Group)
for _, inputFilePath := range inputFilePaths {
path := inputFilePath
g.Go(func() error {
data, err := readFile(path)
if err == nil {
temporaryMap[path] = data
}
return err
})
}
if err := g.Wait(); err != nil {
return nil, err
} else {
for _, inputFilePath := range inputFilePaths {
inputData = append([]template.HTML{temporaryMap[inputFilePath]}, inputData...)
}
}
return inputData, nil
}
// writeOutputFile creates an index.html file at outputDirPath using a template filled with inputData
func writeOutputFile(inputData []template.HTML, outputDirPath string, t *template.Template) error {
fmt.Println("\033[0;34m[2/2]\033[0m generating index.html")
filePath := filepath.Join(outputDirPath, "index.html")
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create file %+s: %v", filePath, err)
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
if err := t.Execute(writer, inputData); err != nil {
return fmt.Errorf("error executing template: %w", err)
}
return nil
}