diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-06-18 19:39:42 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-06-19 10:47:00 +0200 |
commit | 662e12f348a638a6fcc92a416ee7f7c2a7ef8792 (patch) | |
tree | f7ffad68d882a9e25e08c5405c41062e078e76d7 /commands/new.go | |
parent | 422057f60709696bbbd1c38c9ead2bf114d47e31 (diff) |
commands, create: Add .Site to the archetype templates
This commit completes the "The Revival of the Archetypes!"
If `.Site` is used in the arcetype template, the site is built and added to the template context.
Note that this may be potentially time consuming for big sites.
A more complete example would then be for the section `newsletter` and the archetype file `archetypes/newsletter.md`:
```
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
tags:
- x
categories:
- x
draft: true
---
<!--more-->
{{ range first 10 ( where .Site.RegularPages "Type" "cool" ) }}
* {{ .Title }}
{{ end }}
```
And then create a new post with:
```bash
hugo new newsletter/the-latest-cool.stuff.md
```
**Hot Tip:** If you set the `newContentEditor` configuration variable to an editor on your `PATH`, the newly created article will be opened.
The above _newsletter type archetype_ illustrates the possibilities: The full Hugo `.Site` and all of Hugo's template funcs can be used in the archetype file.
Fixes #1629
Diffstat (limited to 'commands/new.go')
-rw-r--r-- | commands/new.go | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/commands/new.go b/commands/new.go index 4288b6b08..aaae5c6ed 100644 --- a/commands/new.go +++ b/commands/new.go @@ -115,13 +115,40 @@ func NewContent(cmd *cobra.Command, args []string) error { kind = contentType } - s, err := hugolib.NewSite(*cfg) - + ps, err := helpers.NewPathSpec(cfg.Fs, cfg.Cfg) if err != nil { - return newSystemError(err) + return err + } + + // If a site isn't in use in the archetype template, we can skip the build. + siteFactory := func(filename string, siteUsed bool) (*hugolib.Site, error) { + if !siteUsed { + return hugolib.NewSite(*cfg) + } + var s *hugolib.Site + if err := c.initSites(); err != nil { + return nil, err + } + + if err := Hugo.Build(hugolib.BuildCfg{SkipRender: true, PrintStats: false}); err != nil { + return nil, err + } + + s = Hugo.Sites[0] + + if len(Hugo.Sites) > 1 { + // Find the best match. + for _, ss := range Hugo.Sites { + if strings.Contains(createPath, "."+ss.Language.Lang) { + s = ss + break + } + } + } + return s, nil } - return create.NewContent(s, kind, createPath) + return create.NewContent(ps, siteFactory, kind, createPath) } func doNewSite(fs *hugofs.Fs, basepath string, force bool) error { |