mirror of
https://github.com/facundoolano/jorge.git
synced 2024-12-25 21:58:28 +01:00
Add site struct and layout support
Squashed commit of the following: commit 0ee8a385f111be807b4485b42316df6698d962f9 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 14:12:22 2024 -0300 load layouts commit 1c2594bb8aa6d6d9fbafcb530fdcdbdec2c146e7 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 13:17:28 2024 -0300 add Site struct, explore some refactors commit 3d8acb3957f5ba38a6e48d2614b9af65f1219298 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 11:33:19 2024 -0300 prepare new phases structure for build command commit fe7dcf9fb08c7b3e5679cf08c80beecc2eeb36e5 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 10:57:52 2024 -0300 set template type commit d9faa70c8d2d23c9b62904e7429a82437517b9c5 Author: facundoolano <facundo.olano@gmail.com> Date: Mon Feb 12 10:49:30 2024 -0300 add Type to template commit 27e0feede10f6c1340ce722085011a5eebcaee13 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 19:01:16 2024 -0300 stub build and render extensions commit e25518de3440cb3df2aa5674523128eff1d23404 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 17:37:30 2024 -0300 pass pre-parsed layouts by arg instead commit b3c2c9ebeb0d07d425f6db6a1bbbb5ee61c04548 Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 15:13:17 2024 -0300 first stab at recursively populating layouts commit 4fe112694a3c44056f7aa5bd2be0794abcf4aa2a Author: facundoolano <facundo.olano@gmail.com> Date: Sun Feb 11 14:33:07 2024 -0300 initial support for page bindings
This commit is contained in:
parent
b7d8d12df7
commit
e97af58830
4 changed files with 273 additions and 66 deletions
|
@ -1,17 +1,20 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/facundoolano/blorg/templates"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/facundoolano/blorg/templates"
|
||||
)
|
||||
|
||||
const SRC_DIR = "src"
|
||||
const TARGET_DIR = "target"
|
||||
const LAYOUT_DIR = "layouts"
|
||||
const FILE_RW_MODE = 0777
|
||||
|
||||
func Init() error {
|
||||
// get working directory
|
||||
// default to .
|
||||
|
@ -24,45 +27,109 @@ func Init() error {
|
|||
// Read the files in src/ render them and copy the result to target/
|
||||
// FIXME pass src and target by arg
|
||||
func Build() error {
|
||||
const FILE_MODE = 0777
|
||||
|
||||
// fail if no src dir
|
||||
_, err := os.ReadDir("src")
|
||||
_, err := os.ReadDir(SRC_DIR)
|
||||
if os.IsNotExist(err) {
|
||||
return errors.New("missing src/ directory")
|
||||
return fmt.Errorf("missing %s directory", SRC_DIR)
|
||||
} else if err != nil {
|
||||
return errors.New("couldn't read src")
|
||||
return fmt.Errorf("couldn't read %s", SRC_DIR)
|
||||
}
|
||||
|
||||
// clear previous target contents
|
||||
os.RemoveAll("target")
|
||||
os.Mkdir("target", FILE_MODE)
|
||||
site := Site{
|
||||
layouts: make(map[string]templates.Template),
|
||||
}
|
||||
|
||||
// render each source file and copy it over to target
|
||||
err = filepath.WalkDir("src", func(path string, entry fs.DirEntry, err error) error {
|
||||
subpath, _ := filepath.Rel("src", path)
|
||||
targetPath := filepath.Join("target", subpath)
|
||||
// FIXME these sound like they should be site methods too
|
||||
PHASES := []func(*Site) error{
|
||||
loadConfig,
|
||||
loadLayouts,
|
||||
loadTemplates,
|
||||
writeTarget,
|
||||
}
|
||||
for _, phaseFun := range PHASES {
|
||||
if err := phaseFun(&site); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if entry.IsDir() {
|
||||
os.MkdirAll(targetPath, FILE_MODE)
|
||||
} else {
|
||||
template, err := templates.Parse(path)
|
||||
return err
|
||||
}
|
||||
|
||||
func loadConfig(site *Site) error {
|
||||
// context["config"]
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadLayouts(site *Site) error {
|
||||
files, err := os.ReadDir(LAYOUT_DIR)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, entry := range files {
|
||||
if !entry.IsDir() {
|
||||
filename := entry.Name()
|
||||
path := filepath.Join(LAYOUT_DIR, filename)
|
||||
templ, err := templates.Parse(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if template != nil {
|
||||
// if a template was found at source, render it
|
||||
targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + template.Ext()
|
||||
layout_name := strings.TrimSuffix(filename, filepath.Ext(filename))
|
||||
site.layouts[layout_name] = *templ
|
||||
}
|
||||
}
|
||||
|
||||
content, err := template.Render()
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadTemplates(site *Site) error {
|
||||
return filepath.WalkDir(SRC_DIR, func(path string, entry fs.DirEntry, err error) error {
|
||||
if !entry.IsDir() {
|
||||
templ, err := templates.Parse(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch templ.Type {
|
||||
case templates.POST:
|
||||
site.posts = append(site.posts, *templ)
|
||||
case templates.PAGE:
|
||||
site.pages = append(site.pages, *templ)
|
||||
}
|
||||
// TODO add tags
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func writeTarget(site *Site) error {
|
||||
// clear previous target contents
|
||||
os.RemoveAll(TARGET_DIR)
|
||||
os.Mkdir(TARGET_DIR, FILE_RW_MODE)
|
||||
|
||||
// walk the source directory, creating directories and files at the target dir
|
||||
templIndex := site.templateIndex()
|
||||
return filepath.WalkDir(SRC_DIR, func(path string, entry fs.DirEntry, err error) error {
|
||||
subpath, _ := filepath.Rel(SRC_DIR, path)
|
||||
targetPath := filepath.Join(TARGET_DIR, subpath)
|
||||
|
||||
if entry.IsDir() {
|
||||
os.MkdirAll(targetPath, FILE_RW_MODE)
|
||||
} else {
|
||||
|
||||
if templ, ok := templIndex[path]; ok {
|
||||
// if a template was found at source, render it
|
||||
content, err := site.render(templ)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// write the file contents over to target at the same location
|
||||
targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + templ.Ext()
|
||||
fmt.Println("writing ", targetPath)
|
||||
return os.WriteFile(targetPath, content, FILE_MODE)
|
||||
return os.WriteFile(targetPath, []byte(content), FILE_RW_MODE)
|
||||
} else {
|
||||
// if a non template was found, copy file as is
|
||||
fmt.Println("writing ", targetPath)
|
||||
|
@ -72,8 +139,6 @@ func Build() error {
|
|||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func copyFile(source string, target string) error {
|
||||
|
|
64
commands/site.go
Normal file
64
commands/site.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/facundoolano/blorg/templates"
|
||||
)
|
||||
|
||||
// TODO review build and other commands and think what can be brought over here.
|
||||
// e.g. SRC and TARGET dir knowledge
|
||||
type Site struct {
|
||||
config map[string]string // may need to make this interface{} if config gets sophisticated
|
||||
layouts map[string]templates.Template
|
||||
posts []templates.Template
|
||||
pages []templates.Template
|
||||
tags map[string]*templates.Template
|
||||
|
||||
renderCache map[string]string
|
||||
}
|
||||
|
||||
func (site Site) render(templ *templates.Template) (string, error) {
|
||||
ctx := site.contextFor(templ)
|
||||
content, err := templ.Render(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// recursively render parent layouts
|
||||
layout := templ.Metadata["layout"]
|
||||
for layout != nil && err == nil {
|
||||
if layout_templ, ok := site.layouts[layout.(string)]; ok {
|
||||
ctx["content"] = content
|
||||
content, err = layout_templ.Render(ctx)
|
||||
layout = layout_templ.Metadata["layout"]
|
||||
} else {
|
||||
return "", fmt.Errorf("layout '%s' not found", layout)
|
||||
}
|
||||
}
|
||||
|
||||
return content, err
|
||||
}
|
||||
|
||||
func (site Site) templateIndex() map[string]*templates.Template {
|
||||
templIndex := make(map[string]*templates.Template)
|
||||
for _, templ := range append(site.posts, site.pages...) {
|
||||
templIndex[templ.SrcPath] = &templ
|
||||
}
|
||||
return templIndex
|
||||
}
|
||||
|
||||
func (site Site) contextFor(templ *templates.Template) map[string]interface{} {
|
||||
bindings := map[string]interface{}{
|
||||
"config": site.config,
|
||||
"posts": site.posts,
|
||||
"tags": site.tags,
|
||||
}
|
||||
if templ.Type == templates.LAYOUT {
|
||||
bindings["layout"] = templ.Metadata
|
||||
} else {
|
||||
// assuming that if it's not a layout then it must be a page
|
||||
bindings["page"] = templ.Metadata
|
||||
}
|
||||
return bindings
|
||||
}
|
|
@ -3,7 +3,6 @@ package templates
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -17,11 +16,35 @@ import (
|
|||
|
||||
const FM_SEPARATOR = "---"
|
||||
|
||||
type Type string
|
||||
|
||||
const (
|
||||
// a file that doesn't have a front matter header, and thus is not renderable.
|
||||
STATIC Type = "static"
|
||||
|
||||
// Templates in the root /layouts/ can be used to wrap around other template's content
|
||||
// by setting the `layout` front matter field.
|
||||
LAYOUT Type = "layout"
|
||||
|
||||
// A template that has a date, and thus can be ordered chronologically in a directory.
|
||||
// They can thus be arranged in archives, feeds, etc.
|
||||
// Posts are also assumed to have a title and can be excerpted.
|
||||
POST Type = "post"
|
||||
|
||||
// The rest of the templates: no layout and no post
|
||||
PAGE Type = "page"
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
srcPath string
|
||||
Type Type
|
||||
SrcPath string
|
||||
Metadata map[string]interface{}
|
||||
}
|
||||
|
||||
// TODO think about knowledge boundaries
|
||||
// should this know to tell if its a layout based on srcPath conventions?
|
||||
// should it be able to detect its own type? does it still make sense to track a template type,
|
||||
// separate from the site?
|
||||
func Parse(path string) (*Template, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
|
@ -35,7 +58,7 @@ func Parse(path string) (*Template, error) {
|
|||
|
||||
// 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
|
||||
return &Template{Type: STATIC}, nil
|
||||
}
|
||||
|
||||
// read and parse the yaml from the front matter
|
||||
|
@ -61,19 +84,30 @@ func Parse(path string) (*Template, error) {
|
|||
}
|
||||
}
|
||||
|
||||
return &Template{srcPath: path, Metadata: metadata}, nil
|
||||
templ := Template{SrcPath: path, Metadata: metadata}
|
||||
|
||||
// FIXME this also should check that it's in the root folder
|
||||
if strings.HasSuffix(filepath.Dir(templ.SrcPath), "layouts") {
|
||||
templ.Type = LAYOUT
|
||||
} else if _, ok := metadata["date"]; ok {
|
||||
templ.Type = POST
|
||||
} else {
|
||||
templ.Type = PAGE
|
||||
}
|
||||
|
||||
return &templ, nil
|
||||
}
|
||||
|
||||
func (templ Template) Ext() string {
|
||||
ext := filepath.Ext(templ.srcPath)
|
||||
ext := filepath.Ext(templ.SrcPath)
|
||||
if ext == ".org" {
|
||||
ext = ".html"
|
||||
}
|
||||
return ext
|
||||
}
|
||||
|
||||
func (templ Template) Render() ([]byte, error) {
|
||||
file, _ := os.Open(templ.srcPath)
|
||||
func (templ Template) Render(context map[string]interface{}) (string, error) {
|
||||
file, _ := os.Open(templ.SrcPath)
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
|
@ -86,31 +120,18 @@ func (templ Template) Render() ([]byte, error) {
|
|||
}
|
||||
|
||||
// now read the proper template contents to memory
|
||||
var contents []byte
|
||||
contents := ""
|
||||
for scanner.Scan() {
|
||||
contents = append(contents, scanner.Text()+"\n"...)
|
||||
contents += scanner.Text() + "\n"
|
||||
}
|
||||
|
||||
if strings.HasSuffix(templ.srcPath, ".org") {
|
||||
if strings.HasSuffix(templ.SrcPath, ".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 {
|
||||
// for other file types, assume a liquid template
|
||||
engine := liquid.NewEngine()
|
||||
out, err := engine.ParseAndRenderString(string(contents), templ.Metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contents = []byte(out)
|
||||
doc := org.New().Parse(strings.NewReader(contents), templ.SrcPath)
|
||||
return doc.Write(org.NewHTMLWriter())
|
||||
}
|
||||
|
||||
// TODO: if layout in metadata, pass the result to the rendered parent
|
||||
|
||||
return contents, nil
|
||||
// for other file types, assume a liquid template
|
||||
engine := liquid.NewEngine()
|
||||
return engine.ParseAndRenderString(contents, context)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package templates
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -21,13 +22,14 @@ tags: ["software", "web"]
|
|||
templ, err := Parse(file.Name())
|
||||
assertEqual(t, err, nil)
|
||||
|
||||
assertEqual(t, templ.Type, PAGE)
|
||||
assertEqual(t, templ.Ext(), ".html")
|
||||
assertEqual(t, templ.Metadata["title"], "my new post")
|
||||
assertEqual(t, templ.Metadata["subtitle"], "a blog post")
|
||||
assertEqual(t, templ.Metadata["tags"].([]interface{})[0], "software")
|
||||
assertEqual(t, templ.Metadata["tags"].([]interface{})[1], "web")
|
||||
|
||||
content, err := templ.Render()
|
||||
content, err := templ.Render(nil)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, string(content), "<p>Hello World!</p>\n")
|
||||
}
|
||||
|
@ -45,7 +47,7 @@ subtitle: a blog post
|
|||
|
||||
templ, err := Parse(file.Name())
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, templ, (*Template)(nil))
|
||||
assertEqual(t, templ.Type, STATIC)
|
||||
|
||||
// not first thing in file, leaving as is
|
||||
input = `#+OPTIONS: toc:nil num:nil
|
||||
|
@ -61,7 +63,7 @@ tags: ["software", "web"]
|
|||
|
||||
templ, err = Parse(file.Name())
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, templ, (*Template)(nil))
|
||||
assertEqual(t, templ.Type, STATIC)
|
||||
}
|
||||
|
||||
func TestInvalidFrontMatter(t *testing.T) {
|
||||
|
@ -94,9 +96,9 @@ title: my new post
|
|||
subtitle: a blog post
|
||||
tags: ["software", "web"]
|
||||
---
|
||||
<h1>{{ title }}</h1>
|
||||
<h2>{{ subtitle }}</h2>
|
||||
<ul>{% for tag in tags %}
|
||||
<h1>{{ page.title }}</h1>
|
||||
<h2>{{ page.subtitle }}</h2>
|
||||
<ul>{% for tag in page.tags %}
|
||||
<li>{{tag}}</li>{% endfor %}
|
||||
</ul>
|
||||
`
|
||||
|
@ -106,7 +108,8 @@ tags: ["software", "web"]
|
|||
|
||||
templ, err := Parse(file.Name())
|
||||
assertEqual(t, err, nil)
|
||||
content, err := templ.Render()
|
||||
ctx := map[string]interface{}{"page": templ.Metadata}
|
||||
content, err := templ.Render(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
expected := `<h1>my new post</h1>
|
||||
<h2>a blog post</h2>
|
||||
|
@ -138,7 +141,7 @@ tags: ["software", "web"]
|
|||
assertEqual(t, err, nil)
|
||||
assertEqual(t, templ.Ext(), ".html")
|
||||
|
||||
content, err := templ.Render()
|
||||
content, err := templ.Render(nil)
|
||||
assertEqual(t, err, nil)
|
||||
expected := `<div id="outline-container-headline-1" class="outline-2">
|
||||
<h2 id="headline-1">
|
||||
|
@ -163,7 +166,55 @@ my Subtitle
|
|||
}
|
||||
|
||||
func TestRenderLiquidLayout(t *testing.T) {
|
||||
// TODO
|
||||
input := `---
|
||||
title: base layout
|
||||
---
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<p>this is the {{layout.title}} that wraps the content of {{ page.title}}</p>
|
||||
{{ content }}
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
base := newFile("layouts/base*.html", input)
|
||||
defer os.Remove(base.Name())
|
||||
baseTempl, err := Parse(base.Name())
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, baseTempl.Type, LAYOUT)
|
||||
|
||||
context := map[string]interface{}{
|
||||
"layouts": map[string]Template{
|
||||
"base": *baseTempl,
|
||||
},
|
||||
}
|
||||
|
||||
input = `---
|
||||
title: my very first post
|
||||
layout: base
|
||||
date: 2023-12-01
|
||||
---
|
||||
<h1>{{page.title}}</h1>`
|
||||
|
||||
post := newFile("src/post1*.html", input)
|
||||
defer os.Remove(post.Name())
|
||||
|
||||
templ, err := Parse(post.Name())
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, templ.Type, POST)
|
||||
content, err := templ.Render(context)
|
||||
assertEqual(t, err, nil)
|
||||
expected := `<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<p>this is the base layout that wraps the content of my very first post</p>
|
||||
<h1>my very first post</h1>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
assertEqual(t, string(content), expected)
|
||||
}
|
||||
|
||||
func TestRenderOrgLayout(t *testing.T) {
|
||||
|
@ -176,8 +227,14 @@ func TestRenderLayoutLayout(t *testing.T) {
|
|||
|
||||
// ------ HELPERS --------
|
||||
|
||||
func newFile(name string, contents string) *os.File {
|
||||
file, _ := os.CreateTemp("", name)
|
||||
func newFile(path string, contents string) *os.File {
|
||||
parts := strings.Split(path, string(filepath.Separator))
|
||||
name := parts[len(parts)-1]
|
||||
path = filepath.Join(parts[:len(parts)-1]...)
|
||||
path = filepath.Join(os.TempDir(), path)
|
||||
os.MkdirAll(path, 0777)
|
||||
|
||||
file, _ := os.CreateTemp(path, name)
|
||||
file.WriteString(contents)
|
||||
return file
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue