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
This commit is contained in:
Bjørn Erik Pedersen
2016-08-07 22:01:55 +02:00
parent 2079a23dd8
commit 54141f71dd
20 changed files with 316 additions and 172 deletions

View File

@@ -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,

View File

@@ -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{}