@@ -2,6 +2,7 @@ package tplimpl
22
33import (
44 "io"
5+ "iter"
56 "regexp"
67 "strconv"
78 "strings"
@@ -44,16 +45,15 @@ var embeddedTemplatesAliases = map[string][]string{
4445 "_shortcodes/twitter.html" : {"_shortcodes/tweet.html" },
4546}
4647
47- func (s * TemplateStore ) parseTemplate (ti * TemplInfo ) error {
48- err := s .tns .doParseTemplate (ti )
48+ func (s * TemplateStore ) parseTemplate (ti * TemplInfo , replace bool ) error {
49+ err := s .tns .doParseTemplate (ti , replace )
4950 if err != nil {
5051 return s .addFileContext (ti , "parse of template failed" , err )
5152 }
52-
5353 return err
5454}
5555
56- func (t * templateNamespace ) doParseTemplate (ti * TemplInfo ) error {
56+ func (t * templateNamespace ) doParseTemplate (ti * TemplInfo , replace bool ) error {
5757 if ! ti .noBaseOf || ti .category == CategoryBaseof {
5858 // Delay parsing until we have the base template.
5959 return nil
@@ -68,7 +68,7 @@ func (t *templateNamespace) doParseTemplate(ti *TemplInfo) error {
6868
6969 if ti .D .IsPlainText {
7070 prototype := t .parseText
71- if prototype .Lookup (name ) != nil {
71+ if ! replace && prototype .Lookup (name ) != nil {
7272 name += "-" + strconv .FormatUint (t .nameCounter .Add (1 ), 10 )
7373 }
7474 templ , err = prototype .New (name ).Parse (ti .content )
@@ -77,7 +77,7 @@ func (t *templateNamespace) doParseTemplate(ti *TemplInfo) error {
7777 }
7878 } else {
7979 prototype := t .parseHTML
80- if prototype .Lookup (name ) != nil {
80+ if ! replace && prototype .Lookup (name ) != nil {
8181 name += "-" + strconv .FormatUint (t .nameCounter .Add (1 ), 10 )
8282 }
8383 templ , err = prototype .New (name ).Parse (ti .content )
@@ -181,19 +181,24 @@ func (t *templateNamespace) applyBaseTemplate(overlay *TemplInfo, base keyTempla
181181 return nil
182182}
183183
184- func (t * templateNamespace ) templatesIn (in tpl.Template ) []tpl.Template {
185- var templs []tpl.Template
186- if textt , ok := in .(* texttemplate.Template ); ok {
187- for _ , t := range textt .Templates () {
188- templs = append (templs , t )
189- }
190- }
191- if htmlt , ok := in .(* htmltemplate.Template ); ok {
192- for _ , t := range htmlt .Templates () {
193- templs = append (templs , t )
184+ func (t * templateNamespace ) templatesIn (in tpl.Template ) iter.Seq [tpl.Template ] {
185+ return func (yield func (t tpl.Template ) bool ) {
186+ switch in := in .(type ) {
187+ case * htmltemplate.Template :
188+ for t := range in .All () {
189+ if ! yield (t ) {
190+ return
191+ }
192+ }
193+
194+ case * texttemplate.Template :
195+ for t := range in .All () {
196+ if ! yield (t ) {
197+ return
198+ }
199+ }
194200 }
195201 }
196- return templs
197202}
198203
199204/*
@@ -337,8 +342,6 @@ func (t *templateNamespace) createPrototypes(init bool) error {
337342 t .prototypeHTML = htmltemplate .Must (t .parseHTML .Clone ())
338343 t .prototypeText = texttemplate .Must (t .parseText .Clone ())
339344 }
340- // t.execHTML = htmltemplate.Must(t.parseHTML.Clone())
341- // t.execText = texttemplate.Must(t.parseText.Clone())
342345
343346 return nil
344347}
@@ -350,3 +353,14 @@ func newTemplateNamespace(funcs map[string]any) *templateNamespace {
350353 standaloneText : texttemplate .New ("" ).Funcs (funcs ),
351354 }
352355}
356+
357+ func isText (t tpl.Template ) bool {
358+ switch t .(type ) {
359+ case * texttemplate.Template :
360+ return true
361+ case * htmltemplate.Template :
362+ return false
363+ default :
364+ panic ("unknown template type" )
365+ }
366+ }
0 commit comments