mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
35bfb66222
commit
18074d0c23
7 changed files with 102 additions and 24 deletions
|
@ -315,8 +315,10 @@ func TestRenderHooksRSS(t *testing.T) {
|
||||||
|
|
||||||
b.WithTemplates("index.html", `
|
b.WithTemplates("index.html", `
|
||||||
{{ $p := site.GetPage "p1.md" }}
|
{{ $p := site.GetPage "p1.md" }}
|
||||||
|
{{ $p2 := site.GetPage "p2.md" }}
|
||||||
|
|
||||||
P1: {{ $p.Content }}
|
P1: {{ $p.Content }}
|
||||||
|
P2: {{ $p2.Content }}
|
||||||
|
|
||||||
`, "index.xml", `
|
`, "index.xml", `
|
||||||
|
|
||||||
|
@ -330,6 +332,8 @@ P3: {{ $p3.Content }}
|
||||||
`,
|
`,
|
||||||
"_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|`,
|
"_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|`,
|
||||||
"_default/_markup/render-link.rss.xml", `xml-link: {{ .Destination | safeURL }}|`,
|
"_default/_markup/render-link.rss.xml", `xml-link: {{ .Destination | safeURL }}|`,
|
||||||
|
"_default/_markup/render-heading.html", `html-heading: {{ .Text }}|`,
|
||||||
|
"_default/_markup/render-heading.rss.xml", `xml-heading: {{ .Text }}|`,
|
||||||
)
|
)
|
||||||
|
|
||||||
b.WithContent("p1.md", `---
|
b.WithContent("p1.md", `---
|
||||||
|
@ -337,12 +341,14 @@ title: "p1"
|
||||||
---
|
---
|
||||||
P1. [I'm an inline-style link](https://www.gohugo.io)
|
P1. [I'm an inline-style link](https://www.gohugo.io)
|
||||||
|
|
||||||
|
# Heading in p1
|
||||||
|
|
||||||
`, "p2.md", `---
|
`, "p2.md", `---
|
||||||
title: "p2"
|
title: "p2"
|
||||||
---
|
---
|
||||||
P1. [I'm an inline-style link](https://www.bep.is)
|
P1. [I'm an inline-style link](https://www.bep.is)
|
||||||
|
|
||||||
|
# Heading in p2
|
||||||
|
|
||||||
`,
|
`,
|
||||||
"p3.md", `---
|
"p3.md", `---
|
||||||
|
@ -356,10 +362,15 @@ P3. [I'm an inline-style link](https://www.example.org)
|
||||||
|
|
||||||
b.Build(BuildCfg{})
|
b.Build(BuildCfg{})
|
||||||
|
|
||||||
b.AssertFileContent("public/index.html", "P1: <p>P1. html-link: https://www.gohugo.io|</p>")
|
b.AssertFileContent("public/index.html", `
|
||||||
|
P1: <p>P1. html-link: https://www.gohugo.io|</p>
|
||||||
|
html-heading: Heading in p1|
|
||||||
|
html-heading: Heading in p2|
|
||||||
|
`)
|
||||||
b.AssertFileContent("public/index.xml", `
|
b.AssertFileContent("public/index.xml", `
|
||||||
P2: <p>P1. xml-link: https://www.bep.is|</p>
|
P2: <p>P1. xml-link: https://www.bep.is|</p>
|
||||||
P3: <p>P3. xml-link: https://www.example.org|</p>
|
P3: <p>P3. xml-link: https://www.example.org|</p>
|
||||||
|
xml-heading: Heading in p2|
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -390,7 +390,7 @@ func (ps *pageState) initCommonProviders(pp pagePaths) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error) {
|
func (p *pageState) createRenderHooks(f output.Format) (hooks.Renderers, error) {
|
||||||
layoutDescriptor := p.getLayoutDescriptor()
|
layoutDescriptor := p.getLayoutDescriptor()
|
||||||
layoutDescriptor.RenderingHook = true
|
layoutDescriptor.RenderingHook = true
|
||||||
layoutDescriptor.LayoutOverride = false
|
layoutDescriptor.LayoutOverride = false
|
||||||
|
@ -401,7 +401,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
|
||||||
layoutDescriptor.Kind = "render-link"
|
layoutDescriptor.Kind = "render-link"
|
||||||
templ, templFound, err := p.s.Tmpl().LookupLayout(layoutDescriptor, f)
|
templ, templFound, err := p.s.Tmpl().LookupLayout(layoutDescriptor, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return renderers, err
|
||||||
}
|
}
|
||||||
if templFound {
|
if templFound {
|
||||||
renderers.LinkRenderer = hookRenderer{
|
renderers.LinkRenderer = hookRenderer{
|
||||||
|
@ -414,7 +414,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
|
||||||
layoutDescriptor.Kind = "render-image"
|
layoutDescriptor.Kind = "render-image"
|
||||||
templ, templFound, err = p.s.Tmpl().LookupLayout(layoutDescriptor, f)
|
templ, templFound, err = p.s.Tmpl().LookupLayout(layoutDescriptor, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return renderers, err
|
||||||
}
|
}
|
||||||
if templFound {
|
if templFound {
|
||||||
renderers.ImageRenderer = hookRenderer{
|
renderers.ImageRenderer = hookRenderer{
|
||||||
|
@ -427,7 +427,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
|
||||||
layoutDescriptor.Kind = "render-heading"
|
layoutDescriptor.Kind = "render-heading"
|
||||||
templ, templFound, err = p.s.Tmpl().LookupLayout(layoutDescriptor, f)
|
templ, templFound, err = p.s.Tmpl().LookupLayout(layoutDescriptor, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return renderers, err
|
||||||
}
|
}
|
||||||
if templFound {
|
if templFound {
|
||||||
renderers.HeadingRenderer = hookRenderer{
|
renderers.HeadingRenderer = hookRenderer{
|
||||||
|
@ -437,7 +437,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &renderers, nil
|
return renderers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pageState) getLayoutDescriptor() output.LayoutDescriptor {
|
func (p *pageState) getLayoutDescriptor() output.LayoutDescriptor {
|
||||||
|
|
|
@ -107,12 +107,39 @@ func (o *pageOutput) initRenderHooks() error {
|
||||||
h, err := ps.createRenderHooks(o.f)
|
h, err := ps.createRenderHooks(o.f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
initErr = err
|
initErr = err
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if h == nil {
|
o.cp.renderHooks.hooks = h
|
||||||
|
|
||||||
|
if !o.cp.renderHooksHaveVariants || h.IsZero() {
|
||||||
|
// Check if there is a different render hooks template
|
||||||
|
// for any of the other page output formats.
|
||||||
|
// If not, we can reuse this.
|
||||||
|
for _, po := range ps.pageOutputs {
|
||||||
|
if po.f.Name != o.f.Name {
|
||||||
|
h2, err := ps.createRenderHooks(po.f)
|
||||||
|
if err != nil {
|
||||||
|
initErr = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
o.cp.renderHooks.hooks = h
|
if h2.IsZero() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.cp.renderHooks.hooks.IsZero() {
|
||||||
|
o.cp.renderHooks.hooks = h2
|
||||||
|
}
|
||||||
|
|
||||||
|
o.cp.renderHooksHaveVariants = !h2.Eq(o.cp.renderHooks.hooks)
|
||||||
|
|
||||||
|
if o.cp.renderHooksHaveVariants {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return initErr
|
return initErr
|
||||||
|
|
|
@ -226,7 +226,7 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type renderHooks struct {
|
type renderHooks struct {
|
||||||
hooks *hooks.Renderers
|
hooks hooks.Renderers
|
||||||
init sync.Once
|
init sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -127,7 +127,7 @@ type DocumentContext struct {
|
||||||
type RenderContext struct {
|
type RenderContext struct {
|
||||||
Src []byte
|
Src []byte
|
||||||
RenderTOC bool
|
RenderTOC bool
|
||||||
RenderHooks *hooks.Renderers
|
RenderHooks hooks.Renderers
|
||||||
}
|
}
|
||||||
|
|
||||||
var FeatureRenderHooks = identity.NewPathIdentity("markup", "renderingHooks")
|
var FeatureRenderHooks = identity.NewPathIdentity("markup", "renderingHooks")
|
||||||
|
|
|
@ -14,7 +14,9 @@
|
||||||
package hooks
|
package hooks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/identity"
|
"github.com/gohugoio/hugo/identity"
|
||||||
)
|
)
|
||||||
|
@ -67,26 +69,64 @@ type Renderers struct {
|
||||||
HeadingRenderer HeadingRenderer
|
HeadingRenderer HeadingRenderer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Renderers) Eq(other interface{}) bool {
|
func (r Renderers) Eq(other interface{}) bool {
|
||||||
ro, ok := other.(*Renderers)
|
ro, ok := other.(Renderers)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if r == nil || ro == nil {
|
|
||||||
return r == nil
|
if r.IsZero() || ro.IsZero() {
|
||||||
|
return r.IsZero() && ro.IsZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.ImageRenderer.GetIdentity() != ro.ImageRenderer.GetIdentity() {
|
var b1, b2 bool
|
||||||
|
b1, b2 = r.ImageRenderer == nil, ro.ImageRenderer == nil
|
||||||
|
if (b1 || b2) && (b1 != b2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !b1 && r.ImageRenderer.GetIdentity() != ro.ImageRenderer.GetIdentity() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.LinkRenderer.GetIdentity() != ro.LinkRenderer.GetIdentity() {
|
b1, b2 = r.LinkRenderer == nil, ro.LinkRenderer == nil
|
||||||
|
if (b1 || b2) && (b1 != b2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !b1 && r.LinkRenderer.GetIdentity() != ro.LinkRenderer.GetIdentity() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.HeadingRenderer.GetIdentity() != ro.HeadingRenderer.GetIdentity() {
|
b1, b2 = r.HeadingRenderer == nil, ro.HeadingRenderer == nil
|
||||||
|
if (b1 || b2) && (b1 != b2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !b1 && r.HeadingRenderer.GetIdentity() != ro.HeadingRenderer.GetIdentity() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r Renderers) IsZero() bool {
|
||||||
|
return r.HeadingRenderer == nil && r.LinkRenderer == nil && r.ImageRenderer == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Renderers) String() string {
|
||||||
|
if r.IsZero() {
|
||||||
|
return "<zero>"
|
||||||
|
}
|
||||||
|
|
||||||
|
var sb strings.Builder
|
||||||
|
|
||||||
|
if r.LinkRenderer != nil {
|
||||||
|
sb.WriteString(fmt.Sprintf("LinkRenderer<%s>|", r.LinkRenderer.GetIdentity()))
|
||||||
|
}
|
||||||
|
if r.HeadingRenderer != nil {
|
||||||
|
sb.WriteString(fmt.Sprintf("HeadingRenderer<%s>|", r.HeadingRenderer.GetIdentity()))
|
||||||
|
}
|
||||||
|
if r.ImageRenderer != nil {
|
||||||
|
sb.WriteString(fmt.Sprintf("ImageRenderer<%s>|", r.ImageRenderer.GetIdentity()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
|
@ -206,12 +206,12 @@ func (r *hookedRenderer) renderDefaultImage(w util.BufWriter, source []byte, nod
|
||||||
|
|
||||||
func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||||
n := node.(*ast.Image)
|
n := node.(*ast.Image)
|
||||||
var h *hooks.Renderers
|
var h hooks.Renderers
|
||||||
|
|
||||||
ctx, ok := w.(*renderContext)
|
ctx, ok := w.(*renderContext)
|
||||||
if ok {
|
if ok {
|
||||||
h = ctx.RenderContext().RenderHooks
|
h = ctx.RenderContext().RenderHooks
|
||||||
ok = h != nil && h.ImageRenderer != nil
|
ok = h.ImageRenderer != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -267,12 +267,12 @@ func (r *hookedRenderer) renderDefaultLink(w util.BufWriter, source []byte, node
|
||||||
|
|
||||||
func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||||
n := node.(*ast.Link)
|
n := node.(*ast.Link)
|
||||||
var h *hooks.Renderers
|
var h hooks.Renderers
|
||||||
|
|
||||||
ctx, ok := w.(*renderContext)
|
ctx, ok := w.(*renderContext)
|
||||||
if ok {
|
if ok {
|
||||||
h = ctx.RenderContext().RenderHooks
|
h = ctx.RenderContext().RenderHooks
|
||||||
ok = h != nil && h.LinkRenderer != nil
|
ok = h.LinkRenderer != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -326,12 +326,12 @@ func (r *hookedRenderer) renderDefaultHeading(w util.BufWriter, source []byte, n
|
||||||
|
|
||||||
func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||||
n := node.(*ast.Heading)
|
n := node.(*ast.Heading)
|
||||||
var h *hooks.Renderers
|
var h hooks.Renderers
|
||||||
|
|
||||||
ctx, ok := w.(*renderContext)
|
ctx, ok := w.(*renderContext)
|
||||||
if ok {
|
if ok {
|
||||||
h = ctx.RenderContext().RenderHooks
|
h = ctx.RenderContext().RenderHooks
|
||||||
ok = h != nil && h.HeadingRenderer != nil
|
ok = h.HeadingRenderer != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Loading…
Reference in a new issue