Permalink
Browse files

all: Refactor to nonglobal template handling

Updates #2701
  • Loading branch information...
1 parent 4ea4359 commit d6000a208c7687ca3a3efd6961ac941ce325e199 @bep bep committed on GitHub Jan 10, 2017
@@ -19,16 +19,17 @@ import (
"html/template"
"net/url"
"os"
- "path/filepath"
"regexp"
"strings"
"testing"
"io/ioutil"
"log"
+ "path/filepath"
- "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/tpl"
+
+ "github.com/spf13/hugo/helpers"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
@@ -106,9 +107,8 @@ void do();
"(?s)^\n<div class=\"highlight\" style=\"background: #f0f0f0\"><pre style=\"line-height: 125%\">.*?void</span>.*?do</span>.*?().*?</pre></div>\n$",
},
} {
- templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
if err != nil {
t.Fatalf("[%d] Handle shortcode error", i)
@@ -150,9 +150,8 @@ func TestShortcodeFigure(t *testing.T) {
"(?s)^\n<figure >.*?<img src=\"/img/hugo-logo.png\" />.*?<figcaption>.*?<p>.*?<a href=\"/img/hugo-logo.png\">.*?Hugo logo.*?</a>.*?</p>.*?</figcaption>.*?</figure>\n$",
},
} {
- templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -175,9 +174,8 @@ func TestShortcodeSpeakerdeck(t *testing.T) {
"(?s)^<script async class='speakerdeck-embed' data-id='4e8126e72d853c0060001f97'.*?>.*?</script>$",
},
} {
- templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -210,9 +208,8 @@ func TestShortcodeYoutube(t *testing.T) {
"(?s)^\n<div class=\"video\">.*?<iframe src=\"//www.youtube.com/embed/w7Ft2ymGmfc\\?autoplay=1\".*?allowfullscreen frameborder=\"0\">.*?</iframe>.*?</div>$",
},
} {
- templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -245,9 +242,8 @@ func TestShortcodeVimeo(t *testing.T) {
"(?s)^<div class=\"video\">.*?<iframe src=\"//player.vimeo.com/video/146022717\" webkitallowfullscreen mozallowfullscreen allowfullscreen>.*?</iframe>.*?</div>$",
},
} {
- templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -274,9 +270,8 @@ func TestShortcodeGist(t *testing.T) {
"(?s)^<script src=\"//gist.github.com/spf13/7896402.js\\?file=img.html\"></script>$",
},
} {
- templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -313,13 +308,14 @@ func TestShortcodeTweet(t *testing.T) {
},
}
- templ := tpl.New(logger)
- templ.Lookup("").Funcs(tweetFuncMap)
+ p, _ := pageFromString(simplePage, "simple.md", func(templ tpl.Template) error {
+ templ.Funcs(tweetFuncMap)
+ return nil
+ })
- p, _ := pageFromString(simplePage, "simple.md")
cacheFileID := viper.GetString("cacheDir") + url.QueryEscape("https://api.twitter.com/1/statuses/oembed.json?id=666616452582129664")
defer os.Remove(cacheFileID)
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -353,7 +349,7 @@ func TestShortcodeInstagram(t *testing.T) {
},
} {
// overload getJSON to return mock API response from Instagram
- tweetFuncMap := template.FuncMap{
+ instagramFuncMap := template.FuncMap{
"getJSON": func(urlParts ...string) interface{} {
var v interface{}
err := json.Unmarshal([]byte(this.resp), &v)
@@ -365,13 +361,14 @@ func TestShortcodeInstagram(t *testing.T) {
},
}
- templ := tpl.New(logger)
- templ.Lookup("").Funcs(tweetFuncMap)
+ p, _ := pageFromString(simplePage, "simple.md", func(templ tpl.Template) error {
+ templ.Funcs(instagramFuncMap)
+ return nil
+ })
- p, _ := pageFromString(simplePage, "simple.md")
cacheFileID := viper.GetString("cacheDir") + url.QueryEscape("https://api.instagram.com/oembed/?url=https://instagram.com/p/BMokmydjG-M/&hidecaption="+this.hidecaption)
defer os.Remove(cacheFileID)
- output, err := HandleShortcodes(this.in, p, templ)
+ output, err := HandleShortcodes(this.in, p)
if err != nil {
t.Fatalf("[%d] Failed to render shortcodes", i)
@@ -15,12 +15,11 @@ package hugolib
import (
"github.com/spf13/hugo/source"
- "github.com/spf13/hugo/tpl"
)
type Handler interface {
FileConvert(*source.File, *Site) HandledResult
- PageConvert(*Page, tpl.Template) HandledResult
+ PageConvert(*Page) HandledResult
Read(*source.File, *Site) HandledResult
Extensions() []string
}
@@ -18,7 +18,6 @@ import (
"github.com/dchest/cssmin"
"github.com/spf13/hugo/source"
- "github.com/spf13/hugo/tpl"
)
func init() {
@@ -32,7 +31,7 @@ func (h basicFileHandler) Read(f *source.File, s *Site) HandledResult {
return HandledResult{file: f}
}
-func (h basicFileHandler) PageConvert(*Page, tpl.Template) HandledResult {
+func (h basicFileHandler) PageConvert(*Page) HandledResult {
return HandledResult{}
}
@@ -74,7 +74,7 @@ func (mh *MetaHandle) Convert(i interface{}, s *Site, results HandleResults) {
return
}
- results <- h.PageConvert(p, s.owner.tmpl)
+ results <- h.PageConvert(p)
}
}
@@ -19,7 +19,6 @@ import (
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
- "github.com/spf13/hugo/tpl"
"github.com/spf13/viper"
)
@@ -56,24 +55,26 @@ type markdownHandler struct {
}
func (h markdownHandler) Extensions() []string { return []string{"mdown", "markdown", "md"} }
-func (h markdownHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
- return commonConvert(p, t)
+func (h markdownHandler) PageConvert(p *Page) HandledResult {
+ return commonConvert(p)
}
type htmlHandler struct {
basicPageHandler
}
func (h htmlHandler) Extensions() []string { return []string{"html", "htm"} }
-func (h htmlHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
+
+// TODO(bep) globals use p.s.t
+func (h htmlHandler) PageConvert(p *Page) HandledResult {
if p.rendered {
panic(fmt.Sprintf("Page %q already rendered, does not need conversion", p.BaseFileName()))
}
// Work on a copy of the raw content from now on.
p.createWorkContentCopy()
- p.ProcessShortcodes(t)
+ p.ProcessShortcodes()
return HandledResult{err: nil}
}
@@ -83,37 +84,37 @@ type asciidocHandler struct {
}
func (h asciidocHandler) Extensions() []string { return []string{"asciidoc", "adoc", "ad"} }
-func (h asciidocHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
- return commonConvert(p, t)
+func (h asciidocHandler) PageConvert(p *Page) HandledResult {
+ return commonConvert(p)
}
type rstHandler struct {
basicPageHandler
}
func (h rstHandler) Extensions() []string { return []string{"rest", "rst"} }
-func (h rstHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
- return commonConvert(p, t)
+func (h rstHandler) PageConvert(p *Page) HandledResult {
+ return commonConvert(p)
}
type mmarkHandler struct {
basicPageHandler
}
func (h mmarkHandler) Extensions() []string { return []string{"mmark"} }
-func (h mmarkHandler) PageConvert(p *Page, t tpl.Template) HandledResult {
- return commonConvert(p, t)
+func (h mmarkHandler) PageConvert(p *Page) HandledResult {
+ return commonConvert(p)
}
-func commonConvert(p *Page, t tpl.Template) HandledResult {
+func commonConvert(p *Page) HandledResult {
if p.rendered {
panic(fmt.Sprintf("Page %q already rendered, does not need conversion", p.BaseFileName()))
}
// Work on a copy of the raw content from now on.
p.createWorkContentCopy()
- p.ProcessShortcodes(t)
+ p.ProcessShortcodes()
// TODO(bep) these page handlers need to be re-evaluated, as it is hard to
// process a page in isolation. See the new preRender func.
View
@@ -34,7 +34,6 @@ import (
type HugoSites struct {
Sites []*Site
- tmpl tpl.Template
runMode runmode
multilingual *Multilingual
@@ -50,7 +49,14 @@ type deps struct {
// The logger to use.
log *jww.Notepad
- // TODO(bep) next in line: Viper, hugofs, template
+ tmpl *tpl.GoHTMLTemplate
+
+ // TODO(bep) next in line: Viper, hugofs
+}
+
+func (d *deps) refreshTemplates(withTemplate ...func(templ tpl.Template) error) {
+ d.tmpl = tpl.New(d.log, withTemplate...)
+ d.tmpl.PrintErrors() // TODO(bep) globals error handling
}
func newDeps(cfg DepsCfg) *deps {
@@ -59,11 +65,12 @@ func newDeps(cfg DepsCfg) *deps {
if logger == nil {
// TODO(bep) globals default log level
//logger = jww.NewNotepad(jww.LevelError, jww.LevelWarn, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
- logger = jww.NewNotepad(jww.LevelFatal, jww.LevelFatal, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
+ logger = jww.NewNotepad(jww.LevelError, jww.LevelError, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
}
return &deps{
- log: logger,
+ log: logger,
+ tmpl: tpl.New(logger, cfg.WithTemplate...),
}
}
@@ -76,8 +83,16 @@ func newHugoSites(cfg DepsCfg, sites ...*Site) (*HugoSites, error) {
return nil, err
}
+ var d *deps
+
+ if sites[0].deps != nil {
+ d = sites[0].deps
+ } else {
+ d = newDeps(cfg)
+ }
+
h := &HugoSites{
- deps: newDeps(cfg),
+ deps: d,
multilingual: langConfig,
Sites: sites}
@@ -91,18 +106,24 @@ func newHugoSites(cfg DepsCfg, sites ...*Site) (*HugoSites, error) {
// NewHugoSitesFromConfiguration creates HugoSites from the global Viper config.
// TODO(bep) globals rename this when all the globals are gone.
func NewHugoSitesFromConfiguration(cfg DepsCfg) (*HugoSites, error) {
- sites, err := createSitesFromConfig()
+ sites, err := createSitesFromConfig(cfg)
if err != nil {
return nil, err
}
return newHugoSites(cfg, sites...)
}
-func createSitesFromConfig() ([]*Site, error) {
+func createSitesFromConfig(cfg DepsCfg) ([]*Site, error) {
+ deps := newDeps(cfg)
+ return createSitesFromDeps(deps)
+}
+
+func createSitesFromDeps(deps *deps) ([]*Site, error) {
var sites []*Site
multilingual := viper.GetStringMap("languages")
+
if len(multilingual) == 0 {
- sites = append(sites, newSite(helpers.NewDefaultLanguage()))
+ sites = append(sites, newSite(helpers.NewDefaultLanguage(), deps))
}
if len(multilingual) > 0 {
@@ -115,7 +136,7 @@ func createSitesFromConfig() ([]*Site, error) {
}
for _, lang := range languages {
- sites = append(sites, newSite(lang))
+ sites = append(sites, newSite(lang, deps))
}
}
@@ -134,7 +155,7 @@ func (h *HugoSites) reset() {
func (h *HugoSites) createSitesFromConfig() error {
- sites, err := createSitesFromConfig()
+ sites, err := createSitesFromDeps(h.deps)
if err != nil {
return err
@@ -192,6 +213,8 @@ type DepsCfg struct {
// The Logger to use.
Logger *jww.Notepad
+
+ WithTemplate []func(templ tpl.Template) error
}
func (h *HugoSites) renderCrossSitesArtifacts() error {
View
@@ -40,7 +40,6 @@ import (
bp "github.com/spf13/hugo/bufferpool"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
- "github.com/spf13/hugo/tpl"
"github.com/spf13/viper"
)
@@ -1284,7 +1283,7 @@ func (p *Page) Render(layout ...string) template.HTML {
l = p.layouts()
}
- return tpl.ExecuteTemplateToHTML(p, l...)
+ return p.s.tmpl.ExecuteTemplateToHTML(p, l...)
}
func (p *Page) determineMarkupType() string {
@@ -1399,8 +1398,8 @@ func (p *Page) SaveSource() error {
return p.SaveSourceAs(p.FullFilePath())
}
-func (p *Page) ProcessShortcodes(t tpl.Template) {
- tmpContent, tmpContentShortCodes, _ := extractAndRenderShortcodes(string(p.workContent), p, t)
+func (p *Page) ProcessShortcodes() {
+ tmpContent, tmpContentShortCodes, _ := extractAndRenderShortcodes(string(p.workContent), p)
p.workContent = []byte(tmpContent)
p.contentShortCodes = tmpContentShortCodes
}
Oops, something went wrong.

0 comments on commit d6000a2

Please sign in to comment.