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-12-27 08:11:19 -05:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-22 12:59:46 -04:00
|
|
|
"html/template"
|
2014-12-27 08:11:19 -05:00
|
|
|
"path/filepath"
|
2017-03-09 13:19:29 -05:00
|
|
|
"strings"
|
2014-12-27 08:11:19 -05:00
|
|
|
"testing"
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/output"
|
2017-01-10 04:55:03 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-12-27 08:11:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSplitPages(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
pages := createTestPages(s, 21)
|
2014-12-27 08:11:19 -05:00
|
|
|
chunks := splitPages(pages, 5)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 5, len(chunks))
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
for i := 0; i < 4; i++ {
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 5, chunks[i].Len())
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
lastChunk := chunks[4]
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 1, lastChunk.Len())
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func TestSplitPageGroups(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
|
|
|
pages := createTestPages(s, 21)
|
2015-07-16 07:17:54 -04:00
|
|
|
groups, _ := pages.GroupBy("Weight", "desc")
|
|
|
|
chunks := splitPageGroups(groups, 5)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 5, len(chunks))
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
firstChunk := chunks[0]
|
|
|
|
|
|
|
|
// alternate weight 5 and 10
|
|
|
|
if groups, ok := firstChunk.(PagesGroup); ok {
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 5, groups.Len())
|
2015-07-16 07:17:54 -04:00
|
|
|
for _, pg := range groups {
|
|
|
|
// first group 10 in weight
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 10, pg.Key)
|
2015-07-16 07:17:54 -04:00
|
|
|
for _, p := range pg.Pages {
|
2017-02-04 22:20:06 -05:00
|
|
|
require.True(t, p.fuzzyWordCount%2 == 0) // magic test
|
2015-07-16 07:17:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatal("Excepted PageGroup")
|
|
|
|
}
|
|
|
|
|
|
|
|
lastChunk := chunks[4]
|
|
|
|
|
|
|
|
if groups, ok := lastChunk.(PagesGroup); ok {
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 1, groups.Len())
|
2015-07-16 07:17:54 -04:00
|
|
|
for _, pg := range groups {
|
|
|
|
// last should have 5 in weight
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 5, pg.Key)
|
2015-07-16 07:17:54 -04:00
|
|
|
for _, p := range pg.Pages {
|
2017-02-04 22:20:06 -05:00
|
|
|
require.True(t, p.fuzzyWordCount%2 != 0) // magic test
|
2015-07-16 07:17:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatal("Excepted PageGroup")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func TestPager(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
|
|
|
pages := createTestPages(s, 21)
|
2015-07-16 07:17:54 -04:00
|
|
|
groups, _ := pages.GroupBy("Weight", "desc")
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
urlFactory := func(page int) string {
|
|
|
|
return fmt.Sprintf("page/%d/", page)
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
_, err := newPaginatorFromPages(pages, -1, urlFactory)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
_, err = newPaginatorFromPageGroups(groups, -1, urlFactory)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
pag, err := newPaginatorFromPages(pages, 5, urlFactory)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
2015-07-16 07:17:54 -04:00
|
|
|
doTestPages(t, pag)
|
|
|
|
first := pag.Pagers()[0].First()
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, "Pager 1", first.String())
|
|
|
|
require.NotEmpty(t, first.Pages())
|
|
|
|
require.Empty(t, first.PageGroups())
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
pag, err = newPaginatorFromPageGroups(groups, 5, urlFactory)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
2015-07-16 07:17:54 -04:00
|
|
|
doTestPages(t, pag)
|
|
|
|
first = pag.Pagers()[0].First()
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotEmpty(t, first.PageGroups())
|
|
|
|
require.Empty(t, first.Pages())
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPages(t *testing.T, paginator *paginator) {
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
paginatorPages := paginator.Pagers()
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 5, len(paginatorPages))
|
|
|
|
require.Equal(t, 21, paginator.TotalNumberOfElements())
|
|
|
|
require.Equal(t, 5, paginator.PageSize())
|
|
|
|
require.Equal(t, 5, paginator.TotalPages())
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
first := paginatorPages[0]
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, template.HTML("page/1/"), first.URL())
|
|
|
|
require.Equal(t, first, first.First())
|
|
|
|
require.True(t, first.HasNext())
|
|
|
|
require.Equal(t, paginatorPages[1], first.Next())
|
|
|
|
require.False(t, first.HasPrev())
|
|
|
|
require.Nil(t, first.Prev())
|
|
|
|
require.Equal(t, 5, first.NumberOfElements())
|
|
|
|
require.Equal(t, 1, first.PageNumber())
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
third := paginatorPages[2]
|
2017-02-04 22:20:06 -05:00
|
|
|
require.True(t, third.HasNext())
|
|
|
|
require.True(t, third.HasPrev())
|
|
|
|
require.Equal(t, paginatorPages[1], third.Prev())
|
2014-12-27 08:11:19 -05:00
|
|
|
|
|
|
|
last := paginatorPages[4]
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, template.HTML("page/5/"), last.URL())
|
|
|
|
require.Equal(t, last, last.Last())
|
|
|
|
require.False(t, last.HasNext())
|
|
|
|
require.Nil(t, last.Next())
|
|
|
|
require.True(t, last.HasPrev())
|
|
|
|
require.Equal(t, 1, last.NumberOfElements())
|
|
|
|
require.Equal(t, 5, last.PageNumber())
|
2015-01-26 19:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPagerNoPages(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
|
|
|
pages := createTestPages(s, 0)
|
2015-07-16 07:17:54 -04:00
|
|
|
groups, _ := pages.GroupBy("Weight", "desc")
|
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
urlFactory := func(page int) string {
|
|
|
|
return fmt.Sprintf("page/%d/", page)
|
|
|
|
}
|
2014-12-27 08:11:19 -05:00
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
paginator, _ := newPaginatorFromPages(pages, 5, urlFactory)
|
|
|
|
doTestPagerNoPages(t, paginator)
|
|
|
|
|
|
|
|
first := paginator.Pagers()[0].First()
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Empty(t, first.PageGroups())
|
|
|
|
require.Empty(t, first.Pages())
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
paginator, _ = newPaginatorFromPageGroups(groups, 5, urlFactory)
|
|
|
|
doTestPagerNoPages(t, paginator)
|
|
|
|
|
|
|
|
first = paginator.Pagers()[0].First()
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Empty(t, first.PageGroups())
|
|
|
|
require.Empty(t, first.Pages())
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPagerNoPages(t *testing.T, paginator *paginator) {
|
2015-01-26 19:27:07 -05:00
|
|
|
paginatorPages := paginator.Pagers()
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 1, len(paginatorPages))
|
|
|
|
require.Equal(t, 0, paginator.TotalNumberOfElements())
|
|
|
|
require.Equal(t, 5, paginator.PageSize())
|
|
|
|
require.Equal(t, 0, paginator.TotalPages())
|
2015-03-05 18:30:51 -05:00
|
|
|
|
|
|
|
// pageOne should be nothing but the first
|
|
|
|
pageOne := paginatorPages[0]
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, pageOne.First())
|
|
|
|
require.False(t, pageOne.HasNext())
|
|
|
|
require.False(t, pageOne.HasPrev())
|
|
|
|
require.Nil(t, pageOne.Next())
|
|
|
|
require.Equal(t, 1, len(pageOne.Pagers()))
|
|
|
|
require.Equal(t, 0, pageOne.Pages().Len())
|
|
|
|
require.Equal(t, 0, pageOne.NumberOfElements())
|
|
|
|
require.Equal(t, 0, pageOne.TotalNumberOfElements())
|
|
|
|
require.Equal(t, 0, pageOne.TotalPages())
|
|
|
|
require.Equal(t, 1, pageOne.PageNumber())
|
|
|
|
require.Equal(t, 5, pageOne.PageSize())
|
2015-03-05 18:30:51 -05:00
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
func TestPaginationURLFactory(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
cfg, fs := newTestCfg()
|
|
|
|
cfg.Set("paginatePath", "zoo")
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
for _, uglyURLs := range []bool{false, true} {
|
2017-06-06 02:43:33 -04:00
|
|
|
for _, canonifyURLs := range []bool{false, true} {
|
|
|
|
t.Run(fmt.Sprintf("uglyURLs=%t,canonifyURLs=%t", uglyURLs, canonifyURLs), func(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
d targetPathDescriptor
|
|
|
|
baseURL string
|
|
|
|
page int
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"HTML home page 32",
|
|
|
|
targetPathDescriptor{Kind: KindHome, Type: output.HTMLFormat}, "http://example.com/", 32, "/zoo/32/"},
|
|
|
|
{"JSON home page 42",
|
|
|
|
targetPathDescriptor{Kind: KindHome, Type: output.JSONFormat}, "http://example.com/", 42, "/zoo/42/"},
|
|
|
|
// Issue #1252
|
|
|
|
{"BaseURL with sub path",
|
|
|
|
targetPathDescriptor{Kind: KindHome, Type: output.HTMLFormat}, "http://example.com/sub/", 999, "/sub/zoo/999/"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
d := test.d
|
|
|
|
cfg.Set("baseURL", test.baseURL)
|
|
|
|
cfg.Set("canonifyURLs", canonifyURLs)
|
|
|
|
cfg.Set("uglyURLs", uglyURLs)
|
|
|
|
d.UglyURLs = uglyURLs
|
|
|
|
|
|
|
|
expected := test.expected
|
|
|
|
|
|
|
|
if canonifyURLs {
|
|
|
|
expected = strings.Replace(expected, "/sub", "", 1)
|
2017-03-09 13:19:29 -05:00
|
|
|
}
|
|
|
|
|
2017-06-06 02:43:33 -04:00
|
|
|
if uglyURLs {
|
2018-07-10 05:55:22 -04:00
|
|
|
expected = expected[:len(expected)-1] + "." + test.d.Type.MediaType.Suffix()
|
2017-06-06 02:43:33 -04:00
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-06-06 02:43:33 -04:00
|
|
|
pathSpec := newTestPathSpec(fs, cfg)
|
|
|
|
d.PathSpec = pathSpec
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-06-06 02:43:33 -04:00
|
|
|
factory := newPaginationURLFactory(d)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-06-06 02:43:33 -04:00
|
|
|
got := factory(test.page)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-06-06 02:43:33 -04:00
|
|
|
require.Equal(t, expected, got)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-06-06 02:43:33 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
func TestPaginator(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2015-03-31 13:12:54 -04:00
|
|
|
for _, useViper := range []bool{false, true} {
|
|
|
|
doTestPaginator(t, useViper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestPaginator(t *testing.T, useViper bool) {
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
cfg, fs := newTestCfg()
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
pagerSize := 5
|
|
|
|
if useViper {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("paginate", pagerSize)
|
2015-03-31 13:12:54 -04:00
|
|
|
} else {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("paginate", -1)
|
2015-03-31 13:12:54 -04:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
s, err := NewSiteForCfg(deps.DepsCfg{Cfg: cfg, Fs: fs})
|
2017-01-10 04:55:03 -05:00
|
|
|
require.NoError(t, err)
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
pages := createTestPages(s, 12)
|
2018-05-08 04:10:13 -04:00
|
|
|
n1, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
|
|
|
n2, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
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
|
|
|
n1.data["Pages"] = pages
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2015-04-21 15:25:42 -04:00
|
|
|
var paginator1 *Pager
|
2015-03-31 13:12:54 -04:00
|
|
|
|
|
|
|
if useViper {
|
|
|
|
paginator1, err = n1.Paginator()
|
|
|
|
} else {
|
|
|
|
paginator1, err = n1.Paginator(pagerSize)
|
|
|
|
}
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, paginator1)
|
|
|
|
require.Equal(t, 3, paginator1.TotalPages())
|
|
|
|
require.Equal(t, 12, paginator1.TotalNumberOfElements())
|
2015-01-26 19:27:07 -05:00
|
|
|
|
|
|
|
n2.paginator = paginator1.Next()
|
|
|
|
paginator2, err := n2.Paginator()
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, paginator2, paginator1.Next())
|
2015-01-26 19:27:07 -05: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
|
|
|
n1.data["Pages"] = createTestPages(s, 1)
|
2015-01-26 19:27:07 -05:00
|
|
|
samePaginator, _ := n1.Paginator()
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, paginator1, samePaginator)
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
pp, _ := s.NewPage("test")
|
2018-05-08 04:10:13 -04:00
|
|
|
p, _ := newPageOutput(pp, false, false, output.HTMLFormat)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
_, err = p.Paginator()
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-01-26 19:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginatorWithNegativePaginate(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t, "paginate", -1)
|
2018-05-08 04:10:13 -04:00
|
|
|
n1, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
2017-03-09 13:19:29 -05:00
|
|
|
_, err := n1.Paginator()
|
2017-01-10 04:55:03 -05:00
|
|
|
require.Error(t, err)
|
2015-01-26 19:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginate(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2015-03-31 13:12:54 -04:00
|
|
|
for _, useViper := range []bool{false, true} {
|
|
|
|
doTestPaginate(t, useViper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-08 10:54:05 -05:00
|
|
|
func TestPaginatorURL(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
cfg, fs := newTestCfg()
|
2017-01-08 10:54:05 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Set("paginate", 2)
|
|
|
|
cfg.Set("paginatePath", "testing")
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-01-08 10:54:05 -05:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
// Issue #2177, do not double encode URLs
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("content", "阅读", fmt.Sprintf("page%d.md", (i+1))),
|
2017-01-08 10:54:05 -05:00
|
|
|
fmt.Sprintf(`---
|
|
|
|
title: Page%d
|
|
|
|
---
|
|
|
|
Conten%d
|
|
|
|
`, (i+1), i+1))
|
|
|
|
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), "<html><body>{{.Content}}</body></html>")
|
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "list.html"),
|
2017-01-08 10:54:05 -05:00
|
|
|
`
|
|
|
|
<html><body>
|
|
|
|
Count: {{ .Paginator.TotalNumberOfElements }}
|
|
|
|
Pages: {{ .Paginator.TotalPages }}
|
|
|
|
{{ range .Paginator.Pagers -}}
|
|
|
|
{{ .PageNumber }}: {{ .URL }}
|
|
|
|
{{ end }}
|
|
|
|
</body></html>`)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
|
2017-01-08 10:54:05 -05:00
|
|
|
|
2017-02-17 14:52:50 -05:00
|
|
|
th := testHelper{s.Cfg, s.Fs, t}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2017-02-17 15:14:52 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "阅读", "testing", "2", "index.html"), "2: /%E9%98%85%E8%AF%BB/testing/2/")
|
2017-01-08 10:54:05 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
func doTestPaginate(t *testing.T, useViper bool) {
|
|
|
|
pagerSize := 5
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
var (
|
|
|
|
s *Site
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
if useViper {
|
2017-02-04 22:20:06 -05:00
|
|
|
s = newTestSite(t, "paginate", pagerSize)
|
2015-03-31 13:12:54 -04:00
|
|
|
} else {
|
2017-02-04 22:20:06 -05:00
|
|
|
s = newTestSite(t, "paginate", -1)
|
2015-03-31 13:12:54 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
pages := createTestPages(s, 6)
|
2018-05-08 04:10:13 -04:00
|
|
|
n1, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
|
|
|
n2, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2015-04-21 15:25:42 -04:00
|
|
|
var paginator1, paginator2 *Pager
|
2015-03-31 13:12:54 -04:00
|
|
|
|
|
|
|
if useViper {
|
|
|
|
paginator1, err = n1.Paginate(pages)
|
|
|
|
} else {
|
|
|
|
paginator1, err = n1.Paginate(pages, pagerSize)
|
|
|
|
}
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, paginator1)
|
|
|
|
require.Equal(t, 2, paginator1.TotalPages())
|
|
|
|
require.Equal(t, 6, paginator1.TotalNumberOfElements())
|
2015-01-26 19:27:07 -05:00
|
|
|
|
|
|
|
n2.paginator = paginator1.Next()
|
2015-03-31 16:33:24 -04:00
|
|
|
if useViper {
|
|
|
|
paginator2, err = n2.Paginate(pages)
|
|
|
|
} else {
|
|
|
|
paginator2, err = n2.Paginate(pages, pagerSize)
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, paginator2, paginator1.Next())
|
2015-01-26 19:27:07 -05:00
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
pp, err := s.NewPage("test")
|
2018-05-08 04:10:13 -04:00
|
|
|
p, _ := newPageOutput(pp, false, false, output.HTMLFormat)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
_, err = p.Paginate(pages)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-01-26 19:27:07 -05:00
|
|
|
}
|
|
|
|
|
2015-03-31 13:12:54 -04:00
|
|
|
func TestInvalidOptions(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
2018-05-08 04:10:13 -04:00
|
|
|
n1, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
_, err := n1.Paginate(createTestPages(s, 1), 1, 2)
|
|
|
|
require.NotNil(t, err)
|
2015-03-31 13:12:54 -04:00
|
|
|
_, err = n1.Paginator(1, 2)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-03-31 13:12:54 -04:00
|
|
|
_, err = n1.Paginator(-1)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-03-31 13:12:54 -04:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:27:07 -05:00
|
|
|
func TestPaginateWithNegativePaginate(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
cfg, fs := newTestCfg()
|
|
|
|
cfg.Set("paginate", -1)
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
s, err := NewSiteForCfg(deps.DepsCfg{Cfg: cfg, Fs: fs})
|
2017-01-10 04:55:03 -05:00
|
|
|
require.NoError(t, err)
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2018-05-08 04:10:13 -04:00
|
|
|
n, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
|
|
|
_, err = n.Paginate(createTestPages(s, 2))
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-01-26 19:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginatePages(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
groups, _ := createTestPages(s, 31).GroupBy("Weight", "desc")
|
2017-03-25 14:36:50 -04:00
|
|
|
pd := targetPathDescriptor{Kind: KindHome, Type: output.HTMLFormat, PathSpec: s.PathSpec, Addends: "t"}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
for i, seq := range []interface{}{createTestPages(s, 11), groups, WeightedPages{}, PageGroup{}, &Pages{}} {
|
2017-03-09 13:19:29 -05:00
|
|
|
v, err := paginatePages(pd, seq, 11)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, v, "Val %d", i)
|
|
|
|
require.Nil(t, err, "Err %d", i)
|
2015-01-26 19:27:07 -05:00
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
_, err := paginatePages(pd, Site{}, 11)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-01-26 19:27:07 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-31 16:33:24 -04:00
|
|
|
// Issue #993
|
|
|
|
func TestPaginatorFollowedByPaginateShouldFail(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t, "paginate", 10)
|
2018-05-08 04:10:13 -04:00
|
|
|
n1, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
|
|
|
n2, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
_, err := n1.Paginator()
|
|
|
|
require.Nil(t, err)
|
|
|
|
_, err = n1.Paginate(createTestPages(s, 2))
|
|
|
|
require.NotNil(t, err)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
_, err = n2.Paginate(createTestPages(s, 2))
|
|
|
|
require.Nil(t, err)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPaginateFollowedByDifferentPaginateShouldFail(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t, "paginate", 10)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
2018-05-08 04:10:13 -04:00
|
|
|
n1, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
|
|
|
n2, _ := newPageOutput(s.newHomePage(), false, false, output.HTMLFormat)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
p1 := createTestPages(s, 2)
|
|
|
|
p2 := createTestPages(s, 10)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
_, err := n1.Paginate(p1)
|
|
|
|
require.Nil(t, err)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
_, err = n1.Paginate(p1)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
_, err = n1.Paginate(p2)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NotNil(t, err)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
_, err = n2.Paginate(p2)
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Nil(t, err)
|
2015-03-31 16:33:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProbablyEqualPageLists(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
|
|
|
fivePages := createTestPages(s, 5)
|
|
|
|
zeroPages := createTestPages(s, 0)
|
|
|
|
zeroPagesByWeight, _ := createTestPages(s, 0).GroupBy("Weight", "asc")
|
|
|
|
fivePagesByWeight, _ := createTestPages(s, 5).GroupBy("Weight", "asc")
|
|
|
|
ninePagesByWeight, _ := createTestPages(s, 9).GroupBy("Weight", "asc")
|
2015-07-16 07:17:54 -04:00
|
|
|
|
2015-03-31 16:33:24 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
v1 interface{}
|
|
|
|
v2 interface{}
|
|
|
|
expect bool
|
|
|
|
}{
|
|
|
|
{nil, nil, true},
|
|
|
|
{"a", "b", true},
|
|
|
|
{"a", fivePages, false},
|
|
|
|
{fivePages, "a", false},
|
2017-02-04 22:20:06 -05:00
|
|
|
{fivePages, createTestPages(s, 2), false},
|
2015-03-31 16:33:24 -04:00
|
|
|
{fivePages, fivePages, true},
|
|
|
|
{zeroPages, zeroPages, true},
|
2015-07-16 07:17:54 -04:00
|
|
|
{fivePagesByWeight, fivePagesByWeight, true},
|
|
|
|
{zeroPagesByWeight, fivePagesByWeight, false},
|
|
|
|
{zeroPagesByWeight, zeroPagesByWeight, true},
|
|
|
|
{fivePagesByWeight, fivePages, false},
|
|
|
|
{fivePagesByWeight, ninePagesByWeight, false},
|
2015-03-31 16:33:24 -04:00
|
|
|
} {
|
|
|
|
result := probablyEqualPageLists(this.v1, this.v2)
|
|
|
|
|
|
|
|
if result != this.expect {
|
2015-04-05 15:03:16 -04:00
|
|
|
t.Errorf("[%d] got %t but expected %t", i, result, this.expect)
|
2015-03-31 16:33:24 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:17:54 -04:00
|
|
|
func TestPage(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2015-07-16 07:17:54 -04:00
|
|
|
urlFactory := func(page int) string {
|
|
|
|
return fmt.Sprintf("page/%d/", page)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
s := newTestSite(t)
|
|
|
|
|
|
|
|
fivePages := createTestPages(s, 7)
|
|
|
|
fivePagesFuzzyWordCount, _ := createTestPages(s, 7).GroupBy("FuzzyWordCount", "asc")
|
2015-07-16 07:17:54 -04:00
|
|
|
|
|
|
|
p1, _ := newPaginatorFromPages(fivePages, 2, urlFactory)
|
|
|
|
p2, _ := newPaginatorFromPageGroups(fivePagesFuzzyWordCount, 2, urlFactory)
|
|
|
|
|
|
|
|
f1 := p1.pagers[0].First()
|
|
|
|
f2 := p2.pagers[0].First()
|
|
|
|
|
|
|
|
page11, _ := f1.page(1)
|
|
|
|
page1Nil, _ := f1.page(3)
|
|
|
|
|
|
|
|
page21, _ := f2.page(1)
|
|
|
|
page2Nil, _ := f2.page(3)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 3, page11.fuzzyWordCount)
|
|
|
|
require.Nil(t, page1Nil)
|
2015-07-16 07:17:54 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
require.Equal(t, 3, page21.fuzzyWordCount)
|
|
|
|
require.Nil(t, page2Nil)
|
2015-07-16 07:17:54 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func createTestPages(s *Site, num int) Pages {
|
2014-12-27 08:11:19 -05:00
|
|
|
pages := make(Pages, num)
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
2017-02-04 22:20:06 -05:00
|
|
|
p := s.newPage(filepath.FromSlash(fmt.Sprintf("/x/y/z/p%d.md", i)))
|
2015-07-16 07:17:54 -04:00
|
|
|
w := 5
|
|
|
|
if i%2 == 0 {
|
|
|
|
w = 10
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
p.fuzzyWordCount = i + 2
|
|
|
|
p.Weight = w
|
|
|
|
pages[i] = p
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return pages
|
|
|
|
}
|