2024-02-11 17:16:10 +01:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-02-15 03:54:46 +01:00
|
|
|
"bytes"
|
2024-02-11 17:16:10 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/niklasfasching/go-org/org"
|
2024-02-15 03:54:46 +01:00
|
|
|
"github.com/osteele/liquid"
|
|
|
|
"github.com/yuin/goldmark"
|
2024-02-11 17:16:10 +01:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const FM_SEPARATOR = "---"
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
type Engine = liquid.Engine
|
|
|
|
|
2024-02-11 17:16:10 +01:00
|
|
|
type Template struct {
|
2024-02-15 03:54:46 +01:00
|
|
|
SrcPath string
|
|
|
|
Metadata map[string]interface{}
|
|
|
|
liquidTemplate liquid.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEngine() *Engine {
|
2024-02-15 17:53:45 +01:00
|
|
|
// a lot of the filters and tags available at jekyll aren't default liquid
|
|
|
|
// manually adding them here as in https://github.com/osteele/gojekyll/blob/main/filters/filters.go
|
|
|
|
|
|
|
|
e := liquid.NewEngine()
|
|
|
|
|
|
|
|
e.RegisterFilter("filter", filter)
|
|
|
|
e.RegisterFilter("group_by", groupByFilter)
|
|
|
|
e.RegisterFilter("group_by_exp", groupByExpFilter)
|
|
|
|
e.RegisterFilter("sort", sortFilter)
|
|
|
|
e.RegisterFilter("where", whereFilter)
|
|
|
|
e.RegisterFilter("where_exp", whereExpFilter)
|
|
|
|
|
|
|
|
e.RegisterFilter("absolute_url", func(s string) string {
|
|
|
|
// FIXME implement after adding a config struct, using the url
|
|
|
|
// return utils.URLJoin(c.AbsoluteURL, c.BaseURL, s)
|
|
|
|
return s
|
|
|
|
})
|
|
|
|
|
|
|
|
return e
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
func Parse(engine *Engine, path string) (*Template, error) {
|
2024-02-11 17:16:10 +01:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
// if the file doesn't start with a front matter delimiter, it's not a template
|
|
|
|
if strings.TrimSpace(line) != FM_SEPARATOR {
|
2024-02-12 19:33:30 +01:00
|
|
|
return nil, nil
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
// extract the yaml front matter and save the rest of the template content separately
|
2024-02-11 17:16:10 +01:00
|
|
|
var yamlContent []byte
|
2024-02-15 03:54:46 +01:00
|
|
|
var liquidContent []byte
|
|
|
|
yamlClosed := false
|
2024-02-11 17:16:10 +01:00
|
|
|
for scanner.Scan() {
|
2024-02-15 03:54:46 +01:00
|
|
|
line := append(scanner.Bytes(), '\n')
|
|
|
|
if yamlClosed {
|
|
|
|
liquidContent = append(liquidContent, line...)
|
|
|
|
} else {
|
|
|
|
if strings.TrimSpace(scanner.Text()) == FM_SEPARATOR {
|
|
|
|
yamlClosed = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
yamlContent = append(yamlContent, line...)
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-15 03:54:46 +01:00
|
|
|
liquidContent = bytes.TrimSuffix(liquidContent, []byte("\n"))
|
|
|
|
|
|
|
|
if !yamlClosed {
|
2024-02-11 17:16:10 +01:00
|
|
|
return nil, errors.New("front matter not closed")
|
|
|
|
}
|
|
|
|
|
2024-02-14 01:32:04 +01:00
|
|
|
metadata := make(map[string]interface{})
|
2024-02-11 17:16:10 +01:00
|
|
|
if len(yamlContent) != 0 {
|
|
|
|
err := yaml.Unmarshal([]byte(yamlContent), &metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid yaml: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
liquid, err := engine.ParseTemplateAndCache(liquidContent, path, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
templ := Template{SrcPath: path, Metadata: metadata, liquidTemplate: *liquid}
|
2024-02-12 19:16:56 +01:00
|
|
|
return &templ, nil
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
// Return the extension for the output format of this template
|
2024-02-11 17:16:10 +01:00
|
|
|
func (templ Template) Ext() string {
|
2024-02-12 19:16:56 +01:00
|
|
|
ext := filepath.Ext(templ.SrcPath)
|
2024-02-15 03:54:46 +01:00
|
|
|
if ext == ".org" || ext == ".md" {
|
|
|
|
return ".html"
|
2024-02-11 18:03:28 +01:00
|
|
|
}
|
|
|
|
return ext
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
func (templ Template) Render(context map[string]interface{}) ([]byte, error) {
|
|
|
|
content, err := templ.liquidTemplate.Render(context)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
ext := filepath.Ext(templ.SrcPath)
|
|
|
|
if ext == ".org" {
|
|
|
|
doc := org.New().Parse(bytes.NewReader(content), templ.SrcPath)
|
|
|
|
contentStr, err := doc.Write(org.NewHTMLWriter())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-02-13 17:43:20 +01:00
|
|
|
}
|
2024-02-15 03:54:46 +01:00
|
|
|
content = []byte(contentStr)
|
|
|
|
} else if ext == ".md" {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := goldmark.Convert(content, &buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
content = buf.Bytes()
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
return content, nil
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|