jorge/markup/templates.go

223 lines
5.9 KiB
Go
Raw Normal View History

package markup
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/facundoolano/go-org/org"
"github.com/osteele/liquid"
"github.com/yuin/goldmark"
gm_highlight "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"gopkg.in/yaml.v3"
)
const FM_SEPARATOR = "---"
const NO_SYNTAX_HIGHLIGHTING = ""
const CODE_TABWIDTH = 4
type Engine = liquid.Engine
type Template struct {
SrcPath string
Metadata map[string]interface{}
liquidTemplate liquid.Template
}
// Create a new template engine, with custom liquid filters.
// The `siteUrl` is necessary to provide context for the absolute_url filter.
func NewEngine(siteUrl string, includesDir string) *Engine {
2024-02-15 17:53:45 +01:00
e := liquid.NewEngine()
loadJekyllFilters(e, siteUrl, includesDir)
2024-02-15 17:53:45 +01:00
return e
}
func EvalExpression(engine *Engine, expression string, context map[string]interface{}) (string, error) {
template := fmt.Sprintf("{{ %s | json }}", expression)
return engine.ParseAndRenderString(template, context)
}
// Try to parse a liquid template at the given location.
// Files starting with front matter (--- sorrrounded yaml)
// are considered templates. If the given file is not headed by front matter
// return (nil, nil).
// The front matter contents are stored in the returned template's Metadata.
func Parse(engine *Engine, 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
}
// extract the yaml front matter and save the rest of the template content separately
var yamlContent []byte
var liquidContent []byte
yamlClosed := false
for scanner.Scan() {
line := append(scanner.Bytes(), '\n')
if yamlClosed {
liquidContent = append(liquidContent, line...)
} else {
if strings.TrimSpace(scanner.Text()) == FM_SEPARATOR {
yamlClosed = true
continue
}
yamlContent = append(yamlContent, line...)
}
}
liquidContent = bytes.TrimSuffix(liquidContent, []byte("\n"))
if !yamlClosed {
return nil, errors.New("front matter not closed")
}
metadata := make(map[string]interface{})
if len(yamlContent) != 0 {
err := yaml.Unmarshal([]byte(yamlContent), &metadata)
if err != nil {
return nil, fmt.Errorf("invalid yaml: %s", err)
}
}
liquid, err := engine.ParseTemplateAndCache(liquidContent, path, 0)
if err != nil {
return nil, err
}
templ := Template{SrcPath: path, Metadata: metadata, liquidTemplate: *liquid}
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
2024-02-12 19:16:56 +01:00
return &templ, nil
}
// Return the extension of this template's source file.
func (templ Template) SrcExt() string {
return filepath.Ext(templ.SrcPath)
}
// Return the extension for the output format of this template
func (templ Template) TargetExt() string {
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
2024-02-12 19:16:56 +01:00
ext := filepath.Ext(templ.SrcPath)
if ext == ".org" || ext == ".md" {
return ".html"
2024-02-11 18:03:28 +01:00
}
return ext
}
func (templ Template) IsDraft() bool {
if draft, ok := templ.Metadata["draft"]; ok {
return draft.(bool)
}
return false
}
func (templ Template) IsPost() bool {
_, ok := templ.Metadata["date"]
return ok
}
// Renders the liquid template with default bindings.
func (templ Template) Render() ([]byte, error) {
ctx := map[string]interface{}{
"page": templ.Metadata,
}
return templ.RenderWith(ctx, NO_SYNTAX_HIGHLIGHTING)
}
// Renders the liquid template with the given context as bindings.
// If the template source is org or md, convert them to html after the
// liquid rendering.
func (templ Template) RenderWith(context map[string]interface{}, hlTheme string) ([]byte, error) {
2024-02-15 22:11:40 +01:00
// liquid rendering
content, err := templ.liquidTemplate.Render(context)
if err != nil {
return nil, err
}
if templ.SrcExt() == ".org" {
2024-02-15 22:11:40 +01:00
// org-mode rendering
doc := org.New().Parse(bytes.NewReader(content), templ.SrcPath)
2024-02-15 22:38:09 +01:00
htmlWriter := org.NewHTMLWriter()
// make * -> h1, ** -> h2, etc
htmlWriter.TopLevelHLevel = 1
// handle relative paths in links
htmlWriter.PrettyRelativeLinks = true
if hlTheme != NO_SYNTAX_HIGHLIGHTING {
htmlWriter.HighlightCodeBlock = highlightCodeBlock(hlTheme)
}
2024-02-15 22:38:09 +01:00
contentStr, err := doc.Write(htmlWriter)
if err != nil {
return nil, err
}
content = []byte(contentStr)
} else if templ.SrcExt() == ".md" {
2024-02-15 22:11:40 +01:00
// markdown rendering
var buf bytes.Buffer
options := make([]goldmark.Option, 0)
if hlTheme != NO_SYNTAX_HIGHLIGHTING {
options = append(options, goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
gm_highlight.NewHighlighting(
gm_highlight.WithStyle(hlTheme),
gm_highlight.WithFormatOptions(html.TabWidth(CODE_TABWIDTH)),
)))
}
md := goldmark.New(options...)
if err := md.Convert(content, &buf); err != nil {
return nil, err
}
content = buf.Bytes()
}
return content, nil
}
func highlightCodeBlock(hlTheme string) func(source string, lang string, inline bool, params map[string]string) string {
// from https://github.com/niklasfasching/go-org/blob/a32df1461eb34a451b1e0dab71bd9b2558ea5dc4/blorg/util.go#L58
return func(source, lang string, inline bool, params map[string]string) string {
var w strings.Builder
l := lexers.Get(lang)
if l == nil {
l = lexers.Fallback
}
l = chroma.Coalesce(l)
it, _ := l.Tokenise(nil, source)
options := []html.Option{
html.TabWidth(CODE_TABWIDTH),
}
if params[":hl_lines"] != "" {
ranges := org.ParseRanges(params[":hl_lines"])
if ranges != nil {
options = append(options, html.HighlightLines(ranges))
}
}
_ = html.New(options...).Format(&w, styles.Get(hlTheme), it)
if inline {
return `<div class="highlight-inline">` + "\n" + w.String() + "\n" + `</div>`
}
return `<div class="highlight">` + "\n" + w.String() + "\n" + `</div>`
}
}