diff options
| author | a73x <[email protected]> | 2024-08-27 19:43:56 +0100 |
|---|---|---|
| committer | a73x <[email protected]> | 2024-08-27 20:23:18 +0100 |
| commit | d38fc83df3824b840f0b3930e4cb7236bdab84b2 (patch) | |
| tree | 586a7f940291f933ef2a7e05b20dff21ded6d78a /pages/pages.go | |
| parent | cffb105a9ff8ed5d7aa04e5f4097368f6be38b8e (diff) | |
feat(content): support templating in content
this is tired person code
don't write tired person code
Diffstat (limited to 'pages/pages.go')
| -rw-r--r-- | pages/pages.go | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/pages/pages.go b/pages/pages.go new file mode 100644 index 0000000..fbd179a --- /dev/null +++ b/pages/pages.go @@ -0,0 +1,107 @@ +package pages + +import ( + "bytes" + "fmt" + "text/template" + + "git.sr.ht/~a73x/home" + "git.sr.ht/~a73x/home/html" + "git.sr.ht/~a73x/home/markdown" +) + +type GlobalState struct { + Collections map[string][]markdown.Content +} + +type ParserPair struct { + GlobalState + markdown.Content +} + +func renderTemplate(config GlobalState, content markdown.Content) (string, error) { + tmpl := content.Meta["template"] + chosenTemplate := fmt.Sprintf("templates/%s.html", tmpl) + t, err := template.ParseFS(home.Content, chosenTemplate, "templates/layouts/*.html") + if err != nil { + return "", fmt.Errorf("failed to parse layouts: %v", err) + } + + contentParser, err := template.New("content").Parse(string(content.Body)) + if err != nil { + return "", fmt.Errorf("failed parsing content: %v", err) + } + + data := ParserPair{ + config, + content, + } + + newContent := &bytes.Buffer{} + if err := contentParser.Execute(newContent, data); err != nil { + return "", fmt.Errorf("failed to execute content template: %v", err) + } + + data.Body = string(html.MDToHTML(newContent.Bytes())) + + b := &bytes.Buffer{} + if err := t.Execute(b, data); err != nil { + return "", err + } + + return b.String(), nil +} + +type Page struct { + Path string + Content string +} + +func Collect() ([]Page, error) { + contents, err := markdown.ParseContents() + if err != nil { + return nil, err + } + + gs := GlobalState{ + Collections: map[string][]markdown.Content{ + "all": contents, + }, + } + + for _, content := range contents { + tags, ok := content.Meta["tags"] + if !ok { + continue + } + + switch tags := tags.(type) { + case string: + gs.Collections[tags] = append(gs.Collections[tags], content) + case []string: + for _, tag := range tags { + gs.Collections[tag] = append(gs.Collections[tag], content) + } + } + } + + pages := []Page{} + for _, content := range contents { + page, err := renderTemplate(gs, content) + if err != nil { + return nil, fmt.Errorf("failed to build site: %v", err) + } + + path := content.Path + if path == "index" { + path = "" + } + + pages = append(pages, Page{ + Path: path, + Content: page, + }) + } + + return pages, nil +} |
