예제 #1
0
func parseCategories() {
	log.Info("Parse Categories")

	f, err := os.OpenFile("conf/category.conf", os.O_RDONLY, 0777)
	log.FatalOnError(err)
	defer f.Close()

	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		sp := strings.SplitN(scanner.Text(), ":", 3)
		c := &Category{}
		if len(sp) > 0 {
			c.Name = sp[0]
		}
		if len(sp) > 1 {
			c.Title = sp[1]
		}
		if len(sp) > 2 {
			c.Description = sp[2]
		}

		categories = append(categories, c)
	}
}
예제 #2
-1
func build() {
	var err error
	t, err = template.ParseGlob("templates/*.t")
	if err != nil {
		log.Fatal(err)
		return
	}
	parseCategories()

	filepath.Walk("posts", func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() && filepath.Ext(path) == ".md" {
			p, err := parsePost(path)
			if err != nil {
				log.Error(err)
				return nil
			}
			var b bytes.Buffer

			if p.HTML != "" {
				b = *bytes.NewBufferString(p.Content)
			} else {
				err = t.ExecuteTemplate(&b, "post.t", p)
				if err != nil {
					log.Error(err)
					return nil
				}
			}

			pages["/"+p.Name] = b
			addPostToCategory(p)
			addPostToFeed(p)
		}
		return nil
	})

	log.Info("create home page")

	var b bytes.Buffer
	err = t.ExecuteTemplate(&b, "index.t", categories)
	log.FatalOnError(err)
	pages["/"] = b

	log.Info("create feeds")
	feed := feed()

	atom, err := feed.ToAtom()
	log.FatalOnError(err)
	pages["/feed"] = *bytes.NewBufferString(atom)

	rss, err := feed.ToRss()
	log.FatalOnError(err)
	pages["/rss"] = *bytes.NewBufferString(rss)
}