From 5b7cb258ec26d7de690099f5dc39935b8d728155 Mon Sep 17 00:00:00 2001 From: Bjørn Erik Pedersen Date: Tue, 30 Jan 2024 11:43:20 +0100 Subject: Create default link and image render hooks Fixes #11933 --- tpl/template_info.go | 4 ++++ .../templates/_default/_markup/render-image.html | 15 +++++++++++++ .../templates/_default/_markup/render-link.html | 26 ++++++++++++++++++++++ tpl/tplimpl/template.go | 25 ++++++++++++++++----- tpl/tplimpl/template_errors.go | 7 +++--- 5 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 tpl/tplimpl/embedded/templates/_default/_markup/render-image.html create mode 100644 tpl/tplimpl/embedded/templates/_default/_markup/render-link.html (limited to 'tpl') diff --git a/tpl/template_info.go b/tpl/template_info.go index b27debf1f..fd126d80f 100644 --- a/tpl/template_info.go +++ b/tpl/template_info.go @@ -25,6 +25,10 @@ type FileInfo interface { Filename() string } +type IsInternalTemplateProvider interface { + IsInternalTemplate() bool +} + type ParseInfo struct { // Set for shortcode templates with any {{ .Inner }} IsInner bool diff --git a/tpl/tplimpl/embedded/templates/_default/_markup/render-image.html b/tpl/tplimpl/embedded/templates/_default/_markup/render-image.html new file mode 100644 index 000000000..c90ababd2 --- /dev/null +++ b/tpl/tplimpl/embedded/templates/_default/_markup/render-image.html @@ -0,0 +1,15 @@ +{{- $u := urls.Parse .Destination -}} +{{- $src := $u.String -}} +{{- if not $u.IsAbs -}} + {{- with or (.Page.Resources.Get $u.Path) (resources.Get $u.Path) -}} + {{- $src = .RelPermalink -}} + {{- end -}} +{{- end -}} +{{- $attributes := dict "alt" .Text "src" $src "title" .Title -}} + +{{- /**/ -}} diff --git a/tpl/tplimpl/embedded/templates/_default/_markup/render-link.html b/tpl/tplimpl/embedded/templates/_default/_markup/render-link.html new file mode 100644 index 000000000..bd64b204b --- /dev/null +++ b/tpl/tplimpl/embedded/templates/_default/_markup/render-link.html @@ -0,0 +1,26 @@ +{{- $u := urls.Parse .Destination -}} +{{- $href := $u.String -}} +{{- if not $u.IsAbs -}} + {{- with or + ($.Page.GetPage $u.Path) + ($.Page.Resources.Get $u.Path) + (resources.Get $u.Path) + -}} + {{- $href = .RelPermalink -}} + {{- with $u.RawQuery -}} + {{- $href = printf "%s?%s" $href . -}} + {{- end -}} + {{- with $u.Fragment -}} + {{- $href = printf "%s#%s" $href . -}} + {{- end -}} + {{- end -}} +{{- end -}} +{{- $attributes := dict "href" $href "title" .Title -}} +{{ .Text | safeHTML }} +{{- /**/ -}} diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index a8ba6815d..63dc29662 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -55,6 +55,7 @@ const ( shortcodesPathPrefix = "shortcodes/" internalPathPrefix = "_internal/" + embeddedPathPrefix = "_embedded/" baseFileBase = "baseof" ) @@ -517,11 +518,19 @@ func (t *templateHandler) findLayout(d layouts.LayoutDescriptor, f output.Format func (t *templateHandler) newTemplateInfo(name, tpl string) templateInfo { var isText bool + var isEmbedded bool + + if strings.HasPrefix(name, embeddedPathPrefix) { + isEmbedded = true + name = strings.TrimPrefix(name, embeddedPathPrefix) + } + name, isText = t.nameIsText(name) return templateInfo{ - name: name, - isText: isText, - template: tpl, + name: name, + isText: isText, + isEmbedded: isEmbedded, + template: tpl, } } @@ -772,7 +781,7 @@ func (t *templateHandler) loadEmbedded() error { } if _, found := t.Lookup(templateName); !found { - if err := t.AddTemplate(templateName, templ); err != nil { + if err := t.AddTemplate(embeddedPathPrefix+templateName, templ); err != nil { return err } } @@ -781,7 +790,7 @@ func (t *templateHandler) loadEmbedded() error { // TODO(bep) avoid reparsing these aliases for _, alias := range aliases { alias = internalPathPrefix + alias - if err := t.AddTemplate(alias, templ); err != nil { + if err := t.AddTemplate(embeddedPathPrefix+alias, templ); err != nil { return err } } @@ -1026,6 +1035,8 @@ func (t *templateNamespace) parse(info templateInfo) (*templateState, error) { return ts, nil } +var _ tpl.IsInternalTemplateProvider = (*templateState)(nil) + type templateState struct { tpl.Template @@ -1037,6 +1048,10 @@ type templateState struct { baseInfo templateInfo // Set when a base template is used. } +func (t *templateState) IsInternalTemplate() bool { + return t.info.isEmbedded +} + func (t *templateState) GetIdentity() identity.Identity { return t.id } diff --git a/tpl/tplimpl/template_errors.go b/tpl/tplimpl/template_errors.go index 34e73a07a..a9d259220 100644 --- a/tpl/tplimpl/template_errors.go +++ b/tpl/tplimpl/template_errors.go @@ -24,9 +24,10 @@ import ( var _ identity.Identity = (*templateInfo)(nil) type templateInfo struct { - name string - template string - isText bool // HTML or plain text template. + name string + template string + isText bool // HTML or plain text template. + isEmbedded bool meta *hugofs.FileMeta } -- cgit v1.2.3