2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2014-09-10 13:43:02 -04:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
2016-08-07 16:01:55 -04:00
|
|
|
"fmt"
|
2015-02-28 12:45:02 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
"github.com/gohugoio/hugo/langs"
|
2015-05-20 02:21:21 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-08-07 16:01:55 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-09-10 13:43:02 -04:00
|
|
|
)
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func TestURLize(t *testing.T) {
|
2016-10-24 07:45:30 -04:00
|
|
|
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
v := newTestCfg()
|
|
|
|
l := langs.NewDefaultLanguage(v)
|
2017-03-25 09:37:04 -04:00
|
|
|
p, _ := NewPathSpec(hugofs.NewMem(v), l)
|
2016-10-24 07:45:30 -04:00
|
|
|
|
2014-09-10 13:43:02 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" foo bar ", "foo-bar"},
|
|
|
|
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
2015-11-25 17:03:28 -05:00
|
|
|
{"foo,bar:foobar", "foobarfoobar"},
|
2014-09-10 13:43:02 -04:00
|
|
|
{"foo/bar.html", "foo/bar.html"},
|
|
|
|
{"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
|
2016-07-11 04:06:40 -04:00
|
|
|
{"100%-google", "100-google"},
|
2014-09-10 13:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.URLize(test.input)
|
2014-09-10 13:43:02 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 06:28:44 -04:00
|
|
|
func TestAbsURL(t *testing.T) {
|
2016-08-08 07:55:18 -04:00
|
|
|
for _, defaultInSubDir := range []bool{true, false} {
|
|
|
|
for _, addLanguage := range []bool{true, false} {
|
|
|
|
for _, m := range []bool{true, false} {
|
|
|
|
for _, l := range []string{"en", "fr"} {
|
|
|
|
doTestAbsURL(t, defaultInSubDir, addLanguage, m, l)
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
v := newTestCfg()
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("multilingual", multilingual)
|
|
|
|
v.Set("defaultContentLanguage", "en")
|
|
|
|
v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
|
2016-08-08 07:55:18 -04:00
|
|
|
|
2015-05-11 06:28:44 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
baseURL string
|
|
|
|
expected string
|
|
|
|
}{
|
2016-08-07 16:01:55 -04:00
|
|
|
{"/test/foo", "http://base/", "http://base/MULTItest/foo"},
|
2016-09-13 15:37:27 -04:00
|
|
|
{"/" + lang + "/test/foo", "http://base/", "http://base/" + lang + "/test/foo"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"", "http://base/ace/", "http://base/ace/MULTI"},
|
|
|
|
{"/test/2/foo/", "http://base", "http://base/MULTItest/2/foo/"},
|
2015-05-11 06:28:44 -04:00
|
|
|
{"http://abs", "http://base/", "http://abs"},
|
2016-04-27 10:54:44 -04:00
|
|
|
{"schema://abs", "http://base/", "schema://abs"},
|
2015-05-11 06:28:44 -04:00
|
|
|
{"//schemaless", "http://base/", "//schemaless"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"test/2/foo/", "http://base/path", "http://base/path/MULTItest/2/foo/"},
|
2016-09-13 15:37:27 -04:00
|
|
|
{lang + "/test/2/foo/", "http://base/path", "http://base/path/" + lang + "/test/2/foo/"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"/test/2/foo/", "http://base/path", "http://base/MULTItest/2/foo/"},
|
|
|
|
{"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
|
2015-05-11 06:28:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("baseURL", test.baseURL)
|
2018-03-21 12:21:46 -04:00
|
|
|
v.Set("contentDir", "content")
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
l := langs.NewLanguage(lang, v)
|
2017-03-25 09:37:04 -04:00
|
|
|
p, _ := NewPathSpec(hugofs.NewMem(v), l)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.AbsURL(test.input, addLanguage)
|
2016-08-07 16:01:55 -04:00
|
|
|
expected := test.expected
|
|
|
|
if multilingual && addLanguage {
|
2016-08-08 07:55:18 -04:00
|
|
|
if !defaultInSubDir && lang == "en" {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
|
|
|
} else {
|
|
|
|
expected = strings.Replace(expected, "MULTI", lang+"/", 1)
|
|
|
|
}
|
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
} else {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
|
|
|
}
|
|
|
|
if output != expected {
|
2016-08-08 07:55:18 -04:00
|
|
|
t.Fatalf("Expected %#v, got %#v\n", expected, output)
|
2015-05-11 06:28:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
func TestIsAbsURL(t *testing.T) {
|
|
|
|
for i, this := range []struct {
|
|
|
|
a string
|
|
|
|
b bool
|
|
|
|
}{
|
|
|
|
{"http://gohugo.io", true},
|
|
|
|
{"https://gohugo.io", true},
|
|
|
|
{"//gohugo.io", true},
|
|
|
|
{"http//gohugo.io", false},
|
|
|
|
{"/content", false},
|
|
|
|
{"content", false},
|
|
|
|
} {
|
|
|
|
require.True(t, IsAbsURL(this.a) == this.b, fmt.Sprintf("Test %d", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 07:59:06 -04:00
|
|
|
func TestRelURL(t *testing.T) {
|
2016-08-08 07:55:18 -04:00
|
|
|
for _, defaultInSubDir := range []bool{true, false} {
|
|
|
|
for _, addLanguage := range []bool{true, false} {
|
|
|
|
for _, m := range []bool{true, false} {
|
|
|
|
for _, l := range []string{"en", "fr"} {
|
|
|
|
doTestRelURL(t, defaultInSubDir, addLanguage, m, l)
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
v := newTestCfg()
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("multilingual", multilingual)
|
|
|
|
v.Set("defaultContentLanguage", "en")
|
|
|
|
v.Set("defaultContentLanguageInSubdir", defaultInSubDir)
|
2016-08-07 16:01:55 -04:00
|
|
|
|
2015-05-11 07:59:06 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
baseURL string
|
|
|
|
canonify bool
|
|
|
|
expected string
|
|
|
|
}{
|
2016-08-07 16:01:55 -04:00
|
|
|
{"/test/foo", "http://base/", false, "MULTI/test/foo"},
|
2016-09-13 15:37:27 -04:00
|
|
|
{"/" + lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
|
|
|
|
{lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
|
2016-08-07 16:01:55 -04:00
|
|
|
{"test.css", "http://base/sub", false, "/subMULTI/test.css"},
|
|
|
|
{"test.css", "http://base/sub", true, "MULTI/test.css"},
|
|
|
|
{"/test/", "http://base/", false, "MULTI/test/"},
|
|
|
|
{"/test/", "http://base/sub/", false, "/subMULTI/test/"},
|
|
|
|
{"/test/", "http://base/sub/", true, "MULTI/test/"},
|
|
|
|
{"", "http://base/ace/", false, "/aceMULTI/"},
|
|
|
|
{"", "http://base/ace", false, "/aceMULTI"},
|
2015-05-11 07:59:06 -04:00
|
|
|
{"http://abs", "http://base/", false, "http://abs"},
|
|
|
|
{"//schemaless", "http://base/", false, "//schemaless"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("baseURL", test.baseURL)
|
|
|
|
v.Set("canonifyURLs", test.canonify)
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
l := langs.NewLanguage(lang, v)
|
2017-03-25 09:37:04 -04:00
|
|
|
p, _ := NewPathSpec(hugofs.NewMem(v), l)
|
2015-05-11 07:59:06 -04:00
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.RelURL(test.input, addLanguage)
|
2016-08-07 16:01:55 -04:00
|
|
|
|
|
|
|
expected := test.expected
|
|
|
|
if multilingual && addLanguage {
|
2016-08-08 07:55:18 -04:00
|
|
|
if !defaultInSubDir && lang == "en" {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
|
|
|
} else {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
|
|
|
|
}
|
2016-08-07 16:01:55 -04:00
|
|
|
} else {
|
|
|
|
expected = strings.Replace(expected, "MULTI", "", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if output != expected {
|
|
|
|
t.Errorf("[%d][%t] Expected %#v, got %#v\n", i, test.canonify, expected, output)
|
2015-05-11 07:59:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func TestSanitizeURL(t *testing.T) {
|
2015-02-28 12:45:02 -05:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
2015-05-05 10:02:52 -04:00
|
|
|
{"http://foo.bar/", "http://foo.bar"},
|
|
|
|
{"http://foo.bar", "http://foo.bar"}, // issue #1105
|
2015-02-28 12:45:02 -05:00
|
|
|
{"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:02:52 -04:00
|
|
|
for i, test := range tests {
|
2015-03-11 13:34:57 -04:00
|
|
|
o1 := SanitizeURL(test.input)
|
|
|
|
o2 := SanitizeURLKeepTrailingSlash(test.input)
|
2015-02-28 12:45:02 -05:00
|
|
|
|
|
|
|
expected2 := test.expected
|
|
|
|
|
|
|
|
if strings.HasSuffix(test.input, "/") && !strings.HasSuffix(expected2, "/") {
|
|
|
|
expected2 += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
if o1 != test.expected {
|
2015-05-05 10:02:52 -04:00
|
|
|
t.Errorf("[%d] 1: Expected %#v, got %#v\n", i, test.expected, o1)
|
2015-02-28 12:45:02 -05:00
|
|
|
}
|
|
|
|
if o2 != expected2 {
|
2015-05-05 10:02:52 -04:00
|
|
|
t.Errorf("[%d] 2: Expected %#v, got %#v\n", i, expected2, o2)
|
2015-02-28 12:45:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:43:02 -04:00
|
|
|
func TestMakePermalink(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
host, link, output string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
|
|
|
|
{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
|
|
|
|
{"http://abc.com", "post/bar", "http://abc.com/post/bar"},
|
|
|
|
{"http://abc.com", "bar", "http://abc.com/bar"},
|
|
|
|
{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
|
|
|
|
{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
|
|
|
output := MakePermalink(d.host, d.link).String()
|
|
|
|
if d.output != output {
|
|
|
|
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func TestURLPrep(t *testing.T) {
|
2014-09-10 13:43:02 -04:00
|
|
|
type test struct {
|
|
|
|
ugly bool
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
2014-11-04 20:28:27 -05:00
|
|
|
{false, "/section/name.html", "/section/name/"},
|
2014-09-10 13:43:02 -04:00
|
|
|
{true, "/section/name/index.html", "/section/name.html"},
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2014-09-10 13:43:02 -04:00
|
|
|
for i, d := range data {
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
v := newTestCfg()
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("uglyURLs", d.ugly)
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
l := langs.NewDefaultLanguage(v)
|
2017-03-25 09:37:04 -04:00
|
|
|
p, _ := NewPathSpec(hugofs.NewMem(v), l)
|
2016-11-26 09:50:32 -05:00
|
|
|
|
|
|
|
output := p.URLPrep(d.input)
|
2014-09-10 13:43:02 -04:00
|
|
|
if d.output != output {
|
|
|
|
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-12 14:28:28 -05:00
|
|
|
func TestAddContextRoot(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-03-11 13:34:57 -04:00
|
|
|
baseURL string
|
2014-12-12 14:28:28 -05:00
|
|
|
url string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"http://example.com/sub/", "/foo", "/sub/foo"},
|
|
|
|
{"http://example.com/sub/", "/foo/index.html", "/sub/foo/index.html"},
|
|
|
|
{"http://example.com/sub1/sub2", "/foo", "/sub1/sub2/foo"},
|
|
|
|
{"http://example.com", "/foo", "/foo"},
|
|
|
|
// cannot guess that the context root is already added int the example below
|
|
|
|
{"http://example.com/sub/", "/sub/foo", "/sub/sub/foo"},
|
|
|
|
{"http://example.com/тря", "/трям/", "/тря/трям/"},
|
2015-01-28 02:18:09 -05:00
|
|
|
{"http://example.com", "/", "/"},
|
|
|
|
{"http://example.com/bar", "//", "/bar/"},
|
2014-12-12 14:28:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2015-03-11 13:34:57 -04:00
|
|
|
output := AddContextRoot(test.baseURL, test.url)
|
2014-12-12 14:28:28 -05:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:43:02 -04:00
|
|
|
func TestPretty(t *testing.T) {
|
2015-03-11 13:34:57 -04:00
|
|
|
assert.Equal(t, PrettifyURLPath("/section/name.html"), "/section/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyURLPath("/section/sub/name.html"), "/section/sub/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyURLPath("/section/name/"), "/section/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyURLPath("/section/name/index.html"), "/section/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyURLPath("/index.html"), "/index.html")
|
|
|
|
assert.Equal(t, PrettifyURLPath("/name.xml"), "/name/index.xml")
|
|
|
|
assert.Equal(t, PrettifyURLPath("/"), "/")
|
|
|
|
assert.Equal(t, PrettifyURLPath(""), "/")
|
|
|
|
assert.Equal(t, PrettifyURL("/section/name.html"), "/section/name")
|
|
|
|
assert.Equal(t, PrettifyURL("/section/sub/name.html"), "/section/sub/name")
|
|
|
|
assert.Equal(t, PrettifyURL("/section/name/"), "/section/name")
|
|
|
|
assert.Equal(t, PrettifyURL("/section/name/index.html"), "/section/name")
|
|
|
|
assert.Equal(t, PrettifyURL("/index.html"), "/")
|
|
|
|
assert.Equal(t, PrettifyURL("/name.xml"), "/name/index.xml")
|
|
|
|
assert.Equal(t, PrettifyURL("/"), "/")
|
|
|
|
assert.Equal(t, PrettifyURL(""), "/")
|
2014-09-10 13:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUgly(t *testing.T) {
|
|
|
|
assert.Equal(t, Uglify("/section/name.html"), "/section/name.html")
|
|
|
|
assert.Equal(t, Uglify("/section/sub/name.html"), "/section/sub/name.html")
|
|
|
|
assert.Equal(t, Uglify("/section/name/"), "/section/name.html")
|
|
|
|
assert.Equal(t, Uglify("/section/name/index.html"), "/section/name.html")
|
|
|
|
assert.Equal(t, Uglify("/index.html"), "/index.html")
|
|
|
|
assert.Equal(t, Uglify("/name.xml"), "/name.xml")
|
|
|
|
assert.Equal(t, Uglify("/"), "/")
|
|
|
|
assert.Equal(t, Uglify(""), "/")
|
|
|
|
}
|