diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-08-07 23:01:55 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-09-06 18:32:18 +0300 |
commit | 54141f71dd0ffbd2af326581b78ecafe7f054f51 (patch) | |
tree | a814b50027d9c9a439aa43eeb734f97e189ed968 /tpl | |
parent | 2079a23dd89734cea39e523faf46e44201151279 (diff) |
Improve language handling in URLs
The current "rendering language" is needed outside of Site. This commit moves the Language type to the helpers package, and then used to get correct correct language configuration in the markdownify template func.
This commit also adds two new template funcs: relLangURL and absLangURL.
See #2309
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/template_funcs.go | 12 | ||||
-rw-r--r-- | tpl/template_funcs_test.go | 12 |
2 files changed, 18 insertions, 6 deletions
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go index 941115cd9..41e616b1b 100644 --- a/tpl/template_funcs.go +++ b/tpl/template_funcs.go @@ -1214,9 +1214,11 @@ func markdownify(in interface{}) (template.HTML, error) { if err != nil { return "", err } - // TODO(bep) ml language + + language := viper.Get("CurrentContentLanguage").(*helpers.Language) + m := helpers.RenderBytes(&helpers.RenderingContext{ - ConfigProvider: viper.GetViper(), + ConfigProvider: language, Content: []byte(text), PageFmt: "markdown"}) m = bytes.TrimPrefix(m, markdownTrimPrefix) m = bytes.TrimSuffix(m, markdownTrimSuffix) @@ -1831,7 +1833,7 @@ func absURL(a interface{}) (template.HTML, error) { if err != nil { return "", nil } - return template.HTML(helpers.AbsURL(s)), nil + return template.HTML(helpers.AbsURL(s, false)), nil } func relURL(a interface{}) (template.HTML, error) { @@ -1839,12 +1841,13 @@ func relURL(a interface{}) (template.HTML, error) { if err != nil { return "", nil } - return template.HTML(helpers.RelURL(s)), nil + return template.HTML(helpers.RelURL(s, false)), nil } func init() { funcMap = template.FuncMap{ "absURL": absURL, + "absLangURL": func(a string) template.HTML { return template.HTML(helpers.AbsURL(a, true)) }, "add": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '+') }, "after": after, "apply": apply, @@ -1898,6 +1901,7 @@ func init() { "readFile": readFileFromWorkingDir, "ref": ref, "relURL": relURL, + "relLangURL": func(a string) template.HTML { return template.HTML(helpers.RelURL(a, true)) }, "relref": relRef, "replace": replace, "replaceRE": replaceRE, diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go index 881523811..3df38f380 100644 --- a/tpl/template_funcs_test.go +++ b/tpl/template_funcs_test.go @@ -71,6 +71,8 @@ func TestFuncsInTemplate(t *testing.T) { workingDir := "/home/hugo" viper.Set("WorkingDir", workingDir) + viper.Set("CurrentContentLanguage", helpers.NewDefaultLanguage()) + viper.Set("Multilingual", true) fs := &afero.MemMapFs{} hugofs.InitFs(fs) @@ -80,7 +82,8 @@ func TestFuncsInTemplate(t *testing.T) { // Add the examples from the docs: As a smoke test and to make sure the examples work. // TODO(bep): docs: fix title example in := - `absURL: {{ "http://gohugo.io/" | absURL }} + `absLangURL: {{ "index.html" | absLangURL }} +absURL: {{ "http://gohugo.io/" | absURL }} absURL: {{ "mystyle.css" | absURL }} absURL: {{ 42 | absURL }} add: {{add 1 2}} @@ -120,6 +123,7 @@ pluralize: {{ "cat" | pluralize }} querify: {{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }} readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }} readFile: {{ readFile "README.txt" }} +relLangURL: {{ "index.html" | relLangURL }} relURL 1: {{ "http://gohugo.io/" | relURL }} relURL 2: {{ "mystyle.css" | relURL }} relURL 3: {{ mul 2 21 | relURL }} @@ -146,7 +150,8 @@ upper: {{upper "BatMan"}} urlize: {{ "Bat Man" | urlize }} ` - expected := `absURL: http://gohugo.io/ + expected := `absLangURL: http://mysite.com/hugo/en/index.html +absURL: http://gohugo.io/ absURL: http://mysite.com/hugo/mystyle.css absURL: http://mysite.com/hugo/42 add: 3 @@ -186,6 +191,7 @@ pluralize: cats querify: bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose readDir: README.txt readFile: Hugo Rocks! +relLangURL: /hugo/en/index.html relURL 1: http://gohugo.io/ relURL 2: /hugo/mystyle.css relURL 3: /hugo/42 @@ -1733,6 +1739,8 @@ func TestReturnWhenSet(t *testing.T) { } func TestMarkdownify(t *testing.T) { + viper.Set("CurrentContentLanguage", helpers.NewDefaultLanguage()) + for i, this := range []struct { in interface{} expect interface{} |