Example #1
0
func TestPageMeta(t *testing.T) {
	Convey("parse page with MdParser", t, func() {
		bytes, err := ioutil.ReadFile("../../source/page/about.md")
		So(err, ShouldBeNil)
		blocks, err := p2.Parse(bytes)
		So(err, ShouldBeNil)
		So(blocks, ShouldHaveLength, 2)

		Convey("check page blocks", func() {
			So(blocks[0].Type(), ShouldEqual, parser.BLOCK_INI)
			So(blocks[1].Type(), ShouldEqual, parser.BLOCK_MARKDOWN)

			Convey("use page blocks", func() {
				b, ok := blocks[0].(parser.MetaBlock)
				So(ok, ShouldBeTrue)
				So(b.Item("title"), ShouldEqual, "About Pugo.Static")

				fi, _ := os.Stat("../../source/page/about.md")
				page, err := model.NewPage(blocks, fi)
				So(err, ShouldBeNil)
				So(page.Title, ShouldEqual, b.Item("title"))
				So(page.Meta["metadata"], ShouldEqual, b.Item("meta", "metadata"))
			})
		})
	})
}
Example #2
0
// read contents, including posts and pages
func (b *Builder) readContents(ctx *Context) {
	filter := func(p string) bool {
		return path.Ext(p) == ".md"
	}
	postData, infoData, err := b.parseDir("post", filter)
	if err != nil {
		ctx.Error = err
		return
	}
	for k, blocks := range postData {
		post, err := model.NewPost(blocks, infoData[k])
		if err != nil {
			ctx.Error = err
			return
		}
		// use named author
		if author, ok := ctx.Authors[post.Author.Name]; ok {
			post.Author = author
		}
		ctx.Posts = append(ctx.Posts, post)
	}
	sort.Sort(model.Posts(ctx.Posts))

	ctx.Tags = make(map[string]*model.Tag)
	ctx.tagPosts = make(map[string][]*model.Post)
	for _, p := range ctx.Posts {
		for i, t := range p.Tags {
			ctx.Tags[t.Name] = p.Tags[i]
			ctx.tagPosts[t.Name] = append(ctx.tagPosts[t.Name], p)
		}
	}

	pageData, infoData, err := b.parseDir("page", filter)
	if err != nil {
		ctx.Error = err
		return
	}
	for k, blocks := range pageData {
		page, err := model.NewPage(blocks, infoData[k])
		if err != nil {
			ctx.Error = err
			return
		}
		// use named author
		if author, ok := ctx.Authors[page.Author.Name]; ok {
			page.Author = author
		}
		ctx.Pages = append(ctx.Pages, page)
	}
}