Repository: gohugoio/hugo (language: go)

Issue:
Reorganize theme skeleton to match new template system



Discussion / hints:

Fix patch:
diff --git a/commands/new.go b/commands/new.go
index 901ea02d615..fdc1f65f2fc 100644
--- a/commands/new.go
+++ b/commands/new.go
@@ -144,7 +144,7 @@ according to your needs.`,
 					createpath := paths.AbsPathify(conf.configs.Base.WorkingDir, filepath.Join(conf.configs.Base.ThemesDir, args[0]))
 					r.Println("Creating new theme in", createpath)
 
-					err = skeletons.CreateTheme(createpath, sourceFs)
+					err = skeletons.CreateTheme(createpath, sourceFs, format)
 					if err != nil {
 						return err
 					}
@@ -152,7 +152,14 @@ according to your needs.`,
 					return nil
 				},
 				withc: func(cmd *cobra.Command, r *rootCommand) {
-					cmd.ValidArgsFunction = cobra.NoFileCompletions
+					cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
+						if len(args) != 0 {
+							return []string{}, cobra.ShellCompDirectiveNoFileComp
+						}
+						return []string{}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveFilterDirs
+					}
+					cmd.Flags().StringVar(&format, "format", "toml", "preferred file format (toml, yaml or json)")
+					_ = cmd.RegisterFlagCompletionFunc("format", cobra.FixedCompletions([]string{"toml", "yaml", "json"}, cobra.ShellCompDirectiveNoFileComp))
 				},
 			},
 		},
diff --git a/create/skeletons/skeletons.go b/create/skeletons/skeletons.go
index 54b6695226d..a6241ef926c 100644
--- a/create/skeletons/skeletons.go
+++ b/create/skeletons/skeletons.go
@@ -34,10 +34,60 @@ var siteFs embed.FS
 var themeFs embed.FS
 
 // CreateTheme creates a theme skeleton.
-func CreateTheme(createpath string, sourceFs afero.Fs) error {
+func CreateTheme(createpath string, sourceFs afero.Fs, format string) error {
 	if exists, _ := helpers.Exists(createpath, sourceFs); exists {
 		return errors.New(createpath + " already exists")
 	}
+
+	format = strings.ToLower(format)
+
+	siteConfig := map[string]any{
+		"baseURL":      "https://example.org/",
+		"languageCode": "en-US",
+		"title":        "My New Hugo Site",
+		"menus": map[string]any{
+			"main": []any{
+				map[string]any{
+					"name":    "Home",
+					"pageRef": "/",
+					"weight":  10,
+				},
+				map[string]any{
+					"name":    "Posts",
+					"pageRef": "/posts",
+					"weight":  20,
+				},
+				map[string]any{
+					"name":    "Tags",
+					"pageRef": "/tags",
+					"weight":  30,
+				},
+			},
+		},
+		"module": map[string]any{
+			"hugoVersion": map[string]any{
+				"extended": false,
+				"min":      "0.146.0",
+			},
+		},
+	}
+
+	err := createSiteConfig(sourceFs, createpath, siteConfig, format)
+	if err != nil {
+		return err
+	}
+
+	defaultArchetype := map[string]any{
+		"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}",
+		"date":  "{{ .Date }}",
+		"draft": true,
+	}
+
+	err = createDefaultArchetype(sourceFs, createpath, defaultArchetype, format)
+	if err != nil {
+		return err
+	}
+
 	return copyFiles(createpath, sourceFs, themeFs)
 }
 
@@ -71,12 +121,24 @@ func CreateSite(createpath string, sourceFs afero.Fs, force bool, format string)
 		}
 	}
 
-	err := newSiteCreateConfig(sourceFs, createpath, format)
+	siteConfig := map[string]any{
+		"baseURL":      "https://example.org/",
+		"title":        "My New Hugo Site",
+		"languageCode": "en-us",
+	}
+
+	err := createSiteConfig(sourceFs, createpath, siteConfig, format)
 	if err != nil {
 		return err
 	}
 
-	err = newSiteCreateArchetype(sourceFs, createpath, format)
+	defaultArchetype := map[string]any{
+		"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}",
+		"date":  "{{ .Date }}",
+		"draft": true,
+	}
+
+	err = createDefaultArchetype(sourceFs, createpath, defaultArchetype, format)
 	if err != nil {
 		return err
 	}
@@ -99,13 +161,7 @@ func copyFiles(createpath string, sourceFs afero.Fs, skeleton embed.FS) error {
 	})
 }
 
-func newSiteCreateConfig(fs afero.Fs, createpath string, format string) (err error) {
-	in := map[string]string{
-		"baseURL":      "https://example.org/",
-		"title":        "My New Hugo Site",
-		"languageCode": "en-us",
-	}
-
+func createSiteConfig(fs afero.Fs, createpath string, in map[string]any, format string) (err error) {
 	var buf bytes.Buffer
 	err = parser.InterfaceToConfig(in, metadecoders.FormatFromString(format), &buf)
 	if err != nil {
@@ -115,13 +171,7 @@ func newSiteCreateConfig(fs afero.Fs, createpath string, format string) (err err
 	return helpers.WriteToDisk(filepath.Join(createpath, "hugo."+format), &buf, fs)
 }
 
-func newSiteCreateArchetype(fs afero.Fs, createpath string, format string) (err error) {
-	in := map[string]any{
-		"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}",
-		"date":  "{{ .Date }}",
-		"draft": true,
-	}
-
+func createDefaultArchetype(fs afero.Fs, createpath string, in map[string]any, format string) (err error) {
 	var buf bytes.Buffer
 	err = parser.InterfaceToFrontMatter(in, metadecoders.FormatFromString(format), &buf)
 	if err != nil {
diff --git a/create/skeletons/theme/LICENSE b/create/skeletons/theme/LICENSE
deleted file mode 100644
index 8aa26455d23..00000000000
--- a/create/skeletons/theme/LICENSE
+++ /dev/null
@@ -1,21 +0,0 @@
-MIT License
-
-Copyright (c) [year] [fullname]
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
diff --git a/create/skeletons/theme/README.md b/create/skeletons/theme/README.md
deleted file mode 100644
index 7cec74e073e..00000000000
--- a/create/skeletons/theme/README.md
+++ /dev/null
@@ -1,7 +0,0 @@
-# Theme Name
-
-## Features
-
-## Installation
-
-## Configuration
diff --git a/create/skeletons/theme/archetypes/default.md b/create/skeletons/theme/archetypes/default.md
deleted file mode 100644
index c6f3fcef6e3..00000000000
--- a/create/skeletons/theme/archetypes/default.md
+++ /dev/null
@@ -1,5 +0,0 @@
-+++
-title = '{{ replace .File.ContentBaseName "-" " " | title }}'
-date = {{ .Date }}
-draft = true
-+++
diff --git a/create/skeletons/theme/hugo.toml b/create/skeletons/theme/hugo.toml
deleted file mode 100644
index 6c35bc476f0..00000000000
--- a/create/skeletons/theme/hugo.toml
+++ /dev/null
@@ -1,23 +0,0 @@
-baseURL = 'https://example.org/'
-languageCode = 'en-US'
-title = 'My New Hugo Site'
-
-[[menus.main]]
-name = 'Home'
-pageRef = '/'
-weight = 10
-
-[[menus.main]]
-name = 'Posts'
-pageRef = '/posts'
-weight = 20
-
-[[menus.main]]
-name = 'Tags'
-pageRef = '/tags'
-weight = 30
-
-[module]
-  [module.hugoVersion]
-    extended = false
-    min = "0.116.0"
diff --git a/create/skeletons/theme/layouts/partials/footer.html b/create/skeletons/theme/layouts/_partials/footer.html
similarity index 100%
rename from create/skeletons/theme/layouts/partials/footer.html
rename to create/skeletons/theme/layouts/_partials/footer.html
diff --git a/create/skeletons/theme/layouts/partials/head.html b/create/skeletons/theme/layouts/_partials/head.html
similarity index 100%
rename from create/skeletons/theme/layouts/partials/head.html
rename to create/skeletons/theme/layouts/_partials/head.html
diff --git a/create/skeletons/theme/layouts/partials/head/css.html b/create/skeletons/theme/layouts/_partials/head/css.html
similarity index 86%
rename from create/skeletons/theme/layouts/partials/head/css.html
rename to create/skeletons/theme/layouts/_partials/head/css.html
index 91b928ded42..d76d23a16ce 100644
--- a/create/skeletons/theme/layouts/partials/head/css.html
+++ b/create/skeletons/theme/layouts/_partials/head/css.html
@@ -1,5 +1,5 @@
 {{- with resources.Get "css/main.css" }}
-  {{- if eq hugo.Environment "development" }}
+  {{- if hugo.IsDevelopment }}
     <link rel="stylesheet" href="{{ .RelPermalink }}">
   {{- else }}
     {{- with . | minify | fingerprint }}
diff --git a/create/skeletons/theme/layouts/_partials/head/js.html b/create/skeletons/theme/layouts/_partials/head/js.html
new file mode 100644
index 00000000000..16ffbedfeaa
--- /dev/null
+++ b/create/skeletons/theme/layouts/_partials/head/js.html
@@ -0,0 +1,16 @@
+{{- with resources.Get "js/main.js" }}
+  {{- $opts := dict
+    "minify" (not hugo.IsDevelopment)
+    "sourceMap" (cond hugo.IsDevelopment "external" "")
+    "targetPath" "js/main.js"
+  }}
+  {{- with . | js.Build $opts }}
+    {{- if hugo.IsDevelopment }}
+      <script src="{{ .RelPermalink }}"></script>
+    {{- else }}
+      {{- with . | fingerprint }}
+        <script src="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous"></script>
+      {{- end }}
+    {{- end }}
+  {{- end }}
+{{- end }}
diff --git a/create/skeletons/theme/layouts/partials/header.html b/create/skeletons/theme/layouts/_partials/header.html
similarity index 100%
rename from create/skeletons/theme/layouts/partials/header.html
rename to create/skeletons/theme/layouts/_partials/header.html
diff --git a/create/skeletons/theme/layouts/partials/menu.html b/create/skeletons/theme/layouts/_partials/menu.html
similarity index 96%
rename from create/skeletons/theme/layouts/partials/menu.html
rename to create/skeletons/theme/layouts/_partials/menu.html
index 71831809606..14245b55dba 100644
--- a/create/skeletons/theme/layouts/partials/menu.html
+++ b/create/skeletons/theme/layouts/_partials/menu.html
@@ -18,7 +18,7 @@
   </nav>
 {{- end }}
 
-{{- define "partials/inline/menu/walk.html" }}
+{{- define "_partials/inline/menu/walk.html" }}
   {{- $page := .page }}
   {{- range .menuEntries }}
     {{- $attrs := dict "href" .URL }}
diff --git a/create/skeletons/theme/layouts/partials/terms.html b/create/skeletons/theme/layouts/_partials/terms.html
similarity index 100%
rename from create/skeletons/theme/layouts/partials/terms.html
rename to create/skeletons/theme/layouts/_partials/terms.html
diff --git a/create/skeletons/theme/layouts/_default/baseof.html b/create/skeletons/theme/layouts/baseof.html
similarity index 100%
rename from create/skeletons/theme/layouts/_default/baseof.html
rename to create/skeletons/theme/layouts/baseof.html
diff --git a/create/skeletons/theme/layouts/_default/home.html b/create/skeletons/theme/layouts/home.html
similarity index 100%
rename from create/skeletons/theme/layouts/_default/home.html
rename to create/skeletons/theme/layouts/home.html
diff --git a/create/skeletons/theme/layouts/_default/list.html b/create/skeletons/theme/layouts/list.html
similarity index 100%
rename from create/skeletons/theme/layouts/_default/list.html
rename to create/skeletons/theme/layouts/list.html
diff --git a/create/skeletons/theme/layouts/partials/head/js.html b/create/skeletons/theme/layouts/partials/head/js.html
deleted file mode 100644
index 18fe84291ae..00000000000
--- a/create/skeletons/theme/layouts/partials/head/js.html
+++ /dev/null
@@ -1,12 +0,0 @@
-{{- with resources.Get "js/main.js" }}
-  {{- if eq hugo.Environment "development" }}
-    {{- with . | js.Build }}
-      <script src="{{ .RelPermalink }}"></script>
-    {{- end }}
-  {{- else }}
-    {{- $opts := dict "minify" true }}
-    {{- with . | js.Build $opts | fingerprint }}
-      <script src="{{ .RelPermalink }}" integrity="{{- .Data.Integrity }}" crossorigin="anonymous"></script>
-    {{- end }}
-  {{- end }}
-{{- end }}
diff --git a/create/skeletons/theme/layouts/_default/single.html b/create/skeletons/theme/layouts/single.html
similarity index 100%
rename from create/skeletons/theme/layouts/_default/single.html
rename to create/skeletons/theme/layouts/single.html
diff --git a/create/skeletons/theme/layouts/taxonomy.html b/create/skeletons/theme/layouts/taxonomy.html
new file mode 100644
index 00000000000..c2e787519e6
--- /dev/null
+++ b/create/skeletons/theme/layouts/taxonomy.html
@@ -0,0 +1,7 @@
+{{ define "main" }}
+  <h1>{{ .Title }}</h1>
+  {{ .Content }}
+  {{ range .Pages }}
+    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
+  {{ end }}
+{{ end }}
diff --git a/create/skeletons/theme/layouts/term.html b/create/skeletons/theme/layouts/term.html
new file mode 100644
index 00000000000..c2e787519e6
--- /dev/null
+++ b/create/skeletons/theme/layouts/term.html
@@ -0,0 +1,7 @@
+{{ define "main" }}
+  <h1>{{ .Title }}</h1>
+  {{ .Content }}
+  {{ range .Pages }}
+    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
+  {{ end }}
+{{ end }}
diff --git a/create/skeletons/theme/theme.toml b/create/skeletons/theme/theme.toml
deleted file mode 100644
index 3ba31646337..00000000000
--- a/create/skeletons/theme/theme.toml
+++ /dev/null
@@ -1,31 +0,0 @@
-name = 'Theme name'
-license = 'MIT'
-licenselink = 'https://github.com/owner/repo/LICENSE'
-description = 'Theme description'
-
-# The home page of the theme, where the source can be found
-homepage = 'https://github.com/owner/repo'
-
-# If you have a running demo of the theme
-demosite = 'https://owner.github.io/repo'
-
-# Taxonomy terms
-tags = ['blog', 'company']
-features = ['some', 'awesome', 'features']
-
-# If the theme has multiple authors
-authors = [
-  {name = 'Name of author', homepage = 'Website of author'},
-  {name = 'Name of author', homepage = 'Website of author'}
-]
-
-# If the theme has a single author
-[author]
-  name = 'Your name'
-  homepage = 'Your website'
-
-# If porting an existing theme
-[original]
-  author = 'Name of original author'
-  homepage = 'Website of original author'
-  repo = 'https://github.com/owner/repo'


Test patch:
diff --git a/testscripts/commands/new.txt b/testscripts/commands/new.txt
index 9057f9972a4..433e238bfc7 100644
--- a/testscripts/commands/new.txt
+++ b/testscripts/commands/new.txt
@@ -20,7 +20,7 @@ exists themes
 
 hugo new theme -h
 stdout 'Create a new theme \(skeleton\) called \[name\] in ./themes'
-hugo new theme mytheme
+hugo new theme mytheme --format yml
 stdout 'Creating new theme'
 ! exists resources
 cd themes
@@ -34,22 +34,21 @@ checkfile content/posts/post-1.md
 checkfile content/posts/post-2.md
 checkfile content/posts/post-3/bryce-canyon.jpg
 checkfile content/posts/post-3/index.md
-checkfile layouts/_default/baseof.html
-checkfile layouts/_default/home.html
-checkfile layouts/_default/list.html
-checkfile layouts/_default/single.html
-checkfile layouts/partials/footer.html
-checkfile layouts/partials/head.html
-checkfile layouts/partials/head/css.html
-checkfile layouts/partials/head/js.html
-checkfile layouts/partials/header.html
-checkfile layouts/partials/menu.html
-checkfile layouts/partials/terms.html
+checkfile layouts/baseof.html
+checkfile layouts/home.html
+checkfile layouts/list.html
+checkfile layouts/single.html
+checkfile layouts/taxonomy.html
+checkfile layouts/term.html
+checkfile layouts/_partials/footer.html
+checkfile layouts/_partials/head.html
+checkfile layouts/_partials/head/css.html
+checkfile layouts/_partials/head/js.html
+checkfile layouts/_partials/header.html
+checkfile layouts/_partials/menu.html
+checkfile layouts/_partials/terms.html
 checkfile static/favicon.ico
-checkfile LICENSE
-checkfile README.md
-checkfile hugo.toml
-checkfile theme.toml
+checkfile hugo.yml
 exists data
 exists i18n