예제 #1
0
파일: hugo.go 프로젝트: tubo028/hugo
func initSites() error {
	if Hugo != nil {
		return nil
	}

	h, err := hugolib.NewHugoSitesFromConfiguration()

	if err != nil {
		return err
	}
	Hugo = h

	return nil
}
예제 #2
0
func convertContents(mark rune) (err error) {
	if err := InitializeConfig(); err != nil {
		return err
	}

	h, err := hugolib.NewHugoSitesFromConfiguration()

	if err != nil {
		return err
	}

	site := h.Sites[0]

	if err = site.Initialise(); err != nil {
		return err
	}

	if site.Source == nil {
		panic(fmt.Sprintf("site.Source not set"))
	}
	if len(site.Source.Files()) < 1 {
		return fmt.Errorf("No source files found")
	}

	jww.FEEDBACK.Println("processing", len(site.Source.Files()), "content files")
	for _, file := range site.Source.Files() {
		jww.INFO.Println("Attempting to convert", file.LogicalName())
		page, err := hugolib.NewPage(file.LogicalName())
		if err != nil {
			return err
		}

		psr, err := parser.ReadFrom(file.Contents)
		if err != nil {
			jww.ERROR.Println("Error processing file:", file.Path())
			return err
		}
		metadata, err := psr.Metadata()
		if err != nil {
			jww.ERROR.Println("Error processing file:", file.Path())
			return err
		}

		// better handling of dates in formats that don't have support for them
		if mark == parser.FormatToLeadRune("json") || mark == parser.FormatToLeadRune("yaml") || mark == parser.FormatToLeadRune("toml") {
			newmetadata := cast.ToStringMap(metadata)
			for k, v := range newmetadata {
				switch vv := v.(type) {
				case time.Time:
					newmetadata[k] = vv.Format(time.RFC3339)
				}
			}
			metadata = newmetadata
		}

		page.SetDir(filepath.Join(helpers.AbsPathify(viper.GetString("contentDir")), file.Dir()))
		page.SetSourceContent(psr.Content())
		if err = page.SetSourceMetaData(metadata, mark); err != nil {
			jww.ERROR.Printf("Failed to set source metadata for file %q: %s. For more info see For more info see https://github.com/spf13/hugo/issues/2458", page.FullFilePath(), err)
			continue
		}

		if outputDir != "" {
			if err = page.SaveSourceAs(filepath.Join(outputDir, page.FullFilePath())); err != nil {
				return fmt.Errorf("Failed to save file %q: %s", page.FullFilePath(), err)
			}
		} else {
			if unsafe {
				if err = page.SaveSource(); err != nil {
					return fmt.Errorf("Failed to save file %q: %s", page.FullFilePath(), err)
				}
			} else {
				jww.FEEDBACK.Println("Unsafe operation not allowed, use --unsafe or set a different output path")
			}
		}
	}
	return
}
예제 #3
0
	RunE: nil,
}

var listDraftsCmd = &cobra.Command{
	Use:   "drafts",
	Short: "List all drafts",
	Long:  `List all of the drafts in your content directory.`,
	RunE: func(cmd *cobra.Command, args []string) error {

		if err := InitializeConfig(); err != nil {
			return err
		}

		viper.Set("buildDrafts", true)

		sites, err := hugolib.NewHugoSitesFromConfiguration()

		if err != nil {
			return newSystemError("Error creating sites", err)
		}

		if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
			return newSystemError("Error Processing Source Content", err)
		}

		for _, p := range sites.Pages() {
			if p.IsDraft() {
				fmt.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
			}

		}