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
|
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"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/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"
)
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
}
// Function to clean HTML tags using bluemonday.
func cleanHTMLTags(htmlString string) string {
p := bluemonday.StrictPolicy()
cleanString := p.Sanitize(htmlString)
return cleanString
}
func initializeProject(projectRoot string) {
fmt.Println("Initializing new project")
}
func main() {
projectRoot := os.Getenv("PROJECT_ROOT")
if projectRoot == "" {
projectRoot = "./"
}
fmt.Println("Come back later! Still WIP!")
os.Exit(0)
// Parsing arguments.
var args struct {
Init bool `arg:"-i,--init" help:"initialize new project"`
Build bool `arg:"-b,--build" help:"build the website"`
}
arg.MustParse(&args)
if !args.Init && !args.Build {
fmt.Println("No arguments provided. Try using `jbmafp --help`")
os.Exit(0)
}
if args.Init {
initializeProject(projectRoot)
os.Exit(0)
}
os.Exit(0)
// 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)
})
// 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...")
}
|