2015-02-08 15:11:04 +00:00
|
|
|
// Copyright © 2014-2015 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
2014-05-02 05:06:01 +00:00
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// 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 22:30:11 +00:00
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-05-02 05:06:01 +00:00
|
|
|
"strings"
|
2015-02-08 15:11:04 +00:00
|
|
|
"time"
|
2014-05-02 05:06:01 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/hugo/create"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2014-11-01 15:57:29 +00:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
2014-05-08 22:30:11 +00:00
|
|
|
"github.com/spf13/hugo/parser"
|
2014-05-02 05:06:01 +00:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2014-05-29 22:40:16 +00:00
|
|
|
"github.com/spf13/viper"
|
2014-05-02 05:06:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var siteType string
|
|
|
|
var configFormat string
|
|
|
|
var contentType string
|
|
|
|
var contentFormat string
|
|
|
|
var contentFrontMatter string
|
|
|
|
|
|
|
|
func init() {
|
2014-05-08 22:30:11 +00:00
|
|
|
newSiteCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "config & frontmatter format")
|
2014-05-29 22:40:16 +00:00
|
|
|
newCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "frontmatter format")
|
2014-05-02 05:06:01 +00:00
|
|
|
newCmd.Flags().StringVarP(&contentType, "kind", "k", "", "Content type to create")
|
|
|
|
newCmd.AddCommand(newSiteCmd)
|
2014-05-08 22:30:11 +00:00
|
|
|
newCmd.AddCommand(newThemeCmd)
|
2014-05-02 05:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var newCmd = &cobra.Command{
|
|
|
|
Use: "new [path]",
|
|
|
|
Short: "Create new content for your site",
|
2014-09-28 04:52:15 +00:00
|
|
|
Long: `Create a new content file and automatically set the date and title.
|
2014-05-02 05:06:01 +00:00
|
|
|
It will guess which kind of file to create based on the path provided.
|
|
|
|
You can also specify the kind with -k KIND
|
|
|
|
If archetypes are provided in your theme or site, they will be used.
|
|
|
|
`,
|
|
|
|
Run: NewContent,
|
|
|
|
}
|
|
|
|
|
2014-05-08 22:30:11 +00: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.
|
|
|
|
Use 'hugo new [contentPath]' to create new content.
|
|
|
|
`,
|
|
|
|
Run: NewSite,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
as you see fit.
|
|
|
|
`,
|
|
|
|
Run: NewTheme,
|
|
|
|
}
|
|
|
|
|
2015-02-08 15:11:04 +00:00
|
|
|
// NewContent adds new content to a Hugo site.
|
2014-05-02 05:06:01 +00:00
|
|
|
func NewContent(cmd *cobra.Command, args []string) {
|
|
|
|
InitializeConfig()
|
|
|
|
|
2014-05-29 22:40:16 +00:00
|
|
|
if cmd.Flags().Lookup("format").Changed {
|
|
|
|
viper.Set("MetaDataFormat", configFormat)
|
|
|
|
}
|
|
|
|
|
2014-05-02 05:06:01 +00:00
|
|
|
if len(args) < 1 {
|
2014-05-08 22:30:11 +00:00
|
|
|
cmd.Usage()
|
2014-05-02 05:06:01 +00:00
|
|
|
jww.FATAL.Fatalln("path needs to be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
createpath := args[0]
|
|
|
|
|
|
|
|
var kind string
|
|
|
|
|
|
|
|
// assume the first directory is the section (kind)
|
2015-03-28 20:46:02 +00:00
|
|
|
if strings.Contains(createpath[1:], helpers.FilePathSeparator) {
|
2014-05-02 05:06:01 +00:00
|
|
|
kind = helpers.GuessSection(createpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if contentType != "" {
|
|
|
|
kind = contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
err := create.NewContent(kind, createpath)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 18:05:02 +00:00
|
|
|
// NewSite creates a new hugo site and initializes a structured Hugo directory.
|
2014-05-08 22:30:11 +00:00
|
|
|
func NewSite(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) < 1 {
|
|
|
|
cmd.Usage()
|
|
|
|
jww.FATAL.Fatalln("path needs to be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
createpath, err := filepath.Abs(filepath.Clean(args[0]))
|
|
|
|
if err != nil {
|
|
|
|
cmd.Usage()
|
|
|
|
jww.FATAL.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2014-11-01 15:57:29 +00:00
|
|
|
if x, _ := helpers.Exists(createpath, hugofs.SourceFs); x {
|
|
|
|
y, _ := helpers.IsDir(createpath, hugofs.SourceFs)
|
|
|
|
if z, _ := helpers.IsEmpty(createpath, hugofs.SourceFs); y && z {
|
2014-05-19 13:16:40 +00:00
|
|
|
jww.INFO.Println(createpath, "already exists and is empty")
|
|
|
|
} else {
|
|
|
|
jww.FATAL.Fatalln(createpath, "already exists and is not empty")
|
|
|
|
}
|
2014-05-08 22:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(createpath, "layouts")
|
|
|
|
mkdir(createpath, "content")
|
|
|
|
mkdir(createpath, "archetypes")
|
|
|
|
mkdir(createpath, "static")
|
2015-01-20 22:08:01 +00:00
|
|
|
mkdir(createpath, "data")
|
2014-05-08 22:30:11 +00:00
|
|
|
|
|
|
|
createConfig(createpath, configFormat)
|
2014-05-02 05:06:01 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 15:11:04 +00:00
|
|
|
// NewTheme creates a new Hugo theme.
|
2014-05-08 22:30:11 +00:00
|
|
|
func NewTheme(cmd *cobra.Command, args []string) {
|
2014-05-02 05:06:01 +00:00
|
|
|
InitializeConfig()
|
|
|
|
|
2014-05-08 22:30:11 +00:00
|
|
|
if len(args) < 1 {
|
|
|
|
cmd.Usage()
|
|
|
|
jww.FATAL.Fatalln("theme name needs to be provided")
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:56:14 +00:00
|
|
|
createpath := helpers.AbsPathify(filepath.Join("themes", args[0]))
|
2014-05-08 22:30:11 +00:00
|
|
|
jww.INFO.Println("creating theme at", createpath)
|
|
|
|
|
2014-11-01 15:57:29 +00:00
|
|
|
if x, _ := helpers.Exists(createpath, hugofs.SourceFs); x {
|
2014-05-08 22:30:11 +00:00
|
|
|
jww.FATAL.Fatalln(createpath, "already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(createpath, "layouts", "_default")
|
2014-05-27 22:30:25 +00:00
|
|
|
mkdir(createpath, "layouts", "partials")
|
2014-05-08 22:30:11 +00:00
|
|
|
|
|
|
|
touchFile(createpath, "layouts", "index.html")
|
|
|
|
touchFile(createpath, "layouts", "_default", "list.html")
|
|
|
|
touchFile(createpath, "layouts", "_default", "single.html")
|
|
|
|
|
2014-05-27 22:30:25 +00:00
|
|
|
touchFile(createpath, "layouts", "partials", "header.html")
|
|
|
|
touchFile(createpath, "layouts", "partials", "footer.html")
|
2014-05-08 22:30:11 +00:00
|
|
|
|
|
|
|
mkdir(createpath, "archetypes")
|
|
|
|
touchFile(createpath, "archetypes", "default.md")
|
|
|
|
|
|
|
|
mkdir(createpath, "static", "js")
|
|
|
|
mkdir(createpath, "static", "css")
|
|
|
|
|
|
|
|
by := []byte(`The MIT License (MIT)
|
|
|
|
|
2015-02-08 15:11:04 +00:00
|
|
|
Copyright (c) ` + time.Now().Format("2006") + ` YOUR_NAME_HERE
|
2014-05-08 22:30:11 +00: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.
|
|
|
|
`)
|
|
|
|
|
2014-11-06 16:56:14 +00:00
|
|
|
err := helpers.WriteToDisk(filepath.Join(createpath, "LICENSE.md"), bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-08 22:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
jww.FATAL.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createThemeMD(createpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkdir(x ...string) {
|
2014-11-06 16:56:14 +00:00
|
|
|
p := filepath.Join(x...)
|
2014-05-08 22:30:11 +00:00
|
|
|
|
2015-02-08 15:11:04 +00:00
|
|
|
err := os.MkdirAll(p, 0777) // before umask
|
2014-05-08 22:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
jww.FATAL.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func touchFile(x ...string) {
|
2014-11-06 16:56:14 +00:00
|
|
|
inpath := filepath.Join(x...)
|
2014-05-08 22:30:11 +00:00
|
|
|
mkdir(filepath.Dir(inpath))
|
2014-11-01 15:57:29 +00:00
|
|
|
err := helpers.WriteToDisk(inpath, bytes.NewReader([]byte{}), hugofs.SourceFs)
|
2014-05-08 22:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
jww.FATAL.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createThemeMD(inpath string) (err error) {
|
|
|
|
|
2015-03-10 15:49:42 +00: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 15:11:04 +00:00
|
|
|
license = "MIT"
|
2015-03-10 15:49:42 +00:00
|
|
|
licenselink = "https://github.com/yourname/yourtheme/blob/master/LICENSE.md"
|
2015-02-08 15:11:04 +00:00
|
|
|
description = ""
|
|
|
|
homepage = "http://siteforthistheme.com/"
|
|
|
|
tags = ["", ""]
|
|
|
|
features = ["", ""]
|
2015-03-10 15:49:42 +00:00
|
|
|
min_version = 0.13
|
2015-02-08 15:11:04 +00:00
|
|
|
|
|
|
|
[author]
|
|
|
|
name = ""
|
|
|
|
homepage = ""
|
|
|
|
|
|
|
|
# If porting an existing theme
|
|
|
|
[original]
|
|
|
|
name = ""
|
|
|
|
homepage = ""
|
|
|
|
repo = ""
|
|
|
|
`)
|
2014-05-08 22:30:11 +00:00
|
|
|
|
2014-11-06 16:56:14 +00:00
|
|
|
err = helpers.WriteToDisk(filepath.Join(inpath, "theme.toml"), bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-08 22:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createConfig(inpath string, kind string) (err error) {
|
2015-02-08 15:11:04 +00:00
|
|
|
in := map[string]string{
|
|
|
|
"baseurl": "http://yourSiteHere/",
|
|
|
|
"title": "My New Hugo Site",
|
|
|
|
"languageCode": "en-us",
|
|
|
|
}
|
2014-05-08 22:30:11 +00:00
|
|
|
kind = parser.FormatSanitize(kind)
|
|
|
|
|
|
|
|
by, err := parser.InterfaceToConfig(in, parser.FormatToLeadRune(kind))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 16:56:14 +00:00
|
|
|
err = helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), bytes.NewReader(by), hugofs.SourceFs)
|
2014-05-08 22:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-05-02 05:06:01 +00:00
|
|
|
}
|