Example #1
0
func genNewArticle(title string) {
	fmt.Printf("genNewArticle: %q\n", title)
	store, err := NewStore()
	if err != nil {
		log.Fatalf("NewStore() failed with %s", err)
	}
	newId := findUniqueArticleId(store.articles)
	name := sanitizeForFile(title) + ".md"
	fmt.Printf("new id: %d, name: %s\n", newId, name)
	t := time.Now()
	dir := "blog_posts"
	d := t.Format("2006-01")
	path := filepath.Join(dir, d, name)
	s := fmt.Sprintf(`Id: %d
Title: %s
Date: %s
Format: Markdown
--------------`, newId, title, t.Format(time.RFC3339))
	for i := 1; i < 10; i++ {
		if !u.PathExists(path) {
			break
		}
		name := sanitizeForFile(title) + "-" + strconv.Itoa(i) + ".md"
		path = filepath.Join(dir, d, name)
	}
	u.PanicIf(u.PathExists(path))
	fmt.Printf("path: %s\n", path)
	u.CreateDirForFileMust(path)
	ioutil.WriteFile(path, []byte(s), 0644)
}
Example #2
0
func splitFile(path string) (string, string) {
	d, err := ioutil.ReadFile(path)
	u.PanicIfErr(err)
	s := string(d)
	s = strings.Replace(s, "\r\n", "\n", -1)
	s = strings.Replace(s, "\r", "\n", -1)
	idx := strings.Index(s, "----------")
	u.PanicIf(idx == -1, "idx == -1")
	hdr := s[:idx]
	hdr = strings.Replace(hdr, "Html", "Markdown", -1)
	hdr = strings.Replace(hdr, "Textile", "Markdown", -1)
	body := s[idx:]
	idx = strings.Index(body, "\n")
	u.PanicIf(idx == -1, "idx == -1")
	body = body[idx+1:]
	return hdr, body
}
Example #3
0
func buildArticlesCache() {
	u.PanicIf(articlesCache.articles != nil)
	articles := store.GetArticles()
	articlesCache.articles = articles
	articlesCache.articlesJs, articlesCache.articlesJsSha1 = buildArticlesJson(articles)
}