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.
|
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-06-12 13:14:29 -04:00
|
|
|
"path"
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
"path/filepath"
|
2014-10-29 00:37:59 -04:00
|
|
|
"regexp"
|
2013-11-18 04:35:56 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-12-28 05:32:02 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2013-11-18 04:35:56 -05:00
|
|
|
)
|
|
|
|
|
2016-03-24 22:12:03 -04:00
|
|
|
// pathPattern represents a string which builds up a URL from attributes
|
|
|
|
type pathPattern string
|
2013-11-18 04:35:56 -05:00
|
|
|
|
2016-03-24 22:12:03 -04:00
|
|
|
// pageToPermaAttribute is the type of a function which, given a page and a tag
|
2013-11-18 04:35:56 -05:00
|
|
|
// can return a string to go in that position in the page (or an error)
|
2016-03-24 22:12:03 -04:00
|
|
|
type pageToPermaAttribute func(*Page, string) (string, error)
|
2013-11-18 04:35:56 -05:00
|
|
|
|
|
|
|
// PermalinkOverrides maps a section name to a PathPattern
|
2016-03-24 22:12:03 -04:00
|
|
|
type PermalinkOverrides map[string]pathPattern
|
2013-11-18 04:35:56 -05:00
|
|
|
|
|
|
|
// knownPermalinkAttributes maps :tags in a permalink specification to a
|
|
|
|
// function which, given a page and the tag, returns the resulting string
|
|
|
|
// to be used to replace that tag.
|
2016-03-24 22:12:03 -04:00
|
|
|
var knownPermalinkAttributes map[string]pageToPermaAttribute
|
2013-11-18 04:35:56 -05:00
|
|
|
|
2014-10-29 00:37:59 -04:00
|
|
|
var attributeRegexp *regexp.Regexp
|
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
// validate determines if a PathPattern is well-formed
|
2016-03-24 22:12:03 -04:00
|
|
|
func (pp pathPattern) validate() bool {
|
2013-11-18 04:35:56 -05:00
|
|
|
fragments := strings.Split(string(pp[1:]), "/")
|
|
|
|
var bail = false
|
|
|
|
for i := range fragments {
|
|
|
|
if bail {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(fragments[i]) == 0 {
|
|
|
|
bail = true
|
|
|
|
continue
|
|
|
|
}
|
2014-10-29 00:37:59 -04:00
|
|
|
|
|
|
|
matches := attributeRegexp.FindAllStringSubmatch(fragments[i], -1)
|
|
|
|
if matches == nil {
|
2013-11-18 04:35:56 -05:00
|
|
|
continue
|
|
|
|
}
|
2014-10-29 00:37:59 -04:00
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
k := strings.ToLower(match[0][1:])
|
|
|
|
if _, ok := knownPermalinkAttributes[k]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type permalinkExpandError struct {
|
2016-03-24 22:12:03 -04:00
|
|
|
pattern pathPattern
|
2013-11-18 04:35:56 -05:00
|
|
|
section string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pee *permalinkExpandError) Error() string {
|
|
|
|
return fmt.Sprintf("error expanding %q section %q: %s", string(pee.pattern), pee.section, pee.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
errPermalinkIllFormed = errors.New("permalink ill-formed")
|
|
|
|
errPermalinkAttributeUnknown = errors.New("permalink attribute not recognised")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Expand on a PathPattern takes a Page and returns the fully expanded Permalink
|
|
|
|
// or an error explaining the failure.
|
2016-03-24 22:12:03 -04:00
|
|
|
func (pp pathPattern) Expand(p *Page) (string, error) {
|
2013-11-18 04:35:56 -05:00
|
|
|
if !pp.validate() {
|
|
|
|
return "", &permalinkExpandError{pattern: pp, section: "<all>", err: errPermalinkIllFormed}
|
|
|
|
}
|
|
|
|
sections := strings.Split(string(pp), "/")
|
|
|
|
for i, field := range sections {
|
2014-10-29 00:37:59 -04:00
|
|
|
if len(field) == 0 {
|
2013-11-18 04:35:56 -05:00
|
|
|
continue
|
|
|
|
}
|
2014-10-29 00:37:59 -04:00
|
|
|
|
|
|
|
matches := attributeRegexp.FindAllStringSubmatch(field, -1)
|
|
|
|
|
|
|
|
if matches == nil {
|
|
|
|
continue
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
2014-10-29 00:37:59 -04:00
|
|
|
|
|
|
|
newField := field
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
attr := match[0][1:]
|
|
|
|
callback, ok := knownPermalinkAttributes[attr]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return "", &permalinkExpandError{pattern: pp, section: strconv.Itoa(i), err: errPermalinkAttributeUnknown}
|
|
|
|
}
|
|
|
|
|
|
|
|
newAttr, err := callback(p, attr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", &permalinkExpandError{pattern: pp, section: strconv.Itoa(i), err: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
newField = strings.Replace(newField, match[0], newAttr, 1)
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
2014-10-29 00:37:59 -04:00
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
sections[i] = newField
|
|
|
|
}
|
|
|
|
return strings.Join(sections, "/"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pageToPermalinkDate(p *Page, dateField string) (string, error) {
|
|
|
|
// a Page contains a Node which provides a field Date, time.Time
|
|
|
|
switch dateField {
|
|
|
|
case "year":
|
|
|
|
return strconv.Itoa(p.Date.Year()), nil
|
|
|
|
case "month":
|
|
|
|
return fmt.Sprintf("%02d", int(p.Date.Month())), nil
|
|
|
|
case "monthname":
|
|
|
|
return p.Date.Month().String(), nil
|
|
|
|
case "day":
|
2016-03-14 15:31:31 -04:00
|
|
|
return fmt.Sprintf("%02d", p.Date.Day()), nil
|
2013-11-18 04:35:56 -05:00
|
|
|
case "weekday":
|
|
|
|
return strconv.Itoa(int(p.Date.Weekday())), nil
|
|
|
|
case "weekdayname":
|
|
|
|
return p.Date.Weekday().String(), nil
|
|
|
|
case "yearday":
|
|
|
|
return strconv.Itoa(p.Date.YearDay()), nil
|
|
|
|
}
|
|
|
|
//TODO: support classic strftime escapes too
|
|
|
|
// (and pass those through despite not being in the map)
|
|
|
|
panic("coding error: should not be here")
|
|
|
|
}
|
|
|
|
|
|
|
|
// pageToPermalinkTitle returns the URL-safe form of the title
|
|
|
|
func pageToPermalinkTitle(p *Page, _ string) (string, error) {
|
|
|
|
// Page contains Node which has Title
|
2015-03-18 01:16:54 -04:00
|
|
|
// (also contains URLPath which has Slug, sometimes)
|
2018-01-15 14:40:39 -05:00
|
|
|
return p.s.PathSpec.URLize(p.title), nil
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
|
|
|
|
2014-05-27 04:14:05 -04:00
|
|
|
// pageToPermalinkFilename returns the URL-safe form of the filename
|
|
|
|
func pageToPermalinkFilename(p *Page, _ string) (string, error) {
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
name := p.File.TranslationBaseName()
|
|
|
|
if name == "index" {
|
|
|
|
// Page bundles; the directory name will hopefully have a better name.
|
2017-12-28 05:32:02 -05:00
|
|
|
dir := strings.TrimSuffix(p.File.Dir(), helpers.FilePathSeparator)
|
|
|
|
_, name = filepath.Split(dir)
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return p.s.PathSpec.URLize(name), nil
|
2014-05-27 04:14:05 -04:00
|
|
|
}
|
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
// if the page has a slug, return the slug, else return the title
|
|
|
|
func pageToPermalinkSlugElseTitle(p *Page, a string) (string, error) {
|
|
|
|
if p.Slug != "" {
|
2014-10-02 18:25:52 -04:00
|
|
|
// Don't start or end with a -
|
2016-10-31 13:03:02 -04:00
|
|
|
// TODO(bep) this doesn't look good... Set the Slug once.
|
2014-10-02 18:25:52 -04:00
|
|
|
if strings.HasPrefix(p.Slug, "-") {
|
|
|
|
p.Slug = p.Slug[1:len(p.Slug)]
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(p.Slug, "-") {
|
|
|
|
p.Slug = p.Slug[0 : len(p.Slug)-1]
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
return p.s.PathSpec.URLize(p.Slug), nil
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
|
|
|
return pageToPermalinkTitle(p, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pageToPermalinkSection(p *Page, _ string) (string, error) {
|
2015-03-18 01:16:54 -04:00
|
|
|
// Page contains Node contains URLPath which has Section
|
2014-10-16 20:20:09 -04:00
|
|
|
return p.Section(), nil
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 13:14:29 -04:00
|
|
|
func pageToPermalinkSections(p *Page, _ string) (string, error) {
|
|
|
|
// TODO(bep) we have some superflous URLize in this file, but let's
|
|
|
|
// deal with that later.
|
2017-07-02 14:14:06 -04:00
|
|
|
return path.Join(p.CurrentSection().sections...), nil
|
2017-06-12 13:14:29 -04:00
|
|
|
}
|
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
func init() {
|
2016-03-24 22:12:03 -04:00
|
|
|
knownPermalinkAttributes = map[string]pageToPermaAttribute{
|
2013-11-18 04:35:56 -05:00
|
|
|
"year": pageToPermalinkDate,
|
|
|
|
"month": pageToPermalinkDate,
|
|
|
|
"monthname": pageToPermalinkDate,
|
|
|
|
"day": pageToPermalinkDate,
|
|
|
|
"weekday": pageToPermalinkDate,
|
|
|
|
"weekdayname": pageToPermalinkDate,
|
|
|
|
"yearday": pageToPermalinkDate,
|
|
|
|
"section": pageToPermalinkSection,
|
2017-06-12 13:14:29 -04:00
|
|
|
"sections": pageToPermalinkSections,
|
2013-11-18 04:35:56 -05:00
|
|
|
"title": pageToPermalinkTitle,
|
|
|
|
"slug": pageToPermalinkSlugElseTitle,
|
2014-05-27 04:14:05 -04:00
|
|
|
"filename": pageToPermalinkFilename,
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
2014-10-29 00:37:59 -04:00
|
|
|
|
2016-11-23 12:28:14 -05:00
|
|
|
attributeRegexp = regexp.MustCompile(`:\w+`)
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|