示例#1
0
文件: post.go 项目: darkhelmet/blargh
func FromFile(path string) (*Post, error) {
	var post Post
	stat, err := os.Stat(path)
	if err != nil {
		return nil, fmt.Errorf("post: failed to stat file: %s", err)
	}
	post.UpdatedAt = &Time{stat.ModTime()}
	content, err := fmatter.ReadFile(path, &post)
	if err != nil {
		return nil, fmt.Errorf("post: failed reading post: %s", err)
	}
	if len(post.Slugs) == 0 {
		return nil, fmt.Errorf("post: %#v must have at least 1 slug", path)
	}
	post.Body = string(content)
	if post.Id == "" {
		guid, err := uuid.NewV4()
		if err != nil {
			return nil, fmt.Errorf("post: failed generating UUID: %s", err)
		}
		post.Id = guid.String()
	}
	html, err := post.html()
	if err != nil {
		return nil, fmt.Errorf("post: failed rendering HTML: %s", err)
	}
	post.HTML = html
	return &post, nil
}
示例#2
0
文件: htmldoc.go 项目: james4k/grout
func (d *HTMLDocument) Read(data M) error {
	var err error
	d.FrontMatter = make(M, 8)
	content, err := fmatter.ReadFile(d.FullPath(), d.FrontMatter)
	if err != nil {
		return err
	}

	d.Template, err = template.New(d.Path()).Parse(string(content))
	return err
}