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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
yaml "gopkg.in/yaml.v3"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"github.com/DavidBelicza/TextRank/v2"
"github.com/alexflint/go-arg"
"github.com/gosimple/slug"
"github.com/microcosm-cc/bluemonday"
"github.com/tdewolff/minify/v2"
mcss "github.com/tdewolff/minify/v2/css"
mhtml "github.com/tdewolff/minify/v2/html"
mjs "github.com/tdewolff/minify/v2/js"
highlighting "github.com/yuin/goldmark-highlighting/v2"
cp "github.com/otiai10/copy"
_ "embed"
)
type ConfigExtrasItem struct {
Type string `yaml:"type"`
Template string `yaml:"template"`
URL string `yaml:"url"`
}
type Config struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
BaseURL string `yaml:"baseurl"`
Language string `yaml:"language"`
Highlighting string `yaml:"highlighting"`
Minify bool `yaml:"minify"`
Extras []ConfigExtrasItem `yaml:"extras"`
}
type Page struct {
Filepath string
Raw string
HTML template.HTML
Text string
Summary string
Meta map[string]interface{}
Title string
Type string
RelPermalink string
Created time.Time
Draft bool
}
//go:embed "files/config.yaml"
var EmbedConfig string
//go:embed "files/first.md"
var EmbedPost string
//go:embed "files/base.html"
var EmbedTemplateBase string
//go:embed "files/index.html"
var EmbedTemplateIndex string
//go:embed "files/post.html"
var EmbedTemplatePost string
//go:embed "files/index.xml"
var EmbedTemplateFeed string
// Function to clean HTML tags using bluemonday.
func cleanHTMLTags(htmlString string) string {
p := bluemonday.StrictPolicy()
cleanString := p.Sanitize(htmlString)
return cleanString
}
func simpleServer(projectRoot string) {
fs := http.FileServer(http.Dir(path.Join(projectRoot, "public")))
http.Handle("/", fs)
log.Println("Server started on http://localhost:6969")
log.Fatal(http.ListenAndServe(":6969", nil))
}
func initializeProject(projectRoot string) {
log.Println("Initializing new project")
if err := os.Mkdir(path.Join(projectRoot, "templates"), 0755); err != nil && !os.IsExist(err) {
log.Println("Error creating directory:", err)
return
}
if err := os.Mkdir(path.Join(projectRoot, "content"), 0755); err != nil && !os.IsExist(err) {
log.Println("Error creating directory:", err)
return
}
if err := os.Mkdir(path.Join(projectRoot, "static"), 0755); err != nil && !os.IsExist(err) {
log.Println("Error creating directory:", err)
return
}
os.WriteFile(path.Join(projectRoot, "templates", ".gitkeep"), []byte{}, 0755)
os.WriteFile(path.Join(projectRoot, "content", ".gitkeep"), []byte{}, 0755)
os.WriteFile(path.Join(projectRoot, "static", ".gitkeep"), []byte{}, 0755)
os.WriteFile(path.Join(projectRoot, "config.yaml"), []byte(EmbedConfig), 0755)
os.WriteFile(path.Join(projectRoot, "content", "first.md"), []byte(EmbedPost), 0755)
os.WriteFile(path.Join(projectRoot, "templates", "base.html"), []byte(EmbedTemplateBase), 0755)
os.WriteFile(path.Join(projectRoot, "templates", "index.html"), []byte(EmbedTemplateIndex), 0755)
os.WriteFile(path.Join(projectRoot, "templates", "post.html"), []byte(EmbedTemplatePost), 0755)
os.WriteFile(path.Join(projectRoot, "templates", "index.xml"), []byte(EmbedTemplateFeed), 0755)
}
func buildProject(projectRoot string) {
// Read config file.
configFilepath := path.Join(projectRoot, "config.yaml")
configFile, err := os.ReadFile(configFilepath)
if err != nil {
panic(err)
}
config := Config{}
err = yaml.Unmarshal(configFile, &config)
if err != nil {
panic(err)
}
// Gets the list of all markdown files.
files, err := filepath.Glob(path.Join(projectRoot, "content/*.md"))
if err != nil {
panic(err)
}
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
meta.Meta,
highlighting.NewHighlighting(
highlighting.WithStyle(config.Highlighting),
),
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
parser.WithBlockParsers(),
parser.WithInlineParsers(),
parser.WithParagraphTransformers(),
parser.WithAttribute(),
),
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
),
)
// Parse all markdown files in content folder.
pages := []Page{}
for _, file := range files {
source, err := os.ReadFile(file)
if err != nil {
panic(err)
}
var buf bytes.Buffer
ctx := parser.NewContext()
if err := md.Convert(source, &buf, parser.WithContext(ctx)); err != nil {
panic(err)
}
// Rank and summarize.
tr := textrank.NewTextRank()
rule := textrank.NewDefaultRule()
language := textrank.NewDefaultLanguage()
algorithmDef := textrank.NewDefaultAlgorithm()
tr.Populate(cleanHTMLTags(buf.String()), language, rule)
tr.Ranking(algorithmDef)
sentences := textrank.FindSentencesByRelationWeight(tr, 50)
sentences = textrank.FindSentencesFrom(tr, 0, 1)
summary := ""
for _, s := range sentences {
summary = strings.ReplaceAll(s.Value, "\n", "")
}
metaData := meta.Get(ctx)
t, _ := time.Parse("2006-01-02T15:04:05-07:00", metaData["date"].(string))
pages = append(pages, Page{
Filepath: file,
Meta: metaData,
Raw: buf.String(),
HTML: template.HTML(buf.String()),
Text: cleanHTMLTags(buf.String()),
Summary: summary,
Title: metaData["title"].(string),
Type: metaData["type"].(string),
RelPermalink: metaData["url"].(string),
Created: t,
Draft: metaData["draft"].(bool),
})
}
// Sorting pages in descending created order.
sort.Slice(pages, func(i, j int) bool {
return pages[i].Created.After(pages[j].Created)
})
// Creates public folder if it doesn't exist yet.
if err := os.Mkdir(path.Join(projectRoot, "public"), 0755); err != nil && !os.IsExist(err) {
log.Println("Error creating directory:", err)
return
}
// Generate HTML files for all pages.
for _, page := range pages {
outFilepath := path.Join(projectRoot, "public", page.Meta["url"].(string))
if !page.Draft {
pageTemplateFilename := fmt.Sprintf("%s.html", page.Meta["type"].(string))
templatePathname := path.Join(projectRoot, "templates", pageTemplateFilename)
baseTemplatePathname := path.Join(projectRoot, "templates/base.html")
t, err := template.ParseFiles(baseTemplatePathname, templatePathname)
if err != nil {
panic(err)
}
type Payload struct {
Config Config
Page Page
}
var buf bytes.Buffer
err = t.Execute(&buf, Payload{
Config: config,
Page: page,
})
if err != nil {
panic(err)
}
outHTML := buf.String()
if config.Minify {
m := minify.New()
m.AddFunc("text/html", mhtml.Minify)
m.AddFunc("text/css", mcss.Minify)
m.AddFunc("application/js", mjs.Minify)
outHTML, err = m.String("text/html", outHTML)
if err != nil {
panic(err)
}
}
os.WriteFile(outFilepath, []byte(outHTML), 0755)
log.Println("Wrote", outFilepath)
} else {
log.Println("Skipped", outFilepath)
}
}
// Generates index page.
{
log.Println("Writing index...")
templatePathname := path.Join(projectRoot, "templates/index.html")
baseTemplatePathname := path.Join(projectRoot, "templates/base.html")
t, err := template.ParseFiles(baseTemplatePathname, templatePathname)
if err != nil {
panic(err)
}
type Payload struct {
Config Config
Pages []Page
}
var buf bytes.Buffer
err = t.Execute(&buf, Payload{
Config: config,
Pages: pages,
})
if err != nil {
panic(err)
}
outHTML := buf.String()
if config.Minify {
m := minify.New()
m.AddFunc("text/html", mhtml.Minify)
m.AddFunc("text/css", mcss.Minify)
m.AddFunc("application/js", mjs.Minify)
outHTML, err = m.String("text/html", outHTML)
if err != nil {
panic(err)
}
}
outFilepath := path.Join(projectRoot, "public", "index.html")
os.WriteFile(outFilepath, []byte(outHTML), 0755)
}
// Copy static files.
{
log.Println("Copying static files...")
err := cp.Copy(path.Join(projectRoot, "static"), path.Join(projectRoot, "public"))
if err != nil {
panic(err)
}
}
// Generates extras.
{
for _, extra := range config.Extras {
log.Printf("Writing extras %s\n", extra.URL)
templatePathname := path.Join(projectRoot, "templates", extra.Template)
t, err := template.ParseFiles(templatePathname)
if err != nil {
panic(err)
}
type Payload struct {
Config Config
Pages []Page
}
var buf bytes.Buffer
err = t.Execute(&buf, Payload{
Config: config,
Pages: pages,
})
if err != nil {
panic(err)
}
outFilepath := path.Join(projectRoot, "public", extra.URL)
os.WriteFile(outFilepath, []byte(buf.String()), 0755)
}
}
// Guess we are done!
log.Println("Done & done...")
}
func newPage(projectRoot string, title string) {
slug := slug.Make(title)
t := time.Now()
filename := fmt.Sprintf("%s-%s.md", t.Format("2006-01-02"), slug)
var lines = []string{
"---",
fmt.Sprintf("title: \"%s\"", title),
fmt.Sprintf("url: %s.html", slug),
fmt.Sprintf("date: %s", t.Format("2006-01-02T15:04:05-07:00")),
"type: post",
"draft: true",
"---",
"",
"Content...",
}
f, err := os.Create(path.Join(projectRoot, "content", filename))
if err != nil {
log.Fatal(err)
}
defer f.Close()
for _, line := range lines {
_, err := f.WriteString(line + "\n")
if err != nil {
log.Fatal(err)
}
}
log.Printf("Page `%s` created\n", filename)
}
func main() {
projectRoot := os.Getenv("PROJECT_ROOT")
if projectRoot == "" {
projectRoot = "./"
}
var args struct {
Init bool `arg:"-i,--init" help:"initialize new project"`
Build bool `arg:"-b,--build" help:"build the website"`
Server bool `arg:"-s,--server" help:"simple embedded HTTP server"`
New bool `arg:"-n,--new" help:"create new page"`
Title string `arg:"positional"`
}
arg.MustParse(&args)
if !args.Init && !args.Build && !args.Server && !args.New {
fmt.Println("No arguments provided. Try using `jbmafp --help`")
os.Exit(0)
}
if args.Init {
initializeProject(projectRoot)
}
if args.Build {
buildProject(projectRoot)
}
if args.Server {
simpleServer(projectRoot)
}
if args.New {
if len(args.Title) == 0 {
fmt.Println("You must provide a title for the new page")
os.Exit(1)
}
newPage(projectRoot, args.Title)
}
}
|