• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang parser.InterfaceToFrontMatter函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: SetSourceMetaData

func (page *Page) SetSourceMetaData(in interface{}, mark rune) (err error) {
	by, err := parser.InterfaceToFrontMatter(in, mark)
	if err != nil {
		return err
	}

	page.sourceFrontmatter = by

	return nil
}
开发者ID:kkreutz,项目名称:hugo,代码行数:10,代码来源:page.go


示例2: SetSourceMetaData

func (p *Page) SetSourceMetaData(in interface{}, mark rune) (err error) {
	by, err := parser.InterfaceToFrontMatter(in, mark)
	if err != nil {
		return err
	}
	by = append(by, '\n')

	p.Source.Frontmatter = by

	return nil
}
开发者ID:JTrembl1,项目名称:hugo,代码行数:11,代码来源:page.go


示例3: parseFrontMatterOnlyFile

func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) ([]byte, int, error) {
	frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
	var mark rune

	switch frontmatter {
	case "toml":
		mark = rune('+')
	case "json":
		mark = rune('{')
	case "yaml":
		mark = rune('-')
	default:
		return []byte{}, http.StatusBadRequest, errors.New("Can't define the frontmatter.")
	}

	f, err := parser.InterfaceToFrontMatter(rawFile, mark)
	fString := string(f)

	// If it's toml or yaml, strip frontmatter identifier
	if frontmatter == "toml" {
		fString = strings.TrimSuffix(fString, "+++\n")
		fString = strings.TrimPrefix(fString, "+++\n")
	}

	if frontmatter == "yaml" {
		fString = strings.TrimSuffix(fString, "---\n")
		fString = strings.TrimPrefix(fString, "---\n")
	}

	f = []byte(fString)

	if err != nil {
		return []byte{}, http.StatusInternalServerError, err
	}

	return f, http.StatusOK, nil
}
开发者ID:giorgil,项目名称:caddy-hugo,代码行数:37,代码来源:post.go


示例4: SetSourceMetaData

func (p *Page) SetSourceMetaData(in interface{}, mark rune) (err error) {
	// See https://github.com/spf13/hugo/issues/2458
	defer func() {
		if r := recover(); r != nil {
			var ok bool
			err, ok = r.(error)
			if !ok {
				err = fmt.Errorf("error from marshal: %v", r)
			}
		}
	}()

	var by []byte

	by, err = parser.InterfaceToFrontMatter(in, mark)
	if err != nil {
		return
	}
	by = append(by, '\n')

	p.Source.Frontmatter = by

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


示例5: servePost

func servePost(w http.ResponseWriter, r *http.Request, filename string) (int, error) {
	// Get the JSON information sent using a buffer
	rawBuffer := new(bytes.Buffer)
	rawBuffer.ReadFrom(r.Body)

	// Creates the raw file "map" using the JSON
	var rawFile map[string]interface{}
	json.Unmarshal(rawBuffer.Bytes(), &rawFile)

	// Initializes the file content to write
	var file []byte

	switch r.Header.Get("X-Content-Type") {
	case "frontmatter-only":
		frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
		var mark rune

		switch frontmatter {
		case "toml":
			mark = rune('+')
		case "json":
			mark = rune('{')
		case "yaml":
			mark = rune('-')
		default:
			return 400, nil
		}

		f, err := parser.InterfaceToFrontMatter(rawFile, mark)
		fString := string(f)

		// If it's toml or yaml, strip frontmatter identifier
		if frontmatter == "toml" {
			fString = strings.TrimSuffix(fString, "+++\n")
			fString = strings.TrimPrefix(fString, "+++\n")
		}

		if frontmatter == "yaml" {
			fString = strings.TrimSuffix(fString, "---\n")
			fString = strings.TrimPrefix(fString, "---\n")
		}

		f = []byte(fString)

		if err != nil {
			w.Write([]byte(err.Error()))
			return 500, err
		}

		file = f
	case "content-only":
		// The main content of the file
		mainContent := rawFile["content"].(string)
		mainContent = "\n\n" + strings.TrimSpace(mainContent)

		file = []byte(mainContent)
	case "complete":
		// The main content of the file
		mainContent := rawFile["content"].(string)
		mainContent = "\n\n" + strings.TrimSpace(mainContent)

		// Removes the main content from the rest of the frontmatter
		delete(rawFile, "content")

		// Converts the frontmatter in JSON
		jsonFrontmatter, err := json.Marshal(rawFile)

		if err != nil {
			w.Write([]byte(err.Error()))
			return 500, err
		}

		// Indents the json
		frontMatterBuffer := new(bytes.Buffer)
		json.Indent(frontMatterBuffer, jsonFrontmatter, "", "  ")

		// Generates the final file
		f := new(bytes.Buffer)
		f.Write(frontMatterBuffer.Bytes())
		f.Write([]byte(mainContent))
		file = f.Bytes()
	default:
		return 400, nil
	}

	// Write the file
	err := ioutil.WriteFile(filename, file, 0666)

	if err != nil {
		w.Write([]byte(err.Error()))
		return 500, err
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write([]byte("{}"))
	return 200, nil
}
开发者ID:th3architect,项目名称:caddy-hugo,代码行数:97,代码来源:editor.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang parser.ReadFrom函数代码示例发布时间:2022-05-28
下一篇:
Golang parser.InterfaceToConfig函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap