jorge/site/site.go

410 lines
11 KiB
Go
Raw Normal View History

2024-02-12 19:59:18 +01:00
package site
import (
"bytes"
2024-02-12 19:59:18 +01:00
"fmt"
"io"
2024-02-12 19:59:18 +01:00
"io/fs"
"os"
"path/filepath"
"runtime"
"slices"
2024-02-12 19:59:18 +01:00
"strings"
"sync"
"time"
2024-02-16 19:29:43 +01:00
"github.com/facundoolano/jorge/config"
"github.com/facundoolano/jorge/markup"
2024-02-15 20:01:35 +01:00
"gopkg.in/yaml.v3"
2024-02-12 19:59:18 +01:00
)
2024-02-27 16:30:00 +01:00
const FILE_RW_MODE = 0666
const DIR_RWE_MODE = 0777
2024-02-12 19:59:18 +01:00
type Site struct {
Config config.Config
layouts map[string]markup.Template
posts []map[string]interface{}
pages []map[string]interface{}
tags map[string][]map[string]interface{}
2024-02-15 20:01:35 +01:00
data map[string]interface{}
2024-02-12 19:59:18 +01:00
templateEngine *markup.Engine
templates map[string]*markup.Template
2024-02-16 19:14:30 +01:00
minifier markup.Minifier
2024-02-12 19:59:18 +01:00
}
func Load(config config.Config) (*Site, error) {
2024-02-12 19:59:18 +01:00
site := Site{
layouts: make(map[string]markup.Template),
templates: make(map[string]*markup.Template),
Config: config,
tags: make(map[string][]map[string]interface{}),
2024-02-15 20:01:35 +01:00
data: make(map[string]interface{}),
templateEngine: markup.NewEngine(config.SiteUrl, config.IncludesDir),
2024-02-12 19:59:18 +01:00
}
if err := site.loadDataFiles(); err != nil {
2024-02-15 20:01:35 +01:00
return nil, err
}
if err := site.loadLayouts(); err != nil {
2024-02-12 19:59:18 +01:00
return nil, err
}
if err := site.loadTemplates(); err != nil {
2024-02-12 19:59:18 +01:00
return nil, err
}
site.minifier = markup.LoadMinifier()
2024-02-16 19:14:30 +01:00
2024-02-12 19:59:18 +01:00
return &site, nil
}
func (site *Site) loadLayouts() error {
files, err := os.ReadDir(site.Config.LayoutsDir)
2024-02-12 19:59:18 +01:00
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(site.Config.LayoutsDir, filename)
templ, err := markup.Parse(site.templateEngine, path)
2024-02-12 19:59:18 +01:00
if err != nil {
return checkFileError(err)
2024-02-12 19:59:18 +01:00
}
layout_name := strings.TrimSuffix(filename, filepath.Ext(filename))
site.layouts[layout_name] = *templ
}
}
return nil
}
func (site *Site) loadDataFiles() error {
files, err := os.ReadDir(site.Config.DataDir)
2024-02-15 20:01:35 +01:00
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(site.Config.DataDir, filename)
2024-02-15 20:01:35 +01:00
yamlContent, err := os.ReadFile(path)
if err != nil {
return err
}
var data interface{}
err = yaml.Unmarshal(yamlContent, &data)
if err != nil {
return err
}
data_name := strings.TrimSuffix(filename, filepath.Ext(filename))
site.data[data_name] = data
}
}
return nil
}
func (site *Site) loadTemplates() error {
if _, err := os.Stat(site.Config.SrcDir); err != nil {
return fmt.Errorf("missing src directory")
2024-02-12 20:27:06 +01:00
}
err := filepath.WalkDir(site.Config.SrcDir, func(path string, entry fs.DirEntry, err error) error {
2024-02-12 19:59:18 +01:00
if !entry.IsDir() {
templ, err := markup.Parse(site.templateEngine, path)
// if something fails or this is not a template, skip
2024-02-12 19:59:18 +01:00
if err != nil || templ == nil {
return checkFileError(err)
2024-02-12 19:59:18 +01:00
}
// set site related (?) metadata. Not sure if this should go elsewhere
relPath, _ := filepath.Rel(site.Config.SrcDir, path)
srcPath, _ := filepath.Rel(site.Config.RootDir, path)
relPath = strings.TrimSuffix(relPath, filepath.Ext(relPath)) + templ.TargetExt()
templ.Metadata["src_path"] = srcPath
templ.Metadata["path"] = relPath
templ.Metadata["url"] = "/" + strings.TrimSuffix(strings.TrimSuffix(relPath, "index.html"), ".html")
2024-02-14 04:05:55 +01:00
templ.Metadata["dir"] = "/" + filepath.Dir(relPath)
// if drafts are disabled, exclude from posts, page and tags indexes, but not from site.templates
// we want to explicitly exclude the template from the target, rather than treating it as a non template file
if !templ.IsDraft() || site.Config.IncludeDrafts {
// posts are templates that can be chronologically sorted --that have a date.
// the rest are pages.
if templ.IsPost() {
// NOTE: getting the excerpt if not set at the front matter requires rendering the template
// which could be too onerous for this stage. Consider postponing setting this and/or caching the
// template render result
templ.Metadata["excerpt"] = getExcerpt(templ)
site.posts = append(site.posts, templ.Metadata)
// also add to tags index
if tags, ok := templ.Metadata["tags"]; ok {
for _, tag := range tags.([]interface{}) {
tag := tag.(string)
site.tags[tag] = append(site.tags[tag], templ.Metadata)
}
}
} else {
// the index pages should be skipped from the page directory
filename := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name()))
if filename != "index" {
site.pages = append(site.pages, templ.Metadata)
}
2024-02-14 04:05:55 +01:00
}
2024-02-12 19:59:18 +01:00
}
site.templates[path] = templ
2024-02-12 19:59:18 +01:00
}
return nil
})
if err != nil {
return err
}
// sort posts by reverse chronological order
Compare := func(a map[string]interface{}, b map[string]interface{}) int {
return b["date"].(time.Time).Compare(a["date"].(time.Time))
}
slices.SortFunc(site.posts, Compare)
for _, posts := range site.tags {
slices.SortFunc(posts, Compare)
}
return nil
2024-02-12 19:59:18 +01:00
}
func (site *Site) Build() error {
// clear previous target contents
os.RemoveAll(site.Config.TargetDir)
2024-02-27 16:30:00 +01:00
os.Mkdir(site.Config.SrcDir, DIR_RWE_MODE)
wg, files := spawnBuildWorkers(site)
defer wg.Wait()
defer close(files)
// walk the source directory, creating directories and files at the target dir
err := filepath.WalkDir(site.Config.SrcDir, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
subpath, _ := filepath.Rel(site.Config.SrcDir, path)
targetPath := filepath.Join(site.Config.TargetDir, subpath)
// if it's a directory, just create the same at the target
if entry.IsDir() {
2024-02-27 16:30:00 +01:00
return os.MkdirAll(targetPath, DIR_RWE_MODE)
}
// if it's a file (either static or template) send the path to a worker to build in target
files <- path
return nil
})
return err
}
// Create a channel to send paths to build and a worker pool to handle them concurrently
func spawnBuildWorkers(site *Site) (*sync.WaitGroup, chan string) {
var wg sync.WaitGroup
files := make(chan string, 20)
for range runtime.NumCPU() {
wg.Add(1)
go func(files <-chan string) {
defer wg.Done()
for path := range files {
site.buildFile(path)
}
}(files)
}
return &wg, files
}
func (site *Site) buildFile(path string) error {
subpath, _ := filepath.Rel(site.Config.SrcDir, path)
targetPath := filepath.Join(site.Config.TargetDir, subpath)
var contentReader io.Reader
var err error
templ, found := site.templates[path]
if !found {
// if no template found at location, treat the file as static write its contents to target
if site.Config.LinkStatic {
// dev optimization: link static files instead of copying them
abs, _ := filepath.Abs(path)
err = os.Symlink(abs, targetPath)
return checkFileError(err)
}
srcFile, err := os.Open(path)
if err != nil {
return checkFileError(err)
}
defer srcFile.Close()
contentReader = srcFile
} else {
if templ.IsDraft() && !site.Config.IncludeDrafts {
fmt.Println("skipping draft", targetPath)
return nil
}
content, err := site.render(templ)
if err != nil {
return err
}
targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + templ.TargetExt()
contentReader = bytes.NewReader(content)
}
// post process file acording to extension and config
targetExt := filepath.Ext(targetPath)
contentReader, err = markup.Smartify(targetExt, contentReader)
if err != nil {
return err
}
2024-02-19 22:03:06 +01:00
contentReader, err = site.injectLiveReload(targetExt, contentReader)
if err != nil {
return err
}
if site.Config.Minify {
contentReader = site.minifier.Minify(targetExt, contentReader)
}
// write the file contents over to target
return writeToFile(targetPath, contentReader)
}
func (site *Site) render(templ *markup.Template) ([]byte, error) {
ctx := map[string]interface{}{
"site": map[string]interface{}{
"config": site.Config.AsContext(),
"posts": site.posts,
"tags": site.tags,
"pages": site.pages,
2024-02-15 20:01:35 +01:00
"data": site.data,
},
}
2024-02-12 19:59:18 +01:00
ctx["page"] = templ.Metadata
content, err := templ.RenderWith(ctx, site.Config.HighlightTheme)
2024-02-12 19:59:18 +01:00
if err != nil {
return nil, err
2024-02-12 19:59:18 +01:00
}
// recursively render parent layouts
layout := templ.Metadata["layout"]
for layout != nil && err == nil {
if layout_templ, ok := site.layouts[layout.(string)]; ok {
ctx["layout"] = layout_templ.Metadata
ctx["content"] = content
content, err = layout_templ.RenderWith(ctx, site.Config.HighlightTheme)
2024-02-15 17:43:12 +01:00
if err != nil {
return nil, err
}
2024-02-12 19:59:18 +01:00
layout = layout_templ.Metadata["layout"]
} else {
return nil, fmt.Errorf("layout '%s' not found", layout)
2024-02-12 19:59:18 +01:00
}
}
return content, nil
}
func checkFileError(err error) error {
// When walking the source dir it can happen that a file is present when walking starts
// but missing or inaccessible when trying to open it (this is particularly frequent with
// backup files from emacs and when running the dev server). We don't want to halt the build
// process in that situation, just inform and continue.
if os.IsNotExist(err) {
// don't abort on missing files, usually spurious temps
fmt.Println("skipping missing file", err)
return nil
}
return err
}
func writeToFile(targetPath string, source io.Reader) error {
targetFile, err := os.Create(targetPath)
if err != nil {
return err
}
defer targetFile.Close()
_, err = io.Copy(targetFile, source)
if err != nil {
return err
}
fmt.Println("wrote", targetPath)
return targetFile.Sync()
2024-02-12 19:59:18 +01:00
}
2024-02-15 22:11:40 +01:00
// Assuming the given template is a post, try to generating an excerpt of it.
// If it contains an `excerpt` key in its metadata use that, otherwise try
// to render it as HTML and extract the text of its first <p>
func getExcerpt(templ *markup.Template) string {
2024-02-15 22:11:40 +01:00
if excerpt, ok := templ.Metadata["excerpt"]; ok {
return excerpt.(string)
}
// if we don't expect this to render to html don't bother parsing it
if templ.TargetExt() != ".html" {
2024-02-15 22:11:40 +01:00
return ""
}
content, err := templ.Render()
2024-02-15 22:11:40 +01:00
if err != nil {
return ""
}
return markup.ExtractFirstParagraph(bytes.NewReader(content))
2024-02-19 22:03:06 +01:00
}
2024-02-15 22:11:40 +01:00
2024-02-19 22:03:06 +01:00
// if live reload is enabled, inject the reload snippet to html files
func (site *Site) injectLiveReload(extension string, contentReader io.Reader) (io.Reader, error) {
if !site.Config.LiveReload || extension != ".html" {
return contentReader, nil
2024-02-15 22:11:40 +01:00
}
2024-02-19 22:03:06 +01:00
const JS_SNIPPET = `
const url = '%s/_events/'
var eventSource;
function newSSE() {
console.log("connecting to server events");
eventSource = new EventSource(url);
eventSource.onmessage = function () {
location.reload()
};
window.onbeforeunload = function() {
eventSource.close();
}
eventSource.onerror = function (event) {
console.error('An error occurred:', event);
eventSource.close();
setTimeout(newSSE, 5000)
};
2024-02-15 22:11:40 +01:00
}
newSSE();`
2024-02-19 22:03:06 +01:00
script := fmt.Sprintf(JS_SNIPPET, site.Config.SiteUrl)
return markup.InjectScript(contentReader, script)
2024-02-15 22:11:40 +01:00
}