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:48:14 -04:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-12-07 13:48:00 -05:00
|
|
|
"path/filepath"
|
2015-04-02 00:40:29 -04:00
|
|
|
"reflect"
|
2015-01-27 17:01:32 -05:00
|
|
|
"runtime"
|
2014-09-18 17:58:44 -04:00
|
|
|
"strconv"
|
2014-09-10 13:48:14 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-09-18 17:58:44 -04:00
|
|
|
"time"
|
2014-11-04 20:28:07 -05: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
|
|
|
"github.com/gohugoio/hugo/langs"
|
|
|
|
|
2016-09-18 13:52:42 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2016-07-10 13:37:27 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2015-04-02 00:40:29 -04:00
|
|
|
"github.com/spf13/viper"
|
2014-09-10 13:48:14 -04:00
|
|
|
)
|
|
|
|
|
2018-08-22 01:18:37 -04:00
|
|
|
func TestMakeSegment(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" FOO bar ", "foo-bar"},
|
|
|
|
{"Foo.Bar/fOO_bAr-Foo", "foo.bar-foo_bar-foo"},
|
|
|
|
{"FOO,bar:FooBar", "foobarfoobar"},
|
|
|
|
{"foo/BAR.HTML", "foo-bar.html"},
|
|
|
|
{"трям/трям", "трям-трям"},
|
|
|
|
{"은행", "은행"},
|
|
|
|
{"Say What??", "say-what"},
|
|
|
|
{"Your #1 Fan", "your-1-fan"},
|
|
|
|
{"Red & Blue", "red-blue"},
|
|
|
|
{"double//slash", "double-slash"},
|
|
|
|
{"My // Taxonomy", "my-taxonomy"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
v := newTestCfg()
|
|
|
|
|
|
|
|
l := langs.NewDefaultLanguage(v)
|
|
|
|
p, err := NewPathSpec(hugofs.NewMem(v), l)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
output := p.MakeSegment(test.input)
|
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
func TestMakePath(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-10-18 04:36:27 -04:00
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
removeAccents bool
|
2014-09-10 13:48:14 -04:00
|
|
|
}{
|
2015-10-18 04:36:27 -04:00
|
|
|
{" Foo bar ", "Foo-bar", true},
|
|
|
|
{"Foo.Bar/foo_Bar-Foo", "Foo.Bar/foo_Bar-Foo", true},
|
2015-11-25 17:03:28 -05:00
|
|
|
{"fOO,bar:foobAR", "fOObarfoobAR", true},
|
2015-10-18 04:36:27 -04:00
|
|
|
{"FOo/BaR.html", "FOo/BaR.html", true},
|
|
|
|
{"трям/трям", "трям/трям", true},
|
|
|
|
{"은행", "은행", true},
|
|
|
|
{"Банковский кассир", "Банковскии-кассир", true},
|
|
|
|
// Issue #1488
|
|
|
|
{"संस्कृत", "संस्कृत", false},
|
2016-03-22 14:43:03 -04:00
|
|
|
{"a%C3%B1ame", "a%C3%B1ame", false}, // Issue #1292
|
2016-03-12 18:48:21 -05:00
|
|
|
{"this+is+a+test", "this+is+a+test", false}, // Issue #1290
|
2017-01-07 13:29:20 -05:00
|
|
|
{"~foo", "~foo", false}, // Issue #2177
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
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("removePathAccents", test.removeAccents)
|
2018-03-21 12:21:46 -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
|
|
|
l := langs.NewDefaultLanguage(v)
|
2017-11-12 04:03:56 -05:00
|
|
|
p, err := NewPathSpec(hugofs.NewMem(v), l)
|
|
|
|
require.NoError(t, err)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.MakePath(test.input)
|
2014-09-10 13:48:14 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 08:53:25 -04:00
|
|
|
func TestMakePathSanitized(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
v := viper.New()
|
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
|
|
|
v.Set("dataDir", "data")
|
|
|
|
v.Set("i18nDir", "i18n")
|
|
|
|
v.Set("layoutDir", "layouts")
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
v.Set("assetDir", "assets")
|
|
|
|
v.Set("resourceDir", "resources")
|
|
|
|
v.Set("publishDir", "public")
|
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.Set("archetypeDir", "archetypes")
|
2018-03-21 12:21:46 -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
|
|
|
l := langs.NewDefaultLanguage(v)
|
2017-03-25 09:37:04 -04:00
|
|
|
p, _ := NewPathSpec(hugofs.NewMem(v), l)
|
2015-09-01 08:53:25 -04:00
|
|
|
|
2014-09-10 13:48:14 -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:48:14 -04:00
|
|
|
{"foo/BAR.HTML", "foo/bar.html"},
|
|
|
|
{"трям/трям", "трям/трям"},
|
|
|
|
{"은행", "은행"},
|
|
|
|
}
|
2015-09-01 08:53:25 -04:00
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.MakePathSanitized(test.input)
|
2015-09-01 08:53:25 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakePathSanitizedDisablePathToLower(t *testing.T) {
|
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("disablePathToLower", true)
|
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
|
|
|
l := langs.NewDefaultLanguage(v)
|
2017-03-25 09:37:04 -04:00
|
|
|
p, _ := NewPathSpec(hugofs.NewMem(v), l)
|
2015-09-01 08:53:25 -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"},
|
2015-09-01 08:53:25 -04:00
|
|
|
{"foo/BAR.HTML", "foo/BAR.HTML"},
|
|
|
|
{"трям/трям", "трям/трям"},
|
|
|
|
{"은행", "은행"},
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
for _, test := range tests {
|
2016-10-24 07:45:30 -04:00
|
|
|
output := p.MakePathSanitized(test.input)
|
2014-09-10 13:48:14 -04:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 04:15:57 -05:00
|
|
|
func TestGetRelativePath(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
base string
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{filepath.FromSlash("/a/b"), filepath.FromSlash("/a"), filepath.FromSlash("b")},
|
2015-05-27 14:41:25 -04:00
|
|
|
{filepath.FromSlash("/a/b/c/"), filepath.FromSlash("/a"), filepath.FromSlash("b/c/")},
|
2015-01-27 04:15:57 -05:00
|
|
|
{filepath.FromSlash("/c"), filepath.FromSlash("/a/b"), filepath.FromSlash("../../c")},
|
|
|
|
{filepath.FromSlash("/c"), "", false},
|
|
|
|
}
|
|
|
|
for i, this := range tests {
|
|
|
|
// ultimately a fancy wrapper around filepath.Rel
|
|
|
|
result, err := GetRelativePath(this.path, this.base)
|
|
|
|
|
|
|
|
if b, ok := this.expect.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] GetRelativePath didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] GetRelativePath failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result != this.expect {
|
|
|
|
t.Errorf("[%d] GetRelativePath got %v but expected %v", i, result, this.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 13:37:27 -04:00
|
|
|
func TestGetRealPath(t *testing.T) {
|
2016-09-18 16:04:32 -04:00
|
|
|
if runtime.GOOS == "windows" && os.Getenv("CI") == "" {
|
|
|
|
t.Skip("Skip TestGetRealPath as os.Symlink needs administrator rights on Windows")
|
|
|
|
}
|
|
|
|
|
2016-07-10 13:37:27 -04:00
|
|
|
d1, err := ioutil.TempDir("", "d1")
|
|
|
|
defer os.Remove(d1)
|
|
|
|
fs := afero.NewOsFs()
|
|
|
|
|
|
|
|
rp1, err := GetRealPath(fs, d1)
|
2016-09-18 13:52:42 -04:00
|
|
|
require.NoError(t, err)
|
2016-07-10 13:37:27 -04:00
|
|
|
assert.Equal(t, d1, rp1)
|
|
|
|
|
|
|
|
sym := filepath.Join(os.TempDir(), "d1sym")
|
|
|
|
err = os.Symlink(d1, sym)
|
2016-09-18 13:52:42 -04:00
|
|
|
require.NoError(t, err)
|
2016-07-10 13:37:27 -04:00
|
|
|
defer os.Remove(sym)
|
|
|
|
|
|
|
|
rp2, err := GetRealPath(fs, sym)
|
2016-09-18 13:52:42 -04:00
|
|
|
require.NoError(t, err)
|
2016-07-10 13:37:27 -04:00
|
|
|
|
|
|
|
// On OS X, the temp folder is itself a symbolic link (to /private...)
|
|
|
|
// This has to do for now.
|
|
|
|
assert.True(t, strings.HasSuffix(rp2, d1))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-09 16:40:15 -05:00
|
|
|
func TestMakePathRelative(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
inPath, path1, path2, output string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{"/abc/bcd/ab.css", "/abc/bcd", "/bbc/bcd", "/ab.css"},
|
|
|
|
{"/abc/bcd/ab.css", "/abcd/bcd", "/abc/bcd", "/ab.css"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2016-03-23 05:03:29 -04:00
|
|
|
output, _ := makePathRelative(d.inPath, d.path1, d.path2)
|
2014-11-09 16:40:15 -05:00
|
|
|
if d.output != output {
|
|
|
|
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 05:03:29 -04:00
|
|
|
_, error := makePathRelative("a/b/c.ss", "/a/c", "/d/c", "/e/f")
|
2014-11-09 16:40:15 -05:00
|
|
|
|
|
|
|
if error == nil {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test failed, expected error")
|
2014-11-09 16:40:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:11:39 -04:00
|
|
|
func TestGetDottedRelativePath(t *testing.T) {
|
|
|
|
// on Windows this will receive both kinds, both country and western ...
|
|
|
|
for _, f := range []func(string) string{filepath.FromSlash, func(s string) string { return s }} {
|
|
|
|
doTestGetDottedRelativePath(f, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestGetDottedRelativePath(urlFixer func(string) string, t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input, expected string
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"", "./"},
|
|
|
|
{urlFixer("/"), "./"},
|
|
|
|
{urlFixer("post"), "../"},
|
|
|
|
{urlFixer("/post"), "../"},
|
|
|
|
{urlFixer("post/"), "../"},
|
|
|
|
{urlFixer("tags/foo.html"), "../"},
|
|
|
|
{urlFixer("/tags/foo.html"), "../"},
|
|
|
|
{urlFixer("/post/"), "../"},
|
|
|
|
{urlFixer("////post/////"), "../"},
|
|
|
|
{urlFixer("/foo/bar/index.html"), "../../"},
|
|
|
|
{urlFixer("/foo/bar/foo/"), "../../../"},
|
|
|
|
{urlFixer("/foo/bar/foo"), "../../../"},
|
|
|
|
{urlFixer("foo/bar/foo/"), "../../../"},
|
|
|
|
{urlFixer("foo/bar/foo/bar"), "../../../../"},
|
|
|
|
{"404.html", "./"},
|
|
|
|
{"404.xml", "./"},
|
|
|
|
{"/404.html", "./"},
|
|
|
|
}
|
|
|
|
for i, d := range data {
|
|
|
|
output := GetDottedRelativePath(d.input)
|
|
|
|
if d.expected != output {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
func TestMakeTitle(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input, expected string
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"Make-Title", "Make Title"},
|
|
|
|
{"MakeTitle", "MakeTitle"},
|
|
|
|
{"make_title", "make_title"},
|
|
|
|
}
|
|
|
|
for i, d := range data {
|
|
|
|
output := MakeTitle(d.input)
|
|
|
|
if d.expected != output {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 11:42:38 -04:00
|
|
|
// Replace Extension is probably poorly named, but the intent of the
|
|
|
|
// function is to accept a path and return only the file name with a
|
|
|
|
// new extension. It's intentionally designed to strip out the path
|
|
|
|
// and only provide the name. We should probably rename the function to
|
|
|
|
// be more explicit at some point.
|
2014-09-10 13:48:14 -04:00
|
|
|
func TestReplaceExtension(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input, newext, expected string
|
|
|
|
}
|
|
|
|
data := []test{
|
2017-02-02 16:25:42 -05:00
|
|
|
// These work according to the above definition
|
2014-09-22 11:42:38 -04:00
|
|
|
{"/some/random/path/file.xml", "html", "file.html"},
|
|
|
|
{"/banana.html", "xml", "banana.xml"},
|
|
|
|
{"./banana.html", "xml", "banana.xml"},
|
|
|
|
{"banana/pie/index.html", "xml", "index.xml"},
|
|
|
|
{"../pies/fish/index.html", "xml", "index.xml"},
|
|
|
|
// but these all fail
|
2014-09-22 12:49:39 -04:00
|
|
|
{"filename-without-an-ext", "ext", "filename-without-an-ext.ext"},
|
2014-09-22 11:42:38 -04:00
|
|
|
{"/filename-without-an-ext", "ext", "filename-without-an-ext.ext"},
|
|
|
|
{"/directory/mydir/", "ext", ".ext"},
|
|
|
|
{"mydir/", "ext", ".ext"},
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2014-12-07 13:48:00 -05:00
|
|
|
output := ReplaceExtension(filepath.FromSlash(d.input), d.newext)
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != output {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirExists(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{".", true},
|
|
|
|
{"./", true},
|
|
|
|
{"..", true},
|
|
|
|
{"../", true},
|
|
|
|
{"./..", true},
|
|
|
|
{"./../", true},
|
2014-12-07 13:48:00 -05:00
|
|
|
{os.TempDir(), true},
|
|
|
|
{os.TempDir() + FilePathSeparator, true},
|
2014-09-10 13:48:14 -04:00
|
|
|
{"/", true},
|
|
|
|
{"/some-really-random-directory-name", false},
|
|
|
|
{"/some/really/random/directory/name", false},
|
|
|
|
{"./some-really-random-local-directory-name", false},
|
|
|
|
{"./some/really/random/local/directory/name", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2014-12-07 13:48:00 -05:00
|
|
|
exists, _ := DirExists(filepath.FromSlash(d.input), new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != exists {
|
|
|
|
t.Errorf("Test %d failed. Expected %t got %t", i, d.expected, exists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsDir(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input string
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"./", true},
|
|
|
|
{"/", true},
|
|
|
|
{"./this-directory-does-not-existi", false},
|
|
|
|
{"/this-absolute-directory/does-not-exist", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2014-11-04 20:28:07 -05:00
|
|
|
|
|
|
|
exists, _ := IsDir(d.input, new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != exists {
|
|
|
|
t.Errorf("Test %d failed. Expected %t got %t", i, d.expected, exists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsEmpty(t *testing.T) {
|
|
|
|
zeroSizedFile, _ := createZeroSizedFileInTempDir()
|
|
|
|
defer deleteFileInTempDir(zeroSizedFile)
|
|
|
|
nonZeroSizedFile, _ := createNonZeroSizedFileInTempDir()
|
|
|
|
defer deleteFileInTempDir(nonZeroSizedFile)
|
|
|
|
emptyDirectory, _ := createEmptyTempDir()
|
|
|
|
defer deleteTempDir(emptyDirectory)
|
|
|
|
nonEmptyZeroLengthFilesDirectory, _ := createTempDirWithZeroLengthFiles()
|
|
|
|
defer deleteTempDir(nonEmptyZeroLengthFilesDirectory)
|
|
|
|
nonEmptyNonZeroLengthFilesDirectory, _ := createTempDirWithNonZeroLengthFiles()
|
|
|
|
defer deleteTempDir(nonEmptyNonZeroLengthFilesDirectory)
|
|
|
|
nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt"
|
2017-02-02 16:25:42 -05:00
|
|
|
nonExistentDir := os.TempDir() + "/this/directory/does/not/exist/"
|
2014-09-10 13:48:14 -04:00
|
|
|
|
|
|
|
fileDoesNotExist := fmt.Errorf("%q path does not exist", nonExistentFile)
|
|
|
|
dirDoesNotExist := fmt.Errorf("%q path does not exist", nonExistentDir)
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
input string
|
|
|
|
expectedResult bool
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{zeroSizedFile.Name(), true, nil},
|
|
|
|
{nonZeroSizedFile.Name(), false, nil},
|
|
|
|
{emptyDirectory, true, nil},
|
|
|
|
{nonEmptyZeroLengthFilesDirectory, false, nil},
|
|
|
|
{nonEmptyNonZeroLengthFilesDirectory, false, nil},
|
|
|
|
{nonExistentFile, false, fileDoesNotExist},
|
|
|
|
{nonExistentDir, false, dirDoesNotExist},
|
|
|
|
}
|
|
|
|
for i, d := range data {
|
2014-11-04 20:28:07 -05:00
|
|
|
exists, err := IsEmpty(d.input, new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expectedResult != exists {
|
|
|
|
t.Errorf("Test %d failed. Expected result %t got %t", i, d.expectedResult, exists)
|
|
|
|
}
|
|
|
|
if d.expectedErr != nil {
|
|
|
|
if d.expectedErr.Error() != err.Error() {
|
|
|
|
t.Errorf("Test %d failed. Expected %q(%#v) got %q(%#v)", i, d.expectedErr, d.expectedErr, err, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if d.expectedErr != err {
|
|
|
|
t.Errorf("Test %d failed. Expected %q(%#v) got %q(%#v)", i, d.expectedErr, d.expectedErr, err, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createZeroSizedFileInTempDir() (*os.File, error) {
|
|
|
|
filePrefix := "_path_test_"
|
|
|
|
f, e := ioutil.TempFile("", filePrefix) // dir is os.TempDir()
|
|
|
|
if e != nil {
|
|
|
|
// if there was an error no file was created.
|
|
|
|
// => no requirement to delete the file
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNonZeroSizedFileInTempDir() (*os.File, error) {
|
|
|
|
f, err := createZeroSizedFileInTempDir()
|
|
|
|
if err != nil {
|
|
|
|
// no file ??
|
|
|
|
}
|
|
|
|
byteString := []byte("byteString")
|
|
|
|
err = ioutil.WriteFile(f.Name(), byteString, 0644)
|
|
|
|
if err != nil {
|
|
|
|
// delete the file
|
|
|
|
deleteFileInTempDir(f)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteFileInTempDir(f *os.File) {
|
|
|
|
err := os.Remove(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
// now what?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEmptyTempDir() (string, error) {
|
|
|
|
dirPrefix := "_dir_prefix_"
|
|
|
|
d, e := ioutil.TempDir("", dirPrefix) // will be in os.TempDir()
|
|
|
|
if e != nil {
|
|
|
|
// no directory to delete - it was never created
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTempDirWithZeroLengthFiles() (string, error) {
|
|
|
|
d, dirErr := createEmptyTempDir()
|
|
|
|
if dirErr != nil {
|
|
|
|
//now what?
|
|
|
|
}
|
|
|
|
filePrefix := "_path_test_"
|
|
|
|
_, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir()
|
|
|
|
if fileErr != nil {
|
|
|
|
// if there was an error no file was created.
|
|
|
|
// but we need to remove the directory to clean-up
|
|
|
|
deleteTempDir(d)
|
|
|
|
return "", fileErr
|
|
|
|
}
|
|
|
|
// the dir now has one, zero length file in it
|
|
|
|
return d, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTempDirWithNonZeroLengthFiles() (string, error) {
|
|
|
|
d, dirErr := createEmptyTempDir()
|
|
|
|
if dirErr != nil {
|
|
|
|
//now what?
|
|
|
|
}
|
|
|
|
filePrefix := "_path_test_"
|
|
|
|
f, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir()
|
|
|
|
if fileErr != nil {
|
|
|
|
// if there was an error no file was created.
|
|
|
|
// but we need to remove the directory to clean-up
|
|
|
|
deleteTempDir(d)
|
|
|
|
return "", fileErr
|
|
|
|
}
|
|
|
|
byteString := []byte("byteString")
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
fileErr = ioutil.WriteFile(f.Name(), byteString, 0644)
|
|
|
|
if fileErr != nil {
|
|
|
|
// delete the file
|
|
|
|
deleteFileInTempDir(f)
|
|
|
|
// also delete the directory
|
|
|
|
deleteTempDir(d)
|
|
|
|
return "", fileErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// the dir now has one, zero length file in it
|
|
|
|
return d, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteTempDir(d string) {
|
|
|
|
err := os.RemoveAll(d)
|
|
|
|
if err != nil {
|
|
|
|
// now what?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
|
|
|
zeroSizedFile, _ := createZeroSizedFileInTempDir()
|
|
|
|
defer deleteFileInTempDir(zeroSizedFile)
|
|
|
|
nonZeroSizedFile, _ := createNonZeroSizedFileInTempDir()
|
|
|
|
defer deleteFileInTempDir(nonZeroSizedFile)
|
|
|
|
emptyDirectory, _ := createEmptyTempDir()
|
|
|
|
defer deleteTempDir(emptyDirectory)
|
|
|
|
nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt"
|
2017-02-02 16:25:42 -05:00
|
|
|
nonExistentDir := os.TempDir() + "/this/directory/does/not/exist/"
|
2014-09-10 13:48:14 -04:00
|
|
|
|
|
|
|
type test struct {
|
|
|
|
input string
|
|
|
|
expectedResult bool
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{zeroSizedFile.Name(), true, nil},
|
|
|
|
{nonZeroSizedFile.Name(), true, nil},
|
|
|
|
{emptyDirectory, true, nil},
|
|
|
|
{nonExistentFile, false, nil},
|
|
|
|
{nonExistentDir, false, nil},
|
|
|
|
}
|
|
|
|
for i, d := range data {
|
2014-11-04 20:28:07 -05:00
|
|
|
exists, err := Exists(d.input, new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expectedResult != exists {
|
|
|
|
t.Errorf("Test %d failed. Expected result %t got %t", i, d.expectedResult, exists)
|
|
|
|
}
|
|
|
|
if d.expectedErr != err {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbsPathify(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
defer viper.Reset()
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
type test struct {
|
2015-01-27 04:15:57 -05:00
|
|
|
inPath, workingDir, expected string
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
data := []test{
|
2015-01-27 04:15:57 -05:00
|
|
|
{os.TempDir(), filepath.FromSlash("/work"), filepath.Clean(os.TempDir())}, // TempDir has trailing slash
|
|
|
|
{"dir", filepath.FromSlash("/work"), filepath.FromSlash("/work/dir")},
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
2015-01-27 17:01:32 -05:00
|
|
|
windowsData := []test{
|
|
|
|
{"c:\\banana\\..\\dir", "c:\\foo", "c:\\dir"},
|
|
|
|
{"\\dir", "c:\\foo", "c:\\foo\\dir"},
|
|
|
|
{"c:\\", "c:\\foo", "c:\\"},
|
|
|
|
}
|
|
|
|
|
|
|
|
unixData := []test{
|
|
|
|
{"/banana/../dir/", "/work", "/dir"},
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
for i, d := range data {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
2015-01-27 04:15:57 -05:00
|
|
|
// todo see comment in AbsPathify
|
2017-02-04 22:20:06 -05:00
|
|
|
ps := newTestDefaultPathSpec("workingDir", d.workingDir)
|
2015-01-27 04:15:57 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
expected := ps.AbsPathify(d.inPath)
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != expected {
|
2015-01-27 06:12:35 -05:00
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expected, expected)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
2015-01-27 17:01:32 -05:00
|
|
|
t.Logf("Running platform specific path tests for %s", runtime.GOOS)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
for i, d := range windowsData {
|
2017-02-04 22:20:06 -05:00
|
|
|
ps := newTestDefaultPathSpec("workingDir", d.workingDir)
|
2015-01-27 17:01:32 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
expected := ps.AbsPathify(d.inPath)
|
2015-01-27 17:01:32 -05:00
|
|
|
if d.expected != expected {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expected, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i, d := range unixData {
|
2017-02-04 22:20:06 -05:00
|
|
|
ps := newTestDefaultPathSpec("workingDir", d.workingDir)
|
2015-01-27 17:01:32 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
expected := ps.AbsPathify(d.inPath)
|
2015-01-27 17:01:32 -05:00
|
|
|
if d.expected != expected {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expected, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
func TestExtNoDelimiter(t *testing.T) {
|
|
|
|
assert := require.New(t)
|
|
|
|
assert.Equal("json", ExtNoDelimiter(filepath.FromSlash("/my/data.json")))
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
func TestFilename(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input, expected string
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"index.html", "index"},
|
|
|
|
{"./index.html", "index"},
|
|
|
|
{"/index.html", "index"},
|
|
|
|
{"index", "index"},
|
|
|
|
{"/tmp/index.html", "index"},
|
|
|
|
{"./filename-no-ext", "filename-no-ext"},
|
|
|
|
{"/filename-no-ext", "filename-no-ext"},
|
|
|
|
{"filename-no-ext", "filename-no-ext"},
|
2017-02-02 16:25:42 -05:00
|
|
|
{"directory/", ""}, // no filename case??
|
2014-09-10 13:48:14 -04:00
|
|
|
{"directory/.hidden.ext", ".hidden"},
|
2014-09-22 11:42:38 -04:00
|
|
|
{"./directory/../~/banana/gold.fish", "gold"},
|
|
|
|
{"../directory/banana.man", "banana"},
|
|
|
|
{"~/mydir/filename.ext", "filename"},
|
|
|
|
{"./directory//tmp/filename.ext", "filename"},
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2014-12-07 13:48:00 -05:00
|
|
|
output := Filename(filepath.FromSlash(d.input))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expected != output {
|
|
|
|
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileAndExt(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
input, expectedFile, expectedExt string
|
|
|
|
}
|
|
|
|
data := []test{
|
|
|
|
{"index.html", "index", ".html"},
|
|
|
|
{"./index.html", "index", ".html"},
|
|
|
|
{"/index.html", "index", ".html"},
|
|
|
|
{"index", "index", ""},
|
|
|
|
{"/tmp/index.html", "index", ".html"},
|
|
|
|
{"./filename-no-ext", "filename-no-ext", ""},
|
|
|
|
{"/filename-no-ext", "filename-no-ext", ""},
|
|
|
|
{"filename-no-ext", "filename-no-ext", ""},
|
2017-02-02 16:25:42 -05:00
|
|
|
{"directory/", "", ""}, // no filename case??
|
2014-09-10 13:48:14 -04:00
|
|
|
{"directory/.hidden.ext", ".hidden", ".ext"},
|
2014-09-22 11:42:38 -04:00
|
|
|
{"./directory/../~/banana/gold.fish", "gold", ".fish"},
|
|
|
|
{"../directory/banana.man", "banana", ".man"},
|
|
|
|
{"~/mydir/filename.ext", "filename", ".ext"},
|
|
|
|
{"./directory//tmp/filename.ext", "filename", ".ext"},
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2015-08-26 15:29:32 -04:00
|
|
|
file, ext := fileAndExt(filepath.FromSlash(d.input), fpb)
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expectedFile != file {
|
|
|
|
t.Errorf("Test %d failed. Expected filename %q got %q.", i, d.expectedFile, file)
|
|
|
|
}
|
|
|
|
if d.expectedExt != ext {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test %d failed. Expected extension %q got %q.", i, d.expectedExt, ext)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathPrep(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrettifyPath(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-23 10:32:06 -05:00
|
|
|
func TestExtractRootPaths(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input []string
|
|
|
|
expected []string
|
|
|
|
}{{[]string{filepath.FromSlash("a/b"), filepath.FromSlash("a/b/c/"), "b",
|
|
|
|
filepath.FromSlash("/c/d"), filepath.FromSlash("d/"), filepath.FromSlash("//e//")},
|
|
|
|
[]string{"a", "a", "b", "c", "d", "e"}}}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
output := ExtractRootPaths(test.input)
|
|
|
|
if !reflect.DeepEqual(output, test.expected) {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
2015-04-02 00:40:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:48:14 -04:00
|
|
|
func TestFindCWD(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
expectedDir string
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
2014-11-04 20:28:27 -05:00
|
|
|
//cwd, _ := os.Getwd()
|
2014-09-10 13:48:14 -04:00
|
|
|
data := []test{
|
2018-02-21 03:23:43 -05:00
|
|
|
//{cwd, nil},
|
|
|
|
// Commenting this out. It doesn't work properly.
|
|
|
|
// There's a good reason why we don't use os.Getwd(), it doesn't actually work the way we want it to.
|
|
|
|
// I really don't know a better way to test this function. - SPF 2014.11.04
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
for i, d := range data {
|
|
|
|
dir, err := FindCWD()
|
|
|
|
if d.expectedDir != dir {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedDir, dir)
|
|
|
|
}
|
|
|
|
if d.expectedErr != err {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, err)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeWriteToDisk(t *testing.T) {
|
2014-09-10 14:47:31 -04:00
|
|
|
emptyFile, _ := createZeroSizedFileInTempDir()
|
|
|
|
defer deleteFileInTempDir(emptyFile)
|
|
|
|
tmpDir, _ := createEmptyTempDir()
|
|
|
|
defer deleteTempDir(tmpDir)
|
2014-09-10 13:48:14 -04:00
|
|
|
|
2014-09-10 14:47:31 -04:00
|
|
|
randomString := "This is a random string!"
|
|
|
|
reader := strings.NewReader(randomString)
|
2014-09-10 13:48:14 -04:00
|
|
|
|
2014-09-10 14:47:31 -04:00
|
|
|
fileExists := fmt.Errorf("%v already exists", emptyFile.Name())
|
2014-09-18 17:58:44 -04:00
|
|
|
|
2014-09-10 14:47:31 -04:00
|
|
|
type test struct {
|
|
|
|
filename string
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:58:44 -04:00
|
|
|
now := time.Now().Unix()
|
|
|
|
nowStr := strconv.FormatInt(now, 10)
|
2014-09-10 14:47:31 -04:00
|
|
|
data := []test{
|
|
|
|
{emptyFile.Name(), fileExists},
|
2014-09-18 17:58:44 -04:00
|
|
|
{tmpDir + "/" + nowStr, nil},
|
2014-09-10 14:47:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2014-11-04 20:28:07 -05:00
|
|
|
e := SafeWriteToDisk(d.filename, reader, new(afero.OsFs))
|
2014-09-10 14:47:31 -04:00
|
|
|
if d.expectedErr != nil {
|
|
|
|
if d.expectedErr.Error() != e.Error() {
|
|
|
|
t.Errorf("Test %d failed. Expected error %q but got %q", i, d.expectedErr.Error(), e.Error())
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
2014-09-18 17:38:07 -04:00
|
|
|
} else {
|
|
|
|
if d.expectedErr != e {
|
|
|
|
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, e)
|
|
|
|
}
|
|
|
|
contents, _ := ioutil.ReadFile(d.filename)
|
|
|
|
if randomString != string(contents) {
|
|
|
|
t.Errorf("Test %d failed. Expected contents %q but got %q", i, randomString, string(contents))
|
|
|
|
}
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
2014-09-18 17:38:07 -04:00
|
|
|
reader.Seek(0, 0)
|
2014-09-10 14:47:31 -04:00
|
|
|
}
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteToDisk(t *testing.T) {
|
|
|
|
emptyFile, _ := createZeroSizedFileInTempDir()
|
|
|
|
defer deleteFileInTempDir(emptyFile)
|
|
|
|
tmpDir, _ := createEmptyTempDir()
|
|
|
|
defer deleteTempDir(tmpDir)
|
|
|
|
|
|
|
|
randomString := "This is a random string!"
|
|
|
|
reader := strings.NewReader(randomString)
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
filename string
|
|
|
|
expectedErr error
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:58:44 -04:00
|
|
|
now := time.Now().Unix()
|
|
|
|
nowStr := strconv.FormatInt(now, 10)
|
2014-09-10 13:48:14 -04:00
|
|
|
data := []test{
|
|
|
|
{emptyFile.Name(), nil},
|
2014-09-18 17:58:44 -04:00
|
|
|
{tmpDir + "/" + nowStr, nil},
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
2014-11-04 20:28:07 -05:00
|
|
|
e := WriteToDisk(d.filename, reader, new(afero.OsFs))
|
2014-09-10 13:48:14 -04:00
|
|
|
if d.expectedErr != e {
|
2014-09-18 17:38:07 -04:00
|
|
|
t.Errorf("Test %d failed. WriteToDisk Error Expected %q but got %q", i, d.expectedErr, e)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
contents, e := ioutil.ReadFile(d.filename)
|
2014-09-18 17:38:07 -04:00
|
|
|
if e != nil {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test %d failed. Could not read file %s. Reason: %s\n", i, d.filename, e)
|
2014-09-18 17:38:07 -04:00
|
|
|
}
|
2014-09-10 13:48:14 -04:00
|
|
|
if randomString != string(contents) {
|
|
|
|
t.Errorf("Test %d failed. Expected contents %q but got %q", i, randomString, string(contents))
|
|
|
|
}
|
2014-09-18 17:38:07 -04:00
|
|
|
reader.Seek(0, 0)
|
2014-09-10 13:48:14 -04:00
|
|
|
}
|
|
|
|
}
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
func TestGetTempDir(t *testing.T) {
|
|
|
|
dir := os.TempDir()
|
|
|
|
if FilePathSeparator != dir[len(dir)-1:] {
|
|
|
|
dir = dir + FilePathSeparator
|
|
|
|
}
|
|
|
|
testDir := "hugoTestFolder" + FilePathSeparator
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"", dir},
|
2015-12-08 16:41:36 -05:00
|
|
|
{testDir + " Foo bar ", dir + testDir + " Foo bar " + FilePathSeparator},
|
2014-12-26 22:40:10 -05:00
|
|
|
{testDir + "Foo.Bar/foo_Bar-Foo", dir + testDir + "Foo.Bar/foo_Bar-Foo" + FilePathSeparator},
|
2015-12-08 16:41:36 -05:00
|
|
|
{testDir + "fOO,bar:foo%bAR", dir + testDir + "fOObarfoo%bAR" + FilePathSeparator},
|
2015-11-25 17:03:28 -05:00
|
|
|
{testDir + "fOO,bar:foobAR", dir + testDir + "fOObarfoobAR" + FilePathSeparator},
|
2014-12-26 22:40:10 -05:00
|
|
|
{testDir + "FOo/BaR.html", dir + testDir + "FOo/BaR.html" + FilePathSeparator},
|
|
|
|
{testDir + "трям/трям", dir + testDir + "трям/трям" + FilePathSeparator},
|
|
|
|
{testDir + "은행", dir + testDir + "은행" + FilePathSeparator},
|
2015-12-08 16:41:36 -05:00
|
|
|
{testDir + "Банковский кассир", dir + testDir + "Банковский кассир" + FilePathSeparator},
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
output := GetTempDir(test.input, new(afero.MemMapFs))
|
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|