Skip to content

Commit

Permalink
Add render template hooks for links and images
Browse files Browse the repository at this point in the history
This commit also revises the change detection for templates used by content files in server mode.

Fixes gohugoio#6545
Fixes gohugoio#4663
  • Loading branch information
bep committed Dec 5, 2019
1 parent 0efb00c commit b68f8d3
Show file tree
Hide file tree
Showing 52 changed files with 1,479 additions and 325 deletions.
15 changes: 8 additions & 7 deletions deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sync"
"time"

"github.com/gohugoio/hugo/markup/converter"
"github.com/pkg/errors"

"github.com/gohugoio/hugo/cache/filecache"
Expand Down Expand Up @@ -223,11 +224,6 @@ func New(cfg DepsCfg) (*Deps, error) {
return nil, err
}

contentSpec, err := helpers.NewContentSpec(cfg.Language, logger, ps.BaseFs.Content.Fs)
if err != nil {
return nil, err
}

sp := source.NewSourceSpec(ps, fs.Source)

timeoutms := cfg.Language.GetInt("timeout")
Expand All @@ -247,7 +243,6 @@ func New(cfg DepsCfg) (*Deps, error) {
translationProvider: cfg.TranslationProvider,
WithTemplate: cfg.WithTemplate,
PathSpec: ps,
ContentSpec: contentSpec,
SourceSpec: sp,
ResourceSpec: resourceSpec,
Cfg: cfg.Language,
Expand Down Expand Up @@ -277,7 +272,13 @@ func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, er
return nil, err
}

d.ContentSpec, err = helpers.NewContentSpec(l, d.Log, d.BaseFs.Content.Fs)
d.ContentSpec, err = helpers.NewContentSpec(
converter.ProviderConfig{
Cfg: l,
Logger: d.Log,
ContentFs: d.BaseFs.Content.Fs,
},
)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion docs/content/en/content-management/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ You can put any file type into your `/content` directories, but Hugo uses the `m
* [Shortcodes](/content-management/shortcodes/) processed
* Layout applied

## List of content formats
{{< deleteme >}}


## List of content formats.




The current list of content formats in Hugo:

Expand Down
1 change: 1 addition & 0 deletions docs/layouts/_markup/render-link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="{{ .Destination | safeURL }}"{{ with .Title }} title="{{ . }}"{{ end }}>😉😉{{ .Text | safeHTML }} 😉😉</a>
1 change: 1 addition & 0 deletions docs/layouts/partials/deleteme.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
THIS IS PARTIAL!!!
3 changes: 3 additions & 0 deletions docs/layouts/shortcodes/deleteme.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DELETEME PARTIAL:

{{ partial "deleteme" }}
13 changes: 4 additions & 9 deletions helpers/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ import (
"unicode"
"unicode/utf8"

"github.com/gohugoio/hugo/common/loggers"

"github.com/gohugoio/hugo/markup/converter"

"github.com/gohugoio/hugo/markup"

bp "github.com/gohugoio/hugo/bufferpool"
"github.com/gohugoio/hugo/config"
"github.com/spf13/afero"

"strings"
)
Expand Down Expand Up @@ -62,7 +59,8 @@ type ContentSpec struct {

// NewContentSpec returns a ContentSpec initialized
// with the appropriate fields from the given config.Provider.
func NewContentSpec(cfg config.Provider, logger *loggers.Logger, contentFs afero.Fs) (*ContentSpec, error) {
func NewContentSpec(pcfg converter.ProviderConfig) (*ContentSpec, error) {
cfg := pcfg.Cfg

spec := &ContentSpec{
summaryLength: cfg.GetInt("summaryLength"),
Expand All @@ -73,11 +71,8 @@ func NewContentSpec(cfg config.Provider, logger *loggers.Logger, contentFs afero
Cfg: cfg,
}

converterProvider, err := markup.NewConverterProvider(converter.ProviderConfig{
Cfg: cfg,
ContentFs: contentFs,
Logger: logger,
})
converterProvider, err := markup.NewConverterProvider(pcfg)

if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion helpers/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"strings"
"testing"

"github.com/gohugoio/hugo/markup/converter"

"github.com/spf13/afero"

"github.com/gohugoio/hugo/common/loggers"
Expand Down Expand Up @@ -110,7 +112,11 @@ func TestNewContentSpec(t *testing.T) {
cfg.Set("buildExpired", true)
cfg.Set("buildDrafts", true)

spec, err := NewContentSpec(cfg, loggers.NewErrorLogger(), afero.NewMemMapFs())
spec, err := NewContentSpec(converter.ProviderConfig{
Cfg: cfg,
Logger: loggers.NewErrorLogger(),
ContentFs: afero.NewMemMapFs(),
})

c.Assert(err, qt.IsNil)
c.Assert(spec.summaryLength, qt.Equals, 32)
Expand Down
12 changes: 9 additions & 3 deletions helpers/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ import (

"github.com/spf13/viper"

"github.com/gohugoio/hugo/common/loggers"

qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/markup/converter"
"github.com/spf13/afero"
)

func TestResolveMarkup(t *testing.T) {
c := qt.New(t)
cfg := viper.New()
spec, err := NewContentSpec(cfg, loggers.NewErrorLogger(), afero.NewMemMapFs())
spec, err := NewContentSpec(
converter.ProviderConfig{
Cfg: cfg,
Logger: loggers.NewErrorLogger(),
ContentFs: afero.NewMemMapFs(),
},
)
c.Assert(err, qt.IsNil)

for i, this := range []struct {
Expand Down
9 changes: 8 additions & 1 deletion helpers/testhelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/markup/converter"
"github.com/spf13/afero"
"github.com/spf13/viper"

Expand Down Expand Up @@ -58,7 +59,13 @@ func newTestCfg() *viper.Viper {

func newTestContentSpec() *ContentSpec {
v := viper.New()
spec, err := NewContentSpec(v, loggers.NewErrorLogger(), afero.NewMemMapFs())
spec, err := NewContentSpec(
converter.ProviderConfig{
Cfg: v,
Logger: loggers.NewErrorLogger(),
ContentFs: afero.NewMemMapFs(),
},
)
if err != nil {
panic(err)
}
Expand Down
102 changes: 102 additions & 0 deletions hugolib/content_render_hooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import "testing"

func TestRenderHooks(t *testing.T) {
config := `
baseURL="https://example.org"
workingDir="/mywork"
`
b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running()
b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`)
b.WithTemplatesAdded("shortcodes/myshortcode1.html", `{{ partial "mypartial1" }}`)
b.WithTemplatesAdded("shortcodes/myshortcode2.html", `{{ partial "mypartial2" }}`)
b.WithTemplatesAdded("shortcodes/myshortcode3.html", `SHORT3|`)
b.WithTemplatesAdded("partials/mypartial1.html", `PARTIAL1`)
b.WithTemplatesAdded("partials/mypartial2.html", `PARTIAL2 {{ partial "mypartial3.html" }}`)
b.WithTemplatesAdded("partials/mypartial3.html", `PARTIAL3`)
b.WithTemplatesAdded("_default/_markup/render-link.html", `{{ .Page.Title }}|{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
b.WithTemplatesAdded("docs/_markup/render-link.html", `Link docs section: {{ .Text | safeHTML }}|END`)
b.WithTemplatesAdded("_default/_markup/render-image.html", `IMAGE: {{ .Page.Title }}|END`)

b.WithContent("blog/p1.md", `---
title: Cool Page
---
[First Link](https://www.google.com "Google's Homepage")
{{< myshortcode3 >}}
[Second Link](https://www.google.com "Google's Homepage")
Image:
![Drag Racing](/images/Dragster.jpg)
`, "blog/p2.md", `---
title: Cool Page2
layout: mylayout
---
{{< myshortcode1 >}}
[Some Text](https://www.google.com "Google's Homepage")
`, "blog/p3.md", `---
title: Cool Page3
---
{{< myshortcode2 >}}
`, "docs/docs1.md", `---
title: Docs 1
---
[Docs 1](https://www.google.com "Google's Homepage")
`)
b.Build(BuildCfg{})
b.AssertFileContent("public/blog/p1/index.html", `
<p>Cool Page|https://www.google.com|Title: Google's Homepage|Text: First Link|END</p>
Text: Second
SHORT3|
SASS
`)
b.AssertFileContent("public/blog/p2/index.html", `PARTIAL`)
b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3`)
b.AssertFileContent("public/docs/docs1/index.html", `Link docs section: Docs 1|END`)

b.EditFiles(
"layouts/_default/_markup/render-link.html", `EDITED: {{ .Destination | safeURL }}|`,
"layouts/docs/_markup/render-link.html", `DOCS EDITED: {{ .Destination | safeURL }}|`,
"layouts/partials/mypartial1.html", `PARTIAL1_EDITED`,
"layouts/partials/mypartial3.html", `PARTIAL3_EDITED`,
"layouts/shortcodes/myshortcode3.html", `SHORT3_EDITED|`,
)
b.Build(BuildCfg{})

b.AssertFileContent("public/blog/p1/index.html", `<p>EDITED: https://www.google.com|</p>`, "SHORT3_EDITED|")
b.AssertFileContent("public/blog/p2/index.html", `PARTIAL1_EDITED`)
b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3_EDITED`)
b.AssertFileContent("public/docs/docs1/index.html", `DOCS EDITED: https://www.google.com|</p>`)

}
Loading

0 comments on commit b68f8d3

Please sign in to comment.