2017-04-09 04:33:04 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
2013-07-04 11:32:55 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2013-07-04 11:32:55 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-04 11:32:55 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2014-09-14 07:23:03 -04:00
|
|
|
"errors"
|
2014-01-29 17:50:31 -05:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
2017-04-04 12:05:19 -04:00
|
|
|
"mime"
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
"net/url"
|
2014-01-29 17:50:31 -05:00
|
|
|
"os"
|
2014-12-27 08:11:19 -05:00
|
|
|
"path/filepath"
|
2017-04-28 03:40:50 -04:00
|
|
|
"sort"
|
2014-08-29 13:40:21 -04:00
|
|
|
"strconv"
|
2014-01-29 17:50:31 -05:00
|
|
|
"strings"
|
2014-03-05 19:07:39 -05:00
|
|
|
"sync"
|
2014-01-29 17:50:31 -05:00
|
|
|
"time"
|
2014-03-31 13:23:34 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2017-04-04 03:12:33 -04:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2017-04-04 03:12:33 -04:00
|
|
|
|
2017-08-01 06:17:51 -04:00
|
|
|
"github.com/markbates/inflect"
|
2016-11-16 15:06:10 -05:00
|
|
|
|
2015-01-30 14:51:06 -05:00
|
|
|
"sync/atomic"
|
|
|
|
|
2016-04-20 10:28:26 -04:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2017-06-13 12:42:45 -04:00
|
|
|
bp "github.com/gohugoio/hugo/bufferpool"
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/output"
|
|
|
|
"github.com/gohugoio/hugo/parser"
|
2017-08-19 07:16:00 -04:00
|
|
|
"github.com/gohugoio/hugo/related"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/source"
|
|
|
|
"github.com/gohugoio/hugo/tpl"
|
|
|
|
"github.com/gohugoio/hugo/transform"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cast"
|
2014-03-31 13:23:34 -04:00
|
|
|
"github.com/spf13/nitro"
|
2014-04-05 01:26:43 -04:00
|
|
|
"github.com/spf13/viper"
|
2013-07-04 11:32:55 -04:00
|
|
|
)
|
|
|
|
|
2013-11-05 00:28:08 -05:00
|
|
|
var _ = transform.AbsURL
|
|
|
|
|
2016-03-09 08:05:31 -05:00
|
|
|
// used to indicate if run as a test.
|
|
|
|
var testMode bool
|
|
|
|
|
2016-03-23 04:43:04 -04:00
|
|
|
var defaultTimer *nitro.B
|
2013-08-07 20:21:22 -04:00
|
|
|
|
2013-10-17 23:57:25 -04:00
|
|
|
// Site contains all the information relevant for constructing a static
|
2013-09-01 00:13:04 -04:00
|
|
|
// site. The basic flow of information is as follows:
|
|
|
|
//
|
|
|
|
// 1. A list of Files is parsed and then converted into Pages.
|
|
|
|
//
|
|
|
|
// 2. Pages contain sections (based on the file they were generated from),
|
|
|
|
// aliases and slugs (included in a pages frontmatter) which are the
|
2013-11-18 04:35:56 -05:00
|
|
|
// various targets that will get generated. There will be canonical
|
|
|
|
// listing. The canonical path can be overruled based on a pattern.
|
2013-09-01 00:13:04 -04:00
|
|
|
//
|
2014-04-08 23:15:57 -04:00
|
|
|
// 3. Taxonomies are created via configuration and will present some aspect of
|
2013-09-01 00:13:04 -04:00
|
|
|
// the final page and typically a perm url.
|
|
|
|
//
|
|
|
|
// 4. All Pages are passed through a template based on their desired
|
2013-11-18 04:35:56 -05:00
|
|
|
// layout based on numerous different elements.
|
2013-09-01 00:13:04 -04:00
|
|
|
//
|
|
|
|
// 5. The entire collection of files is written to disk.
|
2013-07-04 11:32:55 -04:00
|
|
|
type Site struct {
|
2016-08-09 14:06:15 -04:00
|
|
|
owner *HugoSites
|
|
|
|
|
2016-11-03 19:34:25 -04:00
|
|
|
*PageCollections
|
|
|
|
|
|
|
|
Files []*source.File
|
|
|
|
Taxonomies TaxonomyList
|
2016-10-31 14:53:33 -04:00
|
|
|
|
|
|
|
// Plural is what we get in the folder, so keep track of this mapping
|
|
|
|
// to get the singular form from that value.
|
|
|
|
taxonomiesPluralSingular map[string]string
|
|
|
|
|
2017-06-13 12:47:17 -04:00
|
|
|
// This is temporary, see https://github.com/gohugoio/hugo/issues/2835
|
2016-12-26 13:30:57 -05:00
|
|
|
// Maps "actors-gerard-depardieu" to "Gérard Depardieu" when preserveTaxonomyNames
|
|
|
|
// is set.
|
|
|
|
taxonomiesOrigKey map[string]string
|
|
|
|
|
2017-03-03 04:47:43 -05:00
|
|
|
Source source.Input
|
|
|
|
Sections Taxonomy
|
|
|
|
Info SiteInfo
|
|
|
|
Menus Menus
|
|
|
|
timer *nitro.B
|
|
|
|
|
2017-03-06 07:18:33 -05:00
|
|
|
layoutHandler *output.LayoutHandler
|
|
|
|
|
2017-03-03 04:47:43 -05:00
|
|
|
draftCount int
|
|
|
|
futureCount int
|
|
|
|
expiredCount int
|
|
|
|
Data map[string]interface{}
|
|
|
|
Language *helpers.Language
|
2017-01-03 11:28:51 -05:00
|
|
|
|
2017-02-18 04:02:12 -05:00
|
|
|
disabledKinds map[string]bool
|
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
// Output formats defined in site config per Page Kind, or some defaults
|
|
|
|
// if not set.
|
|
|
|
// Output formats defined in Page front matter will override these.
|
|
|
|
outputFormats map[string]output.Formats
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-04-04 03:12:33 -04:00
|
|
|
// All the output formats and media types available for this site.
|
|
|
|
// These values will be merged from the Hugo defaults, the site config and,
|
|
|
|
// finally, the language settings.
|
|
|
|
outputFormatsConfig output.Formats
|
|
|
|
mediaTypesConfig media.Types
|
|
|
|
|
2017-04-28 03:40:50 -04:00
|
|
|
// We render each site for all the relevant output formats in serial with
|
|
|
|
// this rendering context pointing to the current one.
|
|
|
|
rc *siteRenderingContext
|
|
|
|
|
|
|
|
// The output formats that we need to render this site in. This slice
|
|
|
|
// will be fixed once set.
|
|
|
|
// This will be the union of Site.Pages' outputFormats.
|
|
|
|
// This slice will be sorted.
|
|
|
|
renderFormats output.Formats
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
// Logger etc.
|
2017-01-10 04:55:03 -05:00
|
|
|
*deps.Deps `json:"-"`
|
2017-03-24 04:19:51 -04:00
|
|
|
|
2017-07-30 11:46:04 -04:00
|
|
|
// The func used to title case titles.
|
|
|
|
titleFunc func(s string) string
|
|
|
|
|
2017-08-19 07:16:00 -04:00
|
|
|
relatedDocsHandler *relatedDocsHandler
|
|
|
|
|
2017-03-24 04:19:51 -04:00
|
|
|
siteStats *siteStats
|
|
|
|
}
|
|
|
|
|
2017-04-28 03:40:50 -04:00
|
|
|
type siteRenderingContext struct {
|
|
|
|
output.Format
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) initRenderFormats() {
|
|
|
|
formatSet := make(map[string]bool)
|
|
|
|
formats := output.Formats{}
|
|
|
|
for _, p := range s.Pages {
|
|
|
|
for _, f := range p.outputFormats {
|
|
|
|
if !formatSet[f.Name] {
|
|
|
|
formats = append(formats, f)
|
|
|
|
formatSet[f.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(formats)
|
|
|
|
s.renderFormats = formats
|
|
|
|
}
|
|
|
|
|
2017-03-24 04:19:51 -04:00
|
|
|
type siteStats struct {
|
|
|
|
pageCount int
|
|
|
|
pageCountRegular int
|
2016-07-26 04:24:27 -04:00
|
|
|
}
|
|
|
|
|
2017-02-18 04:02:12 -05:00
|
|
|
func (s *Site) isEnabled(kind string) bool {
|
|
|
|
if kind == kindUnknown {
|
|
|
|
panic("Unknown kind")
|
|
|
|
}
|
|
|
|
return !s.disabledKinds[kind]
|
|
|
|
}
|
|
|
|
|
2016-08-07 18:12:06 -04:00
|
|
|
// reset returns a new Site prepared for rebuild.
|
|
|
|
func (s *Site) reset() *Site {
|
2017-03-09 13:19:29 -05:00
|
|
|
return &Site{Deps: s.Deps,
|
2017-04-04 03:12:33 -04:00
|
|
|
layoutHandler: output.NewLayoutHandler(s.PathSpec.ThemeSet()),
|
|
|
|
disabledKinds: s.disabledKinds,
|
2017-07-30 11:46:04 -04:00
|
|
|
titleFunc: s.titleFunc,
|
2017-08-19 07:16:00 -04:00
|
|
|
relatedDocsHandler: newSearchIndexHandler(s.relatedDocsHandler.cfg),
|
2017-04-04 03:12:33 -04:00
|
|
|
outputFormats: s.outputFormats,
|
|
|
|
outputFormatsConfig: s.outputFormatsConfig,
|
|
|
|
mediaTypesConfig: s.mediaTypesConfig,
|
|
|
|
Language: s.Language,
|
|
|
|
owner: s.owner,
|
|
|
|
PageCollections: newPageCollections()}
|
2016-07-26 04:24:27 -04:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
// newSite creates a new site with the given configuration.
|
|
|
|
func newSite(cfg deps.DepsCfg) (*Site, error) {
|
2016-11-03 19:34:25 -04:00
|
|
|
c := newPageCollections()
|
2017-01-09 19:36:59 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
if cfg.Language == nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
cfg.Language = helpers.NewDefaultLanguage(cfg.Cfg)
|
2017-01-09 19:36:59 -05:00
|
|
|
}
|
|
|
|
|
2017-02-18 04:02:12 -05:00
|
|
|
disabledKinds := make(map[string]bool)
|
|
|
|
for _, disabled := range cast.ToStringSlice(cfg.Language.Get("disableKinds")) {
|
|
|
|
disabledKinds[disabled] = true
|
|
|
|
}
|
|
|
|
|
2017-04-04 03:12:33 -04:00
|
|
|
var (
|
|
|
|
mediaTypesConfig []map[string]interface{}
|
|
|
|
outputFormatsConfig []map[string]interface{}
|
|
|
|
|
|
|
|
siteOutputFormatsConfig output.Formats
|
|
|
|
siteMediaTypesConfig media.Types
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add language last, if set, so it gets precedence.
|
|
|
|
for _, cfg := range []config.Provider{cfg.Cfg, cfg.Language} {
|
|
|
|
if cfg.IsSet("mediaTypes") {
|
|
|
|
mediaTypesConfig = append(mediaTypesConfig, cfg.GetStringMap("mediaTypes"))
|
|
|
|
}
|
|
|
|
if cfg.IsSet("outputFormats") {
|
|
|
|
outputFormatsConfig = append(outputFormatsConfig, cfg.GetStringMap("outputFormats"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
siteMediaTypesConfig, err = media.DecodeTypes(mediaTypesConfig...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
siteOutputFormatsConfig, err = output.DecodeFormats(siteMediaTypesConfig, outputFormatsConfig...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-22 06:34:17 -04:00
|
|
|
|
2017-04-04 03:12:33 -04:00
|
|
|
outputFormats, err := createSiteOutputFormats(siteOutputFormatsConfig, cfg.Language)
|
2017-03-22 06:34:17 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-08-19 07:16:00 -04:00
|
|
|
var relatedContentConfig related.Config
|
|
|
|
|
|
|
|
if cfg.Language.IsSet("related") {
|
|
|
|
relatedContentConfig, err = related.DecodeConfig(cfg.Language.Get("related"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
relatedContentConfig = related.DefaultConfig
|
|
|
|
taxonomies := cfg.Language.GetStringMapString("taxonomies")
|
|
|
|
if _, found := taxonomies["tag"]; found {
|
|
|
|
relatedContentConfig.Add(related.IndexConfig{Name: "tags", Weight: 80})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 11:46:04 -04:00
|
|
|
titleFunc := helpers.GetTitleFunc(cfg.Language.GetString("titleCaseStyle"))
|
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
s := &Site{
|
2017-04-04 03:12:33 -04:00
|
|
|
PageCollections: c,
|
|
|
|
layoutHandler: output.NewLayoutHandler(cfg.Cfg.GetString("themesDir") != ""),
|
|
|
|
Language: cfg.Language,
|
|
|
|
disabledKinds: disabledKinds,
|
2017-07-30 11:46:04 -04:00
|
|
|
titleFunc: titleFunc,
|
2017-08-19 07:16:00 -04:00
|
|
|
relatedDocsHandler: newSearchIndexHandler(relatedContentConfig),
|
2017-04-04 03:12:33 -04:00
|
|
|
outputFormats: outputFormats,
|
|
|
|
outputFormatsConfig: siteOutputFormatsConfig,
|
|
|
|
mediaTypesConfig: siteMediaTypesConfig,
|
2017-03-09 13:19:29 -05:00
|
|
|
}
|
2016-10-24 07:45:30 -04:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Info = newSiteInfo(siteBuilderCfg{s: s, pageCollections: c, language: s.Language})
|
2017-03-03 04:47:43 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
return s, nil
|
2016-07-26 04:24:27 -04:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
// NewSite creates a new site with the given dependency configuration.
|
|
|
|
// The site will have a template system loaded and ready to use.
|
|
|
|
// Note: This is mainly used in single site tests.
|
|
|
|
func NewSite(cfg deps.DepsCfg) (*Site, error) {
|
|
|
|
s, err := newSite(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-01 17:04:44 -04:00
|
|
|
}
|
|
|
|
|
2017-02-19 17:53:48 -05:00
|
|
|
if err = applyDepsIfNeeded(cfg, s); err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSiteDefaultLang creates a new site in the default language.
|
|
|
|
// The site will have a template system loaded and ready to use.
|
|
|
|
// Note: This is mainly used in single site tests.
|
2017-03-27 14:43:49 -04:00
|
|
|
func NewSiteDefaultLang(withTemplate ...func(templ tpl.TemplateHandler) error) (*Site, error) {
|
2017-02-04 22:20:06 -05:00
|
|
|
v := viper.New()
|
2017-09-25 02:59:02 -04:00
|
|
|
if err := loadDefaultSettingsFor(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
return newSiteForLang(helpers.NewDefaultLanguage(v), withTemplate...)
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2017-02-15 04:00:34 -05:00
|
|
|
// NewEnglishSite creates a new site in English language.
|
2017-01-10 04:55:03 -05:00
|
|
|
// The site will have a template system loaded and ready to use.
|
|
|
|
// Note: This is mainly used in single site tests.
|
2017-03-27 14:43:49 -04:00
|
|
|
func NewEnglishSite(withTemplate ...func(templ tpl.TemplateHandler) error) (*Site, error) {
|
2017-02-04 22:20:06 -05:00
|
|
|
v := viper.New()
|
2017-09-25 02:59:02 -04:00
|
|
|
if err := loadDefaultSettingsFor(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
return newSiteForLang(helpers.NewLanguage("en", v), withTemplate...)
|
2017-02-15 04:00:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newSiteForLang creates a new site in the given language.
|
2017-03-27 14:43:49 -04:00
|
|
|
func newSiteForLang(lang *helpers.Language, withTemplate ...func(templ tpl.TemplateHandler) error) (*Site, error) {
|
|
|
|
withTemplates := func(templ tpl.TemplateHandler) error {
|
2017-01-10 04:55:03 -05:00
|
|
|
for _, wt := range withTemplate {
|
|
|
|
if err := wt(templ); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-08-01 17:04:44 -04:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2017-03-06 13:54:46 -05:00
|
|
|
cfg := deps.DepsCfg{WithTemplate: withTemplates, Language: lang, Cfg: lang}
|
2017-02-15 04:00:34 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
return NewSiteForCfg(cfg)
|
2017-02-15 04:00:34 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// NewSiteForCfg creates a new site for the given configuration.
|
|
|
|
// The site will have a template system loaded and ready to use.
|
|
|
|
// Note: This is mainly used in single site tests.
|
|
|
|
func NewSiteForCfg(cfg deps.DepsCfg) (*Site, error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
s, err := newSite(cfg)
|
2016-08-01 17:04:44 -04:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-24 07:45:30 -04:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := applyDepsIfNeeded(cfg, s); err != nil {
|
|
|
|
return nil, err
|
2016-08-01 17:04:44 -04:00
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
return s, nil
|
2016-08-01 17:04:44 -04:00
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
type SiteInfo struct {
|
2016-09-08 10:17:28 -04:00
|
|
|
// atomic requires 64-bit alignment for struct field access
|
2016-09-09 08:31:49 -04:00
|
|
|
// According to the docs, " The first word in a global variable or in an
|
|
|
|
// allocated struct or slice can be relied upon to be 64-bit aligned."
|
|
|
|
// Moving paginationPageCount to the top of this struct didn't do the
|
|
|
|
// magic, maybe due to the way SiteInfo is embedded.
|
|
|
|
// Adding the 4 byte padding below does the trick.
|
|
|
|
_ [4]byte
|
|
|
|
paginationPageCount uint64
|
|
|
|
|
2016-11-03 19:34:25 -04:00
|
|
|
Taxonomies TaxonomyList
|
|
|
|
Authors AuthorList
|
|
|
|
Social SiteSocial
|
|
|
|
*PageCollections
|
2016-08-07 18:12:06 -04:00
|
|
|
Files *[]*source.File
|
|
|
|
Menus *Menus
|
|
|
|
Hugo *HugoInfo
|
|
|
|
Title string
|
|
|
|
RSSLink string
|
2016-09-18 13:10:11 -04:00
|
|
|
Author map[string]interface{}
|
2015-05-31 14:30:53 -04:00
|
|
|
LanguageCode string
|
|
|
|
DisqusShortname string
|
2015-10-19 07:31:03 -04:00
|
|
|
GoogleAnalytics string
|
2015-05-31 14:30:53 -04:00
|
|
|
Copyright string
|
|
|
|
LastChange time.Time
|
|
|
|
Permalinks PermalinkOverrides
|
|
|
|
Params map[string]interface{}
|
|
|
|
BuildDrafts bool
|
|
|
|
canonifyURLs bool
|
2017-02-20 03:52:06 -05:00
|
|
|
relativeURLs bool
|
2017-03-09 13:19:29 -05:00
|
|
|
uglyURLs bool
|
2015-05-31 14:30:53 -04:00
|
|
|
preserveTaxonomyNames bool
|
|
|
|
Data *map[string]interface{}
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2016-08-09 14:06:15 -04:00
|
|
|
owner *HugoSites
|
2017-01-10 04:55:03 -05:00
|
|
|
s *Site
|
2016-08-08 07:55:18 -04:00
|
|
|
multilingual *Multilingual
|
|
|
|
Language *helpers.Language
|
|
|
|
LanguagePrefix string
|
|
|
|
Languages helpers.Languages
|
|
|
|
defaultContentLanguageInSubdir bool
|
2016-11-23 16:06:15 -05:00
|
|
|
sectionPagesMenu string
|
2014-04-23 02:52:01 -04:00
|
|
|
}
|
2014-04-23 02:55:43 -04:00
|
|
|
|
2016-11-13 08:27:10 -05:00
|
|
|
func (s *SiteInfo) String() string {
|
|
|
|
return fmt.Sprintf("Site(%q)", s.Title)
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:33:28 -04:00
|
|
|
func (s *SiteInfo) BaseURL() template.URL {
|
|
|
|
return template.URL(s.s.PathSpec.BaseURL.String())
|
|
|
|
}
|
|
|
|
|
2017-11-12 04:03:56 -05:00
|
|
|
// ServerPort returns the port part of the BaseURL, 0 if none found.
|
|
|
|
func (s *SiteInfo) ServerPort() int {
|
|
|
|
ps := s.s.PathSpec.BaseURL.URL().Port()
|
|
|
|
if ps == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
p, err := strconv.Atoi(ps)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2016-08-07 08:03:03 -04:00
|
|
|
// Used in tests.
|
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
type siteBuilderCfg struct {
|
2017-02-04 22:20:06 -05:00
|
|
|
language *helpers.Language
|
2017-01-10 04:55:03 -05:00
|
|
|
s *Site
|
2016-11-03 19:34:25 -04:00
|
|
|
pageCollections *PageCollections
|
|
|
|
baseURL string
|
2016-10-24 07:45:30 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// TODO(bep) get rid of this
|
2016-10-24 07:45:30 -04:00
|
|
|
func newSiteInfo(cfg siteBuilderCfg) SiteInfo {
|
|
|
|
return SiteInfo{
|
2017-01-10 04:55:03 -05:00
|
|
|
s: cfg.s,
|
2016-11-03 19:34:25 -04:00
|
|
|
multilingual: newMultiLingualForLanguage(cfg.language),
|
|
|
|
PageCollections: cfg.pageCollections,
|
2017-02-04 22:20:06 -05:00
|
|
|
Params: make(map[string]interface{}),
|
2016-08-07 08:03:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 13:33:55 -05:00
|
|
|
// SiteSocial is a place to put social details on a site level. These are the
|
|
|
|
// standard keys that themes will expect to have available, but can be
|
|
|
|
// expanded to any others on a per site basis
|
|
|
|
// github
|
|
|
|
// facebook
|
|
|
|
// facebook_admin
|
|
|
|
// twitter
|
|
|
|
// twitter_domain
|
|
|
|
// googleplus
|
|
|
|
// pinterest
|
|
|
|
// instagram
|
|
|
|
// youtube
|
|
|
|
// linkedin
|
|
|
|
type SiteSocial map[string]string
|
|
|
|
|
2017-02-19 17:53:48 -05:00
|
|
|
// Param is a convenience method to do lookups in SiteInfo's Params map.
|
2016-10-17 13:30:21 -04:00
|
|
|
//
|
|
|
|
// This method is also implemented on Page and Node.
|
|
|
|
func (s *SiteInfo) Param(key interface{}) (interface{}, error) {
|
|
|
|
keyStr, err := cast.ToStringE(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keyStr = strings.ToLower(keyStr)
|
|
|
|
return s.Params[keyStr], nil
|
|
|
|
}
|
|
|
|
|
2016-08-07 08:03:03 -04:00
|
|
|
func (s *SiteInfo) IsMultiLingual() bool {
|
|
|
|
return len(s.Languages) > 1
|
|
|
|
}
|
|
|
|
|
2017-04-04 12:14:41 -04:00
|
|
|
func (s *SiteInfo) refLink(ref string, page *Page, relative bool, outputFormat string) (string, error) {
|
2015-03-11 13:34:57 -04:00
|
|
|
var refURL *url.URL
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
var err error
|
|
|
|
|
2017-07-15 05:05:14 -04:00
|
|
|
ref = filepath.ToSlash(ref)
|
|
|
|
ref = strings.TrimPrefix(ref, "/")
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
refURL, err = url.Parse(ref)
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-03-07 06:53:20 -05:00
|
|
|
var target *Page
|
|
|
|
var link string
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
if refURL.Path != "" {
|
2017-05-26 03:12:19 -04:00
|
|
|
target := s.getPage(KindPage, refURL.Path)
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
|
|
|
|
if target == nil {
|
2015-03-11 13:34:57 -04:00
|
|
|
return "", fmt.Errorf("No page found with path or logical name \"%s\".\n", refURL.Path)
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
}
|
|
|
|
|
2017-04-04 12:14:41 -04:00
|
|
|
var permalinker Permalinker = target
|
|
|
|
|
|
|
|
if outputFormat != "" {
|
|
|
|
o := target.OutputFormats().Get(outputFormat)
|
|
|
|
|
|
|
|
if o == nil {
|
|
|
|
return "", fmt.Errorf("Output format %q not found for page %q", outputFormat, refURL.Path)
|
|
|
|
}
|
|
|
|
permalinker = o
|
|
|
|
}
|
|
|
|
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
if relative {
|
2017-04-04 12:14:41 -04:00
|
|
|
link = permalinker.RelPermalink()
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
} else {
|
2017-04-04 12:14:41 -04:00
|
|
|
link = permalinker.Permalink()
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
if refURL.Fragment != "" {
|
|
|
|
link = link + "#" + refURL.Fragment
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
if refURL.Path != "" && target != nil && !target.getRenderingConfig().PlainIDAnchors {
|
|
|
|
link = link + ":" + target.UniqueID()
|
|
|
|
} else if page != nil && !page.getRenderingConfig().PlainIDAnchors {
|
|
|
|
link = link + ":" + page.UniqueID()
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:28:48 -05:00
|
|
|
// Ref will give an absolute URL to ref in the given Page.
|
2017-04-04 12:14:41 -04:00
|
|
|
func (s *SiteInfo) Ref(ref string, page *Page, options ...string) (string, error) {
|
|
|
|
outputFormat := ""
|
|
|
|
if len(options) > 0 {
|
|
|
|
outputFormat = options[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.refLink(ref, page, false, outputFormat)
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
}
|
|
|
|
|
2016-01-15 18:28:48 -05:00
|
|
|
// RelRef will give an relative URL to ref in the given Page.
|
2017-04-04 12:14:41 -04:00
|
|
|
func (s *SiteInfo) RelRef(ref string, page *Page, options ...string) (string, error) {
|
|
|
|
outputFormat := ""
|
|
|
|
if len(options) > 0 {
|
|
|
|
outputFormat = options[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.refLink(ref, page, true, outputFormat)
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
}
|
|
|
|
|
2014-12-27 08:11:19 -05:00
|
|
|
func (s *SiteInfo) addToPaginationPageCount(cnt uint64) {
|
|
|
|
atomic.AddUint64(&s.paginationPageCount, cnt)
|
|
|
|
}
|
|
|
|
|
2013-10-25 18:03:14 -04:00
|
|
|
type runmode struct {
|
2014-01-29 17:50:31 -05:00
|
|
|
Watching bool
|
2013-10-25 18:03:14 -04:00
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
func (s *Site) running() bool {
|
2016-08-08 04:12:39 -04:00
|
|
|
return s.owner.runMode.Watching
|
2013-10-25 18:03:14 -04:00
|
|
|
}
|
|
|
|
|
2013-10-03 09:44:45 -04:00
|
|
|
func init() {
|
2016-03-23 04:43:04 -04:00
|
|
|
defaultTimer = nitro.Initalize()
|
2013-10-03 09:44:45 -04:00
|
|
|
}
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
func (s *Site) timerStep(step string) {
|
2014-01-29 17:50:31 -05:00
|
|
|
if s.timer == nil {
|
2016-03-23 04:43:04 -04:00
|
|
|
s.timer = defaultTimer
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
s.timer.Step(step)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2016-08-12 18:33:17 -04:00
|
|
|
type whatChanged struct {
|
|
|
|
source bool
|
|
|
|
other bool
|
|
|
|
}
|
|
|
|
|
2017-04-04 12:05:19 -04:00
|
|
|
// RegisterMediaTypes will register the Site's media types in the mime
|
|
|
|
// package, so it will behave correctly with Hugo's built-in server.
|
|
|
|
func (s *Site) RegisterMediaTypes() {
|
|
|
|
for _, mt := range s.mediaTypesConfig {
|
|
|
|
// The last one will win if there are any duplicates.
|
2017-04-06 11:39:20 -04:00
|
|
|
_ = mime.AddExtensionType("."+mt.Suffix, mt.Type()+"; charset=utf-8")
|
2017-04-04 12:05:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:05:16 -04:00
|
|
|
// reBuild partially rebuilds a site given the filesystem events.
|
2016-07-28 03:30:58 -04:00
|
|
|
// It returns whetever the content source was changed.
|
2016-11-10 14:55:52 -05:00
|
|
|
func (s *Site) reProcess(events []fsnotify.Event) (whatChanged, error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.DEBUG.Printf("Rebuild for events %q", events)
|
2016-07-26 08:44:37 -04:00
|
|
|
|
2015-12-22 00:10:01 -05:00
|
|
|
s.timerStep("initialize rebuild")
|
2016-07-26 08:44:37 -04:00
|
|
|
|
2015-12-22 00:10:01 -05:00
|
|
|
// First we need to determine what changed
|
|
|
|
|
2016-01-11 12:06:52 -05:00
|
|
|
sourceChanged := []fsnotify.Event{}
|
2016-07-28 03:30:58 -04:00
|
|
|
sourceReallyChanged := []fsnotify.Event{}
|
2016-01-11 12:06:52 -05:00
|
|
|
tmplChanged := []fsnotify.Event{}
|
|
|
|
dataChanged := []fsnotify.Event{}
|
2016-07-28 03:30:58 -04:00
|
|
|
i18nChanged := []fsnotify.Event{}
|
2017-03-10 14:54:50 -05:00
|
|
|
shortcodesChanged := make(map[string]bool)
|
2016-01-28 09:31:25 -05:00
|
|
|
// prevent spamming the log on changes
|
|
|
|
logger := helpers.NewDistinctFeedbackLogger()
|
2017-04-12 04:07:08 -04:00
|
|
|
seen := make(map[fsnotify.Event]bool)
|
2016-01-28 09:31:25 -05:00
|
|
|
|
2016-01-11 12:06:52 -05:00
|
|
|
for _, ev := range events {
|
2017-04-12 04:07:08 -04:00
|
|
|
// Avoid processing the same event twice.
|
|
|
|
if seen[ev] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[ev] = true
|
|
|
|
|
2016-07-10 13:37:27 -04:00
|
|
|
if s.isContentDirEvent(ev) {
|
2017-06-08 14:00:05 -04:00
|
|
|
logger.Println("Source changed", ev)
|
2016-01-11 12:06:52 -05:00
|
|
|
sourceChanged = append(sourceChanged, ev)
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
2016-09-11 13:47:12 -04:00
|
|
|
if s.isLayoutDirEvent(ev) {
|
2017-06-08 14:00:05 -04:00
|
|
|
logger.Println("Template changed", ev)
|
2016-01-11 12:06:52 -05:00
|
|
|
tmplChanged = append(tmplChanged, ev)
|
2017-03-10 14:54:50 -05:00
|
|
|
|
|
|
|
if strings.Contains(ev.Name, "shortcodes") {
|
|
|
|
clearIsInnerShortcodeCache()
|
|
|
|
shortcode := filepath.Base(ev.Name)
|
|
|
|
shortcode = strings.TrimSuffix(shortcode, filepath.Ext(shortcode))
|
|
|
|
shortcodesChanged[shortcode] = true
|
|
|
|
}
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
2016-07-10 13:37:27 -04:00
|
|
|
if s.isDataDirEvent(ev) {
|
2017-06-08 14:00:05 -04:00
|
|
|
logger.Println("Data changed", ev)
|
2016-01-28 09:31:25 -05:00
|
|
|
dataChanged = append(dataChanged, ev)
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
2016-09-11 13:47:12 -04:00
|
|
|
if s.isI18nEvent(ev) {
|
2017-06-08 14:00:05 -04:00
|
|
|
logger.Println("i18n changed", ev)
|
2016-07-28 03:30:58 -04:00
|
|
|
i18nChanged = append(dataChanged, ev)
|
|
|
|
}
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if len(tmplChanged) > 0 || len(i18nChanged) > 0 {
|
2017-01-10 04:55:03 -05:00
|
|
|
sites := s.owner.Sites
|
|
|
|
first := sites[0]
|
|
|
|
|
|
|
|
// TOD(bep) globals clean
|
2017-02-04 22:20:06 -05:00
|
|
|
if err := first.Deps.LoadResources(); err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
s.TemplateHandler().PrintErrors()
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
for i := 1; i < len(sites); i++ {
|
|
|
|
site := sites[i]
|
|
|
|
var err error
|
|
|
|
site.Deps, err = first.Deps.ForLanguage(site.Language)
|
|
|
|
if err != nil {
|
|
|
|
return whatChanged{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:10:01 -05:00
|
|
|
s.timerStep("template prep")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dataChanged) > 0 {
|
2017-04-06 11:39:20 -04:00
|
|
|
if err := s.readDataFromSourceFS(); err != nil {
|
|
|
|
s.Log.ERROR.Println(err)
|
|
|
|
}
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
|
|
|
|
2016-01-08 18:37:37 -05:00
|
|
|
// If a content file changes, we need to reload only it and re-render the entire site.
|
2016-01-07 21:48:13 -05:00
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
// First step is to read the changed files and (re)place them in site.AllPages
|
2016-03-17 10:51:52 -04:00
|
|
|
// This includes processing any meta-data for that content
|
|
|
|
|
|
|
|
// The second step is to convert the content into HTML
|
|
|
|
// This includes processing any shortcodes that may be present.
|
|
|
|
|
|
|
|
// We do this in parallel... even though it's likely only one file at a time.
|
|
|
|
// We need to process the reading prior to the conversion for each file, but
|
|
|
|
// we can convert one file while another one is still reading.
|
2016-09-23 13:04:19 -04:00
|
|
|
errs := make(chan error, 2)
|
2016-03-17 10:51:52 -04:00
|
|
|
readResults := make(chan HandledResult)
|
|
|
|
filechan := make(chan *source.File)
|
|
|
|
convertResults := make(chan HandledResult)
|
|
|
|
pageChan := make(chan *Page)
|
|
|
|
fileConvChan := make(chan *source.File)
|
|
|
|
coordinator := make(chan bool)
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
go sourceReader(s, filechan, readResults, wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg2 := &sync.WaitGroup{}
|
|
|
|
wg2.Add(4)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
go fileConverter(s, fileConvChan, convertResults, wg2)
|
2017-02-04 22:20:06 -05:00
|
|
|
go pageConverter(pageChan, convertResults, wg2)
|
2016-03-17 10:51:52 -04:00
|
|
|
}
|
2016-01-08 18:37:37 -05:00
|
|
|
|
2017-06-07 16:31:57 -04:00
|
|
|
sp := source.NewSourceSpec(s.Cfg, s.Fs)
|
|
|
|
fs := sp.NewFilesystem("")
|
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
for _, ev := range sourceChanged {
|
2016-07-28 03:30:58 -04:00
|
|
|
// The incrementalReadCollator below will also make changes to the site's pages,
|
|
|
|
// so we do this first to prevent races.
|
2016-03-17 10:51:52 -04:00
|
|
|
if ev.Op&fsnotify.Remove == fsnotify.Remove {
|
|
|
|
//remove the file & a create will follow
|
2016-07-10 13:37:27 -04:00
|
|
|
path, _ := helpers.GetRelativePath(ev.Name, s.getContentDir(ev.Name))
|
2017-06-08 14:00:05 -04:00
|
|
|
s.removePageByPathPrefix(path)
|
2016-03-17 10:51:52 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-01-25 14:40:44 -05:00
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
// Some editors (Vim) sometimes issue only a Rename operation when writing an existing file
|
|
|
|
// Sometimes a rename operation means that file has been renamed other times it means
|
|
|
|
// it's been updated
|
|
|
|
if ev.Op&fsnotify.Rename == fsnotify.Rename {
|
|
|
|
// If the file is still on disk, it's only been updated, if it's not, it's been moved
|
2017-01-10 04:55:03 -05:00
|
|
|
if ex, err := afero.Exists(s.Fs.Source, ev.Name); !ex || err != nil {
|
2016-07-10 13:37:27 -04:00
|
|
|
path, _ := helpers.GetRelativePath(ev.Name, s.getContentDir(ev.Name))
|
2016-04-07 14:03:03 -04:00
|
|
|
s.removePageByPath(path)
|
2016-01-11 12:06:52 -05:00
|
|
|
continue
|
|
|
|
}
|
2016-03-17 10:51:52 -04:00
|
|
|
}
|
2016-01-11 12:06:52 -05:00
|
|
|
|
2017-06-07 16:31:57 -04:00
|
|
|
// ignore files shouldn't be proceed
|
|
|
|
if fi, err := s.Fs.Source.Stat(ev.Name); err != nil {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if ok, err := fs.ShouldRead(ev.Name, fi); err != nil || !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
sourceReallyChanged = append(sourceReallyChanged, ev)
|
|
|
|
}
|
|
|
|
|
|
|
|
go incrementalReadCollator(s, readResults, pageChan, fileConvChan, coordinator, errs)
|
2017-03-08 03:58:58 -05:00
|
|
|
go converterCollator(convertResults, errs)
|
2016-07-28 03:30:58 -04:00
|
|
|
|
|
|
|
for _, ev := range sourceReallyChanged {
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
file, err := s.reReadFile(ev.Name)
|
2016-06-02 16:47:28 -04:00
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
if err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Println("Error reading file", ev.Name, ";", err)
|
2016-06-02 16:47:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if file != nil {
|
|
|
|
filechan <- file
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
2016-01-08 18:37:37 -05:00
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
}
|
2016-07-28 03:30:58 -04:00
|
|
|
|
2017-03-27 19:18:15 -04:00
|
|
|
for shortcode := range shortcodesChanged {
|
2017-03-10 14:54:50 -05:00
|
|
|
// There are certain scenarios that, when a shortcode changes,
|
|
|
|
// it isn't sufficient to just rerender the already parsed shortcode.
|
|
|
|
// One example is if the user adds a new shortcode to the content file first,
|
|
|
|
// and then creates the shortcode on the file system.
|
|
|
|
// To handle these scenarios, we must do a full reprocessing of the
|
|
|
|
// pages that keeps a reference to the changed shortcode.
|
|
|
|
pagesWithShortcode := s.findPagesByShortcode(shortcode)
|
|
|
|
for _, p := range pagesWithShortcode {
|
|
|
|
p.rendered = false
|
|
|
|
pageChan <- p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
// we close the filechan as we have sent everything we want to send to it.
|
|
|
|
// this will tell the sourceReaders to stop iterating on that channel
|
|
|
|
close(filechan)
|
|
|
|
|
|
|
|
// waiting for the sourceReaders to all finish
|
|
|
|
wg.Wait()
|
|
|
|
// Now closing readResults as this will tell the incrementalReadCollator to
|
|
|
|
// stop iterating over that.
|
|
|
|
close(readResults)
|
2016-01-08 18:37:37 -05:00
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
// once readResults is finished it will close coordinator and move along
|
|
|
|
<-coordinator
|
|
|
|
// allow that routine to finish, then close page & fileconvchan as we've sent
|
|
|
|
// everything to them we need to.
|
|
|
|
close(pageChan)
|
|
|
|
close(fileConvChan)
|
2016-01-07 21:48:13 -05:00
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
wg2.Wait()
|
|
|
|
close(convertResults)
|
2016-01-07 21:48:13 -05:00
|
|
|
|
2016-03-17 10:51:52 -04:00
|
|
|
s.timerStep("read & convert pages from source")
|
2016-01-07 21:48:13 -05:00
|
|
|
|
2016-09-23 13:04:19 -04:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Println(err)
|
2016-09-23 13:04:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 18:33:17 -04:00
|
|
|
changed := whatChanged{
|
|
|
|
source: len(sourceChanged) > 0,
|
|
|
|
other: len(tmplChanged) > 0 || len(i18nChanged) > 0 || len(dataChanged) > 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed, nil
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2015-12-22 00:10:01 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 14:24:56 -05:00
|
|
|
func (s *Site) loadData(sources []source.Input) (err error) {
|
2017-02-04 22:20:06 -05:00
|
|
|
s.Log.DEBUG.Printf("Load Data from %d source(s)", len(sources))
|
2015-01-20 17:08:01 -05:00
|
|
|
s.Data = make(map[string]interface{})
|
2015-02-08 15:09:14 -05:00
|
|
|
var current map[string]interface{}
|
2015-02-11 14:24:56 -05:00
|
|
|
for _, currentSource := range sources {
|
|
|
|
for _, r := range currentSource.Files() {
|
|
|
|
// Crawl in data tree to insert data
|
|
|
|
current = s.Data
|
|
|
|
for _, key := range strings.Split(r.Dir(), helpers.FilePathSeparator) {
|
|
|
|
if key != "" {
|
|
|
|
if _, ok := current[key]; !ok {
|
|
|
|
current[key] = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
current = current[key].(map[string]interface{})
|
2015-01-20 17:08:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
data, err := s.readData(r)
|
2015-02-11 14:24:56 -05:00
|
|
|
if err != nil {
|
2017-04-18 12:47:37 -04:00
|
|
|
s.Log.WARN.Printf("Failed to read data from %s: %s", filepath.Join(r.Path(), r.LogicalName()), err)
|
|
|
|
continue
|
2015-02-11 14:24:56 -05:00
|
|
|
}
|
2015-01-20 17:08:01 -05:00
|
|
|
|
2015-04-22 12:36:07 -04:00
|
|
|
if data == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-02-11 14:24:56 -05:00
|
|
|
// Copy content from current to data when needed
|
|
|
|
if _, ok := current[r.BaseFileName()]; ok {
|
|
|
|
data := data.(map[string]interface{})
|
|
|
|
|
|
|
|
for key, value := range current[r.BaseFileName()].(map[string]interface{}) {
|
|
|
|
if _, override := data[key]; override {
|
|
|
|
// filepath.Walk walks the files in lexical order, '/' comes before '.'
|
|
|
|
// this warning could happen if
|
|
|
|
// 1. A theme uses the same key; the main data folder wins
|
|
|
|
// 2. A sub folder uses the same key: the sub folder wins
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.WARN.Printf("Data for key '%s' in path '%s' is overridden in subfolder", key, r.Path())
|
2015-02-11 14:24:56 -05:00
|
|
|
}
|
|
|
|
data[key] = value
|
2015-01-20 17:08:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 14:24:56 -05:00
|
|
|
// Insert data
|
|
|
|
current[r.BaseFileName()] = data
|
|
|
|
}
|
2015-01-20 17:08:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-03 11:28:51 -05:00
|
|
|
func (s *Site) readData(f *source.File) (interface{}, error) {
|
2015-01-20 17:08:01 -05:00
|
|
|
switch f.Extension() {
|
|
|
|
case "yaml", "yml":
|
2015-03-11 13:34:57 -04:00
|
|
|
return parser.HandleYAMLMetaData(f.Bytes())
|
2015-01-20 17:08:01 -05:00
|
|
|
case "json":
|
2015-03-11 13:34:57 -04:00
|
|
|
return parser.HandleJSONMetaData(f.Bytes())
|
2015-01-20 17:08:01 -05:00
|
|
|
case "toml":
|
2015-03-11 13:34:57 -04:00
|
|
|
return parser.HandleTOMLMetaData(f.Bytes())
|
2015-01-20 17:08:01 -05:00
|
|
|
default:
|
2017-02-04 22:20:06 -05:00
|
|
|
return nil, fmt.Errorf("Data not supported for extension '%s'", f.Extension())
|
2016-07-28 03:30:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
func (s *Site) readDataFromSourceFS() error {
|
2017-02-04 22:20:06 -05:00
|
|
|
sp := source.NewSourceSpec(s.Cfg, s.Fs)
|
2015-02-11 14:24:56 -05:00
|
|
|
dataSources := make([]source.Input, 0, 2)
|
2017-02-04 22:20:06 -05:00
|
|
|
dataSources = append(dataSources, sp.NewFilesystem(s.absDataDir()))
|
2015-02-11 14:24:56 -05:00
|
|
|
|
|
|
|
// have to be last - duplicate keys in earlier entries will win
|
2017-01-10 04:55:03 -05:00
|
|
|
themeDataDir, err := s.PathSpec.GetThemeDataDirPath()
|
2015-02-11 14:24:56 -05:00
|
|
|
if err == nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
dataSources = append(dataSources, sp.NewFilesystem(themeDataDir))
|
2015-02-11 14:24:56 -05:00
|
|
|
}
|
|
|
|
|
2015-12-22 00:10:01 -05:00
|
|
|
err = s.loadData(dataSources)
|
|
|
|
s.timerStep("load data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
func (s *Site) process(config BuildCfg) (err error) {
|
2015-12-22 00:10:01 -05:00
|
|
|
s.timerStep("Go initialization")
|
|
|
|
if err = s.initialize(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
s.timerStep("initialize")
|
2015-12-22 00:10:01 -05:00
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
if err = s.readDataFromSourceFS(); err != nil {
|
2015-01-20 17:08:01 -05:00
|
|
|
return
|
|
|
|
}
|
2015-02-08 15:09:14 -05:00
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
s.timerStep("load i18n")
|
2016-07-28 03:30:58 -04:00
|
|
|
return s.createPages()
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
}
|
|
|
|
|
2017-02-19 05:15:08 -05:00
|
|
|
func (s *Site) setupSitePages() {
|
|
|
|
var siteLastChange time.Time
|
|
|
|
|
2017-02-28 03:06:33 -05:00
|
|
|
for i, page := range s.RegularPages {
|
|
|
|
if i < len(s.RegularPages)-1 {
|
|
|
|
page.Next = s.RegularPages[i+1]
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2013-09-20 20:03:43 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
if i > 0 {
|
2017-02-28 03:06:33 -05:00
|
|
|
page.Prev = s.RegularPages[i-1]
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2017-02-19 05:15:08 -05:00
|
|
|
|
|
|
|
// Determine Site.Info.LastChange
|
|
|
|
// Note that the logic to determine which date to use for Lastmod
|
|
|
|
// is already applied, so this is *the* date to use.
|
|
|
|
// We cannot just pick the last page in the default sort, because
|
|
|
|
// that may not be ordered by date.
|
|
|
|
if page.Lastmod.After(siteLastChange) {
|
|
|
|
siteLastChange = page.Lastmod
|
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2017-02-19 05:15:08 -05:00
|
|
|
|
|
|
|
s.Info.LastChange = siteLastChange
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
|
|
|
|
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
|
|
|
func (s *Site) render(config *BuildCfg, outFormatIdx int) (err error) {
|
2017-05-05 03:24:37 -04:00
|
|
|
|
|
|
|
if outFormatIdx == 0 {
|
|
|
|
if err = s.preparePages(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.timerStep("prepare pages")
|
|
|
|
|
2017-06-20 04:30:40 -04:00
|
|
|
// Note that even if disableAliases is set, the aliases themselves are
|
|
|
|
// preserved on page. The motivation with this is to be able to generate
|
|
|
|
// 301 redirects in a .htacess file and similar using a custom output format.
|
|
|
|
if !s.Cfg.GetBool("disableAliases") {
|
|
|
|
// Aliases must be rendered before pages.
|
|
|
|
// Some sites, Hugo docs included, have faulty alias definitions that point
|
|
|
|
// to itself or another real page. These will be overwritten in the next
|
|
|
|
// step.
|
|
|
|
if err = s.renderAliases(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.timerStep("render and write aliases")
|
2017-05-05 03:24:37 -04:00
|
|
|
}
|
2017-02-18 04:02:12 -05:00
|
|
|
|
2016-10-31 05:23:01 -04:00
|
|
|
}
|
|
|
|
|
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
|
|
|
if err = s.renderPages(config.RecentlyVisited); err != nil {
|
2014-01-29 17:50:31 -05:00
|
|
|
return
|
|
|
|
}
|
2017-05-05 03:24:37 -04:00
|
|
|
|
|
|
|
s.timerStep("render and write pages")
|
2016-11-08 17:34:52 -05:00
|
|
|
|
2017-04-28 03:40:50 -04:00
|
|
|
// TODO(bep) render consider this, ref. render404 etc.
|
2017-05-05 03:24:37 -04:00
|
|
|
if outFormatIdx > 0 {
|
|
|
|
return
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2016-11-08 17:34:52 -05:00
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
if err = s.renderSitemap(); err != nil {
|
2014-05-06 06:50:23 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.timerStep("render and write Sitemap")
|
2015-12-08 16:13:09 -05:00
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
if err = s.renderRobotsTXT(); err != nil {
|
2015-12-08 16:13:09 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
s.timerStep("render and write robots.txt")
|
|
|
|
|
2016-11-08 17:34:52 -05:00
|
|
|
if err = s.render404(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.timerStep("render and write 404")
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-05-01 13:20:58 -04:00
|
|
|
func (s *Site) Initialise() (err error) {
|
|
|
|
return s.initialize()
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:24:49 -04:00
|
|
|
func (s *Site) initialize() (err error) {
|
2016-07-28 03:30:58 -04:00
|
|
|
defer s.initializeSiteInfo()
|
|
|
|
s.Menus = Menus{}
|
|
|
|
|
|
|
|
// May be supplied in tests.
|
|
|
|
if s.Source != nil && len(s.Source.Files()) > 0 {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.DEBUG.Println("initialize: Source is already set")
|
2016-07-28 03:30:58 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
if err = s.checkDirectories(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
sp := source.NewSourceSpec(s.Cfg, s.Fs)
|
2017-11-19 07:47:36 -05:00
|
|
|
s.Source = sp.NewFilesystem(s.absContentDir())
|
2013-07-04 11:32:55 -04:00
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
return
|
2013-09-12 19:17:53 -04:00
|
|
|
}
|
|
|
|
|
2016-08-05 10:11:03 -04:00
|
|
|
// HomeAbsURL is a convenience method giving the absolute URL to the home page.
|
2016-08-04 16:12:19 -04:00
|
|
|
func (s *SiteInfo) HomeAbsURL() string {
|
2016-08-07 16:01:55 -04:00
|
|
|
base := ""
|
2016-08-07 08:03:03 -04:00
|
|
|
if s.IsMultiLingual() {
|
2016-08-04 16:12:19 -04:00
|
|
|
base = s.Language.Lang
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
return s.owner.AbsURL(base, false)
|
2016-08-04 16:12:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SitemapAbsURL is a convenience method giving the absolute URL to the sitemap.
|
|
|
|
func (s *SiteInfo) SitemapAbsURL() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
sitemapDefault := parseSitemap(s.s.Cfg.GetStringMap("sitemap"))
|
2016-09-30 10:24:09 -04:00
|
|
|
p := s.HomeAbsURL()
|
|
|
|
if !strings.HasSuffix(p, "/") {
|
|
|
|
p += "/"
|
|
|
|
}
|
|
|
|
p += sitemapDefault.Filename
|
|
|
|
return p
|
2016-08-04 16:12:19 -04:00
|
|
|
}
|
|
|
|
|
2013-09-12 19:17:53 -04:00
|
|
|
func (s *Site) initializeSiteInfo() {
|
2016-07-24 07:58:27 -04:00
|
|
|
var (
|
2016-11-21 17:02:52 -05:00
|
|
|
lang = s.Language
|
2016-08-07 16:01:55 -04:00
|
|
|
languages helpers.Languages
|
2016-07-24 07:58:27 -04:00
|
|
|
)
|
|
|
|
|
2016-08-08 04:12:39 -04:00
|
|
|
if s.owner != nil && s.owner.multilingual != nil {
|
|
|
|
languages = s.owner.multilingual.Languages
|
2016-07-24 07:58:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
params := lang.Params()
|
2014-04-05 01:26:43 -04:00
|
|
|
|
2014-05-10 20:27:49 -04:00
|
|
|
permalinks := make(PermalinkOverrides)
|
2017-02-04 22:20:06 -05:00
|
|
|
for k, v := range s.Cfg.GetStringMapString("permalinks") {
|
2016-03-24 22:12:03 -04:00
|
|
|
permalinks[k] = pathPattern(v)
|
2014-04-05 01:26:43 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
defaultContentInSubDir := s.Cfg.GetBool("defaultContentLanguageInSubdir")
|
|
|
|
defaultContentLanguage := s.Cfg.GetString("defaultContentLanguage")
|
2016-08-08 07:55:18 -04:00
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
languagePrefix := ""
|
2016-08-08 07:55:18 -04:00
|
|
|
if s.multilingualEnabled() && (defaultContentInSubDir || lang.Lang != defaultContentLanguage) {
|
2016-07-24 07:58:27 -04:00
|
|
|
languagePrefix = "/" + lang.Lang
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
|
2016-08-08 04:12:39 -04:00
|
|
|
var multilingual *Multilingual
|
|
|
|
if s.owner != nil {
|
|
|
|
multilingual = s.owner.multilingual
|
|
|
|
}
|
|
|
|
|
2014-04-24 12:18:47 -04:00
|
|
|
s.Info = SiteInfo{
|
2016-10-24 14:56:00 -04:00
|
|
|
Title: lang.GetString("title"),
|
2016-09-18 13:10:11 -04:00
|
|
|
Author: lang.GetStringMap("author"),
|
2016-08-08 07:55:18 -04:00
|
|
|
Social: lang.GetStringMapString("social"),
|
2016-10-24 14:56:00 -04:00
|
|
|
LanguageCode: lang.GetString("languageCode"),
|
2016-08-08 07:55:18 -04:00
|
|
|
Copyright: lang.GetString("copyright"),
|
2016-10-24 14:56:00 -04:00
|
|
|
DisqusShortname: lang.GetString("disqusShortname"),
|
2016-08-08 07:55:18 -04:00
|
|
|
multilingual: multilingual,
|
|
|
|
Language: lang,
|
|
|
|
LanguagePrefix: languagePrefix,
|
|
|
|
Languages: languages,
|
|
|
|
defaultContentLanguageInSubdir: defaultContentInSubDir,
|
2016-11-23 16:06:15 -05:00
|
|
|
sectionPagesMenu: lang.GetString("sectionPagesMenu"),
|
2016-10-24 14:56:00 -04:00
|
|
|
GoogleAnalytics: lang.GetString("googleAnalytics"),
|
2017-02-04 22:20:06 -05:00
|
|
|
BuildDrafts: s.Cfg.GetBool("buildDrafts"),
|
|
|
|
canonifyURLs: s.Cfg.GetBool("canonifyURLs"),
|
2017-02-20 03:52:06 -05:00
|
|
|
relativeURLs: s.Cfg.GetBool("relativeURLs"),
|
2017-03-09 13:19:29 -05:00
|
|
|
uglyURLs: s.Cfg.GetBool("uglyURLs"),
|
2016-10-24 14:56:00 -04:00
|
|
|
preserveTaxonomyNames: lang.GetBool("preserveTaxonomyNames"),
|
2016-11-03 19:34:25 -04:00
|
|
|
PageCollections: s.PageCollections,
|
2016-08-08 07:55:18 -04:00
|
|
|
Files: &s.Files,
|
|
|
|
Menus: &s.Menus,
|
|
|
|
Params: params,
|
|
|
|
Permalinks: permalinks,
|
|
|
|
Data: &s.Data,
|
2016-08-09 14:06:15 -04:00
|
|
|
owner: s.owner,
|
2017-01-10 04:55:03 -05:00
|
|
|
s: s,
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2016-10-24 07:45:30 -04:00
|
|
|
|
2017-05-17 12:57:44 -04:00
|
|
|
rssOutputFormat, found := s.outputFormats[KindHome].GetByName(output.RSSFormat.Name)
|
|
|
|
|
|
|
|
if found {
|
|
|
|
s.Info.RSSLink = s.permalink(rssOutputFormat.BaseFilename())
|
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2016-09-11 13:47:12 -04:00
|
|
|
func (s *Site) dataDir() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
return s.Cfg.GetString("dataDir")
|
2016-09-11 13:47:12 -04:00
|
|
|
}
|
2017-02-19 17:53:48 -05:00
|
|
|
|
2015-01-20 17:08:01 -05:00
|
|
|
func (s *Site) absDataDir() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
return s.PathSpec.AbsPathify(s.dataDir())
|
2016-09-11 13:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) i18nDir() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
return s.Cfg.GetString("i18nDir")
|
2015-01-20 17:08:01 -05:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:35:16 -04:00
|
|
|
func (s *Site) absI18nDir() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
return s.PathSpec.AbsPathify(s.i18nDir())
|
2016-09-11 13:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) isI18nEvent(e fsnotify.Event) bool {
|
|
|
|
if s.getI18nDir(e.Name) != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return s.getThemeI18nDir(e.Name) != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) getI18nDir(path string) string {
|
2017-01-03 11:28:51 -05:00
|
|
|
return s.getRealDir(s.absI18nDir(), path)
|
2016-09-11 13:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) getThemeI18nDir(path string) string {
|
2017-02-19 17:53:48 -05:00
|
|
|
if !s.PathSpec.ThemeSet() {
|
2016-09-11 13:47:12 -04:00
|
|
|
return ""
|
|
|
|
}
|
2017-02-19 17:53:48 -05:00
|
|
|
return s.getRealDir(filepath.Join(s.PathSpec.GetThemeDir(), s.i18nDir()), path)
|
2016-05-14 00:35:16 -04:00
|
|
|
}
|
|
|
|
|
2016-07-10 13:37:27 -04:00
|
|
|
func (s *Site) isDataDirEvent(e fsnotify.Event) bool {
|
2016-09-11 13:47:12 -04:00
|
|
|
if s.getDataDir(e.Name) != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return s.getThemeDataDir(e.Name) != ""
|
2016-07-10 13:37:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) getDataDir(path string) string {
|
2017-01-03 11:28:51 -05:00
|
|
|
return s.getRealDir(s.absDataDir(), path)
|
2016-07-10 13:37:27 -04:00
|
|
|
}
|
|
|
|
|
2016-09-11 13:47:12 -04:00
|
|
|
func (s *Site) getThemeDataDir(path string) string {
|
2017-02-19 17:53:48 -05:00
|
|
|
if !s.PathSpec.ThemeSet() {
|
2016-09-11 13:47:12 -04:00
|
|
|
return ""
|
|
|
|
}
|
2017-02-19 17:53:48 -05:00
|
|
|
return s.getRealDir(filepath.Join(s.PathSpec.GetThemeDir(), s.dataDir()), path)
|
2014-04-10 08:10:12 -04:00
|
|
|
}
|
|
|
|
|
2016-09-11 13:47:12 -04:00
|
|
|
func (s *Site) layoutDir() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
return s.Cfg.GetString("layoutDir")
|
2016-09-11 13:47:12 -04:00
|
|
|
}
|
|
|
|
|
2016-07-10 13:37:27 -04:00
|
|
|
func (s *Site) isLayoutDirEvent(e fsnotify.Event) bool {
|
2016-09-11 13:47:12 -04:00
|
|
|
if s.getLayoutDir(e.Name) != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return s.getThemeLayoutDir(e.Name) != ""
|
2016-07-10 13:37:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) getLayoutDir(path string) string {
|
2017-02-19 17:53:48 -05:00
|
|
|
return s.getRealDir(s.PathSpec.GetLayoutDirPath(), path)
|
2016-07-10 13:37:27 -04:00
|
|
|
}
|
|
|
|
|
2016-09-11 13:47:12 -04:00
|
|
|
func (s *Site) getThemeLayoutDir(path string) string {
|
2017-02-19 17:53:48 -05:00
|
|
|
if !s.PathSpec.ThemeSet() {
|
2016-09-11 13:47:12 -04:00
|
|
|
return ""
|
|
|
|
}
|
2017-02-19 17:53:48 -05:00
|
|
|
return s.getRealDir(filepath.Join(s.PathSpec.GetThemeDir(), s.layoutDir()), path)
|
2016-09-11 13:47:12 -04:00
|
|
|
}
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
func (s *Site) absContentDir() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
return s.PathSpec.AbsPathify(s.Cfg.GetString("contentDir"))
|
2013-08-07 20:21:22 -04:00
|
|
|
}
|
|
|
|
|
2016-07-10 13:37:27 -04:00
|
|
|
func (s *Site) isContentDirEvent(e fsnotify.Event) bool {
|
|
|
|
return s.getContentDir(e.Name) != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) getContentDir(path string) string {
|
2017-01-03 11:28:51 -05:00
|
|
|
return s.getRealDir(s.absContentDir(), path)
|
2016-07-10 13:37:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// getRealDir gets the base path of the given path, also handling the case where
|
|
|
|
// base is a symlinked folder.
|
2017-01-03 11:28:51 -05:00
|
|
|
func (s *Site) getRealDir(base, path string) string {
|
2016-07-10 13:37:27 -04:00
|
|
|
|
|
|
|
if strings.HasPrefix(path, base) {
|
|
|
|
return base
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
realDir, err := helpers.GetRealPath(s.Fs.Source, base)
|
2016-07-10 13:37:27 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Printf("Failed to get real path for %s: %s", path, err)
|
2016-07-10 13:37:27 -04:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(path, realDir) {
|
|
|
|
return realDir
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2013-08-07 20:21:22 -04:00
|
|
|
func (s *Site) absPublishDir() string {
|
2017-02-04 22:20:06 -05:00
|
|
|
return s.PathSpec.AbsPathify(s.Cfg.GetString("publishDir"))
|
2013-08-07 20:21:22 -04:00
|
|
|
}
|
|
|
|
|
2013-09-25 00:24:49 -04:00
|
|
|
func (s *Site) checkDirectories() (err error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
if b, _ := helpers.DirExists(s.absContentDir(), s.Fs.Source); !b {
|
2016-11-18 16:54:57 -05:00
|
|
|
return errors.New("No source directory found, expecting to find it at " + s.absContentDir())
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
return
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
// reReadFile resets file to be read from disk again
|
|
|
|
func (s *Site) reReadFile(absFilePath string) (*source.File, error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.INFO.Println("rereading", absFilePath)
|
2016-01-07 21:48:13 -05:00
|
|
|
var file *source.File
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
reader, err := source.NewLazyFileReader(s.Fs.Source, absFilePath)
|
2016-01-07 21:48:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
sp := source.NewSourceSpec(s.Cfg, s.Fs)
|
|
|
|
file, err = sp.NewFileFromAbs(s.getContentDir(absFilePath), absFilePath, reader)
|
2016-01-07 21:48:13 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
func (s *Site) readPagesFromSource() chan error {
|
2014-01-29 17:50:31 -05:00
|
|
|
if s.Source == nil {
|
|
|
|
panic(fmt.Sprintf("s.Source not set %s", s.absContentDir()))
|
|
|
|
}
|
2015-12-21 19:47:48 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.DEBUG.Printf("Read %d pages from source", len(s.Source.Files()))
|
2016-01-31 10:21:12 -05:00
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
errs := make(chan error)
|
2014-01-29 17:50:31 -05:00
|
|
|
if len(s.Source.Files()) < 1 {
|
2016-01-31 10:21:12 -05:00
|
|
|
close(errs)
|
|
|
|
return errs
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
|
2014-08-29 13:40:21 -04:00
|
|
|
files := s.Source.Files()
|
2014-10-20 17:42:16 -04:00
|
|
|
results := make(chan HandledResult)
|
2014-09-05 06:57:32 -04:00
|
|
|
filechan := make(chan *source.File)
|
2014-08-29 13:40:21 -04:00
|
|
|
wg := &sync.WaitGroup{}
|
2017-02-21 12:56:56 -05:00
|
|
|
numWorkers := getGoMaxProcs() * 4
|
|
|
|
wg.Add(numWorkers)
|
|
|
|
for i := 0; i < numWorkers; i++ {
|
2014-10-16 20:20:09 -04:00
|
|
|
go sourceReader(s, filechan, results, wg)
|
2014-08-29 13:40:21 -04:00
|
|
|
}
|
2014-03-05 19:07:39 -05:00
|
|
|
|
2014-08-29 13:40:21 -04:00
|
|
|
// we can only have exactly one result collator, since it makes changes that
|
|
|
|
// must be synchronized.
|
2014-09-05 06:57:32 -04:00
|
|
|
go readCollator(s, results, errs)
|
2014-08-20 11:09:35 -04:00
|
|
|
|
2014-09-05 06:57:32 -04:00
|
|
|
for _, file := range files {
|
|
|
|
filechan <- file
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
|
2014-09-05 06:57:32 -04:00
|
|
|
close(filechan)
|
2014-03-05 19:07:39 -05:00
|
|
|
wg.Wait()
|
2014-08-29 13:40:21 -04:00
|
|
|
close(results)
|
|
|
|
|
2015-12-21 19:47:48 -05:00
|
|
|
return errs
|
|
|
|
}
|
2014-09-05 06:57:32 -04:00
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
func (s *Site) convertSource() chan error {
|
2015-12-21 19:47:48 -05:00
|
|
|
errs := make(chan error)
|
|
|
|
results := make(chan HandledResult)
|
2014-11-04 00:36:05 -05:00
|
|
|
pageChan := make(chan *Page)
|
|
|
|
fileConvChan := make(chan *source.File)
|
2017-02-21 12:56:56 -05:00
|
|
|
numWorkers := getGoMaxProcs() * 4
|
2015-12-21 19:47:48 -05:00
|
|
|
wg := &sync.WaitGroup{}
|
2014-09-05 06:57:32 -04:00
|
|
|
|
2017-02-21 12:56:56 -05:00
|
|
|
for i := 0; i < numWorkers; i++ {
|
|
|
|
wg.Add(2)
|
2014-11-04 00:36:05 -05:00
|
|
|
go fileConverter(s, fileConvChan, results, wg)
|
2017-02-04 22:20:06 -05:00
|
|
|
go pageConverter(pageChan, results, wg)
|
2014-09-05 06:57:32 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 03:58:58 -05:00
|
|
|
go converterCollator(results, errs)
|
2014-09-05 06:57:32 -04:00
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
for _, p := range s.rawAllPages {
|
2016-08-08 03:05:16 -04:00
|
|
|
if p.shouldBuild() {
|
|
|
|
pageChan <- p
|
|
|
|
}
|
2014-09-05 06:57:32 -04:00
|
|
|
}
|
|
|
|
|
2014-10-20 20:15:33 -04:00
|
|
|
for _, f := range s.Files {
|
2014-11-04 00:36:05 -05:00
|
|
|
fileConvChan <- f
|
2014-10-20 20:15:33 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 00:36:05 -05:00
|
|
|
close(pageChan)
|
|
|
|
close(fileConvChan)
|
2014-09-05 06:57:32 -04:00
|
|
|
wg.Wait()
|
|
|
|
close(results)
|
|
|
|
|
2015-12-21 19:47:48 -05:00
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
func (s *Site) createPages() error {
|
|
|
|
readErrs := <-s.readPagesFromSource()
|
2015-12-21 19:47:48 -05:00
|
|
|
s.timerStep("read pages from source")
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
renderErrs := <-s.convertSource()
|
2015-12-21 19:47:48 -05:00
|
|
|
s.timerStep("convert source")
|
2014-09-05 06:57:32 -04:00
|
|
|
|
|
|
|
if renderErrs == nil && readErrs == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if renderErrs == nil {
|
|
|
|
return readErrs
|
|
|
|
}
|
|
|
|
if readErrs == nil {
|
|
|
|
return renderErrs
|
|
|
|
}
|
2015-12-21 19:47:48 -05:00
|
|
|
|
2014-09-05 06:57:32 -04:00
|
|
|
return fmt.Errorf("%s\n%s", readErrs, renderErrs)
|
2014-08-29 13:40:21 -04:00
|
|
|
}
|
|
|
|
|
2014-10-20 17:42:16 -04:00
|
|
|
func sourceReader(s *Site, files <-chan *source.File, results chan<- HandledResult, wg *sync.WaitGroup) {
|
2014-09-14 07:01:40 -04:00
|
|
|
defer wg.Done()
|
2014-09-05 06:57:32 -04:00
|
|
|
for file := range files {
|
2016-01-07 21:48:13 -05:00
|
|
|
readSourceFile(s, file, results)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSourceFile(s *Site, file *source.File, results chan<- HandledResult) {
|
|
|
|
h := NewMetaHandler(file.Extension())
|
|
|
|
if h != nil {
|
|
|
|
h.Read(file, s, results)
|
|
|
|
} else {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Println("Unsupported File Type", file.Path())
|
2014-09-05 06:57:32 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-29 13:40:21 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func pageConverter(pages <-chan *Page, results HandleResults, wg *sync.WaitGroup) {
|
2014-09-14 07:01:40 -04:00
|
|
|
defer wg.Done()
|
2014-09-05 06:57:32 -04:00
|
|
|
for page := range pages {
|
2014-11-20 12:39:09 -05:00
|
|
|
var h *MetaHandle
|
2014-11-04 00:44:30 -05:00
|
|
|
if page.Markup != "" {
|
2014-11-20 12:39:09 -05:00
|
|
|
h = NewMetaHandler(page.Markup)
|
2014-11-04 00:44:30 -05:00
|
|
|
} else {
|
2014-11-20 12:39:09 -05:00
|
|
|
h = NewMetaHandler(page.File.Extension())
|
2014-11-04 00:44:30 -05:00
|
|
|
}
|
2014-10-20 20:15:33 -04:00
|
|
|
if h != nil {
|
2017-02-04 22:20:06 -05:00
|
|
|
// Note that we convert pages from the site's rawAllPages collection
|
|
|
|
// Which may contain pages from multiple sites, so we use the Page's site
|
|
|
|
// for the conversion.
|
|
|
|
h.Convert(page, page.s, results)
|
2014-10-20 20:15:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-29 13:40:21 -04:00
|
|
|
|
2014-10-20 20:15:33 -04:00
|
|
|
func fileConverter(s *Site, files <-chan *source.File, results HandleResults, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
for file := range files {
|
2014-11-20 12:39:09 -05:00
|
|
|
h := NewMetaHandler(file.Extension())
|
2014-10-20 20:15:33 -04:00
|
|
|
if h != nil {
|
|
|
|
h.Convert(file, s, results)
|
2014-08-29 13:40:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 03:58:58 -05:00
|
|
|
func converterCollator(results <-chan HandledResult, errs chan<- error) {
|
2014-09-05 06:57:32 -04:00
|
|
|
errMsgs := []string{}
|
|
|
|
for r := range results {
|
|
|
|
if r.err != nil {
|
|
|
|
errMsgs = append(errMsgs, r.err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errMsgs) == 0 {
|
|
|
|
errs <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errs <- fmt.Errorf("Errors rendering pages: %s", strings.Join(errMsgs, "\n"))
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
func (s *Site) replaceFile(sf *source.File) {
|
2016-01-11 10:48:23 -05:00
|
|
|
for i, f := range s.Files {
|
|
|
|
if f.Path() == sf.Path() {
|
|
|
|
s.Files[i] = sf
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a match isn't found, then append it
|
|
|
|
s.Files = append(s.Files, sf)
|
|
|
|
}
|
|
|
|
|
2016-01-08 18:37:37 -05:00
|
|
|
func incrementalReadCollator(s *Site, results <-chan HandledResult, pageChan chan *Page, fileConvChan chan *source.File, coordinator chan bool, errs chan<- error) {
|
2014-08-29 13:40:21 -04:00
|
|
|
errMsgs := []string{}
|
|
|
|
for r := range results {
|
|
|
|
if r.err != nil {
|
2014-11-20 12:39:09 -05:00
|
|
|
errMsgs = append(errMsgs, r.Error())
|
2014-08-29 13:40:21 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:15:33 -04:00
|
|
|
if r.page == nil {
|
2016-04-07 14:03:03 -04:00
|
|
|
s.replaceFile(r.file)
|
2016-01-08 18:37:37 -05:00
|
|
|
fileConvChan <- r.file
|
2014-10-20 20:15:33 -04:00
|
|
|
} else {
|
2016-04-07 14:03:03 -04:00
|
|
|
s.replacePage(r.page)
|
2016-01-08 18:37:37 -05:00
|
|
|
pageChan <- r.page
|
2016-01-07 21:48:13 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-29 13:40:21 -04:00
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
s.rawAllPages.Sort()
|
2016-01-08 18:37:37 -05:00
|
|
|
close(coordinator)
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
if len(errMsgs) == 0 {
|
|
|
|
errs <- nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errs <- fmt.Errorf("Errors reading pages: %s", strings.Join(errMsgs, "\n"))
|
|
|
|
}
|
2014-08-29 13:40:21 -04:00
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
func readCollator(s *Site, results <-chan HandledResult, errs chan<- error) {
|
2016-11-07 14:24:37 -05:00
|
|
|
if s.PageCollections == nil {
|
|
|
|
panic("No page collections")
|
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
errMsgs := []string{}
|
|
|
|
for r := range results {
|
|
|
|
if r.err != nil {
|
|
|
|
errMsgs = append(errMsgs, r.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// !page == file
|
|
|
|
if r.page == nil {
|
|
|
|
s.Files = append(s.Files, r.file)
|
|
|
|
} else {
|
2016-04-07 14:03:03 -04:00
|
|
|
s.addPage(r.page)
|
2014-08-29 13:40:21 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-20 20:15:33 -04:00
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
s.rawAllPages.Sort()
|
2014-08-29 13:40:21 -04:00
|
|
|
if len(errMsgs) == 0 {
|
|
|
|
errs <- nil
|
2014-09-05 06:57:32 -04:00
|
|
|
return
|
2014-08-29 13:40:21 -04:00
|
|
|
}
|
2014-09-05 06:57:32 -04:00
|
|
|
errs <- fmt.Errorf("Errors reading pages: %s", strings.Join(errMsgs, "\n"))
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
func (s *Site) buildSiteMeta() (err error) {
|
2016-11-10 14:55:52 -05:00
|
|
|
defer s.timerStep("build Site meta")
|
2016-11-03 19:34:25 -04:00
|
|
|
|
2016-11-11 03:01:47 -05:00
|
|
|
if len(s.Pages) == 0 {
|
2014-04-23 02:59:19 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.assembleTaxonomies()
|
2016-11-03 19:34:25 -04:00
|
|
|
|
2016-11-11 03:01:47 -05:00
|
|
|
for _, p := range s.AllPages {
|
2016-11-16 11:52:03 -05:00
|
|
|
// this depends on taxonomies
|
|
|
|
p.setValuesForKind(s)
|
2016-11-03 19:34:25 -04:00
|
|
|
}
|
|
|
|
|
2014-04-23 02:59:19 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-24 18:11:08 -04:00
|
|
|
func (s *Site) getMenusFromConfig() Menus {
|
|
|
|
|
|
|
|
ret := Menus{}
|
|
|
|
|
2016-09-12 14:41:52 -04:00
|
|
|
if menus := s.Language.GetStringMap("menu"); menus != nil {
|
2014-04-24 18:11:08 -04:00
|
|
|
for name, menu := range menus {
|
|
|
|
m, err := cast.ToSliceE(menu)
|
|
|
|
if err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Printf("unable to process menus in site config\n")
|
|
|
|
s.Log.ERROR.Println(err)
|
2014-04-24 18:11:08 -04:00
|
|
|
} else {
|
|
|
|
for _, entry := range m {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.DEBUG.Printf("found menu: %q, in site config\n", name)
|
2014-04-24 18:11:08 -04:00
|
|
|
|
|
|
|
menuEntry := MenuEntry{Menu: name}
|
|
|
|
ime, err := cast.ToStringMapE(entry)
|
|
|
|
if err != nil {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Printf("unable to process menus in site config\n")
|
|
|
|
s.Log.ERROR.Println(err)
|
2014-04-24 18:11:08 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
menuEntry.marshallMap(ime)
|
2015-05-09 14:54:11 -04:00
|
|
|
menuEntry.URL = s.Info.createNodeMenuEntryURL(menuEntry.URL)
|
2014-12-12 14:28:28 -05:00
|
|
|
|
2014-04-24 18:11:08 -04:00
|
|
|
if ret[name] == nil {
|
|
|
|
ret[name] = &Menu{}
|
|
|
|
}
|
2016-03-22 19:29:39 -04:00
|
|
|
*ret[name] = ret[name].add(&menuEntry)
|
2014-04-24 18:11:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-05-09 14:54:11 -04:00
|
|
|
func (s *SiteInfo) createNodeMenuEntryURL(in string) string {
|
|
|
|
|
|
|
|
if !strings.HasPrefix(in, "/") {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
// make it match the nodes
|
|
|
|
menuEntryURL := in
|
2017-01-10 04:55:03 -05:00
|
|
|
menuEntryURL = helpers.SanitizeURLKeepTrailingSlash(s.s.PathSpec.URLize(menuEntryURL))
|
2015-05-09 14:54:11 -04:00
|
|
|
if !s.canonifyURLs {
|
2017-04-07 12:33:28 -04:00
|
|
|
menuEntryURL = helpers.AddContextRoot(s.s.PathSpec.BaseURL.String(), menuEntryURL)
|
2015-05-09 14:54:11 -04:00
|
|
|
}
|
|
|
|
return menuEntryURL
|
|
|
|
}
|
|
|
|
|
2014-04-23 02:59:19 -04:00
|
|
|
func (s *Site) assembleMenus() {
|
2015-12-22 00:10:01 -05:00
|
|
|
s.Menus = Menus{}
|
2014-04-23 02:59:19 -04:00
|
|
|
|
|
|
|
type twoD struct {
|
|
|
|
MenuName, EntryName string
|
|
|
|
}
|
|
|
|
flat := map[twoD]*MenuEntry{}
|
|
|
|
children := map[twoD]Menu{}
|
|
|
|
|
2017-01-22 06:28:31 -05:00
|
|
|
// add menu entries from config to flat hash
|
2014-04-24 18:11:08 -04:00
|
|
|
menuConfig := s.getMenusFromConfig()
|
|
|
|
for name, menu := range menuConfig {
|
|
|
|
for _, me := range *menu {
|
2014-05-14 18:01:13 -04:00
|
|
|
flat[twoD{name, me.KeyName()}] = me
|
2014-04-24 18:11:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 16:06:15 -05:00
|
|
|
sectionPagesMenu := s.Info.sectionPagesMenu
|
2016-11-11 03:01:47 -05:00
|
|
|
pages := s.Pages
|
2017-01-22 06:28:31 -05:00
|
|
|
|
|
|
|
if sectionPagesMenu != "" {
|
|
|
|
for _, p := range pages {
|
|
|
|
if p.Kind == KindSection {
|
2017-04-09 04:33:04 -04:00
|
|
|
// From Hugo 0.22 we have nested sections, but until we get a
|
|
|
|
// feel of how that would work in this setting, let us keep
|
|
|
|
// this menu for the top level only.
|
2017-02-20 03:33:35 -05:00
|
|
|
id := p.Section()
|
|
|
|
if _, ok := flat[twoD{sectionPagesMenu, id}]; ok {
|
2017-01-22 06:28:31 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
me := MenuEntry{Identifier: id,
|
2017-01-22 06:28:31 -05:00
|
|
|
Name: p.LinkTitle(),
|
|
|
|
Weight: p.Weight,
|
|
|
|
URL: p.RelPermalink()}
|
|
|
|
flat[twoD{sectionPagesMenu, me.KeyName()}] = &me
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 12:11:06 -05:00
|
|
|
|
2017-01-22 06:28:31 -05:00
|
|
|
// Add menu entries provided by pages
|
|
|
|
for _, p := range pages {
|
2014-04-23 02:59:19 -04:00
|
|
|
for name, me := range p.Menus() {
|
2014-05-14 18:01:13 -04:00
|
|
|
if _, ok := flat[twoD{name, me.KeyName()}]; ok {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Printf("Two or more menu items have the same name/identifier in Menu %q: %q.\nRename or set an unique identifier.\n", name, me.KeyName())
|
2015-01-22 11:23:01 -05:00
|
|
|
continue
|
2014-05-14 18:01:13 -04:00
|
|
|
}
|
|
|
|
flat[twoD{name, me.KeyName()}] = me
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Children Menus First
|
|
|
|
for _, e := range flat {
|
|
|
|
if e.Parent != "" {
|
2016-03-22 19:29:39 -04:00
|
|
|
children[twoD{e.Menu, e.Parent}] = children[twoD{e.Menu, e.Parent}].add(e)
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Placing Children in Parents (in flat)
|
|
|
|
for p, childmenu := range children {
|
|
|
|
_, ok := flat[twoD{p.MenuName, p.EntryName}]
|
|
|
|
if !ok {
|
2015-03-18 01:16:54 -04:00
|
|
|
// if parent does not exist, create one without a URL
|
|
|
|
flat[twoD{p.MenuName, p.EntryName}] = &MenuEntry{Name: p.EntryName, URL: ""}
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
flat[twoD{p.MenuName, p.EntryName}].Children = childmenu
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assembling Top Level of Tree
|
|
|
|
for menu, e := range flat {
|
|
|
|
if e.Parent == "" {
|
|
|
|
_, ok := s.Menus[menu.MenuName]
|
|
|
|
if !ok {
|
|
|
|
s.Menus[menu.MenuName] = &Menu{}
|
|
|
|
}
|
2016-03-22 19:29:39 -04:00
|
|
|
*s.Menus[menu.MenuName] = s.Menus[menu.MenuName].add(e)
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
func (s *Site) getTaxonomyKey(key string) string {
|
|
|
|
if s.Info.preserveTaxonomyNames {
|
|
|
|
// Keep as is
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
return s.PathSpec.MakePathSanitized(key)
|
|
|
|
}
|
2017-02-18 04:02:12 -05:00
|
|
|
|
|
|
|
// We need to create the top level taxonomy early in the build process
|
|
|
|
// to be able to determine the page Kind correctly.
|
|
|
|
func (s *Site) createTaxonomiesEntries() {
|
2014-04-08 23:15:57 -04:00
|
|
|
s.Taxonomies = make(TaxonomyList)
|
2017-02-18 04:02:12 -05:00
|
|
|
taxonomies := s.Language.GetStringMapString("taxonomies")
|
|
|
|
for _, plural := range taxonomies {
|
|
|
|
s.Taxonomies[plural] = make(Taxonomy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) assembleTaxonomies() {
|
2016-10-31 14:53:33 -04:00
|
|
|
s.taxonomiesPluralSingular = make(map[string]string)
|
2016-12-26 13:30:57 -05:00
|
|
|
s.taxonomiesOrigKey = make(map[string]string)
|
2014-01-29 17:50:31 -05:00
|
|
|
|
2016-11-18 17:21:45 -05:00
|
|
|
taxonomies := s.Language.GetStringMapString("taxonomies")
|
2016-12-26 13:30:57 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.INFO.Printf("found taxonomies: %#v\n", taxonomies)
|
2014-04-07 23:29:35 -04:00
|
|
|
|
2016-10-31 14:53:33 -04:00
|
|
|
for singular, plural := range taxonomies {
|
|
|
|
s.taxonomiesPluralSingular[plural] = singular
|
2016-11-09 14:55:42 -05:00
|
|
|
|
2016-11-11 03:01:47 -05:00
|
|
|
for _, p := range s.Pages {
|
2015-05-31 14:30:53 -04:00
|
|
|
vals := p.getParam(plural, !s.Info.preserveTaxonomyNames)
|
2014-01-29 17:50:31 -05:00
|
|
|
weight := p.GetParam(plural + "_weight")
|
|
|
|
if weight == nil {
|
|
|
|
weight = 0
|
|
|
|
}
|
|
|
|
if vals != nil {
|
2014-09-05 09:29:01 -04:00
|
|
|
if v, ok := vals.([]string); ok {
|
2014-01-29 17:50:31 -05:00
|
|
|
for _, idx := range v {
|
|
|
|
x := WeightedPage{weight.(int), p}
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Taxonomies[plural].add(s.getTaxonomyKey(idx), x)
|
2016-12-26 13:30:57 -05:00
|
|
|
if s.Info.preserveTaxonomyNames {
|
|
|
|
// Need to track the original
|
2017-01-10 04:55:03 -05:00
|
|
|
s.taxonomiesOrigKey[fmt.Sprintf("%s-%s", plural, s.PathSpec.MakePathSanitized(idx))] = idx
|
2016-12-26 13:30:57 -05:00
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2014-09-05 09:29:01 -04:00
|
|
|
} else if v, ok := vals.(string); ok {
|
|
|
|
x := WeightedPage{weight.(int), p}
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Taxonomies[plural].add(s.getTaxonomyKey(v), x)
|
2016-12-26 13:30:57 -05:00
|
|
|
if s.Info.preserveTaxonomyNames {
|
|
|
|
// Need to track the original
|
2017-01-10 04:55:03 -05:00
|
|
|
s.taxonomiesOrigKey[fmt.Sprintf("%s-%s", plural, s.PathSpec.MakePathSanitized(v))] = v
|
2016-12-26 13:30:57 -05:00
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
} else {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.ERROR.Printf("Invalid %s in %s\n", plural, p.File.Path())
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 23:15:57 -04:00
|
|
|
for k := range s.Taxonomies[plural] {
|
|
|
|
s.Taxonomies[plural][k].Sort()
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 02:59:19 -04:00
|
|
|
s.Info.Taxonomies = s.Taxonomies
|
|
|
|
}
|
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
// Prepare site for a new full build.
|
|
|
|
func (s *Site) resetBuildState() {
|
|
|
|
|
2017-08-19 07:16:00 -04:00
|
|
|
s.relatedDocsHandler = newSearchIndexHandler(s.relatedDocsHandler.cfg)
|
2016-11-09 03:58:18 -05:00
|
|
|
s.PageCollections = newPageCollectionsFromPages(s.rawAllPages)
|
2017-01-05 14:57:24 -05:00
|
|
|
// TODO(bep) get rid of this double
|
|
|
|
s.Info.PageCollections = s.PageCollections
|
2016-03-06 10:33:32 -05:00
|
|
|
|
|
|
|
s.Info.paginationPageCount = 0
|
2016-07-28 03:30:58 -04:00
|
|
|
s.draftCount = 0
|
|
|
|
s.futureCount = 0
|
2017-02-21 07:55:08 -05:00
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
s.expiredCount = 0
|
2016-03-06 10:33:32 -05:00
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
for _, p := range s.rawAllPages {
|
2016-03-06 10:33:32 -05:00
|
|
|
p.scratch = newScratch()
|
2017-04-09 04:33:04 -04:00
|
|
|
p.subSections = Pages{}
|
|
|
|
p.parent = nil
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2016-11-16 11:52:03 -05:00
|
|
|
func (s *Site) kindFromSections(sections []string) string {
|
2016-11-01 11:47:15 -04:00
|
|
|
if _, isTaxonomy := s.Taxonomies[sections[0]]; isTaxonomy {
|
|
|
|
if len(sections) == 1 {
|
2016-11-13 05:43:23 -05:00
|
|
|
return KindTaxonomyTerm
|
2016-11-01 11:47:15 -04:00
|
|
|
}
|
2016-11-13 05:43:23 -05:00
|
|
|
return KindTaxonomy
|
2016-10-31 14:53:33 -04:00
|
|
|
}
|
2016-11-13 05:43:23 -05:00
|
|
|
return KindSection
|
2016-10-31 14:53:33 -04:00
|
|
|
}
|
|
|
|
|
2017-03-26 13:34:30 -04:00
|
|
|
func (s *Site) layouts(p *PageOutput) ([]string, error) {
|
2017-03-18 11:46:10 -04:00
|
|
|
return s.layoutHandler.For(p.layoutDescriptor, "", p.outputFormat)
|
2017-03-07 08:20:39 -05:00
|
|
|
}
|
|
|
|
|
2016-10-31 05:23:01 -04:00
|
|
|
func (s *Site) preparePages() error {
|
|
|
|
var errors []error
|
|
|
|
|
2016-11-11 03:01:47 -05:00
|
|
|
for _, p := range s.Pages {
|
2016-10-31 05:23:01 -04:00
|
|
|
if err := p.prepareLayouts(); err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
2016-10-31 13:03:02 -04:00
|
|
|
if err := p.prepareData(s); err != nil {
|
2016-10-31 05:23:01 -04:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) != 0 {
|
|
|
|
return fmt.Errorf("Prepare pages failed: %.100q…", errors)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-14 07:23:03 -04:00
|
|
|
func errorCollator(results <-chan error, errs chan<- error) {
|
2014-09-14 07:01:40 -04:00
|
|
|
errMsgs := []string{}
|
|
|
|
for err := range results {
|
|
|
|
if err != nil {
|
|
|
|
errMsgs = append(errMsgs, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errMsgs) == 0 {
|
|
|
|
errs <- nil
|
2014-09-14 07:23:03 -04:00
|
|
|
} else {
|
|
|
|
errs <- errors.New(strings.Join(errMsgs, "\n"))
|
2014-09-14 07:01:40 -04:00
|
|
|
}
|
2014-09-14 07:23:03 -04:00
|
|
|
close(errs)
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2014-04-10 08:10:12 -04:00
|
|
|
func (s *Site) appendThemeTemplates(in []string) []string {
|
2017-02-19 17:53:48 -05:00
|
|
|
if !s.PathSpec.ThemeSet() {
|
2016-03-17 18:36:11 -04:00
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
out := []string{}
|
|
|
|
// First place all non internal templates
|
|
|
|
for _, t := range in {
|
|
|
|
if !strings.HasPrefix(t, "_internal/") {
|
|
|
|
out = append(out, t)
|
2014-04-10 08:10:12 -04:00
|
|
|
}
|
2016-03-17 18:36:11 -04:00
|
|
|
}
|
2014-04-10 08:10:12 -04:00
|
|
|
|
2016-03-17 18:36:11 -04:00
|
|
|
// Then place theme templates with the same names
|
|
|
|
for _, t := range in {
|
|
|
|
if !strings.HasPrefix(t, "_internal/") {
|
|
|
|
out = append(out, "theme/"+t)
|
2014-04-10 08:10:12 -04:00
|
|
|
}
|
2016-03-17 18:36:11 -04:00
|
|
|
}
|
2014-11-14 12:14:52 -05:00
|
|
|
|
2016-03-17 18:36:11 -04:00
|
|
|
// Lastly place internal templates
|
|
|
|
for _, t := range in {
|
|
|
|
if strings.HasPrefix(t, "_internal/") {
|
|
|
|
out = append(out, t)
|
2014-04-10 08:10:12 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-17 18:36:11 -04:00
|
|
|
return out
|
|
|
|
|
2014-04-10 08:10:12 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:43:04 -04:00
|
|
|
// Stats prints Hugo builds stats to the console.
|
|
|
|
// This is what you see after a successful hugo build.
|
2016-07-28 03:30:58 -04:00
|
|
|
func (s *Site) Stats() {
|
2017-09-27 15:17:43 -04:00
|
|
|
s.Log.FEEDBACK.Printf("\nBuilt site for language %s:\n", s.Language.Lang)
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.FEEDBACK.Println(s.draftStats())
|
|
|
|
s.Log.FEEDBACK.Println(s.futureStats())
|
|
|
|
s.Log.FEEDBACK.Println(s.expiredStats())
|
2017-03-24 04:19:51 -04:00
|
|
|
s.Log.FEEDBACK.Printf("%d regular pages created\n", s.siteStats.pageCountRegular)
|
|
|
|
s.Log.FEEDBACK.Printf("%d other pages created\n", (s.siteStats.pageCount - s.siteStats.pageCountRegular))
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.FEEDBACK.Printf("%d non-page files copied\n", len(s.Files))
|
|
|
|
s.Log.FEEDBACK.Printf("%d paginator pages created\n", s.Info.paginationPageCount)
|
2014-04-05 01:26:43 -04:00
|
|
|
|
2017-03-08 08:19:23 -05:00
|
|
|
if s.isEnabled(KindTaxonomy) {
|
|
|
|
taxonomies := s.Language.GetStringMapString("taxonomies")
|
|
|
|
|
|
|
|
for _, pl := range taxonomies {
|
|
|
|
s.Log.FEEDBACK.Printf("%d %s created\n", len(s.Taxonomies[pl]), pl)
|
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2016-05-14 00:35:16 -04:00
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
|
|
|
|
2017-04-09 04:33:04 -04:00
|
|
|
// GetPage looks up a page of a given type in the path given.
|
2016-11-12 11:30:21 -05:00
|
|
|
// {{ with .Site.GetPage "section" "blog" }}{{ .Title }}{{ end }}
|
|
|
|
//
|
2017-04-09 04:33:04 -04:00
|
|
|
// This will return nil when no page could be found, and will return the
|
|
|
|
// first page found if the key is ambigous.
|
2017-05-22 18:20:31 -04:00
|
|
|
func (s *SiteInfo) GetPage(typ string, path ...string) (*Page, error) {
|
|
|
|
return s.getPage(typ, path...), nil
|
2016-11-12 11:30:21 -05:00
|
|
|
}
|
|
|
|
|
2017-03-23 15:05:10 -04:00
|
|
|
func (s *Site) permalinkForOutputFormat(link string, f output.Format) (string, error) {
|
|
|
|
var (
|
|
|
|
baseURL string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if f.Protocol != "" {
|
|
|
|
baseURL, err = s.PathSpec.BaseURL.WithProtocol(f.Protocol)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
baseURL = s.PathSpec.BaseURL.String()
|
|
|
|
}
|
|
|
|
return s.permalinkForBaseURL(link, baseURL), nil
|
|
|
|
}
|
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
func (s *Site) permalink(link string) string {
|
2017-03-23 15:05:10 -04:00
|
|
|
return s.permalinkForBaseURL(link, s.PathSpec.BaseURL.String())
|
|
|
|
|
|
|
|
}
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2017-03-23 15:05:10 -04:00
|
|
|
func (s *Site) permalinkForBaseURL(link, baseURL string) string {
|
2017-03-09 13:19:29 -05:00
|
|
|
link = strings.TrimPrefix(link, "/")
|
|
|
|
if !strings.HasSuffix(baseURL, "/") {
|
|
|
|
baseURL += "/"
|
|
|
|
}
|
|
|
|
return baseURL + link
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
2016-10-24 07:45:30 -04:00
|
|
|
|
2015-01-30 14:51:06 -05:00
|
|
|
func (s *Site) renderAndWriteXML(name string, dest string, d interface{}, layouts ...string) error {
|
2017-01-10 04:55:03 -05:00
|
|
|
s.Log.DEBUG.Printf("Render XML for %q to %q", name, dest)
|
2015-01-30 14:51:06 -05:00
|
|
|
renderBuffer := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(renderBuffer)
|
|
|
|
renderBuffer.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n")
|
2014-12-18 14:59:39 -05:00
|
|
|
|
2017-04-04 07:32:29 -04:00
|
|
|
if err := s.renderForLayouts(name, d, renderBuffer, layouts...); err != nil {
|
|
|
|
helpers.DistinctWarnLog.Println(err)
|
|
|
|
return nil
|
2016-07-25 16:22:09 -04:00
|
|
|
}
|
|
|
|
|
2015-01-30 14:51:06 -05:00
|
|
|
outBuffer := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(outBuffer)
|
|
|
|
|
2015-05-15 18:11:39 -04:00
|
|
|
var path []byte
|
2017-02-20 03:52:06 -05:00
|
|
|
if s.Info.relativeURLs {
|
2015-05-15 18:11:39 -04:00
|
|
|
path = []byte(helpers.GetDottedRelativePath(dest))
|
|
|
|
} else {
|
2017-11-12 04:03:56 -05:00
|
|
|
s := s.PathSpec.BaseURL.String()
|
2015-05-15 18:11:39 -04:00
|
|
|
if !strings.HasSuffix(s, "/") {
|
|
|
|
s += "/"
|
|
|
|
}
|
|
|
|
path = []byte(s)
|
|
|
|
}
|
|
|
|
transformer := transform.NewChain(transform.AbsURLInXML)
|
2017-04-06 11:39:20 -04:00
|
|
|
if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil {
|
|
|
|
helpers.DistinctErrorLog.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-30 14:51:06 -05:00
|
|
|
|
2017-03-16 05:04:30 -04:00
|
|
|
return s.publish(dest, outBuffer)
|
2015-01-30 14:51:06 -05:00
|
|
|
|
2014-11-04 00:39:37 -05:00
|
|
|
}
|
2013-10-01 22:58:15 -04:00
|
|
|
|
2017-04-02 06:22:54 -04:00
|
|
|
func (s *Site) renderAndWritePage(name string, dest string, p *PageOutput, layouts ...string) error {
|
2015-01-30 15:05:05 -05:00
|
|
|
renderBuffer := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(renderBuffer)
|
|
|
|
|
2017-04-04 07:32:29 -04:00
|
|
|
if err := s.renderForLayouts(p.Kind, p, renderBuffer, layouts...); err != nil {
|
|
|
|
helpers.DistinctWarnLog.Println(err)
|
|
|
|
return nil
|
2016-07-25 16:22:09 -04:00
|
|
|
}
|
|
|
|
|
2017-04-23 16:03:25 -04:00
|
|
|
if renderBuffer.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:05:05 -05:00
|
|
|
outBuffer := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(outBuffer)
|
2014-01-29 17:50:31 -05:00
|
|
|
|
|
|
|
transformLinks := transform.NewEmptyTransforms()
|
|
|
|
|
2017-04-07 05:01:36 -04:00
|
|
|
isHTML := p.outputFormat.IsHTML
|
2014-01-29 17:50:31 -05:00
|
|
|
|
2017-04-07 05:01:36 -04:00
|
|
|
if isHTML {
|
|
|
|
if s.Info.relativeURLs || s.Info.canonifyURLs {
|
|
|
|
transformLinks = append(transformLinks, transform.AbsURL)
|
|
|
|
}
|
2014-05-16 17:49:27 -04:00
|
|
|
|
2017-04-07 05:01:36 -04:00
|
|
|
if s.running() && s.Cfg.GetBool("watch") && !s.Cfg.GetBool("disableLiveReload") {
|
2017-09-23 11:08:48 -04:00
|
|
|
transformLinks = append(transformLinks, transform.LiveReloadInject(s.Cfg.GetInt("liveReloadPort")))
|
2017-04-07 05:01:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// For performance reasons we only inject the Hugo generator tag on the home page.
|
|
|
|
if p.IsHome() {
|
|
|
|
if !s.Cfg.GetBool("disableHugoGeneratorInject") {
|
|
|
|
transformLinks = append(transformLinks, transform.HugoGeneratorInject)
|
|
|
|
}
|
2016-06-04 07:45:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:11:39 -04:00
|
|
|
var path []byte
|
|
|
|
|
2017-02-20 03:52:06 -05:00
|
|
|
if s.Info.relativeURLs {
|
2017-03-09 13:19:29 -05:00
|
|
|
path = []byte(helpers.GetDottedRelativePath(dest))
|
2017-02-19 17:53:48 -05:00
|
|
|
} else if s.Info.canonifyURLs {
|
2017-11-12 04:03:56 -05:00
|
|
|
url := s.PathSpec.BaseURL.String()
|
2017-02-19 17:53:48 -05:00
|
|
|
if !strings.HasSuffix(url, "/") {
|
|
|
|
url += "/"
|
2015-05-15 18:11:39 -04:00
|
|
|
}
|
2017-02-19 17:53:48 -05:00
|
|
|
path = []byte(url)
|
2015-05-15 18:11:39 -04:00
|
|
|
}
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
transformer := transform.NewChain(transformLinks...)
|
2017-04-06 11:39:20 -04:00
|
|
|
if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil {
|
|
|
|
helpers.DistinctErrorLog.Println(err)
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-30 15:05:05 -05:00
|
|
|
|
2017-04-04 07:32:29 -04:00
|
|
|
return s.publish(dest, outBuffer)
|
2014-11-04 00:39:37 -05:00
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
|
2017-07-14 04:26:51 -04:00
|
|
|
func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts ...string) (err error) {
|
|
|
|
var templ tpl.Template
|
|
|
|
|
2017-07-14 04:08:04 -04:00
|
|
|
defer func() {
|
2017-07-14 04:26:51 -04:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
templName := ""
|
|
|
|
if templ != nil {
|
|
|
|
templName = templ.Name()
|
|
|
|
}
|
|
|
|
helpers.DistinctErrorLog.Printf("Failed to render %q: %s", templName, r)
|
|
|
|
// TOD(bep) we really need to fix this. Also see below.
|
|
|
|
if !s.running() && !testMode {
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 04:08:04 -04:00
|
|
|
}()
|
2017-07-14 04:26:51 -04:00
|
|
|
|
|
|
|
templ = s.findFirstTemplate(layouts...)
|
2017-03-27 14:43:49 -04:00
|
|
|
if templ == nil {
|
2017-04-04 07:32:29 -04:00
|
|
|
return fmt.Errorf("[%s] Unable to locate layout for %q: %s\n", s.Language.Lang, name, layouts)
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
|
2017-07-14 04:26:51 -04:00
|
|
|
if err = templ.Execute(w, d); err != nil {
|
2014-01-29 17:50:31 -05:00
|
|
|
// Behavior here should be dependent on if running in server or watch mode.
|
2017-04-02 06:22:54 -04:00
|
|
|
helpers.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err)
|
2016-04-07 14:03:03 -04:00
|
|
|
if !s.running() && !testMode {
|
2016-03-09 08:05:31 -05:00
|
|
|
// TODO(bep) check if this can be propagated
|
2014-01-29 17:50:31 -05:00
|
|
|
os.Exit(-1)
|
2016-03-09 08:05:31 -05:00
|
|
|
} else if testMode {
|
2017-07-14 04:26:51 -04:00
|
|
|
return
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 04:26:51 -04:00
|
|
|
return
|
2013-10-01 22:58:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (s *Site) findFirstTemplate(layouts ...string) tpl.Template {
|
2014-04-08 23:15:57 -04:00
|
|
|
for _, layout := range layouts {
|
2017-03-27 14:43:49 -04:00
|
|
|
if templ := s.Tmpl.Lookup(layout); templ != nil {
|
|
|
|
return templ
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
return nil
|
2013-08-27 05:09:50 -04:00
|
|
|
}
|
|
|
|
|
2017-03-16 04:09:26 -04:00
|
|
|
func (s *Site) publish(path string, r io.Reader) (err error) {
|
2017-03-16 05:04:30 -04:00
|
|
|
path = filepath.Join(s.absPublishDir(), path)
|
2017-03-16 04:09:26 -04:00
|
|
|
return helpers.WriteToDisk(path, r, s.Fs.Destination)
|
|
|
|
}
|
|
|
|
|
2014-08-20 11:09:35 -04:00
|
|
|
func (s *Site) draftStats() string {
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
switch s.draftCount {
|
|
|
|
case 0:
|
2015-03-12 10:46:42 -04:00
|
|
|
return "0 draft content"
|
2014-08-20 11:09:35 -04:00
|
|
|
case 1:
|
2015-03-12 10:46:42 -04:00
|
|
|
msg = "1 draft rendered"
|
2014-08-20 11:09:35 -04:00
|
|
|
default:
|
|
|
|
msg = fmt.Sprintf("%d drafts rendered", s.draftCount)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if s.Cfg.GetBool("buildDrafts") {
|
2014-08-20 11:09:35 -04:00
|
|
|
return fmt.Sprintf("%d of ", s.draftCount) + msg
|
2014-08-25 13:13:11 -04:00
|
|
|
}
|
2014-08-20 11:09:35 -04:00
|
|
|
|
|
|
|
return "0 of " + msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) futureStats() string {
|
2014-08-25 13:13:11 -04:00
|
|
|
var msg string
|
|
|
|
|
|
|
|
switch s.futureCount {
|
|
|
|
case 0:
|
2015-08-04 13:59:32 -04:00
|
|
|
return "0 future content"
|
2014-08-25 13:13:11 -04:00
|
|
|
case 1:
|
2015-08-04 13:59:32 -04:00
|
|
|
msg = "1 future rendered"
|
2014-08-25 13:13:11 -04:00
|
|
|
default:
|
2016-06-13 11:38:39 -04:00
|
|
|
msg = fmt.Sprintf("%d futures rendered", s.futureCount)
|
2014-08-25 13:13:11 -04:00
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if s.Cfg.GetBool("buildFuture") {
|
2014-08-25 13:13:11 -04:00
|
|
|
return fmt.Sprintf("%d of ", s.futureCount) + msg
|
|
|
|
}
|
|
|
|
|
|
|
|
return "0 of " + msg
|
2014-08-20 11:09:35 -04:00
|
|
|
}
|
2014-08-29 13:40:21 -04:00
|
|
|
|
2016-05-19 15:17:36 -04:00
|
|
|
func (s *Site) expiredStats() string {
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
switch s.expiredCount {
|
|
|
|
case 0:
|
|
|
|
return "0 expired content"
|
|
|
|
case 1:
|
|
|
|
msg = "1 expired rendered"
|
|
|
|
default:
|
|
|
|
msg = fmt.Sprintf("%d expired rendered", s.expiredCount)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if s.Cfg.GetBool("buildExpired") {
|
2016-05-19 15:17:36 -04:00
|
|
|
return fmt.Sprintf("%d of ", s.expiredCount) + msg
|
|
|
|
}
|
|
|
|
|
|
|
|
return "0 of " + msg
|
|
|
|
}
|
|
|
|
|
2014-08-29 13:40:21 -04:00
|
|
|
func getGoMaxProcs() int {
|
|
|
|
if gmp := os.Getenv("GOMAXPROCS"); gmp != "" {
|
|
|
|
if p, err := strconv.Atoi(gmp); err != nil {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
2016-11-16 15:06:10 -05:00
|
|
|
|
2017-03-17 11:35:09 -04:00
|
|
|
func (s *Site) newNodePage(typ string, sections ...string) *Page {
|
2017-03-06 07:18:33 -05:00
|
|
|
p := &Page{
|
2017-02-04 22:20:06 -05:00
|
|
|
language: s.Language,
|
2016-11-16 15:06:10 -05:00
|
|
|
pageInit: &pageInit{},
|
|
|
|
Kind: typ,
|
|
|
|
Data: make(map[string]interface{}),
|
|
|
|
Site: &s.Info,
|
2017-03-17 11:35:09 -04:00
|
|
|
sections: sections,
|
2017-01-03 11:28:51 -05:00
|
|
|
s: s}
|
2017-03-17 11:35:09 -04:00
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
p.outputFormats = p.s.outputFormats[p.Kind]
|
2017-03-17 11:35:09 -04:00
|
|
|
|
2017-03-06 07:18:33 -05:00
|
|
|
return p
|
|
|
|
|
2016-11-16 15:06:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) newHomePage() *Page {
|
|
|
|
p := s.newNodePage(KindHome)
|
|
|
|
p.Title = s.Info.Title
|
|
|
|
pages := Pages{}
|
|
|
|
p.Data["Pages"] = pages
|
|
|
|
p.Pages = pages
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) newTaxonomyPage(plural, key string) *Page {
|
|
|
|
|
2017-03-17 11:35:09 -04:00
|
|
|
p := s.newNodePage(KindTaxonomy, plural, key)
|
2016-11-16 15:06:10 -05:00
|
|
|
|
|
|
|
if s.Info.preserveTaxonomyNames {
|
2017-02-23 04:03:48 -05:00
|
|
|
// Keep (mostly) as is in the title
|
|
|
|
// We make the first character upper case, mostly because
|
|
|
|
// it is easier to reason about in the tests.
|
|
|
|
p.Title = helpers.FirstUpper(key)
|
2017-01-10 04:55:03 -05:00
|
|
|
key = s.PathSpec.MakePathSanitized(key)
|
2016-11-16 15:06:10 -05:00
|
|
|
} else {
|
2017-07-30 11:46:04 -04:00
|
|
|
p.Title = strings.Replace(s.titleFunc(key), "-", " ", -1)
|
2016-11-16 15:06:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2017-04-09 04:33:04 -04:00
|
|
|
func (s *Site) newSectionPage(name string) *Page {
|
2017-03-17 11:35:09 -04:00
|
|
|
p := s.newNodePage(KindSection, name)
|
2016-11-16 15:06:10 -05:00
|
|
|
|
2017-04-09 04:33:04 -04:00
|
|
|
sectionName := helpers.FirstUpper(name)
|
2017-02-04 22:20:06 -05:00
|
|
|
if s.Cfg.GetBool("pluralizeListTitles") {
|
2016-11-16 15:06:10 -05:00
|
|
|
p.Title = inflect.Pluralize(sectionName)
|
|
|
|
} else {
|
|
|
|
p.Title = sectionName
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) newTaxonomyTermsPage(plural string) *Page {
|
2017-03-17 11:35:09 -04:00
|
|
|
p := s.newNodePage(KindTaxonomyTerm, plural)
|
2017-07-30 11:46:04 -04:00
|
|
|
p.Title = s.titleFunc(plural)
|
2016-11-16 15:06:10 -05:00
|
|
|
return p
|
|
|
|
}
|