当前位置: 首页>>代码示例>>Golang>>正文


Golang hugolib.NewPage函数代码示例

本文整理汇总了Golang中github.com/spf13/hugo/hugolib.NewPage函数的典型用法代码示例。如果您正苦于以下问题:Golang NewPage函数的具体用法?Golang NewPage怎么用?Golang NewPage使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewPage函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewContent

// NewContent creates a new content file in the content directory based upon the
// given kind, which is used to lookup an archetype.
func NewContent(fs afero.Fs, kind, name string) (err error) {
	jww.INFO.Println("attempting to create ", name, "of", kind)

	location := FindArchetype(fs, kind)

	var by []byte

	if location != "" {
		by, err = afero.ReadFile(fs, location)
		if err != nil {
			jww.ERROR.Println(err)
		}
	}
	if location == "" || err != nil {
		by = []byte("+++\n title = \"title\"\n draft = true \n+++\n")
	}

	psr, err := parser.ReadFrom(bytes.NewReader(by))
	if err != nil {
		return err
	}

	metadata, err := createMetadata(psr, name)
	if err != nil {
		jww.ERROR.Printf("Error processing archetype file %s: %s\n", location, err)
		return err
	}

	page, err := hugolib.NewPage(name)
	if err != nil {
		return err
	}

	if err = page.SetSourceMetaData(metadata, parser.FormatToLeadRune(viper.GetString("metaDataFormat"))); err != nil {
		return
	}

	page.SetSourceContent(psr.Content())

	if err = page.SafeSaveSourceAs(filepath.Join(viper.GetString("contentDir"), name)); err != nil {
		return
	}
	jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created")

	editor := viper.GetString("newContentEditor")

	if editor != "" {
		jww.FEEDBACK.Printf("Editing %s with %q ...\n", name, editor)

		cmd := exec.Command(editor, helpers.AbsPathify(path.Join(viper.GetString("contentDir"), name)))
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		return cmd.Run()
	}

	return nil
}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:61,代码来源:content.go

示例2: convertJekyllPost

func convertJekyllPost(path, relPath, targetDir string, draft bool) error {
	jww.TRACE.Println("Converting", path)

	filename := filepath.Base(path)
	postDate, postName, err := parseJekyllFilename(filename)
	if err != nil {
		jww.WARN.Printf("Failed to parse filename '%s': %s. Skipping.", filename, err)
		return nil
	}

	jww.TRACE.Println(filename, postDate, postName)

	targetFile := filepath.Join(targetDir, relPath)
	targetParentDir := filepath.Dir(targetFile)
	os.MkdirAll(targetParentDir, 0777)

	contentBytes, err := ioutil.ReadFile(path)
	if err != nil {
		jww.ERROR.Println("Read file error:", path)
		return err
	}

	psr, err := parser.ReadFrom(bytes.NewReader(contentBytes))
	if err != nil {
		jww.ERROR.Println("Parse file error:", path)
		return err
	}

	metadata, err := psr.Metadata()
	if err != nil {
		jww.ERROR.Println("Processing file error:", path)
		return err
	}

	newmetadata, err := convertJekyllMetaData(metadata, postName, postDate, draft)
	if err != nil {
		jww.ERROR.Println("Convert metadata error:", path)
		return err
	}

	jww.TRACE.Println(newmetadata)
	content := convertJekyllContent(newmetadata, string(psr.Content()))

	page, err := hugolib.NewPage(filename)
	if err != nil {
		jww.ERROR.Println("New page error", filename)
		return err
	}

	page.SetDir(targetParentDir)
	page.SetSourceContent([]byte(content))
	page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune("yaml"))
	page.SaveSourceAs(targetFile)

	jww.TRACE.Println("Target file:", targetFile)

	return nil
}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:58,代码来源:import_jekyll.go

示例3: Save

func (p *Page) Save() error {
	page, err := hugolib.NewPage(p.Path)

	if err != nil {
		return err
	}

	page.SetSourceContent([]byte(p.Body))

	return page.SafeSaveSourceAs(p.Path)
}
开发者ID:topiary-io,项目名称:topiary,代码行数:11,代码来源:static.go

示例4: contentSave

func (c *Content) contentSave() error {
	page, err := hugolib.NewPage(c.Path)

	if err != nil {
		return err
	}

	page.SetSourceMetaData(c.Metadata, '+')
	page.SetSourceContent([]byte(c.Body))

	return page.SafeSaveSourceAs(c.Path)
}
开发者ID:topiary-io,项目名称:topiary,代码行数:12,代码来源:content.go

示例5: saveMarkdownFile

func saveMarkdownFile(metadata interface{}, content string, path string) error {
	dir, filename := filepath.Split(path)
	page, err := hugolib.NewPage(filename)
	if err != nil {
		return err
	}

	page.SetDir(dir)
	page.SetSourceContent([]byte(content))
	page.SetSourceMetaData(metadata, parser.FormatToLeadRune("yaml"))
	page.SaveSourceAs(path)

	return nil
}
开发者ID:coderzh,项目名称:gohugo.org,代码行数:14,代码来源:updatedoc.go

示例6: convertContents

func convertContents(mark rune) (err error) {
	InitializeConfig()
	site := &hugolib.Site{}

	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:", path.Join(file.Dir, file.LogicalName))
			return err
		}
		metadata, err := psr.Metadata()
		if err != nil {
			jww.ERROR.Println("Error processing file:", path.Join(file.Dir, file.LogicalName))
			return err
		}

		page.Dir = file.Dir
		page.SetSourceContent(psr.Content())
		page.SetSourceMetaData(metadata, mark)

		if OutputDir != "" {
			page.SaveSourceAs(path.Join(OutputDir, page.FullFilePath()))
		} else {
			if Unsafe {
				page.SaveSource()
			} else {
				jww.FEEDBACK.Println("Unsafe operation not allowed, use --unsafe or set a different output path")
			}
		}
	}
	return
}
开发者ID:kkreutz,项目名称:hugo,代码行数:50,代码来源:convert.go

示例7: NewContent

func NewContent(kind, name string) (err error) {
	jww.INFO.Println("attempting to create ", name, "of", kind)

	location := FindArchetype(kind)

	var by []byte

	if location != "" {
		by, err = ioutil.ReadFile(location)
		if err != nil {
			jww.ERROR.Println(err)
		}
	}
	if location == "" || err != nil {
		by = []byte("+++\n title = \"title\"\n draft = true \n+++\n")
	}

	psr, err := parser.ReadFrom(bytes.NewReader(by))
	if err != nil {
		return err
	}
	metadata, err := psr.Metadata()
	if err != nil {
		return err
	}
	newmetadata, err := cast.ToStringMapE(metadata)
	if err != nil {
		return err
	}

	for k, _ := range newmetadata {
		switch strings.ToLower(k) {
		case "date":
			newmetadata[k] = time.Now()
		case "title":
			newmetadata[k] = helpers.MakeTitle(helpers.Filename(name))
		}
	}

	caseimatch := func(m map[string]interface{}, key string) bool {
		for k, _ := range m {
			if strings.ToLower(k) == strings.ToLower(key) {
				return true
			}
		}
		return false
	}

	if !caseimatch(newmetadata, "date") {
		newmetadata["date"] = time.Now()
	}

	if !caseimatch(newmetadata, "title") {
		newmetadata["title"] = helpers.MakeTitle(helpers.Filename(name))
	}

	page, err := hugolib.NewPage(name)
	if err != nil {
		return err
	}

	if x := viper.GetString("MetaDataFormat"); x == "json" || x == "yaml" || x == "toml" {
		newmetadata["date"] = time.Now().Format(time.RFC3339)
	}

	page.Dir = viper.GetString("sourceDir")
	page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune(viper.GetString("MetaDataFormat")))

	if err = page.SafeSaveSourceAs(path.Join(viper.GetString("contentDir"), name)); err != nil {
		return
	}
	jww.FEEDBACK.Println(helpers.AbsPathify(path.Join(viper.GetString("contentDir"), name)), "created")

	return nil
}
开发者ID:hugo-alves,项目名称:hugo,代码行数:75,代码来源:content.go

示例8: convertContents

func convertContents(mark rune) (err error) {
	if err := InitializeConfig(); err != nil {
		return err
	}
	site := &hugolib.Site{}

	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())
		page.SetSourceMetaData(metadata, mark)

		if outputDir != "" {
			page.SaveSourceAs(filepath.Join(outputDir, page.FullFilePath()))
		} else {
			if unsafe {
				page.SaveSource()
			} else {
				jww.FEEDBACK.Println("Unsafe operation not allowed, use --unsafe or set a different output path")
			}
		}
	}
	return
}
开发者ID:XCMer,项目名称:hugo,代码行数:64,代码来源:convert.go


注:本文中的github.com/spf13/hugo/hugolib.NewPage函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。