commands/new: Improve theme creation

- Update the skeleton structure to match the new template system.
- Add a --format flag to the `hugo new theme` command to control the
  format of the site configuration and default archetype files.
- Remove theme.toml. This file's presence can be confusing for new
  users, and the README in the themes repository already has an example.
- Remove the LICENSE and README files from the skeleton. These files
  are not needed for a theme to work, and they can be added later by
  the user if desired.

Closes #13489
Closes #13544
This commit is contained in:
Joe Mooring 2025-03-30 12:31:12 -07:00 committed by Bjørn Erik Pedersen
parent e6e18e9122
commit 24ac6a9de9
22 changed files with 123 additions and 136 deletions

View file

@ -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))
},
},
},

View file

@ -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 {

View file

@ -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.

View file

@ -1,7 +0,0 @@
# Theme Name
## Features
## Installation
## Configuration

View file

@ -1,5 +0,0 @@
+++
title = '{{ replace .File.ContentBaseName "-" " " | title }}'
date = {{ .Date }}
draft = true
+++

View file

@ -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"

View file

@ -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 }}

View file

@ -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 }}

View file

@ -18,7 +18,7 @@ Renders a menu for the given menu ID.
</nav>
{{- end }}
{{- define "partials/inline/menu/walk.html" }}
{{- define "_partials/inline/menu/walk.html" }}
{{- $page := .page }}
{{- range .menuEntries }}
{{- $attrs := dict "href" .URL }}

View file

@ -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 }}

View file

@ -0,0 +1,7 @@
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ range .Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{ end }}

View file

@ -0,0 +1,7 @@
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ range .Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{ end }}

View file

@ -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'

View file

@ -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