2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2015-02-08 10:11:04 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-02 01:06:01 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-02 01:06:01 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2014-05-08 18:30:11 -04:00
|
|
|
"bytes"
|
2015-12-08 17:38:26 -05:00
|
|
|
"errors"
|
2014-05-08 18:30:11 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-05-02 01:06:01 -04:00
|
|
|
"strings"
|
2015-02-08 10:11:04 -05:00
|
|
|
"time"
|
2014-05-02 01:06:01 -04:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/hugo/create"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2014-11-01 11:57:29 -04:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
2014-05-08 18:30:11 -04:00
|
|
|
"github.com/spf13/hugo/parser"
|
2014-05-02 01:06:01 -04:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2014-05-29 18:40:16 -04:00
|
|
|
"github.com/spf13/viper"
|
2014-05-02 01:06:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var siteType string
|
|
|
|
var configFormat string
|
|
|
|
var contentType string
|
|
|
|
var contentFormat string
|
|
|
|
var contentFrontMatter string
|
|
|
|
|
|
|
|
func init() {
|
2014-05-08 18:30:11 -04:00
|
|
|
newSiteCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "config & frontmatter format")
|
2015-09-25 17:39:46 -04:00
|
|
|
newSiteCmd.Flags().Bool("force", false, "Init inside non-empty directory")
|
2014-05-29 18:40:16 -04:00
|
|
|
newCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "frontmatter format")
|
2014-05-02 01:06:01 -04:00
|
|
|
newCmd.Flags().StringVarP(&contentType, "kind", "k", "", "Content type to create")
|
|
|
|
newCmd.AddCommand(newSiteCmd)
|
2014-05-08 18:30:11 -04:00
|
|
|
newCmd.AddCommand(newThemeCmd)
|
2014-05-02 01:06:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var newCmd = &cobra.Command{
|
|
|
|
Use: "new [path]",
|
|
|
|
Short: "Create new content for your site",
|
2014-09-28 00:52:15 -04:00
|
|
|
Long: `Create a new content file and automatically set the date and title.
|
2014-05-02 01:06:01 -04:00
|
|
|
It will guess which kind of file to create based on the path provided.
|
2015-08-04 05:15:12 -04:00
|
|
|
|
|
|
|
You can also specify the kind with ` + "`-k KIND`" + `.
|
|
|
|
|
|
|
|
If archetypes are provided in your theme or site, they will be used.`,
|
|
|
|
|
2015-12-02 05:42:53 -05:00
|
|
|
RunE: NewContent,
|
2014-05-02 01:06:01 -04:00
|
|
|
}
|
|
|
|
|
2014-05-08 18:30:11 -04:00
|
|
|
var newSiteCmd = &cobra.Command{
|
|
|
|
Use: "site [path]",
|
|
|
|
Short: "Create a new site (skeleton)",
|
|
|
|
Long: `Create a new site in the provided directory.
|
|
|
|
The new site will have the correct structure, but no content or theme yet.
|
2015-08-04 05:15:12 -04:00
|
|
|
Use ` + "`hugo new [contentPath]`" + ` to create new content.`,
|
2015-12-02 05:42:53 -05:00
|
|
|
RunE: NewSite,
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var newThemeCmd = &cobra.Command{
|
|
|
|
Use: "theme [name]",
|
|
|
|
Short: "Create a new theme",
|
|
|
|
Long: `Create a new theme (skeleton) called [name] in the current directory.
|
|
|
|
New theme is a skeleton. Please add content to the touched files. Add your
|
|
|
|
name to the copyright line in the license and adjust the theme.toml file
|
2015-08-04 05:15:12 -04:00
|
|
|
as you see fit.`,
|
2015-12-02 05:42:53 -05:00
|
|
|
RunE: NewTheme,
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
2015-02-08 10:11:04 -05:00
|
|
|
// NewContent adds new content to a Hugo site.
|
2015-12-02 05:42:53 -05:00
|
|
|
func NewContent(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := InitializeConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-02 01:06:01 -04:00
|
|
|
|
2014-05-29 18:40:16 -04:00
|
|
|
if cmd.Flags().Lookup("format").Changed {
|
|
|
|
viper.Set("MetaDataFormat", configFormat)
|
|
|
|
}
|
|
|
|
|
2014-05-02 01:06:01 -04:00
|
|
|
if len(args) < 1 {
|
2015-12-02 05:42:53 -05:00
|
|
|
return newUserError("path needs to be provided")
|
2014-05-02 01:06:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
createpath := args[0]
|
|
|
|
|
|
|
|
var kind string
|
|
|
|
|
2015-05-12 12:12:58 -04:00
|
|
|
createpath, kind = newContentPathSection(createpath)
|
2014-05-02 01:06:01 -04:00
|
|
|
|
|
|
|
if contentType != "" {
|
|
|
|
kind = contentType
|
|
|
|
}
|
|
|
|
|
2015-12-02 05:42:53 -05:00
|
|
|
return create.NewContent(kind, createpath)
|
|
|
|
|
2014-05-02 01:06:01 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 17:39:46 -04:00
|
|
|
func doNewSite(basepath string, force bool) error {
|
|
|
|
dirs := []string{
|
|
|
|
filepath.Join(basepath, "layouts"),
|
|
|
|
filepath.Join(basepath, "content"),
|
|
|
|
filepath.Join(basepath, "archetypes"),
|
|
|
|
filepath.Join(basepath, "static"),
|
|
|
|
filepath.Join(basepath, "data"),
|
2015-12-03 20:32:12 -05:00
|
|
|
filepath.Join(basepath, "themes"),
|
2015-09-25 17:39:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if exists, _ := helpers.Exists(basepath, hugofs.SourceFs); exists {
|
|
|
|
if isDir, _ := helpers.IsDir(basepath, hugofs.SourceFs); !isDir {
|
|
|
|
return errors.New(basepath + " already exists but not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty, _ := helpers.IsEmpty(basepath, hugofs.SourceFs)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !isEmpty && !force:
|
2015-10-17 08:05:48 -04:00
|
|
|
return errors.New(basepath + " already exists and is not empty")
|
2015-09-25 17:39:46 -04:00
|
|
|
|
|
|
|
case !isEmpty && force:
|
|
|
|
all := append(dirs, filepath.Join(basepath, "config."+configFormat))
|
|
|
|
for _, path := range all {
|
|
|
|
if exists, _ := helpers.Exists(path, hugofs.SourceFs); exists {
|
|
|
|
return errors.New(path + " already exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
hugofs.SourceFs.MkdirAll(dir, 0777)
|
|
|
|
}
|
|
|
|
|
|
|
|
createConfig(basepath, configFormat)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-11 13:05:02 -05:00
|
|
|
// NewSite creates a new hugo site and initializes a structured Hugo directory.
|
2015-12-02 05:42:53 -05:00
|
|
|
func NewSite(cmd *cobra.Command, args []string) error {
|
2014-05-08 18:30:11 -04:00
|
|
|
if len(args) < 1 {
|
2015-12-02 05:42:53 -05:00
|
|
|
return newUserError("path needs to be provided")
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
createpath, err := filepath.Abs(filepath.Clean(args[0]))
|
|
|
|
if err != nil {
|
2015-12-02 05:42:53 -05:00
|
|
|
return newUserError(err)
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 17:39:46 -04:00
|
|
|
forceNew, _ := cmd.Flags().GetBool("force")
|
2015-12-02 05:42:53 -05:00
|
|
|
|
|
|
|
return doNewSite(createpath, forceNew)
|
|
|
|
|
2014-05-02 01:06:01 -04:00
|
|
|
}
|
|
|
|
|
2015-02-08 10:11:04 -05:00
|
|
|
// NewTheme creates a new Hugo theme.
|
2015-12-02 05:42:53 -05:00
|
|
|
func NewTheme(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := InitializeConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-02 01:06:01 -04:00
|
|
|
|
2014-05-08 18:30:11 -04:00
|
|
|
if len(args) < 1 {
|
2015-12-02 05:42:53 -05:00
|
|
|
|
|
|
|
return newUserError("theme name needs to be provided")
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 11:56:14 -05:00
|
|
|
createpath := helpers.AbsPathify(filepath.Join("themes", args[0]))
|
2014-05-08 18:30:11 -04:00
|
|
|
jww.INFO.Println("creating theme at", createpath)
|
|
|
|
|
2014-11-01 11:57:29 -04:00
|
|
|
if x, _ := helpers.Exists(createpath, hugofs.SourceFs); x {
|
2015-12-02 10:27:08 -05:00
|
|
|
return newUserError(createpath, "already exists")
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(createpath, "layouts", "_default")
|
2014-05-27 18:30:25 -04:00
|
|
|
mkdir(createpath, "layouts", "partials")
|
2014-05-08 18:30:11 -04:00
|
|
|
|
|
|
|
touchFile(createpath, "layouts", "index.html")
|
2015-09-04 07:43:09 -04:00
|
|
|
touchFile(createpath, "layouts", "404.html")
|
2014-05-08 18:30:11 -04:00
|
|
|
touchFile(createpath, "layouts", "_default", "list.html")
|
|
|
|
touchFile(createpath, "layouts", "_default", "single.html")
|
|
|
|
|
2014-05-27 18:30:25 -04:00
|
|
|
touchFile(createpath, "layouts", "partials", "header.html")
|
|
|
|
touchFile(createpath, "layouts", "partials", "footer.html")
|
2014-05-08 18:30:11 -04:00
|
|
|
|
|
|
|
mkdir(createpath, "archetypes")
|
2015-10-14 14:12:59 -04:00
|
|
|
|
|
|
|
archDefault := []byte("+++\n+++\n")
|
|
|
|
|
|
|
|
err := helpers.WriteToDisk(filepath.Join(createpath, "archetypes", "default.md"), bytes.NewReader(archDefault), hugofs.SourceFs)
|
|
|
|
if err != nil {
|
2015-12-02 10:27:08 -05:00
|
|
|
return err
|
2015-10-14 14:12:59 -04:00
|
|
|
}
|
2014-05-08 18:30:11 -04:00
|
|
|
|
|
|
|
mkdir(createpath, "static", "js")
|
|
|
|
mkdir(createpath, "static", "css")
|
|
|
|
|
|
|
|
by := []byte(`The MIT License (MIT)
|
|
|
|
|
2015-02-08 10:11:04 -05:00
|
|
|
Copyright (c) ` + time.Now().Format("2006") + ` YOUR_NAME_HERE
|
2014-05-08 18:30:11 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
`)
|
|
|
|
|
2015-10-14 14:12:59 -04:00
|
|
|
err = helpers.WriteToDisk(filepath.Join(createpath, "LICENSE.md"), bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
2015-12-02 10:27:08 -05:00
|
|
|
return err
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
createThemeMD(createpath)
|
2015-12-02 05:42:53 -05:00
|
|
|
|
|
|
|
return nil
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func mkdir(x ...string) {
|
2014-11-06 11:56:14 -05:00
|
|
|
p := filepath.Join(x...)
|
2014-05-08 18:30:11 -04:00
|
|
|
|
2015-02-08 10:11:04 -05:00
|
|
|
err := os.MkdirAll(p, 0777) // before umask
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
|
|
|
jww.FATAL.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func touchFile(x ...string) {
|
2014-11-06 11:56:14 -05:00
|
|
|
inpath := filepath.Join(x...)
|
2014-05-08 18:30:11 -04:00
|
|
|
mkdir(filepath.Dir(inpath))
|
2014-11-01 11:57:29 -04:00
|
|
|
err := helpers.WriteToDisk(inpath, bytes.NewReader([]byte{}), hugofs.SourceFs)
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
|
|
|
jww.FATAL.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createThemeMD(inpath string) (err error) {
|
|
|
|
|
2015-03-10 11:49:42 -04:00
|
|
|
by := []byte(`# theme.toml template for a Hugo theme
|
|
|
|
# See https://github.com/spf13/hugoThemes#themetoml for an example
|
|
|
|
|
|
|
|
name = "` + strings.Title(helpers.MakeTitle(filepath.Base(inpath))) + `"
|
2015-02-08 10:11:04 -05:00
|
|
|
license = "MIT"
|
2015-03-10 11:49:42 -04:00
|
|
|
licenselink = "https://github.com/yourname/yourtheme/blob/master/LICENSE.md"
|
2015-02-08 10:11:04 -05:00
|
|
|
description = ""
|
|
|
|
homepage = "http://siteforthistheme.com/"
|
|
|
|
tags = ["", ""]
|
|
|
|
features = ["", ""]
|
2015-12-01 12:25:56 -05:00
|
|
|
min_version = 0.15
|
2015-02-08 10:11:04 -05:00
|
|
|
|
|
|
|
[author]
|
|
|
|
name = ""
|
|
|
|
homepage = ""
|
|
|
|
|
|
|
|
# If porting an existing theme
|
|
|
|
[original]
|
|
|
|
name = ""
|
|
|
|
homepage = ""
|
|
|
|
repo = ""
|
|
|
|
`)
|
2014-05-08 18:30:11 -04:00
|
|
|
|
2014-11-06 11:56:14 -05:00
|
|
|
err = helpers.WriteToDisk(filepath.Join(inpath, "theme.toml"), bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:12:58 -04:00
|
|
|
func newContentPathSection(path string) (string, string) {
|
|
|
|
// Forward slashes is used in all examples. Convert if needed.
|
|
|
|
// Issue #1133
|
|
|
|
createpath := strings.Replace(path, "/", helpers.FilePathSeparator, -1)
|
|
|
|
var section string
|
|
|
|
// assume the first directory is the section (kind)
|
|
|
|
if strings.Contains(createpath[1:], helpers.FilePathSeparator) {
|
|
|
|
section = helpers.GuessSection(createpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return createpath, section
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:30:11 -04:00
|
|
|
func createConfig(inpath string, kind string) (err error) {
|
2015-02-08 10:11:04 -05:00
|
|
|
in := map[string]string{
|
2015-05-01 18:24:46 -04:00
|
|
|
"baseurl": "http://replace-this-with-your-hugo-site.com/",
|
2015-02-08 10:11:04 -05:00
|
|
|
"title": "My New Hugo Site",
|
|
|
|
"languageCode": "en-us",
|
|
|
|
}
|
2014-05-08 18:30:11 -04:00
|
|
|
kind = parser.FormatSanitize(kind)
|
|
|
|
|
|
|
|
by, err := parser.InterfaceToConfig(in, parser.FormatToLeadRune(kind))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 11:56:14 -05:00
|
|
|
err = helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-05-02 01:06:01 -04:00
|
|
|
}
|