Skip to content

Commit

Permalink
tpl: Refactor package
Browse files Browse the repository at this point in the history
Now:

* The template API lives in /tpl
* The rest lives in /tpl/tplimpl

This is bound te be more improved in the future.

Updates #2701
  • Loading branch information
bep committed Feb 17, 2017
1 parent 93ca7c9 commit c507e27
Show file tree
Hide file tree
Showing 23 changed files with 661 additions and 662 deletions.
8 changes: 4 additions & 4 deletions deps/deps.go
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/hugo/config"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl"
jww "github.com/spf13/jwalterweatherman"
)

Expand All @@ -20,7 +20,7 @@ type Deps struct {
Log *jww.Notepad `json:"-"`

// The templates to use.
Tmpl tplapi.Template `json:"-"`
Tmpl tpl.Template `json:"-"`

// The file systems to use.
Fs *hugofs.Fs `json:"-"`
Expand All @@ -40,7 +40,7 @@ type Deps struct {
Language *helpers.Language

templateProvider ResourceProvider
WithTemplate func(templ tplapi.Template) error `json:"-"`
WithTemplate func(templ tpl.Template) error `json:"-"`

translationProvider ResourceProvider
}
Expand Down Expand Up @@ -147,7 +147,7 @@ type DepsCfg struct {

// Template handling.
TemplateProvider ResourceProvider
WithTemplate func(templ tplapi.Template) error
WithTemplate func(templ tpl.Template) error

// i18n handling.
TranslationProvider ResourceProvider
Expand Down
6 changes: 3 additions & 3 deletions hugolib/embedded_shortcodes_test.go
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/spf13/hugo/deps"

"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -335,7 +335,7 @@ func TestShortcodeTweet(t *testing.T) {
th = testHelper{cfg}
)

withTemplate := func(templ tplapi.Template) error {
withTemplate := func(templ tpl.Template) error {
templ.Funcs(tweetFuncMap)
return nil
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestShortcodeInstagram(t *testing.T) {
th = testHelper{cfg}
)

withTemplate := func(templ tplapi.Template) error {
withTemplate := func(templ tpl.Template) error {
templ.Funcs(instagramFuncMap)
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions hugolib/hugo_sites.go
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/spf13/hugo/i18n"
"github.com/spf13/hugo/tpl"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl/tplimpl"
)

// HugoSites represents the sites to build. Each site represents a language.
Expand Down Expand Up @@ -72,7 +72,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {

func applyDepsIfNeeded(cfg deps.DepsCfg, sites ...*Site) error {
if cfg.TemplateProvider == nil {
cfg.TemplateProvider = tpl.DefaultTemplateProvider
cfg.TemplateProvider = tplimpl.DefaultTemplateProvider
}

if cfg.TranslationProvider == nil {
Expand Down Expand Up @@ -121,8 +121,8 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
return newHugoSites(cfg, sites...)
}

func (s *Site) withSiteTemplates(withTemplates ...func(templ tplapi.Template) error) func(templ tplapi.Template) error {
return func(templ tplapi.Template) error {
func (s *Site) withSiteTemplates(withTemplates ...func(templ tpl.Template) error) func(templ tpl.Template) error {
return func(templ tpl.Template) error {
templ.LoadTemplates(s.absLayoutDir())
if s.hasTheme() {
templ.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
Expand Down Expand Up @@ -191,7 +191,7 @@ func (h *HugoSites) reset() {
h.Sites[i] = s.reset()
}

tpl.ResetCaches()
tplimpl.ResetCaches()
}

func (h *HugoSites) createSitesFromConfig() error {
Expand Down Expand Up @@ -553,7 +553,7 @@ func (h *HugoSites) Pages() Pages {
return h.Sites[0].AllPages
}

func handleShortcodes(p *Page, t tplapi.Template, rawContentCopy []byte) ([]byte, error) {
func handleShortcodes(p *Page, t tpl.Template, rawContentCopy []byte) ([]byte, error) {
if len(p.contentShortCodes) > 0 {
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", len(p.contentShortCodes), p.BaseFileName())
shortcodes, err := executeShortcodeFuncMap(p.contentShortCodes)
Expand Down
4 changes: 2 additions & 2 deletions hugolib/shortcode.go
Expand Up @@ -26,7 +26,7 @@ import (

bp "github.com/spf13/hugo/bufferpool"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl"
)

// ShortcodeWithPage is the "." context in a shortcode template.
Expand Down Expand Up @@ -541,7 +541,7 @@ func replaceShortcodeTokens(source []byte, prefix string, replacements map[strin
return source, nil
}

func getShortcodeTemplate(name string, t tplapi.Template) *template.Template {
func getShortcodeTemplate(name string, t tpl.Template) *template.Template {
if x := t.Lookup("shortcodes/" + name + ".html"); x != nil {
return x
}
Expand Down
38 changes: 19 additions & 19 deletions hugolib/shortcode_test.go
Expand Up @@ -25,12 +25,12 @@ import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl"
"github.com/stretchr/testify/require"
)

// TODO(bep) remove
func pageFromString(in, filename string, withTemplate ...func(templ tplapi.Template) error) (*Page, error) {
func pageFromString(in, filename string, withTemplate ...func(templ tpl.Template) error) (*Page, error) {
s := newTestSite(nil)
if len(withTemplate) > 0 {
// Have to create a new site
Expand All @@ -47,11 +47,11 @@ func pageFromString(in, filename string, withTemplate ...func(templ tplapi.Templ
return s.NewPageFrom(strings.NewReader(in), filename)
}

func CheckShortCodeMatch(t *testing.T, input, expected string, withTemplate func(templ tplapi.Template) error) {
func CheckShortCodeMatch(t *testing.T, input, expected string, withTemplate func(templ tpl.Template) error) {
CheckShortCodeMatchAndError(t, input, expected, withTemplate, false)
}

func CheckShortCodeMatchAndError(t *testing.T, input, expected string, withTemplate func(templ tplapi.Template) error, expectError bool) {
func CheckShortCodeMatchAndError(t *testing.T, input, expected string, withTemplate func(templ tpl.Template) error, expectError bool) {

cfg, fs := newTestCfg()

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestNonSC(t *testing.T) {
// Issue #929
func TestHyphenatedSC(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("hyphenated-video.html", `Playing Video {{ .Get 0 }}`)
return nil
}
Expand All @@ -111,7 +111,7 @@ func TestHyphenatedSC(t *testing.T) {
// Issue #1753
func TestNoTrailingNewline(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("a.html", `{{ .Get 0 }}`)
return nil
}
Expand All @@ -121,7 +121,7 @@ func TestNoTrailingNewline(t *testing.T) {

func TestPositionalParamSC(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 0 }}`)
return nil
}
Expand All @@ -135,7 +135,7 @@ func TestPositionalParamSC(t *testing.T) {

func TestPositionalParamIndexOutOfBounds(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 1 }}`)
return nil
}
Expand All @@ -146,7 +146,7 @@ func TestPositionalParamIndexOutOfBounds(t *testing.T) {

func TestNamedParamSC(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("img.html", `<img{{ with .Get "src" }} src="{{.}}"{{end}}{{with .Get "class"}} class="{{.}}"{{end}}>`)
return nil
}
Expand All @@ -161,7 +161,7 @@ func TestNamedParamSC(t *testing.T) {
// Issue #2294
func TestNestedNamedMissingParam(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("acc.html", `<div class="acc">{{ .Inner }}</div>`)
tem.AddInternalShortcode("div.html", `<div {{with .Get "class"}} class="{{ . }}"{{ end }}>{{ .Inner }}</div>`)
tem.AddInternalShortcode("div2.html", `<div {{with .Get 0}} class="{{ . }}"{{ end }}>{{ .Inner }}</div>`)
Expand All @@ -174,7 +174,7 @@ func TestNestedNamedMissingParam(t *testing.T) {

func TestIsNamedParamsSC(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("byposition.html", `<div id="{{ .Get 0 }}">`)
tem.AddInternalShortcode("byname.html", `<div id="{{ .Get "id" }}">`)
tem.AddInternalShortcode("ifnamedparams.html", `<div id="{{ if .IsNamedParams }}{{ .Get "id" }}{{ else }}{{ .Get 0 }}{{end}}">`)
Expand All @@ -190,7 +190,7 @@ func TestIsNamedParamsSC(t *testing.T) {

func TestInnerSC(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)
return nil
}
Expand All @@ -201,7 +201,7 @@ func TestInnerSC(t *testing.T) {

func TestInnerSCWithMarkdown(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)
return nil
}
Expand All @@ -215,7 +215,7 @@ func TestInnerSCWithMarkdown(t *testing.T) {

func TestInnerSCWithAndWithoutMarkdown(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)
return nil
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func TestEmbeddedSC(t *testing.T) {

func TestNestedSC(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("scn1.html", `<div>Outer, inner is {{ .Inner }}</div>`)
tem.AddInternalShortcode("scn2.html", `<div>SC2</div>`)
return nil
Expand All @@ -258,7 +258,7 @@ func TestNestedSC(t *testing.T) {

func TestNestedComplexSC(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("row.html", `-row-{{ .Inner}}-rowStop-`)
tem.AddInternalShortcode("column.html", `-col-{{.Inner }}-colStop-`)
tem.AddInternalShortcode("aside.html", `-aside-{{ .Inner }}-asideStop-`)
Expand All @@ -274,7 +274,7 @@ func TestNestedComplexSC(t *testing.T) {

func TestParentShortcode(t *testing.T) {
t.Parallel()
wt := func(tem tplapi.Template) error {
wt := func(tem tpl.Template) error {
tem.AddInternalShortcode("r1.html", `1: {{ .Get "pr1" }} {{ .Inner }}`)
tem.AddInternalShortcode("r2.html", `2: {{ .Parent.Get "pr1" }}{{ .Get "pr2" }} {{ .Inner }}`)
tem.AddInternalShortcode("r3.html", `3: {{ .Parent.Parent.Get "pr1" }}{{ .Parent.Get "pr2" }}{{ .Get "pr3" }} {{ .Inner }}`)
Expand Down Expand Up @@ -342,7 +342,7 @@ func TestExtractShortcodes(t *testing.T) {
fmt.Sprintf("Hello %sworld%s. And that's it.", testScPlaceholderRegexp, testScPlaceholderRegexp), ""},
} {

p, _ := pageFromString(simplePage, "simple.md", func(templ tplapi.Template) error {
p, _ := pageFromString(simplePage, "simple.md", func(templ tpl.Template) error {
templ.AddInternalShortcode("tag.html", `tag`)
templ.AddInternalShortcode("sc1.html", `sc1`)
templ.AddInternalShortcode("sc2.html", `sc2`)
Expand Down Expand Up @@ -514,7 +514,7 @@ tags:
sources[i] = source.ByteSource{Name: filepath.FromSlash(test.contentPath), Content: []byte(test.content)}
}

addTemplates := func(templ tplapi.Template) error {
addTemplates := func(templ tpl.Template) error {
templ.AddTemplate("_default/single.html", "{{.Content}}")

templ.AddInternalShortcode("b.html", `b`)
Expand Down
10 changes: 5 additions & 5 deletions hugolib/site.go
Expand Up @@ -40,7 +40,7 @@ import (
"github.com/spf13/hugo/parser"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl"
"github.com/spf13/hugo/transform"
"github.com/spf13/nitro"
"github.com/spf13/viper"
Expand Down Expand Up @@ -149,7 +149,7 @@ func NewSite(cfg deps.DepsCfg) (*Site, error) {
// NewSiteDefaultLang creates a new site in the default language.
// The site will have a template system loaded and ready to use.
// Note: This is mainly used in single site tests.
func NewSiteDefaultLang(withTemplate ...func(templ tplapi.Template) error) (*Site, error) {
func NewSiteDefaultLang(withTemplate ...func(templ tpl.Template) error) (*Site, error) {
v := viper.New()
loadDefaultSettingsFor(v)
return newSiteForLang(helpers.NewDefaultLanguage(v), withTemplate...)
Expand All @@ -158,15 +158,15 @@ func NewSiteDefaultLang(withTemplate ...func(templ tplapi.Template) error) (*Sit
// NewEnglishSite creates a new site in English language.
// The site will have a template system loaded and ready to use.
// Note: This is mainly used in single site tests.
func NewEnglishSite(withTemplate ...func(templ tplapi.Template) error) (*Site, error) {
func NewEnglishSite(withTemplate ...func(templ tpl.Template) error) (*Site, error) {
v := viper.New()
loadDefaultSettingsFor(v)
return newSiteForLang(helpers.NewLanguage("en", v), withTemplate...)
}

// newSiteForLang creates a new site in the given language.
func newSiteForLang(lang *helpers.Language, withTemplate ...func(templ tplapi.Template) error) (*Site, error) {
withTemplates := func(templ tplapi.Template) error {
func newSiteForLang(lang *helpers.Language, withTemplate ...func(templ tpl.Template) error) (*Site, error) {
withTemplates := func(templ tpl.Template) error {
for _, wt := range withTemplate {
if err := wt(templ); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions hugolib/sitemap_test.go
Expand Up @@ -19,7 +19,7 @@ import (
"reflect"

"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl"
)

const sitemapTemplate = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
Expand Down Expand Up @@ -48,7 +48,7 @@ func doTestSitemapOutput(t *testing.T, internal bool) {
depsCfg := deps.DepsCfg{Fs: fs, Cfg: cfg}

if !internal {
depsCfg.WithTemplate = func(templ tplapi.Template) error {
depsCfg.WithTemplate = func(templ tpl.Template) error {
templ.AddTemplate("sitemap.xml", sitemapTemplate)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions hugolib/testhelpers_test.go
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/tplapi"
"github.com/spf13/hugo/tpl"
"github.com/spf13/viper"

"io/ioutil"
Expand Down Expand Up @@ -66,9 +66,9 @@ func newDebugLogger() *jww.Notepad {
return jww.NewNotepad(jww.LevelDebug, jww.LevelError, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
}

func createWithTemplateFromNameValues(additionalTemplates ...string) func(templ tplapi.Template) error {
func createWithTemplateFromNameValues(additionalTemplates ...string) func(templ tpl.Template) error {

return func(templ tplapi.Template) error {
return func(templ tpl.Template) error {
for i := 0; i < len(additionalTemplates); i += 2 {
err := templ.AddTemplate(additionalTemplates[i], additionalTemplates[i+1])
if err != nil {
Expand Down

0 comments on commit c507e27

Please sign in to comment.