aboutsummaryrefslogtreecommitdiffhomepage
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2024-01-30 11:43:20 +0100
committerBjørn Erik Pedersen <[email protected]>2024-01-30 20:12:19 +0100
commit5b7cb258ec26d7de690099f5dc39935b8d728155 (patch)
tree53544c8ab46e3fffda4a1e33c5d6e705183e2652 /tpl
parent80595bbe3e7901ecd6200e59d43af142c3c85b6b (diff)
downloadhugo-5b7cb258ec26d7de690099f5dc39935b8d728155.tar.gz
hugo-5b7cb258ec26d7de690099f5dc39935b8d728155.zip
Create default link and image render hooks
Fixes #11933
Diffstat (limited to 'tpl')
-rw-r--r--tpl/template_info.go4
-rw-r--r--tpl/tplimpl/embedded/templates/_default/_markup/render-image.html15
-rw-r--r--tpl/tplimpl/embedded/templates/_default/_markup/render-link.html26
-rw-r--r--tpl/tplimpl/template.go25
-rw-r--r--tpl/tplimpl/template_errors.go7
5 files changed, 69 insertions, 8 deletions
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 -}}
+<img
+ {{- range $k, $v := $attributes -}}
+ {{- if $v -}}
+ {{- printf " %s=%q" $k $v | safeHTMLAttr -}}
+ {{- end -}}
+ {{- end -}}>
+{{- /**/ -}}
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 -}}
+<a
+ {{- range $k, $v := $attributes -}}
+ {{- if $v -}}
+ {{- printf " %s=%q" $k $v | safeHTMLAttr -}}
+ {{- end -}}
+ {{- end -}}
+ >{{ .Text | safeHTML }}</a>
+{{- /**/ -}}
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
}