2024-02-11 17:16:10 +01:00
|
|
|
// TODO consider making this another package
|
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/niklasfasching/go-org/org"
|
2024-02-11 17:30:10 +01:00
|
|
|
"gopkg.in/osteele/liquid.v1"
|
2024-02-11 17:16:10 +01:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const FM_SEPARATOR = "---"
|
|
|
|
|
|
|
|
type Template struct {
|
2024-02-12 19:16:56 +01:00
|
|
|
SrcPath string
|
2024-02-11 17:16:10 +01:00
|
|
|
Metadata map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(path string) (*Template, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// read and parse the yaml from the front matter
|
|
|
|
var yamlContent []byte
|
|
|
|
closed := false
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.TrimSpace(line) == FM_SEPARATOR {
|
|
|
|
closed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
yamlContent = append(yamlContent, []byte(line+"\n")...)
|
|
|
|
}
|
|
|
|
if !closed {
|
|
|
|
return nil, errors.New("front matter not closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var metadata map[string]interface{}
|
|
|
|
if len(yamlContent) != 0 {
|
|
|
|
err := yaml.Unmarshal([]byte(yamlContent), &metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid yaml: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:16:56 +01:00
|
|
|
templ := Template{SrcPath: path, Metadata: metadata}
|
|
|
|
return &templ, nil
|
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-11 18:03:28 +01:00
|
|
|
if ext == ".org" {
|
|
|
|
ext = ".html"
|
|
|
|
}
|
|
|
|
return ext
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:16:56 +01:00
|
|
|
func (templ Template) Render(context map[string]interface{}) (string, error) {
|
|
|
|
file, _ := os.Open(templ.SrcPath)
|
2024-02-11 17:16:10 +01:00
|
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
// first line is the front matter delimiter, Scan to skip
|
|
|
|
// and keep skipping until the closing delimiter
|
|
|
|
scanner.Scan()
|
|
|
|
scanner.Scan()
|
|
|
|
for scanner.Text() != FM_SEPARATOR {
|
|
|
|
scanner.Scan()
|
|
|
|
}
|
|
|
|
|
|
|
|
// now read the proper template contents to memory
|
2024-02-12 19:16:56 +01:00
|
|
|
contents := ""
|
2024-02-11 17:16:10 +01:00
|
|
|
for scanner.Scan() {
|
2024-02-12 19:16:56 +01:00
|
|
|
contents += scanner.Text() + "\n"
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:16:56 +01:00
|
|
|
if strings.HasSuffix(templ.SrcPath, ".org") {
|
2024-02-11 17:16:10 +01:00
|
|
|
// if it's an org file, convert to html
|
2024-02-12 19:16:56 +01:00
|
|
|
doc := org.New().Parse(strings.NewReader(contents), templ.SrcPath)
|
|
|
|
return doc.Write(org.NewHTMLWriter())
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2024-02-12 19:16:56 +01:00
|
|
|
// for other file types, assume a liquid template
|
|
|
|
engine := liquid.NewEngine()
|
|
|
|
return engine.ParseAndRenderString(contents, context)
|
2024-02-11 17:16:10 +01:00
|
|
|
}
|