-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
203 lines (170 loc) · 3.9 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
package main
import (
"errors"
"html/template"
"log"
"os"
"path/filepath"
"github.com/urfave/cli/v2"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
type Config struct {
Dir string
Out string
File string
}
type Layout struct {
Title string
Content []template.HTML
}
var AppConfig = Config{
Dir: "./",
Out: "./",
File: "",
}
var AppLayout = Layout{
Title: "Liquipage",
Content: []template.HTML{},
}
func main() {
app := cli.NewApp()
app.Name = "liquipage"
app.Usage = "Tiny static site generator from markdown files."
app.Version = "0.0.0"
app.UsageText = "liquipage --dir path/to/docs"
app.Authors = []*cli.Author{
{
Name: "Eduardo Correia",
Email: "[email protected]",
},
}
app.Commands = []*cli.Command{
{
Name: "build",
Aliases: []string{"b"},
Usage: "Generates a static page from markdown files in directory",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
Value: "./",
Aliases: []string{"d"},
Usage: "directory containing the markdown files",
Destination: &AppConfig.Dir,
},
&cli.StringFlag{
Name: "out",
Value: "./",
Aliases: []string{"o"},
Usage: "output directory for the html file",
Destination: &AppConfig.Out,
},
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "a single file",
Destination: &AppConfig.File,
},
&cli.StringFlag{
Name: "title",
Aliases: []string{"t"},
Usage: "title for you static page",
Destination: &AppLayout.Title,
},
},
Action: func(c *cli.Context) error {
if c.NArg() > 0 {
return errors.New("too many args where given")
}
paths, err := getMDFiles()
if err != nil {
return err
}
mds := []string{}
for _, v := range paths {
content, err := getContentFromMDFile(v)
if err != nil {
return err
}
if len(content) == 0 {
continue
}
mds = append(mds, content)
}
err = generateHTMLFile(mds)
if err != nil {
return err
}
return nil
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func getMDFiles() ([]string, error) {
paths := []string{}
if len(AppConfig.File) > 0 {
paths = append(paths, AppConfig.File)
return paths, nil
}
err := filepath.Walk(AppConfig.Dir, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".md" {
paths = append(paths, path)
}
return err
})
if err != nil {
return nil, err
}
if len(paths) == 0 {
return nil, errors.New("no markdown files found")
}
return paths, nil
}
func getContentFromMDFile(path string) (string, error) {
b, err := os.ReadFile(path)
if err != nil {
return "", err
}
content := string(b)
return content, nil
}
func generateHTMLFile(mds []string) error {
for _, v := range mds {
md := []byte(v)
html := convertMDToHTML(md)
AppLayout.Content = append(AppLayout.Content, template.HTML(html))
}
tmpl := template.Must(template.ParseFiles("layout.html"))
if AppConfig.Out != "./" {
if _, err := os.Stat(AppConfig.Out); os.IsNotExist(err) {
err := os.MkdirAll(AppConfig.Out, 0755)
if err != nil {
return err
}
}
}
filepath := filepath.Join(AppConfig.Out, "index.html")
file, err := os.Create(filepath)
if err != nil {
return err
}
defer file.Close()
err = tmpl.Execute(file, AppLayout)
if err != nil {
return err
}
return nil
}
func convertMDToHTML(md []byte) []byte {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(md)
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
return markdown.Render(doc, renderer)
}