Esempio n. 1
0
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
}
Esempio n. 2
0
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
}