diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-09-19 07:48:17 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-09-23 19:27:23 +0200 |
commit | 2650fa772b40846d9965f8c5f169286411f3beb2 (patch) | |
tree | 26d209ebea23611c146d851cb12827e793eaf6d5 /commands | |
parent | ef525b15d4584886b52428bd7a35de835ab07a48 (diff) |
Add directory based archetypes
Given this content:
```bash
archetypes
├── default.md
└── post-bundle
├── bio.md
├── images
│ └── featured.jpg
└── index.md
```
```bash
hugo new --kind post-bundle post/my-post
```
Will create a new folder in `/content/post/my-post` with the same set of files as in the `post-bundle` archetypes folder.
This commit also improves the archetype language detection, so, if you use template code in your content files, the `.Site` you get is for the correct language. This also means that it is now possible to translate strings defined in the `i18n` bundles, e.g. `{{ i18n "hello" }}`.
Fixes #4535
Diffstat (limited to 'commands')
-rw-r--r-- | commands/new.go | 45 | ||||
-rw-r--r-- | commands/new_content_test.go | 2 |
2 files changed, 11 insertions, 36 deletions
diff --git a/commands/new.go b/commands/new.go index e70658511..f6e944397 100644 --- a/commands/new.go +++ b/commands/new.go @@ -85,45 +85,13 @@ func (n *newCmd) newContent(cmd *cobra.Command, args []string) error { var kind string - createPath, kind = newContentPathSection(createPath) + createPath, kind = newContentPathSection(c.hugo, createPath) if n.contentType != "" { kind = n.contentType } - cfg := c.DepsCfg - - ps, err := helpers.NewPathSpec(cfg.Fs, cfg.Cfg) - if err != nil { - 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.hugo.Build(hugolib.BuildCfg{SkipRender: true}); err != nil { - return nil, err - } - - s = c.hugo.Sites[0] - - if len(c.hugo.Sites) > 1 { - // Find the best match. - for _, ss := range c.hugo.Sites { - if strings.Contains(createPath, "."+ss.Language.Lang) { - s = ss - break - } - } - } - return s, nil - } - - return create.NewContent(ps, siteFactory, kind, createPath) + return create.NewContent(c.hugo, kind, createPath) } func mkdir(x ...string) { @@ -144,10 +112,17 @@ func touchFile(fs afero.Fs, x ...string) { } } -func newContentPathSection(path string) (string, string) { +func newContentPathSection(h *hugolib.HugoSites, path string) (string, string) { // Forward slashes is used in all examples. Convert if needed. // Issue #1133 createpath := filepath.FromSlash(path) + + if h != nil { + for _, s := range h.Sites { + createpath = strings.TrimPrefix(createpath, s.PathSpec.ContentDir) + } + } + var section string // assume the first directory is the section (kind) if strings.Contains(createpath[1:], helpers.FilePathSeparator) { diff --git a/commands/new_content_test.go b/commands/new_content_test.go index 364e0f783..fb8bca7b4 100644 --- a/commands/new_content_test.go +++ b/commands/new_content_test.go @@ -25,7 +25,7 @@ import ( // Issue #1133 func TestNewContentPathSectionWithForwardSlashes(t *testing.T) { - p, s := newContentPathSection("/post/new.md") + p, s := newContentPathSection(nil, "/post/new.md") assert.Equal(t, filepath.FromSlash("/post/new.md"), p) assert.Equal(t, "post", s) } |