summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-10-24 13:45:30 +0200
committerGitHub <noreply@github.com>2016-10-24 13:45:30 +0200
commita10b2cd372798c4e4b862f0ec03010d2aea2ff1e (patch)
treef768c420aac0008e4d118709e13fda278a7588c5 /hugolib
parentdffd7da07c3fb198acfa6c4664b53132c4cabe55 (diff)
Avoid reading from Viper for path and URL funcs
The gain, given the "real sites benchmark" below, is obvious: ``` benchmark old ns/op new ns/op delta BenchmarkHugo-4 14497594101 13084156335 -9.75% benchmark old allocs new allocs delta BenchmarkHugo-4 57404335 48282002 -15.89% benchmark old bytes new bytes delta BenchmarkHugo-4 9933505624 9721984424 -2.13% ``` Fixes #2495
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/hugo_sites_test.go1
-rw-r--r--hugolib/node.go2
-rw-r--r--hugolib/page.go9
-rw-r--r--hugolib/pageSort_test.go5
-rw-r--r--hugolib/page_permalink_test.go6
-rw-r--r--hugolib/page_test.go3
-rw-r--r--hugolib/pagination.go6
-rw-r--r--hugolib/pagination_test.go4
-rw-r--r--hugolib/permalinks.go8
-rw-r--r--hugolib/permalinks_test.go4
-rw-r--r--hugolib/site.go83
-rw-r--r--hugolib/taxonomy.go2
12 files changed, 85 insertions, 48 deletions
diff --git a/hugolib/hugo_sites_test.go b/hugolib/hugo_sites_test.go
index b7a52b1e4..be2ba9803 100644
--- a/hugolib/hugo_sites_test.go
+++ b/hugolib/hugo_sites_test.go
@@ -35,6 +35,7 @@ func testCommonResetState() {
hugofs.InitMemFs()
viper.Reset()
viper.SetFs(hugofs.Source())
+ helpers.ResetConfigProvider()
loadDefaultSettings()
// Default is false, but true is easier to use as default in tests
diff --git a/hugolib/node.go b/hugolib/node.go
index eefb78bec..16c83e2e8 100644
--- a/hugolib/node.go
+++ b/hugolib/node.go
@@ -178,7 +178,7 @@ func (n *Node) URL() string {
}
func (n *Node) Permalink() string {
- return permalink(n.URL())
+ return n.Site.permalink(n.URL())
}
// Scratch returns the writable context associated with this Node.
diff --git a/hugolib/page.go b/hugolib/page.go
index fee7c334a..484425697 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -569,9 +569,9 @@ func (p *Page) analyzePage() {
func (p *Page) permalink() (*url.URL, error) {
baseURL := string(p.Site.BaseURL)
- dir := strings.TrimSpace(helpers.MakePath(filepath.ToSlash(strings.ToLower(p.Source.Dir()))))
- pSlug := strings.TrimSpace(helpers.URLize(p.Slug))
- pURL := strings.TrimSpace(helpers.URLize(p.URLPath.URL))
+ dir := strings.TrimSpace(p.Site.pathSpec.MakePath(filepath.ToSlash(strings.ToLower(p.Source.Dir()))))
+ pSlug := strings.TrimSpace(p.Site.pathSpec.URLize(p.Slug))
+ pURL := strings.TrimSpace(p.Site.pathSpec.URLize(p.URLPath.URL))
var permalink string
var err error
@@ -1171,5 +1171,6 @@ func (p *Page) TargetPath() (outfile string) {
outfile = helpers.ReplaceExtension(p.Source.TranslationBaseName(), p.Extension())
}
- return p.addLangFilepathPrefix(filepath.Join(strings.ToLower(helpers.MakePath(p.Source.Dir())), strings.TrimSpace(outfile)))
+ return p.addLangFilepathPrefix(filepath.Join(strings.ToLower(
+ p.Site.pathSpec.MakePath(p.Source.Dir())), strings.TrimSpace(outfile)))
}
diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go
index 23a3fd07c..738fcc80e 100644
--- a/hugolib/pageSort_test.go
+++ b/hugolib/pageSort_test.go
@@ -20,6 +20,7 @@ import (
"testing"
"time"
+ "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/stretchr/testify/assert"
)
@@ -134,6 +135,8 @@ func setSortVals(dates [3]time.Time, titles [3]string, weights [3]int, pages Pag
func createSortTestPages(num int) Pages {
pages := make(Pages, num)
+ info := newSiteInfo(siteBuilderCfg{baseURL: "http://base", language: helpers.NewDefaultLanguage()})
+
for i := 0; i < num; i++ {
pages[i] = &Page{
Node: Node{
@@ -141,7 +144,7 @@ func createSortTestPages(num int) Pages {
Section: "z",
URL: fmt.Sprintf("http://base/x/y/p%d.html", i),
},
- Site: newSiteInfoDefaultLanguage("http://base/"),
+ Site: &info,
},
Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
}
diff --git a/hugolib/page_permalink_test.go b/hugolib/page_permalink_test.go
index d185bebab..eb3a6c878 100644
--- a/hugolib/page_permalink_test.go
+++ b/hugolib/page_permalink_test.go
@@ -18,6 +18,7 @@ import (
"path/filepath"
"testing"
+ "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/spf13/viper"
)
@@ -59,17 +60,18 @@ func TestPermalink(t *testing.T) {
}
viper.Set("DefaultExtension", "html")
-
for i, test := range tests {
viper.Set("uglyurls", test.uglyURLs)
viper.Set("canonifyurls", test.canonifyURLs)
+ info := newSiteInfo(siteBuilderCfg{baseURL: string(test.base), language: helpers.NewDefaultLanguage()})
+
p := &Page{
Node: Node{
URLPath: URLPath{
Section: "z",
URL: test.url,
},
- Site: newSiteInfoDefaultLanguage(string(test.base)),
+ Site: &info,
},
Source: Source{File: *source.NewFile(filepath.FromSlash(test.file))},
}
diff --git a/hugolib/page_test.go b/hugolib/page_test.go
index 0bb924f01..342ff3bd5 100644
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -1122,7 +1122,8 @@ func TestPagePaths(t *testing.T) {
for _, test := range tests {
p, _ := NewPageFrom(strings.NewReader(test.content), filepath.FromSlash(test.path))
- p.Node.Site = newSiteInfoDefaultLanguage("")
+ info := newSiteInfo(siteBuilderCfg{language: helpers.NewDefaultLanguage()})
+ p.Node.Site = &info
if test.hasPermalink {
p.Node.Site.Permalinks = siteParmalinksSetting
diff --git a/hugolib/pagination.go b/hugolib/pagination.go
index 5bba5d89f..81f3e0cda 100644
--- a/hugolib/pagination.go
+++ b/hugolib/pagination.go
@@ -508,16 +508,16 @@ func newPaginator(elements []paginatedElement, total, size int, urlFactory pagin
}
func newPaginationURLFactory(pathElements ...string) paginationURLFactory {
- paginatePath := helpers.Config().GetString("paginatePath")
+ pathSpec := helpers.CurrentPathSpec()
return func(page int) string {
var rel string
if page == 1 {
rel = fmt.Sprintf("/%s/", path.Join(pathElements...))
} else {
- rel = fmt.Sprintf("/%s/%s/%d/", path.Join(pathElements...), paginatePath, page)
+ rel = fmt.Sprintf("/%s/%s/%d/", path.Join(pathElements...), pathSpec.PaginatePath(), page)
}
- return helpers.URLizeAndPrep(rel)
+ return pathSpec.URLizeAndPrep(rel)
}
}
diff --git a/hugolib/pagination_test.go b/hugolib/pagination_test.go
index 786650469..7bbc38058 100644
--- a/hugolib/pagination_test.go
+++ b/hugolib/pagination_test.go
@@ -19,6 +19,7 @@ import (
"path/filepath"
"testing"
+ "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/source"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
@@ -453,6 +454,7 @@ func TestPage(t *testing.T) {
func createTestPages(num int) Pages {
pages := make(Pages, num)
+ info := newSiteInfo(siteBuilderCfg{baseURL: "http://base/", language: helpers.NewDefaultLanguage()})
for i := 0; i < num; i++ {
pages[i] = &Page{
Node: Node{
@@ -460,7 +462,7 @@ func createTestPages(num int) Pages {
Section: "z",
URL: fmt.Sprintf("http://base/x/y/p%d.html", i),
},
- Site: newSiteInfoDefaultLanguage("http://base/"),
+ Site: &info,
},
Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))},
}
diff --git a/hugolib/permalinks.go b/hugolib/permalinks.go
index 581f8740f..c149ae13d 100644
--- a/hugolib/permalinks.go
+++ b/hugolib/permalinks.go
@@ -19,8 +19,6 @@ import (
"regexp"
"strconv"
"strings"
-
- "github.com/spf13/hugo/helpers"
)
// pathPattern represents a string which builds up a URL from attributes
@@ -152,14 +150,14 @@ func pageToPermalinkDate(p *Page, dateField string) (string, error) {
func pageToPermalinkTitle(p *Page, _ string) (string, error) {
// Page contains Node which has Title
// (also contains URLPath which has Slug, sometimes)
- return helpers.URLize(p.Title), nil
+ return p.Site.pathSpec.URLize(p.Title), nil
}
// pageToPermalinkFilename returns the URL-safe form of the filename
func pageToPermalinkFilename(p *Page, _ string) (string, error) {
//var extension = p.Source.Ext
//var name = p.Source.Path()[0 : len(p.Source.Path())-len(extension)]
- return helpers.URLize(p.Source.TranslationBaseName()), nil
+ return p.Site.pathSpec.URLize(p.Source.TranslationBaseName()), nil
}
// if the page has a slug, return the slug, else return the title
@@ -173,7 +171,7 @@ func pageToPermalinkSlugElseTitle(p *Page, a string) (string, error) {
if strings.HasSuffix(p.Slug, "-") {
p.Slug = p.Slug[0 : len(p.Slug)-1]
}
- return helpers.URLize(p.Slug), nil
+ return p.Site.pathSpec.URLize(p.Slug), nil
}
return pageToPermalinkTitle(p, a)
}
diff --git a/hugolib/permalinks_test.go b/hugolib/permalinks_test.go
index 672cf2d66..bdc96d41a 100644
--- a/hugolib/permalinks_test.go
+++ b/hugolib/permalinks_test.go
@@ -16,6 +16,8 @@ package hugolib
import (
"strings"
"testing"
+
+ "github.com/spf13/hugo/helpers"
)
// testdataPermalinks is used by a couple of tests; the expandsTo content is
@@ -70,6 +72,8 @@ func TestPermalinkValidation(t *testing.T) {
func TestPermalinkExpansion(t *testing.T) {
page, err := NewPageFrom(strings.NewReader(simplePageJSON), "blue/test-page.md")
+ info := newSiteInfo(siteBuilderCfg{language: helpers.NewDefaultLanguage()})
+ page.Site = &info
if err != nil {
t.Fatalf("failed before we began, could not parse SIMPLE_PAGE_JSON: %s", err)
}
diff --git a/hugolib/site.go b/hugolib/site.go
index f6a787298..7f96bb5aa 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -117,7 +117,8 @@ func (s *Site) reset() *Site {
// newSite creates a new site in the given language.
func newSite(lang *helpers.Language) *Site {
- return &Site{Language: lang, Info: SiteInfo{multilingual: newMultiLingualForLanguage(lang)}}
+ return &Site{Language: lang, Info: newSiteInfo(siteBuilderCfg{language: lang})}
+
}
// newSite creates a new site in the default language.
@@ -139,9 +140,12 @@ func newSiteFromSources(pathContentPairs ...string) *Site {
sources = append(sources, source.ByteSource{Name: filepath.FromSlash(path), Content: []byte(content)})
}
+ lang := helpers.NewDefaultLanguage()
+
return &Site{
Source: &source.InMemorySource{ByteSource: sources},
- Language: helpers.NewDefaultLanguage(),
+ Language: lang,
+ Info: newSiteInfo(siteBuilderCfg{language: lang}),
}
}
@@ -195,16 +199,25 @@ type SiteInfo struct {
LanguagePrefix string
Languages helpers.Languages
defaultContentLanguageInSubdir bool
+
+ pathSpec *helpers.PathSpec
}
// Used in tests.
-func newSiteInfoDefaultLanguage(baseURL string, pages ...*Page) *SiteInfo {
- ps := Pages(pages)
- return &SiteInfo{
- BaseURL: template.URL(baseURL),
- rawAllPages: &ps,
- multilingual: newMultiLingualDefaultLanguage(),
+type siteBuilderCfg struct {
+ language *helpers.Language
+ baseURL string
+
+ pages *Pages
+}
+
+func newSiteInfo(cfg siteBuilderCfg) SiteInfo {
+ return SiteInfo{
+ BaseURL: template.URL(cfg.baseURL),
+ rawAllPages: cfg.pages,
+ pathSpec: helpers.NewPathSpecFromConfig(cfg.language),
+ multilingual: newMultiLingualForLanguage(cfg.language),
}
}
@@ -808,7 +821,9 @@ func (s *Site) setCurrentLanguageConfig() error {
// There are sadly some global template funcs etc. that need the language information.
viper.Set("Multilingual", s.multilingualEnabled())
viper.Set("CurrentContentLanguage", s.Language)
- return tpl.SetTranslateLang(s.Language.Lang)
+ // Cache the current config.
+ helpers.InitConfigProviderForCurrentContentLanguage()
+ return tpl.SetTranslateLang(s.Language)
}
func (s *Site) render() (err error) {
@@ -887,7 +902,7 @@ func (s *SiteInfo) HomeAbsURL() string {
if s.IsMultiLingual() {
base = s.Language.Lang
}
- return helpers.AbsURL(base, false)
+ return s.pathSpec.AbsURL(base, false)
}
// SitemapAbsURL is a convenience method giving the absolute URL to the sitemap.
@@ -946,7 +961,6 @@ func (s *Site) initializeSiteInfo() {
Languages: languages,
defaultContentLanguageInSubdir: defaultContentInSubDir,
GoogleAnalytics: lang.GetString("GoogleAnalytics"),
- RSSLink: permalinkStr(lang.GetString("RSSUri")),
BuildDrafts: viper.GetBool("BuildDrafts"),
canonifyURLs: viper.GetBool("CanonifyURLs"),
preserveTaxonomyNames: lang.GetBool("PreserveTaxonomyNames"),
@@ -959,7 +973,10 @@ func (s *Site) initializeSiteInfo() {
Permalinks: permalinks,
Data: &s.Data,
owner: s.owner,
+ pathSpec: helpers.NewPathSpecFromConfig(lang),
}
+
+ s.Info.RSSLink = s.Info.permalinkStr(lang.GetString("RSSUri"))
}
func (s *Site) hasTheme() bool {
@@ -1407,7 +1424,7 @@ func (s *SiteInfo) createNodeMenuEntryURL(in string) string {
}
// make it match the nodes
menuEntryURL := in
- menuEntryURL = helpers.SanitizeURLKeepTrailingSlash(helpers.URLize(menuEntryURL))
+ menuEntryURL = helpers.SanitizeURLKeepTrailingSlash(s.pathSpec.URLize(menuEntryURL))
if !s.canonifyURLs {
menuEntryURL = helpers.AddContextRoot(string(s.BaseURL), menuEntryURL)
}
@@ -1586,13 +1603,13 @@ func (s *Site) renderAliases() error {
if s.owner.multilingual.enabled() {
mainLang := s.owner.multilingual.DefaultLang.Lang
if s.Info.defaultContentLanguageInSubdir {
- mainLangURL := helpers.AbsURL(mainLang, false)
+ mainLangURL := s.Info.pathSpec.AbsURL(mainLang, false)
jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)
if err := s.publishDestAlias(s.languageAliasTarget(), "/", mainLangURL, nil); err != nil {
return err
}
} else {
- mainLangURL := helpers.AbsURL("", false)
+ mainLangURL := s.Info.pathSpec.AbsURL("", false)
jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)
if err := s.publishDestAlias(s.languageAliasTarget(), mainLang, mainLangURL, nil); err != nil {
return err
@@ -1763,7 +1780,7 @@ func (s *Site) newTaxonomyNode(prepare bool, t taxRenderInfo, counter int) (*Nod
n := s.nodeLookup(fmt.Sprintf("tax-%s-%s", t.plural, key), counter, prepare)
if s.Info.preserveTaxonomyNames {
- key = helpers.MakePathSanitized(key)
+ key = s.Info.pathSpec.MakePathSanitized(key)
}
base := t.plural + "/" + key
@@ -1952,7 +1969,7 @@ func (s *Site) renderSectionLists(prepare bool) error {
[]string{"section/" + section + ".html", "_default/section.html", "_default/list.html", "indexes/" + section + ".html", "_default/indexes.html"})
if s.Info.preserveTaxonomyNames {
- section = helpers.MakePathSanitized(section)
+ section = s.Info.pathSpec.MakePathSanitized(section)
}
base := n.addLangPathPrefix(section)
@@ -1966,7 +1983,7 @@ func (s *Site) renderSectionLists(prepare bool) error {
paginatePath := helpers.Config().GetString("paginatePath")
// write alias for page 1
- s.writeDestAlias(helpers.PaginateAliasPath(base, 1), permalink(base), nil)
+ s.writeDestAlias(helpers.PaginateAliasPath(base, 1), s.Info.permalink(base), nil)
pagers := n.paginator.Pagers()
@@ -2111,6 +2128,15 @@ func (s *Site) newHomeNode(prepare bool, counter int) *Node {
return n
}
+func (s *Site) newPage() *Page {
+ page := &Page{}
+ page.language = s.Language
+ page.Date = s.Info.LastChange
+ page.Lastmod = s.Info.LastChange
+ page.Site = &s.Info
+ return page
+}
+
func (s *Site) renderSitemap() error {
if viper.GetBool("DisableSitemap") {
return nil
@@ -2123,11 +2149,7 @@ func (s *Site) renderSitemap() error {
// Prepend homepage to the list of pages
pages := make(Pages, 0)
- page := &Page{}
- page.language = s.Language
- page.Date = s.Info.LastChange
- page.Lastmod = s.Info.LastChange
- page.Site = &s.Info
+ page := s.newPage()
page.URLPath.URL = ""
page.Sitemap.ChangeFreq = sitemapDefault.ChangeFreq
page.Sitemap.Priority = sitemapDefault.Priority
@@ -2199,18 +2221,21 @@ func (s *Site) Stats() {
}
func (s *Site) setURLs(n *Node, in string) {
- n.URLPath.URL = helpers.URLizeAndPrep(in)
- n.URLPath.Permalink = permalink(n.URLPath.URL)
- n.RSSLink = template.HTML(permalink(in + ".xml"))
+ n.URLPath.URL = s.Info.pathSpec.URLizeAndPrep(in)
+ n.URLPath.Permalink = s.Info.permalink(n.URLPath.URL)
+ n.RSSLink = template.HTML(s.Info.permalink(in + ".xml"))
}
-func permalink(plink string) string {
- return permalinkStr(plink)
+func (s *SiteInfo) permalink(plink string) string {
+ return s.permalinkStr(plink)
}
-func permalinkStr(plink string) string {
- return helpers.MakePermalink(viper.GetString("BaseURL"), helpers.URLizeAndPrep(plink)).String()
+func (s *SiteInfo) permalinkStr(plink string) string {
+ return helpers.MakePermalink(
+ viper.GetString("BaseURL"),
+ s.pathSpec.URLizeAndPrep(plink)).String()
}
+
func (s *Site) newNode(nodeID string) *Node {
return s.nodeLookup(nodeID, 0, true)
}
diff --git a/hugolib/taxonomy.go b/hugolib/taxonomy.go
index ffe586ffc..112d22b37 100644
--- a/hugolib/taxonomy.go
+++ b/hugolib/taxonomy.go
@@ -52,7 +52,7 @@ type OrderedTaxonomyEntry struct {
// KeyPrep... Taxonomies should be case insensitive. Can make it easily conditional later.
func kp(in string) string {
- return helpers.MakePathSanitized(in)
+ return helpers.CurrentPathSpec().MakePathSanitized(in)
}
// Get the weighted pages for the given key.