2024-02-12 19:59:18 +01:00
|
|
|
package site
|
|
|
|
|
|
|
|
import (
|
2024-02-15 03:54:46 +01:00
|
|
|
"bytes"
|
2024-02-12 19:59:18 +01:00
|
|
|
"fmt"
|
2024-02-15 03:54:46 +01:00
|
|
|
"io"
|
2024-02-12 19:59:18 +01:00
|
|
|
"io/fs"
|
2024-03-02 20:16:40 +01:00
|
|
|
"maps"
|
2024-02-12 19:59:18 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-02-18 01:13:50 +01:00
|
|
|
"runtime"
|
2024-02-14 01:32:04 +01:00
|
|
|
"slices"
|
2024-02-12 19:59:18 +01:00
|
|
|
"strings"
|
2024-02-18 01:13:50 +01:00
|
|
|
"sync"
|
2024-02-14 01:32:04 +01:00
|
|
|
"time"
|
|
|
|
|
2024-02-16 19:29:43 +01:00
|
|
|
"github.com/facundoolano/jorge/config"
|
2024-02-26 16:16:06 +01:00
|
|
|
"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-15 03:54:46 +01:00
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
type site struct {
|
|
|
|
config config.Config
|
2024-02-26 16:16:06 +01:00
|
|
|
layouts map[string]markup.Template
|
2024-02-14 01:32:04 +01:00
|
|
|
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
|
|
|
|
2024-02-26 16:16:06 +01:00
|
|
|
templateEngine *markup.Engine
|
|
|
|
templates map[string]*markup.Template
|
2024-02-16 19:14:30 +01:00
|
|
|
|
2024-02-26 16:16:06 +01:00
|
|
|
minifier markup.Minifier
|
2024-02-12 19:59:18 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
// Load the site project pointed by `config`, then walk `config.SrcDir`
|
|
|
|
// and recreate it at `config.TargetDir` by rendering template files and copying static ones.
|
|
|
|
// The previous target dir contents are deleted.
|
|
|
|
func Build(config config.Config) error {
|
|
|
|
site, err := load(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return site.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new site instance by scanning the project directories
|
|
|
|
// pointed by `config`, loading layouts, templates and data files.
|
|
|
|
func load(config config.Config) (*site, error) {
|
|
|
|
site := site{
|
2024-02-26 16:16:06 +01:00
|
|
|
layouts: make(map[string]markup.Template),
|
|
|
|
templates: make(map[string]*markup.Template),
|
2024-03-04 03:19:09 +01:00
|
|
|
config: config,
|
2024-02-15 03:54:46 +01:00
|
|
|
tags: make(map[string][]map[string]interface{}),
|
2024-02-15 20:01:35 +01:00
|
|
|
data: make(map[string]interface{}),
|
2024-02-26 16:16:06 +01:00
|
|
|
templateEngine: markup.NewEngine(config.SiteUrl, config.IncludesDir),
|
2024-02-12 19:59:18 +01:00
|
|
|
}
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
if err := site.loadDataFiles(); err != nil {
|
2024-02-15 20:01:35 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
if err := site.loadLayouts(); err != nil {
|
2024-02-12 19:59:18 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-16 16:39:19 +01:00
|
|
|
if err := site.loadTemplates(); err != nil {
|
2024-02-12 19:59:18 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-15 23:55:50 +01:00
|
|
|
site.minifier = markup.LoadMinifier(config.MinifyExclusions)
|
2024-02-16 19:14:30 +01:00
|
|
|
|
2024-02-12 19:59:18 +01:00
|
|
|
return &site, nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
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()
|
2024-03-04 03:19:09 +01:00
|
|
|
path := filepath.Join(site.config.LayoutsDir, filename)
|
2024-02-26 16:16:06 +01:00
|
|
|
templ, err := markup.Parse(site.templateEngine, path)
|
2024-02-12 19:59:18 +01:00
|
|
|
if err != nil {
|
2024-02-21 22:46:37 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
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()
|
2024-03-04 03:19:09 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
func (site *site) loadTemplates() error {
|
|
|
|
if _, err := os.Stat(site.config.SrcDir); err != nil {
|
2024-02-24 16:39:45 +01:00
|
|
|
return fmt.Errorf("missing src directory")
|
2024-02-12 20:27:06 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +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() {
|
2024-02-26 16:16:06 +01:00
|
|
|
templ, err := markup.Parse(site.templateEngine, path)
|
2024-02-15 22:02:24 +01:00
|
|
|
// if something fails or this is not a template, skip
|
2024-02-12 19:59:18 +01:00
|
|
|
if err != nil || templ == nil {
|
2024-02-18 01:13:50 +01:00
|
|
|
return checkFileError(err)
|
2024-02-12 19:59:18 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 01:32:04 +01:00
|
|
|
// set site related (?) metadata. Not sure if this should go elsewhere
|
2024-03-04 03:19:09 +01:00
|
|
|
relPath, _ := filepath.Rel(site.config.SrcDir, path)
|
|
|
|
srcPath, _ := filepath.Rel(site.config.RootDir, path)
|
2024-02-27 16:24:45 +01:00
|
|
|
relPath = strings.TrimSuffix(relPath, filepath.Ext(relPath)) + templ.TargetExt()
|
2024-02-21 22:41:03 +01:00
|
|
|
templ.Metadata["src_path"] = srcPath
|
2024-02-14 01:32:04 +01:00
|
|
|
templ.Metadata["path"] = relPath
|
2024-02-16 16:39:19 +01:00
|
|
|
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)
|
2024-02-14 01:32:04 +01:00
|
|
|
|
2024-02-27 16:24:45 +01:00
|
|
|
// 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
|
2024-03-04 03:19:09 +01:00
|
|
|
if !templ.IsDraft() || site.config.IncludeDrafts {
|
2024-02-27 16:24:45 +01:00
|
|
|
// posts are templates that can be chronologically sorted --that have a date.
|
|
|
|
// the rest are pages.
|
|
|
|
if templ.IsPost() {
|
|
|
|
|
2024-07-03 20:08:53 +02:00
|
|
|
templ.Metadata["content"], templ.Metadata["excerpt"] = getPreviewContent(templ)
|
2024-02-27 16:24:45 +01:00
|
|
|
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)
|
|
|
|
}
|
2024-02-14 01:32:04 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:24:45 +01:00
|
|
|
} 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
|
|
|
}
|
2024-02-27 16:24:45 +01:00
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
site.templates[path] = templ
|
2024-02-12 19:59:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2024-02-14 01:32:04 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-02 20:16:40 +01:00
|
|
|
// sort by reverse chronological order when date is present
|
|
|
|
// otherwise by path alphabetical
|
|
|
|
CompareTemplates := func(a map[string]interface{}, b map[string]interface{}) int {
|
|
|
|
if bdate, ok := b["date"]; ok {
|
|
|
|
if adate, ok := a["date"]; ok {
|
|
|
|
return bdate.(time.Time).Compare(adate.(time.Time))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Compare(a["path"].(string), b["path"].(string))
|
2024-02-14 01:32:04 +01:00
|
|
|
}
|
2024-03-02 20:16:40 +01:00
|
|
|
slices.SortFunc(site.posts, CompareTemplates)
|
|
|
|
slices.SortFunc(site.pages, CompareTemplates)
|
2024-02-14 01:32:04 +01:00
|
|
|
for _, posts := range site.tags {
|
2024-03-02 20:16:40 +01:00
|
|
|
slices.SortFunc(posts, CompareTemplates)
|
2024-02-14 01:32:04 +01:00
|
|
|
}
|
2024-03-02 20:16:40 +01:00
|
|
|
|
|
|
|
// populate previous and next in template index
|
|
|
|
site.addPrevNext(site.pages)
|
|
|
|
site.addPrevNext(site.posts)
|
|
|
|
|
2024-02-14 01:32:04 +01:00
|
|
|
return nil
|
2024-02-12 19:59:18 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
func (site *site) addPrevNext(posts []map[string]interface{}) {
|
2024-03-02 20:16:40 +01:00
|
|
|
for i, post := range posts {
|
2024-03-04 03:19:09 +01:00
|
|
|
path := filepath.Join(site.config.RootDir, post["src_path"].(string))
|
2024-03-02 20:16:40 +01:00
|
|
|
|
|
|
|
// only consider them part of the same collection if they share the directory
|
|
|
|
if i > 0 && post["dir"] == posts[i-1]["dir"] {
|
|
|
|
// make a copy of the map, without prev/next (to avoid weird recursion)
|
|
|
|
previous := maps.Clone(posts[i-1])
|
|
|
|
delete(previous, "previous")
|
|
|
|
delete(previous, "next")
|
|
|
|
site.templates[path].Metadata["previous"] = previous
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < len(posts)-1 && post["dir"] == posts[i+1]["dir"] {
|
|
|
|
next := maps.Clone(posts[i+1])
|
|
|
|
delete(next, "previous")
|
|
|
|
delete(next, "next")
|
|
|
|
site.templates[path].Metadata["next"] = next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
// Walk the `site.Config.SrcDir` directory and reproduce it at `site.Config.TargetDir`,
|
|
|
|
// rendering template files and copying static ones.
|
|
|
|
func (site *site) build() error {
|
2024-02-15 03:54:46 +01:00
|
|
|
// clear previous target contents
|
2024-03-04 03:19:09 +01:00
|
|
|
os.RemoveAll(site.config.TargetDir)
|
2024-02-15 03:54:46 +01:00
|
|
|
|
2024-02-18 01:13:50 +01:00
|
|
|
wg, files := spawnBuildWorkers(site)
|
|
|
|
defer wg.Wait()
|
|
|
|
defer close(files)
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
// walk the source directory, creating directories and files at the target dir
|
2024-03-04 03:19:09 +01:00
|
|
|
return filepath.WalkDir(site.config.SrcDir, func(path string, entry fs.DirEntry, err error) error {
|
2024-02-15 03:54:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-15 23:55:50 +01:00
|
|
|
if strings.HasPrefix(filepath.Base(path), ".") {
|
|
|
|
// skip dot files and directories
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-04 03:19:09 +01:00
|
|
|
subpath, _ := filepath.Rel(site.config.SrcDir, path)
|
|
|
|
targetPath := filepath.Join(site.config.TargetDir, subpath)
|
2024-02-15 03:54:46 +01:00
|
|
|
|
|
|
|
// 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)
|
2024-02-15 03:54:46 +01:00
|
|
|
}
|
2024-02-18 01:13:50 +01:00
|
|
|
// if it's a file (either static or template) send the path to a worker to build in target
|
|
|
|
files <- path
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2024-02-15 03:54:46 +01:00
|
|
|
|
2024-02-18 01:13:50 +01:00
|
|
|
// Create a channel to send paths to build and a worker pool to handle them concurrently
|
2024-03-04 03:19:09 +01:00
|
|
|
func spawnBuildWorkers(site *site) (*sync.WaitGroup, chan string) {
|
2024-02-18 01:13:50 +01:00
|
|
|
|
|
|
|
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 {
|
2024-03-07 16:50:00 +01:00
|
|
|
err := site.buildFile(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error in %s: %s\n", path, err)
|
|
|
|
}
|
2024-02-16 16:39:19 +01:00
|
|
|
}
|
2024-02-18 01:13:50 +01:00
|
|
|
}(files)
|
|
|
|
}
|
|
|
|
return &wg, files
|
|
|
|
}
|
2024-02-16 16:39:19 +01:00
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
func (site *site) buildFile(path string) error {
|
|
|
|
subpath, _ := filepath.Rel(site.config.SrcDir, path)
|
|
|
|
targetPath := filepath.Join(site.config.TargetDir, subpath)
|
2024-02-18 01:13:50 +01:00
|
|
|
|
|
|
|
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
|
2024-03-04 03:19:09 +01:00
|
|
|
if site.config.LinkStatic {
|
2024-02-18 01:13:50 +01:00
|
|
|
// dev optimization: link static files instead of copying them
|
|
|
|
abs, _ := filepath.Abs(path)
|
|
|
|
err = os.Symlink(abs, targetPath)
|
|
|
|
return checkFileError(err)
|
2024-02-15 03:54:46 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 01:13:50 +01:00
|
|
|
srcFile, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return checkFileError(err)
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
contentReader = srcFile
|
|
|
|
} else {
|
2024-03-04 03:19:09 +01:00
|
|
|
if templ.IsDraft() && !site.config.IncludeDrafts {
|
2024-02-27 16:24:45 +01:00
|
|
|
fmt.Println("skipping draft", targetPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-18 01:13:50 +01:00
|
|
|
content, err := site.render(templ)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-02-15 03:54:46 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:24:45 +01:00
|
|
|
targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + templ.TargetExt()
|
2024-02-18 01:13:50 +01:00
|
|
|
contentReader = bytes.NewReader(content)
|
|
|
|
}
|
2024-07-04 20:18:23 +02:00
|
|
|
targetExt := filepath.Ext(targetPath)
|
|
|
|
|
|
|
|
// arrange paths to ensure pretty uris, eg move blog/tags.html to blog/tags/index.html
|
|
|
|
if targetExt == ".html" && filepath.Base(targetPath) != "index.html" {
|
|
|
|
targetDir := strings.TrimSuffix(targetPath, ".html")
|
|
|
|
targetPath = filepath.Join(targetDir, "index.html")
|
|
|
|
err = os.MkdirAll(targetDir, DIR_RWE_MODE)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-02-15 03:54:46 +01:00
|
|
|
|
2024-02-26 16:16:06 +01:00
|
|
|
// post process file acording to extension and config
|
|
|
|
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
|
2024-02-18 01:13:50 +01:00
|
|
|
}
|
2024-03-04 03:19:09 +01:00
|
|
|
if site.config.Minify {
|
2024-03-15 23:55:50 +01:00
|
|
|
contentReader = site.minifier.Minify(subpath, contentReader)
|
2024-02-26 16:16:06 +01:00
|
|
|
}
|
2024-02-18 01:13:50 +01:00
|
|
|
|
|
|
|
// write the file contents over to target
|
|
|
|
return writeToFile(targetPath, contentReader)
|
2024-02-15 03:54:46 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 03:19:09 +01:00
|
|
|
func (site *site) render(templ *markup.Template) ([]byte, error) {
|
2024-02-14 01:32:04 +01:00
|
|
|
ctx := map[string]interface{}{
|
|
|
|
"site": map[string]interface{}{
|
2024-03-04 03:19:09 +01:00
|
|
|
"config": site.config.AsContext(),
|
2024-02-14 01:32:04 +01:00
|
|
|
"posts": site.posts,
|
|
|
|
"tags": site.tags,
|
|
|
|
"pages": site.pages,
|
2024-02-15 20:01:35 +01:00
|
|
|
"data": site.data,
|
2024-02-14 01:32:04 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:59:18 +01:00
|
|
|
ctx["page"] = templ.Metadata
|
2024-03-04 03:19:09 +01:00
|
|
|
content, err := templ.RenderWith(ctx, site.config.HighlightTheme)
|
2024-02-12 19:59:18 +01:00
|
|
|
if err != nil {
|
2024-02-15 03:54:46 +01:00
|
|
|
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
|
2024-03-04 03:19:09 +01:00
|
|
|
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 {
|
2024-02-15 03:54:46 +01:00
|
|
|
return nil, fmt.Errorf("layout '%s' not found", layout)
|
2024-02-12 19:59:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
2024-02-18 01:13:50 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-15 03:54:46 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-21 16:56:22 +01:00
|
|
|
fmt.Println("wrote", targetPath)
|
2024-02-15 03:54:46 +01:00
|
|
|
return targetFile.Sync()
|
2024-02-12 19:59:18 +01:00
|
|
|
}
|
2024-02-15 22:11:40 +01:00
|
|
|
|
2024-07-03 20:08:53 +02:00
|
|
|
// Assuming the given template is a post, try to generating a preview version of its context
|
|
|
|
// and an excerpt of it. If the metadata contains an `excerpt` key use that, use the first <p>
|
|
|
|
// from the context preview.
|
|
|
|
func getPreviewContent(templ *markup.Template) (string, string) {
|
2024-02-15 22:11:40 +01:00
|
|
|
// if we don't expect this to render to html don't bother parsing it
|
2024-02-27 16:24:45 +01:00
|
|
|
if templ.TargetExt() != ".html" {
|
2024-07-03 20:08:53 +02:00
|
|
|
return "", ""
|
2024-02-15 22:11:40 +01:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:40:40 +01:00
|
|
|
content, err := templ.Render()
|
2024-02-15 22:11:40 +01:00
|
|
|
if err != nil {
|
2024-07-03 20:08:53 +02:00
|
|
|
return "", ""
|
2024-02-15 22:11:40 +01:00
|
|
|
}
|
2024-07-03 20:08:53 +02:00
|
|
|
|
|
|
|
if excerpt, ok := templ.Metadata["excerpt"]; ok {
|
|
|
|
return string(content), excerpt.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
excerpt := markup.ExtractFirstParagraph(bytes.NewReader(content))
|
|
|
|
return string(content), excerpt
|
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
|
2024-03-04 03:19:09 +01:00
|
|
|
func (site *site) injectLiveReload(extension string, contentReader io.Reader) (io.Reader, error) {
|
|
|
|
if !site.config.LiveReload || extension != ".html" {
|
2024-02-19 22:03:06 +01:00
|
|
|
return contentReader, nil
|
2024-02-15 22:11:40 +01:00
|
|
|
}
|
|
|
|
|
2024-02-19 22:03:06 +01:00
|
|
|
const JS_SNIPPET = `
|
2024-03-05 20:34:16 +01:00
|
|
|
const url = location.origin + '/_events/'
|
2024-02-28 14:26:59 +01:00
|
|
|
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);
|
2024-02-29 17:37:22 +01:00
|
|
|
eventSource.close();
|
|
|
|
setTimeout(newSSE, 5000)
|
2024-02-28 14:26:59 +01:00
|
|
|
};
|
2024-02-15 22:11:40 +01:00
|
|
|
}
|
2024-02-28 14:26:59 +01:00
|
|
|
newSSE();`
|
2024-03-05 20:34:16 +01:00
|
|
|
return markup.InjectScript(contentReader, JS_SNIPPET)
|
2024-02-15 22:11:40 +01:00
|
|
|
}
|