Skip to content

Commit

Permalink
[builtin] add support for apply template in templates.
Browse files Browse the repository at this point in the history
- New builtin function `apply` for apply template in templates.
- Improve generator and event handler for the HTTP server.
- Fix errors about `funcMap`
- Update logo for Pagine v2
  • Loading branch information
jellyterra committed Jun 13, 2024
1 parent 9e19025 commit 29a15b7
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<img src="https://github.com/jellyterra/artworks/raw/master/logo/pagine.svg" width="410.4" height="140" alt="Pagine logo" />
<img src="https://github.com/jellyterra/artworks/raw/master/logo/pagine_2.svg" height="140" alt="Pagine logo" />

# Pagine v2

Expand Down
42 changes: 33 additions & 9 deletions cmd/pagine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
)

var (
Expand Down Expand Up @@ -63,6 +65,14 @@ func generateAll(root, dest vfs.DirFS) error {
return err
}

err = os.RemoveAll(dest.Path)
switch {
case err == nil:
case os.IsNotExist(err):
default:
return err
}

err = fs.WalkDir(root, "/", func(path string, entry fs.DirEntry, err error) error {
if err != nil || entry.IsDir() {
return err
Expand Down Expand Up @@ -163,19 +173,33 @@ func serve(root, dest vfs.DirFS) error {
return err
}

var updated atomic.Bool

go func() {
select {
case e := <-w.Events:
fmt.Println(e)
err := generateAll(root, dest)
if err != nil {
fmt.Println(err)
for {
if !updated.Load() {
updated.Store(true)
err := generateAll(root, dest)
if err != nil {
fmt.Println(err)
}
}
time.Sleep(1 * time.Second)
}
}()

err := notify()
if err != nil {
fmt.Println(err)
go func() {
for {
select {
case e := <-w.Events:
fmt.Println(e)
updated.Store(false)
}

err := notify()
if err != nil {
fmt.Println(err)
}
}
}()

Expand Down
4 changes: 4 additions & 0 deletions render/asciidoc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package render

import (
Expand Down
8 changes: 6 additions & 2 deletions render/error.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package render

import "fmt"

type NoRendererFoundError struct {
type NotFoundError struct {
Path string
}

func (e *NoRendererFoundError) Error() string {
func (e *NotFoundError) Error() string {
return fmt.Sprint("no renderer found for the content type: ", e.Path)
}
4 changes: 4 additions & 0 deletions structure/builtin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package structure

func add(aInt, bInt any) int { return aInt.(int) + bInt.(int) }
Expand Down
27 changes: 27 additions & 0 deletions structure/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package structure

var emptyFuncMap = map[string]any{
"add": add,
"sub": sub,
"mul": mul,
"div": div,
"mod": mod,

"getAttr": empty,

"apply": __empty,
"embed": _empty,
"render": _empty,
"renderMarkdown": _empty,
"renderAsciidoc": _empty,
}

func empty() any { return "" }

func _empty(_ any) any { return "" }

func __empty(_ any, _ any) any { return "" }
4 changes: 4 additions & 0 deletions structure/env.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package structure

import (
Expand Down
4 changes: 4 additions & 0 deletions structure/hierarchy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package structure

import (
Expand Down
16 changes: 4 additions & 12 deletions structure/template.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package structure

import (
Expand Down Expand Up @@ -69,15 +73,3 @@ func LoadTemplate(root vfs.DirFS) (*Template, error) {
GoTemplate: goTemplate,
}, nil
}

var emptyFuncMap = map[string]any{
"getAttr": empty,
"embed": _empty,
"render": _empty,
"renderMarkdown": _empty,
"renderAsciidoc": _empty,
}

func empty() any { return nil }

func _empty(_ any) any { return "" }
36 changes: 28 additions & 8 deletions structure/unit.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package structure

import (
"fmt"
"bytes"
"github.com/webpagine/pagine/v2/collection"
"github.com/webpagine/pagine/v2/render"
"github.com/webpagine/pagine/v2/vfs"
"html/template"
"maps"
"path/filepath"
"strings"
Expand All @@ -30,7 +35,7 @@ type Unit struct {
}

func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define map[string]any) ([]error, error) {
var errors []error
var errors collection.Array[error]

templateName, templateKey := ParseTemplatePair(u.Template)

Expand All @@ -50,15 +55,16 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define
)

renderFromPath := func(r render.Renderer, pathStr any) string {
result, err := render.FromPath(render.Asciidoc, root, pathStr.(string))
result, err := render.FromPath(r, root, pathStr.(string))
if err != nil {
errors = append(errors, err)
errors.Append(err)
return ""
}
return result
}

funcMap := map[string]any{
var funcMap map[string]any
funcMap = map[string]any{
"add": add,
"sub": sub,
"mul": mul,
Expand All @@ -70,18 +76,32 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define

"getAttr": func() any { return attr },

"apply": func(pathStr any, data any) any {
t, err := template.New(filepath.Base(pathStr.(string))).Funcs(funcMap).ParseFS(root, pathStr.(string))
if err != nil {
errors.Append(err)
return ""
}
b := bytes.NewBuffer(nil)
err = t.Execute(b, data)
if err != nil {
errors.Append(err)
return ""
}
return b.String()
},
"embed": func(pathStr any) any {
b, err := root.ReadFile(pathStr.(string))
if err != nil {
errors = append(errors, err)
errors.Append(err)
return ""
}
return string(b)
},
"render": func(pathStr any) any {
r, ok := render.Renderers[filepath.Ext(pathStr.(string))[1:]]
if !ok {
errors = append(errors, fmt.Errorf("unknown template %q", pathStr))
errors.Append(&render.NotFoundError{Path: pathStr.(string)})
return ""
}
return renderFromPath(r, pathStr)
Expand All @@ -103,5 +123,5 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define
return nil, err
}

return errors, nil
return errors.RawArray, nil
}
4 changes: 4 additions & 0 deletions vfs/dirfs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 Jelly Terra
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0
// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/.

package vfs

import (
Expand Down

0 comments on commit 29a15b7

Please sign in to comment.