2016-03-21 23:28:42 +00:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-12-27 03:40:10 +00:00
|
|
|
//
|
2015-11-24 03:16:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-12-27 03:40:10 +00:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-24 03:16:36 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-27 03:40:10 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-03-13 22:55:02 +00:00
|
|
|
package data
|
2014-12-27 03:40:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-28 05:36:06 +00:00
|
|
|
"fmt"
|
2014-12-27 03:40:10 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2017-03-31 06:54:22 +00:00
|
|
|
"sync"
|
2014-12-27 03:40:10 +00:00
|
|
|
"testing"
|
2017-03-31 06:54:22 +00:00
|
|
|
"time"
|
2014-12-27 03:40:10 +00:00
|
|
|
|
2017-06-13 16:42:45 +00:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"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 14:01:25 +00:00
|
|
|
"github.com/gohugoio/hugo/langs"
|
2017-06-13 17:07:35 +00:00
|
|
|
"github.com/spf13/afero"
|
2015-05-28 05:36:06 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-05-02 03:41:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-12-27 03:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestScpGetLocal(t *testing.T) {
|
2017-02-05 03:20:06 +00:00
|
|
|
t.Parallel()
|
|
|
|
v := viper.New()
|
|
|
|
fs := hugofs.NewMem(v)
|
2014-12-27 03:40:10 +00:00
|
|
|
ps := helpers.FilePathSeparator
|
2017-01-10 09:55:03 +00:00
|
|
|
|
2014-12-27 03:40:10 +00:00
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
|
|
|
}{
|
|
|
|
{"testpath" + ps + "test.txt", []byte(`T€st Content 123 fOO,bar:foo%bAR`)},
|
|
|
|
{"FOo" + ps + "BaR.html", []byte(`FOo/BaR.html T€st Content 123`)},
|
|
|
|
{"трям" + ps + "трям", []byte(`T€st трям/трям Content 123`)},
|
|
|
|
{"은행", []byte(`T€st C은행ontent 123`)},
|
|
|
|
{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
r := bytes.NewReader(test.content)
|
2017-01-10 09:55:03 +00:00
|
|
|
err := helpers.WriteToDisk(test.path, r, fs.Source)
|
2014-12-27 03:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:04:21 +00:00
|
|
|
c, err := getLocal(test.path, fs.Source, v)
|
2014-12-27 03:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting resource content: %s", err)
|
|
|
|
}
|
2016-11-23 17:53:50 +00:00
|
|
|
if !bytes.Equal(c, test.content) {
|
2014-12-27 03:40:10 +00:00
|
|
|
t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) {
|
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(handler))
|
|
|
|
client := &http.Client{
|
2015-12-18 14:52:17 +00:00
|
|
|
Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) {
|
2015-12-18 22:23:37 +00:00
|
|
|
// Remove when https://github.com/golang/go/issues/13686 is fixed
|
2015-12-18 22:09:50 +00:00
|
|
|
r.Host = "gohugo.io"
|
2015-12-18 14:52:17 +00:00
|
|
|
return url.Parse(testServer.URL)
|
|
|
|
}},
|
2014-12-27 03:40:10 +00:00
|
|
|
}
|
|
|
|
return testServer, client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScpGetRemote(t *testing.T) {
|
2017-02-05 03:20:06 +00:00
|
|
|
t.Parallel()
|
2014-12-27 03:40:10 +00:00
|
|
|
fs := new(afero.MemMapFs)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
2015-02-02 09:14:59 +00:00
|
|
|
ignore bool
|
2014-12-27 03:40:10 +00:00
|
|
|
}{
|
2015-02-02 09:14:59 +00:00
|
|
|
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
|
|
|
|
{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`), false},
|
|
|
|
{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`), false},
|
|
|
|
{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`), true},
|
2014-12-27 03:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-05-02 03:41:08 +00:00
|
|
|
msg := fmt.Sprintf("%v", test)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", test.path, nil)
|
|
|
|
require.NoError(t, err, msg)
|
2014-12-27 03:40:10 +00:00
|
|
|
|
|
|
|
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(test.content)
|
|
|
|
})
|
|
|
|
defer func() { srv.Close() }()
|
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
cfg := viper.New()
|
|
|
|
|
2017-05-02 03:41:08 +00:00
|
|
|
c, err := getRemote(req, fs, cfg, cl)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
assert.Equal(t, string(test.content), string(c))
|
|
|
|
|
|
|
|
c, err = getCache(req.URL.String(), fs, cfg, test.ignore)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
|
2015-02-02 09:14:59 +00:00
|
|
|
if test.ignore {
|
2017-05-02 03:41:08 +00:00
|
|
|
assert.Empty(t, c, msg)
|
2015-02-02 09:14:59 +00:00
|
|
|
} else {
|
2017-05-02 03:41:08 +00:00
|
|
|
assert.Equal(t, string(test.content), string(c))
|
|
|
|
|
2014-12-27 03:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 06:54:22 +00:00
|
|
|
func TestScpGetRemoteParallel(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-05-02 03:41:08 +00:00
|
|
|
|
2018-03-21 16:21:46 +00:00
|
|
|
ns := newTestNs()
|
2017-05-02 03:41:08 +00:00
|
|
|
|
2017-03-31 06:54:22 +00:00
|
|
|
content := []byte(`T€st Content 123`)
|
|
|
|
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(content)
|
|
|
|
})
|
|
|
|
defer func() { srv.Close() }()
|
|
|
|
|
2017-05-02 03:41:08 +00:00
|
|
|
url := "http://Foo.Bar/foo_Bar-Foo"
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
require.NoError(t, err)
|
2017-03-31 06:54:22 +00:00
|
|
|
|
2017-05-02 03:41:08 +00:00
|
|
|
for _, ignoreCache := range []bool{false, true} {
|
2017-03-31 06:54:22 +00:00
|
|
|
cfg := viper.New()
|
|
|
|
cfg.Set("ignoreCache", ignoreCache)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(gor int) {
|
|
|
|
defer wg.Done()
|
|
|
|
for j := 0; j < 10; j++ {
|
2017-05-02 03:41:08 +00:00
|
|
|
c, err := getRemote(req, ns.deps.Fs.Source, ns.deps.Cfg, cl)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, string(content), string(c))
|
2017-03-31 06:54:22 +00:00
|
|
|
|
|
|
|
time.Sleep(23 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2015-05-28 05:36:06 +00:00
|
|
|
}
|
2017-03-13 22:55:02 +00:00
|
|
|
|
|
|
|
func newDeps(cfg config.Provider) *deps.Deps {
|
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 14:01:25 +00:00
|
|
|
l := langs.NewLanguage("en", cfg)
|
2017-03-13 22:55:02 +00:00
|
|
|
l.Set("i18nDir", "i18n")
|
2017-09-25 06:59:02 +00:00
|
|
|
cs, err := helpers.NewContentSpec(l)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-13 22:55:02 +00:00
|
|
|
return &deps.Deps{
|
|
|
|
Cfg: cfg,
|
|
|
|
Fs: hugofs.NewMem(l),
|
2017-09-25 06:59:02 +00:00
|
|
|
ContentSpec: cs,
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 16:21:46 +00:00
|
|
|
|
|
|
|
func newTestNs() *Namespace {
|
|
|
|
v := viper.New()
|
|
|
|
v.Set("contentDir", "content")
|
|
|
|
return New(newDeps(v))
|
|
|
|
}
|