summaryrefslogtreecommitdiffstats
path: root/create
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-03-01 15:01:25 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-06-10 23:55:20 +0200
commit80230f26a3020ff33bac2bef01b2c0e314b89f86 (patch)
tree6da3114350477866065d265d6e1db9cb55639dc1 /create
parent6464981adb4d7d0f41e8e2c987342082982210a1 (diff)
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
Diffstat (limited to 'create')
-rw-r--r--create/content.go53
-rw-r--r--create/content_template_handler.go7
-rw-r--r--create/content_test.go13
3 files changed, 33 insertions, 40 deletions
diff --git a/create/content.go b/create/content.go
index 29fe47394..6d022282e 100644
--- a/create/content.go
+++ b/create/content.go
@@ -16,6 +16,7 @@ package create
import (
"bytes"
+ "fmt"
"os"
"os/exec"
"path/filepath"
@@ -31,6 +32,7 @@ func NewContent(
ps *helpers.PathSpec,
siteFactory func(filename string, siteUsed bool) (*hugolib.Site, error), kind, targetPath string) error {
ext := helpers.Ext(targetPath)
+ fs := ps.BaseFs.SourceFilesystems.Archetypes.Fs
jww.INFO.Printf("attempting to create %q of %q of ext %q", targetPath, kind, ext)
@@ -40,9 +42,9 @@ func NewContent(
siteUsed := false
if archetypeFilename != "" {
- f, err := ps.Fs.Source.Open(archetypeFilename)
+ f, err := fs.Open(archetypeFilename)
if err != nil {
- return err
+ return fmt.Errorf("failed to open archetype file: %s", err)
}
defer f.Close()
@@ -71,7 +73,7 @@ func NewContent(
targetDir := filepath.Dir(targetPath)
if targetDir != "" && targetDir != "." {
- exists, _ = helpers.Exists(targetDir, ps.Fs.Source)
+ exists, _ = helpers.Exists(targetDir, fs)
}
if exists {
@@ -101,42 +103,27 @@ func NewContent(
return nil
}
-// FindArchetype takes a given kind/archetype of content and returns an output
-// path for that archetype. If no archetype is found, an empty string is
-// returned.
+// FindArchetype takes a given kind/archetype of content and returns the path
+// to the archetype in the archetype filesystem, blank if none found.
func findArchetype(ps *helpers.PathSpec, kind, ext string) (outpath string) {
- search := []string{ps.AbsPathify(ps.Cfg.GetString("archetypeDir"))}
+ fs := ps.BaseFs.Archetypes.Fs
- if ps.Cfg.GetString("theme") != "" {
- themeDir := filepath.Join(ps.AbsPathify(ps.Cfg.GetString("themesDir")+"/"+ps.Cfg.GetString("theme")), "/archetypes/")
- if _, err := ps.Fs.Source.Stat(themeDir); os.IsNotExist(err) {
- jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", ps.Cfg.GetString("theme"), themeDir)
+ // If the new content isn't in a subdirectory, kind == "".
+ // Therefore it should be excluded otherwise `is a directory`
+ // error will occur. github.com/gohugoio/hugo/issues/411
+ var pathsToCheck = []string{"default"}
+
+ if ext != "" {
+ if kind != "" {
+ pathsToCheck = append([]string{kind + ext, "default" + ext}, pathsToCheck...)
} else {
- search = append(search, themeDir)
+ pathsToCheck = append([]string{"default" + ext}, pathsToCheck...)
}
}
- for _, x := range search {
- // If the new content isn't in a subdirectory, kind == "".
- // Therefore it should be excluded otherwise `is a directory`
- // error will occur. github.com/gohugoio/hugo/issues/411
- var pathsToCheck = []string{"default"}
-
- if ext != "" {
- if kind != "" {
- pathsToCheck = append([]string{kind + ext, "default" + ext}, pathsToCheck...)
- } else {
- pathsToCheck = append([]string{"default" + ext}, pathsToCheck...)
- }
- }
-
- for _, p := range pathsToCheck {
- curpath := filepath.Join(x, p)
- jww.DEBUG.Println("checking", curpath, "for archetypes")
- if exists, _ := helpers.Exists(curpath, ps.Fs.Source); exists {
- jww.INFO.Println("curpath: " + curpath)
- return curpath
- }
+ for _, p := range pathsToCheck {
+ if exists, _ := helpers.Exists(p, fs); exists {
+ return p
}
}
diff --git a/create/content_template_handler.go b/create/content_template_handler.go
index 17e52cae0..37eed52cf 100644
--- a/create/content_template_handler.go
+++ b/create/content_template_handler.go
@@ -89,10 +89,11 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
)
ps, err := helpers.NewPathSpec(s.Deps.Fs, s.Deps.Cfg)
- sp := source.NewSourceSpec(ps, ps.Fs.Source)
if err != nil {
return nil, err
}
+ sp := source.NewSourceSpec(ps, ps.Fs.Source)
+
f := sp.NewFileInfo("", targetPath, false, nil)
name := f.TranslationBaseName()
@@ -115,9 +116,9 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
// TODO(bep) archetype revive the issue about wrong tpl funcs arg order
archetypeTemplate = []byte(ArchetypeTemplateTemplate)
} else {
- archetypeTemplate, err = afero.ReadFile(s.Fs.Source, archetypeFilename)
+ archetypeTemplate, err = afero.ReadFile(s.BaseFs.Archetypes.Fs, archetypeFilename)
if err != nil {
- return nil, fmt.Errorf("Failed to read archetype file %q: %s", archetypeFilename, err)
+ return nil, fmt.Errorf("failed to read archetype file %s", err)
}
}
diff --git a/create/content_test.go b/create/content_test.go
index 62d5ed1da..e9d46becf 100644
--- a/create/content_test.go
+++ b/create/content_test.go
@@ -58,17 +58,15 @@ func TestNewContent(t *testing.T) {
for _, c := range cases {
cfg, fs := newTestCfg()
- ps, err := helpers.NewPathSpec(fs, cfg)
- require.NoError(t, err)
+ require.NoError(t, initFs(fs))
h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs})
require.NoError(t, err)
- require.NoError(t, initFs(fs))
siteFactory := func(filename string, siteUsed bool) (*hugolib.Site, error) {
return h.Sites[0], nil
}
- require.NoError(t, create.NewContent(ps, siteFactory, c.kind, c.path))
+ require.NoError(t, create.NewContent(h.PathSpec, siteFactory, c.kind, c.path))
fname := filepath.Join("content", filepath.FromSlash(c.path))
content := readFileFromFs(t, fs.Source, fname)
@@ -89,6 +87,7 @@ func initViper(v *viper.Viper) {
v.Set("layoutDir", "layouts")
v.Set("i18nDir", "i18n")
v.Set("theme", "sample")
+ v.Set("archetypeDir", "archetypes")
}
func initFs(fs *hugofs.Fs) error {
@@ -187,6 +186,12 @@ func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string {
func newTestCfg() (*viper.Viper, *hugofs.Fs) {
v := viper.New()
+ v.Set("contentDir", "content")
+ v.Set("dataDir", "data")
+ v.Set("i18nDir", "i18n")
+ v.Set("layoutDir", "layouts")
+ v.Set("archetypeDir", "archetypes")
+
fs := hugofs.NewMem(v)
v.SetFs(fs.Source)