-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
223 lines (203 loc) · 5.77 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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"bytes"
"github.com/alecthomas/chroma/formatters/html"
mathjax "github.com/litao91/goldmark-mathjax"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
goldhtml "github.com/yuin/goldmark/renderer/html"
"gopkg.in/osteele/liquid.v1"
"gopkg.in/yaml.v2"
)
// LoadYAML reads a YAML formatted file and returns an interface
func LoadYAML(file string) map[interface{}]interface{} {
fileData, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
data := make(map[interface{}]interface{})
err = yaml.Unmarshal(fileData, &data)
if err != nil {
log.Fatal(err)
}
return data
}
func fileNameWithoutExtension(fileName string) string {
return strings.TrimSuffix(filepath.Base(fileName), filepath.Ext(fileName))
}
// LoadTemplate reads an HTML file and returns the content as a string
func LoadTemplate(file string) string {
template, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
templateStr := string(template)
return templateStr
}
// ClearDirWithExtension removes all files in the directory with a given extension
func ClearDirWithExtension(dir string, ext string) error {
names, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, fileInfo := range names {
file := filepath.Join([]string{dir, fileInfo.Name()}...)
if filepath.Ext(file) == ext {
os.RemoveAll(file)
}
}
return nil
}
type MarkdownDocument struct {
file string
fileName string
source []byte
html bytes.Buffer
metaData map[string]interface{}
}
// ParseMarkdownFile loads a markdown file, parses YAML headers, and returns a MarkdownDocument struct
func ParseMarkdownFile(markdown goldmark.Markdown, file string) MarkdownDocument {
source, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
context := parser.NewContext()
if err := markdown.Convert(source, &buf, parser.WithContext(context)); err != nil {
panic(err)
}
metaData := meta.Get(context)
fileName := fileNameWithoutExtension(file)
doc := MarkdownDocument{file, fileName, source, buf, metaData}
return doc
}
func main() {
var outputDir string
var templatesDir string
var docsDir string
var configFile string
var clean bool
var highlightingStyle string
var cssFile string
flag.StringVar(&outputDir, "output", "./public", "Output directory")
flag.StringVar(&templatesDir, "templates", "./templates", "Templates directory")
flag.StringVar(&docsDir, "docs", "./docs", "Markdown documents directory")
flag.StringVar(&configFile, "config", "./config.yml", "YAML configuration file")
flag.StringVar(&highlightingStyle, "highlighting", "pygments", "Syntax highlighting style")
flag.StringVar(&cssFile, "css", "./public/assets/css/style.css", "CSS file to minify and pass as a template variable")
flag.BoolVar(&clean, "clean", false, "Remove HTML files in output directory before processing")
flag.Parse()
if clean {
log.Println("Cleaning ", outputDir)
err := ClearDirWithExtension(outputDir, ".html")
if err != nil {
log.Fatal(err)
}
}
// create output directory if it doesn't exist
err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}
// load config
config := LoadYAML(configFile)
// load layout template as a string
layoutTemplateFile := filepath.Join(templatesDir, "layout.html")
layoutTemplateStr := LoadTemplate(layoutTemplateFile)
// create a markdown
markdown := goldmark.New(
goldmark.WithExtensions(
meta.Meta,
mathjax.MathJax,
extension.Table,
highlighting.NewHighlighting(
highlighting.WithStyle(highlightingStyle),
highlighting.WithFormatOptions(
html.WithLineNumbers(false),
),
),
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
goldhtml.WithHardWraps(),
goldhtml.WithXHTML(),
goldhtml.WithUnsafe(),
),
)
// minify the CSS file
cssFileData, err := ioutil.ReadFile(cssFile)
if errors.Is(err, os.ErrNotExist) {
fmt.Printf("CSS file %s does not exist\n", cssFile)
}
mini := minify.New()
mini.AddFunc("text/css", css.Minify)
stylesheet, err := mini.Bytes("text/css", cssFileData)
if err != nil {
panic(err)
}
// layout params
bindings := map[string]interface{}{
"page": "home",
"config": config,
"stylesheet": stylesheet,
}
// render index.html
engine := liquid.NewEngine()
indexHTML, err := engine.ParseAndRenderString(layoutTemplateStr, bindings)
if err != nil {
log.Fatalln(err)
}
// write index.html
indexFile := filepath.Join(outputDir, "index.html")
err2 := ioutil.WriteFile(indexFile, []byte(indexHTML), os.ModePerm)
if err2 != nil {
log.Fatalln(err2)
}
// iterate over markdown files in docsDir
names, err := ioutil.ReadDir(docsDir)
if err != nil {
panic(err)
}
for _, fileInfo := range names {
file := filepath.Join([]string{docsDir, fileInfo.Name()}...)
if filepath.Ext(file) == ".md" {
// load and parse the markdown file
doc := ParseMarkdownFile(markdown, file)
docHTMLFile := filepath.Join(outputDir, doc.fileName+".html")
// template variables
bindings := map[string]interface{}{
"page": "article",
"config": config,
"meta": doc.metaData,
"slug": doc.fileName,
"content": doc.html.String(),
"stylesheet": stylesheet,
}
// render the document
docHTML, err := engine.ParseAndRenderString(layoutTemplateStr, bindings)
if err != nil {
log.Fatalln(err)
}
// write document HTML
err2 := ioutil.WriteFile(docHTMLFile, []byte(docHTML), os.ModePerm)
if err2 != nil {
log.Fatalln(err2)
}
}
}
}