From 775125e66de4ac93101f5f0021f0ad96779dc2fe Mon Sep 17 00:00:00 2001 From: early Date: Mon, 2 Sep 2024 12:33:35 -0600 Subject: [PATCH] allow using parts/pages as base for new ones --- internal/compile/compile.go | 2 +- internal/compile/template.go | 14 +++++++++- page/page.go | 46 ++++++++++++++++++++++++------ part/part.go | 54 ++++++++++++++++++++++++++++++------ 4 files changed, 97 insertions(+), 19 deletions(-) diff --git a/internal/compile/compile.go b/internal/compile/compile.go index 11bf780..132fab2 100644 --- a/internal/compile/compile.go +++ b/internal/compile/compile.go @@ -16,13 +16,13 @@ import ( type Source interface { Name() string - FileName() string File() include.Opener Includes() []Source } type TemplateSource interface { Source + IncludeTagName() bool TemplateFuncs() template.FuncMap OnLoad() render.OnLoadFunc } diff --git a/internal/compile/template.go b/internal/compile/template.go index 84ae617..dc602e0 100644 --- a/internal/compile/template.go +++ b/internal/compile/template.go @@ -431,11 +431,23 @@ func insertTemplateSource(subSource TemplateSource, context *html.Node) (*comput context.InsertBefore(up, head) // Insert an assignment to $compute so we can always raise // scope, even in pipelines - n.InsertBefore(&html.Node{ + context.InsertBefore(&html.Node{ Type: html.TextNode, Data: "{{ $compute := .compute }}", }, head) context.InsertBefore(down, nil) + + // If not using custom tags for this subSource, remove them. + if !subSource.IncludeTagName() { + n := context.FirstChild + for n != nil { + fmt.Println(n, context, n.Parent) + context.RemoveChild(n) + context.Parent.InsertBefore(n, context) + n = context.FirstChild + } + context.Parent.RemoveChild(context) + } return compute, nil } diff --git a/page/page.go b/page/page.go index 16b8a1f..a619ba1 100644 --- a/page/page.go +++ b/page/page.go @@ -12,10 +12,9 @@ import ( ) type Page struct { - name string - fileName string - source include.Opener - raw string + name string + source include.Opener + raw string includes []compile.Source onLoad render.OnLoadFunc @@ -28,6 +27,18 @@ type Page struct { type Config func(*Page) +func Name(name string) Config { + return func(p *Page) { + p.name = name + } +} + +func Source(source string) Config { + return func(p *Page) { + p.source = include.File(source, "git.earlybird.gay/today-engine/part") + } +} + func Funcs(funcs template.FuncMap) Config { return func(p *Page) { for name, f := range funcs { @@ -58,7 +69,6 @@ func New(name string, source string, optional ...func(*Page)) *Page { p := new(Page) // Assign basic parameters p.name = name - p.fileName = source p.source = include.File(source, "git.earlybird.gay/today-engine/page") p.onLoad = func(ctx context.Context, d render.Data) error { return nil @@ -91,6 +101,23 @@ func New(name string, source string, optional ...func(*Page)) *Page { return p } +// With returns a shallow copy of p with all of optional applied to it. +func (p *Page) With(optional ...func(*Page)) *Page { + q := &Page{ + name: p.name, + source: p.source, + + includes: p.includes, + onLoad: p.onLoad, + templateFuncs: p.templateFuncs, + } + + for _, of := range optional { + of(q) + } + return q +} + // Getters // These are mostly to implement interfaces that make pages usable around the // project. @@ -99,14 +126,15 @@ func (p *Page) Name() string { return p.name } -func (p *Page) FileName() string { - return p.fileName -} - func (p *Page) File() include.Opener { return p.source } +// Pages are never included as a child, so this is always false. +func (p *Page) IncludeTagName() bool { + return false +} + func (p *Page) TemplateFuncs() template.FuncMap { return p.templateFuncs } diff --git a/part/part.go b/part/part.go index ceb8293..7ef9c72 100644 --- a/part/part.go +++ b/part/part.go @@ -11,10 +11,10 @@ import ( ) type Part struct { - name string - fileName string - source include.Opener + name string + source include.Opener + noTag bool includes []compile.Source onLoad render.OnLoadFunc templateFuncs template.FuncMap @@ -22,6 +22,18 @@ type Part struct { type Config func(*Part) +func Name(name string) Config { + return func(p *Part) { + p.name = name + } +} + +func Source(source string) Config { + return func(p *Part) { + p.source = include.File(source, "git.earlybird.gay/today-engine/part") + } +} + func Funcs(funcs template.FuncMap) Config { return func(p *Part) { for name, f := range funcs { @@ -42,11 +54,20 @@ func OnLoad(f render.OnLoadFunc) Config { } } +// Tag(false) will disable the inclusion of the "custom tag" for this part. +// When using Web Components, the user-defined name is included in the HTML +// document. This is also the default behavior for template parts, but you may +// disable it with this config when it causes problems. +func Tag(useTag bool) Config { + return func(p *Part) { + p.noTag = !useTag + } +} + func New(name string, source string, optional ...func(*Part)) *Part { p := new(Part) // Assign basic parameters p.name = name - p.fileName = source p.source = include.File(source, "git.earlybird.gay/today-engine/part") p.onLoad = func(ctx context.Context, data render.Data) error { return nil @@ -59,18 +80,35 @@ func New(name string, source string, optional ...func(*Part)) *Part { return p } -func (p *Part) Name() string { - return p.name +// With returns a shallow copy of p with all of optional applied to it. +func (p *Part) With(optional ...func(*Part)) *Part { + q := &Part{ + name: p.name, + source: p.source, + + includes: p.includes, + onLoad: p.onLoad, + templateFuncs: p.templateFuncs, + } + + for _, of := range optional { + of(q) + } + return q } -func (p *Part) FileName() string { - return p.fileName +func (p *Part) Name() string { + return p.name } func (p *Part) File() include.Opener { return p.source } +func (p *Part) IncludeTagName() bool { + return !p.noTag +} + func (p *Part) TemplateFuncs() template.FuncMap { return p.templateFuncs } -- 2.39.5