2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
hugolib: Extract date and slug from filename
This commit makes it possible to extract the date from the content filename. Also, the filenames in these cases will make for very poor permalinks, so we will also use the remaining part as the page `slug` if that value is not set in front matter.
This should make it easier to move content from Jekyll to Hugo.
To enable, put this in your `config.toml`:
```toml
[frontmatter]
date = [":filename", ":default"]
```
This commit is also a spring cleaning of how the different dates are configured in Hugo. Hugo will check for dates following the configuration from left to right, starting with `:filename` etc.
So, if you want to use the `file modification time`, this can be a good configuration:
```toml
[frontmatter]
date = [ "date",":fileModTime", ":default"]
lastmod = ["lastmod" ,":fileModTime", ":default"]
```
The current `:default` values for the different dates are
```toml
[frontmatter]
date = ["date","publishDate", "lastmod"]
lastmod = ["lastmod", "date","publishDate"]
publishDate = ["publishDate", "date"]
expiryDate = ["expiryDate"]
```
The above will now be the same as:
```toml
[frontmatter]
date = [":default"]
lastmod = [":default"]
publishDate = [":default"]
expiryDate = [":default"]
```
Note:
* We have some built-in aliases to the above: lastmod => modified, publishDate => pubdate, published and expiryDate => unpublishdate.
* If you want a new configuration for, say, `date`, you can provide only that line, and the rest will be preserved.
* All the keywords to the right that does not start with a ":" maps to front matter parameters, and can be any date param (e.g. `myCustomDateParam`).
* The keywords to the left are the **4 predefined dates in Hugo**, i.e. they are constant values.
* The current "special date handlers" are `:fileModTime` and `:filename`. We will soon add `:git` to that list.
Fixes #285
Closes #3310
Closes #3762
Closes #4340
2018-03-11 06:32:55 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pagemeta
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
import (
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2020-03-20 04:37:21 -04:00
|
|
|
const (
|
|
|
|
Never = "never"
|
|
|
|
Always = "always"
|
|
|
|
ListLocally = "local"
|
2020-10-06 05:19:31 -04:00
|
|
|
Link = "link"
|
2020-03-20 04:37:21 -04:00
|
|
|
)
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
var defaultBuildConfig = BuildConfig{
|
2020-03-20 04:37:21 -04:00
|
|
|
List: Always,
|
2020-10-06 05:19:31 -04:00
|
|
|
Render: Always,
|
2019-09-10 05:26:34 -04:00
|
|
|
PublishResources: true,
|
|
|
|
set: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildConfig holds configuration options about how to handle a Page in Hugo's
|
|
|
|
// build process.
|
|
|
|
type BuildConfig struct {
|
|
|
|
// Whether to add it to any of the page collections.
|
2020-03-20 04:37:21 -04:00
|
|
|
// Note that the page can always be found with .Site.GetPage.
|
|
|
|
// Valid values: never, always, local.
|
|
|
|
// Setting it to 'local' means they will be available via the local
|
|
|
|
// page collections, e.g. $section.Pages.
|
|
|
|
// Note: before 0.57.2 this was a bool, so we accept those too.
|
|
|
|
List string
|
2019-09-10 05:26:34 -04:00
|
|
|
|
|
|
|
// Whether to render it.
|
2020-10-06 05:19:31 -04:00
|
|
|
// Valid values: never, always, link.
|
|
|
|
// The value link means it will not be rendered, but it will get a RelPermalink/Permalink.
|
|
|
|
// Note that before 0.76.0 this was a bool, so we accept those too.
|
|
|
|
Render string
|
2019-09-10 05:26:34 -04:00
|
|
|
|
|
|
|
// Whether to publish its resources. These will still be published on demand,
|
|
|
|
// but enabling this can be useful if the originals (e.g. images) are
|
|
|
|
// never used.
|
|
|
|
PublishResources bool
|
|
|
|
|
|
|
|
set bool // BuildCfg is non-zero if this is set to true.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable sets all options to their off value.
|
|
|
|
func (b *BuildConfig) Disable() {
|
2020-03-20 04:37:21 -04:00
|
|
|
b.List = Never
|
2020-10-06 05:19:31 -04:00
|
|
|
b.Render = Never
|
2019-09-10 05:26:34 -04:00
|
|
|
b.PublishResources = false
|
|
|
|
b.set = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BuildConfig) IsZero() bool {
|
|
|
|
return !b.set
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func DecodeBuildConfig(m any) (BuildConfig, error) {
|
2019-09-10 05:26:34 -04:00
|
|
|
b := defaultBuildConfig
|
|
|
|
if m == nil {
|
|
|
|
return b, nil
|
|
|
|
}
|
2020-03-20 04:37:21 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
err := mapstructure.WeakDecode(m, &b)
|
2020-03-20 04:37:21 -04:00
|
|
|
|
|
|
|
// In 0.67.1 we changed the list attribute from a bool to a string (enum).
|
|
|
|
// Bool values will become 0 or 1.
|
|
|
|
switch b.List {
|
|
|
|
case "0":
|
|
|
|
b.List = Never
|
|
|
|
case "1":
|
|
|
|
b.List = Always
|
|
|
|
case Always, Never, ListLocally:
|
|
|
|
default:
|
|
|
|
b.List = Always
|
|
|
|
}
|
|
|
|
|
2020-10-06 05:19:31 -04:00
|
|
|
// In 0.76.0 we changed the Render from bool to a string.
|
|
|
|
switch b.Render {
|
|
|
|
case "0":
|
|
|
|
b.Render = Never
|
|
|
|
case "1":
|
|
|
|
b.Render = Always
|
|
|
|
case Always, Never, Link:
|
|
|
|
default:
|
|
|
|
b.Render = Always
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
return b, err
|
|
|
|
}
|