Esempio n. 1
0
File: new.go Progetto: maruel/hugo
// NewContent adds new content to a Hugo site.
func NewContent(cmd *cobra.Command, args []string) {
	InitializeConfig()

	if cmd.Flags().Lookup("format").Changed {
		viper.Set("MetaDataFormat", configFormat)
	}

	if len(args) < 1 {
		cmd.Usage()
		jww.FATAL.Fatalln("path needs to be provided")
	}

	createpath := args[0]

	var kind string

	createpath, kind = newContentPathSection(createpath)

	if contentType != "" {
		kind = contentType
	}

	err := create.NewContent(kind, createpath)
	if err != nil {
		jww.ERROR.Println(err)
	}
}
Esempio n. 2
0
File: new.go Progetto: vinchu/hugo
func NewContent(cmd *cobra.Command, args []string) {
	InitializeConfig()

	if len(args) < 1 {
		cmd.Usage()
		jww.FATAL.Fatalln("path needs to be provided")
	}

	createpath := args[0]

	var kind string

	// assume the first directory is the section (kind)
	if strings.Contains(createpath[1:], "/") {
		kind = helpers.GuessSection(createpath)
	}

	if contentType != "" {
		kind = contentType
	}

	err := create.NewContent(kind, createpath)
	if err != nil {
		jww.ERROR.Println(err)
	}
}
Esempio n. 3
0
File: new.go Progetto: nitoyon/hugo
// NewContent adds new content to a Hugo site.
func NewContent(cmd *cobra.Command, args []string) error {
	if err := InitializeConfig(); err != nil {
		return err
	}

	if flagChanged(cmd.Flags(), "format") {
		viper.Set("MetaDataFormat", configFormat)
	}

	if flagChanged(cmd.Flags(), "editor") {
		viper.Set("NewContentEditor", contentEditor)
	}

	if len(args) < 1 {
		return newUserError("path needs to be provided")
	}

	createpath := args[0]

	var kind string

	createpath, kind = newContentPathSection(createpath)

	if contentType != "" {
		kind = contentType
	}

	return create.NewContent(kind, createpath)

}
Esempio n. 4
0
File: new.go Progetto: nurhavid/hugo
// NewContent adds new content to a Hugo site.
func NewContent(cmd *cobra.Command, args []string) {
	InitializeConfig()

	if cmd.Flags().Lookup("format").Changed {
		viper.Set("MetaDataFormat", configFormat)
	}

	if len(args) < 1 {
		cmd.Usage()
		jww.FATAL.Fatalln("path needs to be provided")
	}

	createpath := args[0]

	var kind string

	// assume the first directory is the section (kind)
	if strings.Contains(createpath[1:], helpers.FilePathSeparator) {
		kind = helpers.GuessSection(createpath)
	}

	if contentType != "" {
		kind = contentType
	}

	err := create.NewContent(kind, createpath)
	if err != nil {
		jww.ERROR.Println(err)
	}
}
Esempio n. 5
0
func TestNewContent(t *testing.T) {
	initViper()

	err := initFs()
	if err != nil {
		t.Fatalf("initialization error: %s", err)
	}

	cases := []struct {
		kind          string
		path          string
		resultStrings []string
	}{
		{"post", "post/sample-1.md", []string{`title = "sample 1"`, `test = "test1"`}},
		{"stump", "stump/sample-2.md", []string{`title = "sample 2"`}},     // no archetype file
		{"", "sample-3.md", []string{`title = "sample 3"`}},                // no archetype
		{"product", "product/sample-4.md", []string{`title = "sample 4"`}}, // empty archetype front matter
	}

	for i, c := range cases {
		err = create.NewContent(hugofs.Source(), c.kind, c.path)
		if err != nil {
			t.Errorf("[%d] NewContent: %s", i, err)
		}

		fname := filepath.Join(os.TempDir(), "content", filepath.FromSlash(c.path))
		_, err = hugofs.Source().Stat(fname)
		if err != nil {
			t.Errorf("[%d] Stat: %s", i, err)
		}

		for _, v := range c.resultStrings {
			found, err := afero.FileContainsBytes(hugofs.Source(), fname, []byte(v))
			if err != nil {
				t.Errorf("[%d] FileContainsBytes: %s", i, err)
			}
			if !found {
				t.Errorf("content missing from output: %q", v)
			}
		}
	}
}