2017-06-18 13:06:28 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package create
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2018-01-28 13:38:10 -05:00
|
|
|
"path/filepath"
|
2017-06-23 03:29:59 -04:00
|
|
|
"strings"
|
2017-06-18 13:06:28 -04:00
|
|
|
"time"
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2017-06-18 13:39:42 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-18 13:06:28 -04:00
|
|
|
"github.com/gohugoio/hugo/source"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugolib"
|
|
|
|
"github.com/gohugoio/hugo/tpl"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
2017-06-18 13:39:42 -04:00
|
|
|
// ArchetypeFileData represents the data available to an archetype template.
|
|
|
|
type ArchetypeFileData struct {
|
|
|
|
// The archetype content type, either given as --kind option or extracted
|
|
|
|
// from the target path's section, i.e. "blog/mypost.md" will resolve to
|
|
|
|
// "blog".
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// The current date and time as a RFC3339 formatted string, suitable for use in front matter.
|
|
|
|
Date string
|
|
|
|
|
|
|
|
// The Site, fully equipped with all the pages etc. Note: This will only be set if it is actually
|
|
|
|
// used in the archetype template. Also, if this is a multilingual setup,
|
|
|
|
// this site is the site that best matches the target content file, based
|
|
|
|
// on the presence of language code in the filename.
|
2018-05-25 11:35:06 -04:00
|
|
|
Site *hugolib.SiteInfo
|
2017-06-18 13:39:42 -04:00
|
|
|
|
2018-01-28 13:38:10 -05:00
|
|
|
// Name will in most cases be the same as TranslationBaseName, e.g. "my-post".
|
|
|
|
// But if that value is "index" (bundles), the Name is instead the owning folder.
|
|
|
|
// This is the value you in most cases would want to use to construct the title in your
|
|
|
|
// archetype template.
|
|
|
|
Name string
|
|
|
|
|
2017-06-18 13:39:42 -04:00
|
|
|
// The target content file. Note that the .Content will be empty, as that
|
|
|
|
// has not been created yet.
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
source.File
|
2017-06-18 13:39:42 -04:00
|
|
|
}
|
|
|
|
|
2017-06-18 13:06:28 -04:00
|
|
|
const (
|
2017-08-07 14:19:24 -04:00
|
|
|
// ArchetypeTemplateTemplate is used as initial template when adding an archetype template.
|
2017-06-22 14:39:55 -04:00
|
|
|
ArchetypeTemplateTemplate = `---
|
2018-01-28 13:38:10 -05:00
|
|
|
title: "{{ replace .Name "-" " " | title }}"
|
2017-06-22 14:30:01 -04:00
|
|
|
date: {{ .Date }}
|
|
|
|
draft: true
|
2017-06-22 14:39:55 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
`
|
2017-06-18 13:06:28 -04:00
|
|
|
)
|
|
|
|
|
2017-06-23 03:29:59 -04:00
|
|
|
var (
|
|
|
|
archetypeShortcodeReplacementsPre = strings.NewReplacer(
|
|
|
|
"{{<", "{x{<",
|
|
|
|
"{{%", "{x{%",
|
|
|
|
">}}", ">}x}",
|
|
|
|
"%}}", "%}x}")
|
|
|
|
|
|
|
|
archetypeShortcodeReplacementsPost = strings.NewReplacer(
|
|
|
|
"{x{<", "{{<",
|
|
|
|
"{x{%", "{{%",
|
|
|
|
">}x}", ">}}",
|
|
|
|
"%}x}", "%}}")
|
|
|
|
)
|
|
|
|
|
2018-09-19 01:48:17 -04:00
|
|
|
func executeArcheTypeAsTemplate(s *hugolib.Site, name, kind, targetPath, archetypeFilename string) ([]byte, error) {
|
2017-06-18 13:06:28 -04:00
|
|
|
var (
|
|
|
|
archetypeContent []byte
|
|
|
|
archetypeTemplate []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
f, err := s.SourceSpec.NewFileInfoFrom(targetPath, targetPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-18 13:06:28 -04:00
|
|
|
|
2018-09-19 01:48:17 -04:00
|
|
|
if name == "" {
|
|
|
|
name = f.TranslationBaseName()
|
2018-03-21 12:21:46 -04:00
|
|
|
|
2018-09-19 01:48:17 -04:00
|
|
|
if name == "index" || name == "_index" {
|
|
|
|
// Page bundles; the directory name will hopefully have a better name.
|
|
|
|
dir := strings.TrimSuffix(f.Dir(), helpers.FilePathSeparator)
|
|
|
|
_, name = filepath.Split(dir)
|
|
|
|
}
|
2018-01-28 13:38:10 -05:00
|
|
|
}
|
|
|
|
|
2017-06-18 13:39:42 -04:00
|
|
|
data := ArchetypeFileData{
|
2017-06-18 13:06:28 -04:00
|
|
|
Type: kind,
|
|
|
|
Date: time.Now().Format(time.RFC3339),
|
2018-01-28 13:38:10 -05:00
|
|
|
Name: name,
|
2017-06-18 13:06:28 -04:00
|
|
|
File: f,
|
2019-09-10 05:26:34 -04:00
|
|
|
Site: s.Info,
|
2017-06-18 13:06:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if archetypeFilename == "" {
|
|
|
|
// TODO(bep) archetype revive the issue about wrong tpl funcs arg order
|
2017-06-22 14:39:55 -04:00
|
|
|
archetypeTemplate = []byte(ArchetypeTemplateTemplate)
|
2017-06-18 13:06:28 -04:00
|
|
|
} else {
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
archetypeTemplate, err = afero.ReadFile(s.BaseFs.Archetypes.Fs, archetypeFilename)
|
2017-06-18 13:06:28 -04:00
|
|
|
if err != nil {
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
return nil, fmt.Errorf("failed to read archetype file %s", err)
|
2017-06-18 13:06:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-23 03:29:59 -04:00
|
|
|
// The archetype template may contain shortcodes, and these does not play well
|
|
|
|
// with the Go templates. Need to set some temporary delimiters.
|
|
|
|
archetypeTemplate = []byte(archetypeShortcodeReplacementsPre.Replace(string(archetypeTemplate)))
|
|
|
|
|
2017-06-18 13:06:28 -04:00
|
|
|
// Reuse the Hugo template setup to get the template funcs properly set up.
|
2020-01-15 09:59:56 -05:00
|
|
|
templateHandler := s.Deps.Tmpl().(tpl.TemplateManager)
|
|
|
|
templateName := helpers.Filename(archetypeFilename)
|
|
|
|
if err := templateHandler.AddTemplate("_text/"+templateName, string(archetypeTemplate)); err != nil {
|
2018-10-03 08:58:09 -04:00
|
|
|
return nil, errors.Wrapf(err, "Failed to parse archetype file %q:", archetypeFilename)
|
2017-06-18 13:06:28 -04:00
|
|
|
}
|
|
|
|
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
templ, _ := templateHandler.Lookup(templateName)
|
2017-06-18 13:06:28 -04:00
|
|
|
|
|
|
|
var buff bytes.Buffer
|
2019-12-10 13:56:44 -05:00
|
|
|
if err := templateHandler.Execute(templ, &buff, data); err != nil {
|
2018-10-03 08:58:09 -04:00
|
|
|
return nil, errors.Wrapf(err, "Failed to process archetype file %q:", archetypeFilename)
|
2017-06-18 13:06:28 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 03:29:59 -04:00
|
|
|
archetypeContent = []byte(archetypeShortcodeReplacementsPost.Replace(buff.String()))
|
2017-06-18 13:06:28 -04:00
|
|
|
|
|
|
|
return archetypeContent, nil
|
|
|
|
}
|