gitee.com/mirrors/Hugo-Go@v0.47.1/commands/new_theme.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package commands
    15  
    16  import (
    17  	"bytes"
    18  	"errors"
    19  	"path/filepath"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/gohugoio/hugo/helpers"
    24  	"github.com/gohugoio/hugo/hugofs"
    25  	"github.com/spf13/cobra"
    26  	jww "github.com/spf13/jwalterweatherman"
    27  )
    28  
    29  var _ cmder = (*newThemeCmd)(nil)
    30  
    31  type newThemeCmd struct {
    32  	*baseCmd
    33  	hugoBuilderCommon
    34  }
    35  
    36  func newNewThemeCmd() *newThemeCmd {
    37  	ccmd := &newThemeCmd{baseCmd: newBaseCmd(nil)}
    38  
    39  	cmd := &cobra.Command{
    40  		Use:   "theme [name]",
    41  		Short: "Create a new theme",
    42  		Long: `Create a new theme (skeleton) called [name] in the current directory.
    43  New theme is a skeleton. Please add content to the touched files. Add your
    44  name to the copyright line in the license and adjust the theme.toml file
    45  as you see fit.`,
    46  		RunE: ccmd.newTheme,
    47  	}
    48  
    49  	ccmd.cmd = cmd
    50  
    51  	return ccmd
    52  }
    53  
    54  // newTheme creates a new Hugo theme template
    55  func (n *newThemeCmd) newTheme(cmd *cobra.Command, args []string) error {
    56  	c, err := initializeConfig(false, false, &n.hugoBuilderCommon, n, nil)
    57  
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	if len(args) < 1 {
    63  		return newUserError("theme name needs to be provided")
    64  	}
    65  
    66  	createpath := c.hugo.PathSpec.AbsPathify(filepath.Join(c.Cfg.GetString("themesDir"), args[0]))
    67  	jww.FEEDBACK.Println("Creating theme at", createpath)
    68  
    69  	cfg := c.DepsCfg
    70  
    71  	if x, _ := helpers.Exists(createpath, cfg.Fs.Source); x {
    72  		return errors.New(createpath + " already exists")
    73  	}
    74  
    75  	mkdir(createpath, "layouts", "_default")
    76  	mkdir(createpath, "layouts", "partials")
    77  
    78  	touchFile(cfg.Fs.Source, createpath, "layouts", "index.html")
    79  	touchFile(cfg.Fs.Source, createpath, "layouts", "404.html")
    80  	touchFile(cfg.Fs.Source, createpath, "layouts", "_default", "list.html")
    81  	touchFile(cfg.Fs.Source, createpath, "layouts", "_default", "single.html")
    82  
    83  	baseofDefault := []byte(`<!DOCTYPE html>
    84  <html>
    85      {{- partial "head.html" . -}}
    86      <body>
    87          {{- partial "header.html" . -}}
    88          <div id="content">
    89          {{- block "main" . }}{{- end }}
    90          </div>
    91          {{- partial "footer.html" . -}}
    92      </body>
    93  </html>
    94  `)
    95  	err = helpers.WriteToDisk(filepath.Join(createpath, "layouts", "_default", "baseof.html"), bytes.NewReader(baseofDefault), cfg.Fs.Source)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	touchFile(cfg.Fs.Source, createpath, "layouts", "partials", "head.html")
   101  	touchFile(cfg.Fs.Source, createpath, "layouts", "partials", "header.html")
   102  	touchFile(cfg.Fs.Source, createpath, "layouts", "partials", "footer.html")
   103  
   104  	mkdir(createpath, "archetypes")
   105  
   106  	archDefault := []byte("+++\n+++\n")
   107  
   108  	err = helpers.WriteToDisk(filepath.Join(createpath, "archetypes", "default.md"), bytes.NewReader(archDefault), cfg.Fs.Source)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	mkdir(createpath, "static", "js")
   114  	mkdir(createpath, "static", "css")
   115  
   116  	by := []byte(`The MIT License (MIT)
   117  
   118  Copyright (c) ` + time.Now().Format("2006") + ` YOUR_NAME_HERE
   119  
   120  Permission is hereby granted, free of charge, to any person obtaining a copy of
   121  this software and associated documentation files (the "Software"), to deal in
   122  the Software without restriction, including without limitation the rights to
   123  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
   124  the Software, and to permit persons to whom the Software is furnished to do so,
   125  subject to the following conditions:
   126  
   127  The above copyright notice and this permission notice shall be included in all
   128  copies or substantial portions of the Software.
   129  
   130  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   131  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   132  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   133  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   134  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   135  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   136  `)
   137  
   138  	err = helpers.WriteToDisk(filepath.Join(createpath, "LICENSE"), bytes.NewReader(by), cfg.Fs.Source)
   139  	if err != nil {
   140  		return err
   141  	}
   142  
   143  	n.createThemeMD(cfg.Fs, createpath)
   144  
   145  	return nil
   146  }
   147  
   148  func (n *newThemeCmd) createThemeMD(fs *hugofs.Fs, inpath string) (err error) {
   149  
   150  	by := []byte(`# theme.toml template for a Hugo theme
   151  # See https://github.com/gohugoio/hugoThemes#themetoml for an example
   152  
   153  name = "` + strings.Title(helpers.MakeTitle(filepath.Base(inpath))) + `"
   154  license = "MIT"
   155  licenselink = "https://github.com/yourname/yourtheme/blob/master/LICENSE"
   156  description = ""
   157  homepage = "http://example.com/"
   158  tags = []
   159  features = []
   160  min_version = "0.41"
   161  
   162  [author]
   163    name = ""
   164    homepage = ""
   165  
   166  # If porting an existing theme
   167  [original]
   168    name = ""
   169    homepage = ""
   170    repo = ""
   171  `)
   172  
   173  	err = helpers.WriteToDisk(filepath.Join(inpath, "theme.toml"), bytes.NewReader(by), fs.Source)
   174  	if err != nil {
   175  		return
   176  	}
   177  
   178  	return nil
   179  }