mirror of
https://github.com/facundoolano/jorge.git
synced 2024-12-28 22:26:16 +01:00
73bbaa2c50
Squashed commit of the following: commit 7c5b6bf95b14e402b68b141a7d60ccb1468f12b3 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 13:15:08 2024 -0300 restore other tests commit 5cf5c43856fc1a9e8f23dc74b81607ab7387f4c3 Author: facundoolano <facundo.olano@gmail.com> Date: Sat Feb 10 23:13:31 2024 -0300 restore a test commit acca0936a42b8b915c25f96c6b435887d7235c23 Author: facundoolano <facundo.olano@gmail.com> Date: Sat Feb 10 22:52:41 2024 -0300 fix a bunch of bugs commit 6f8074402338194ebebaaf929a1d85fdbf0d5e22 Author: facundoolano <facundo.olano@gmail.com> Date: Sat Feb 10 22:00:43 2024 -0300 implement methods commit 5cfeb1ea8600317d8849c6dca63a009237e033af Author: facundoolano <facundo.olano@gmail.com> Date: Sat Feb 10 20:16:10 2024 -0300 add template package and struct commit 7a7b79e006ff6629cbf9445927e84e9c1600667b Author: facundoolano <facundo.olano@gmail.com> Date: Sat Feb 10 20:08:43 2024 -0300 stub template interface
105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
// TODO consider making this another package
|
|
package templates
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/niklasfasching/go-org/org"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
const FM_SEPARATOR = "---"
|
|
|
|
type Template struct {
|
|
srcPath string
|
|
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 {
|
|
return nil, nil
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
return &Template{srcPath: path, Metadata: metadata}, nil
|
|
}
|
|
|
|
func (templ Template) Ext() string {
|
|
return filepath.Ext(templ.srcPath)
|
|
}
|
|
|
|
func (templ Template) Render() ([]byte, error) {
|
|
file, _ := os.Open(templ.srcPath)
|
|
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
|
|
var contents []byte
|
|
for scanner.Scan() {
|
|
contents = append(contents, scanner.Text()+"\n"...)
|
|
}
|
|
|
|
if templ.Ext() == ".org" {
|
|
// if it's an org file, convert to html
|
|
doc := org.New().Parse(bytes.NewReader(contents), templ.srcPath)
|
|
html, err := doc.Write(org.NewHTMLWriter())
|
|
contents = []byte(html)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
} else {
|
|
// TODO for other file types, assume a liquid template
|
|
}
|
|
|
|
// TODO: if layout in metadata, pass the result to the rendered parent
|
|
|
|
return contents, nil
|
|
}
|