コード例 #1
0
ファイル: layout.go プロジェクト: wickedchicken/blarg
func json_export(w http.ResponseWriter, req *http.Request, blog_config map[string]interface{}, keygen func(appengine.Context) ([]*datastore.Key, error)) {

	appcontext := appengine.NewContext(req)
	postchan := make(chan post.FullPost, 16)
	errchan := make(chan error)

	keys, err := keygen(appcontext)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	posts := map[string]jsonpost{}

	idx := len(keys)
	if idx < 1 {
		io.WriteString(w, fmt.Sprintf("{}"))
	} else {
		go post.GetPosts(appcontext, keys, 0, len(keys), postchan, errchan)
		for p := range postchan {
			posts[p.Post.StickyUrl] = jsonpost{
				p.Post.Title,
				p.Content,
				p.Post.Postdate,
				p.Post.StickyUrl,
				p.Post.Tags,
			}
		}

		err, ok := <-errchan
		if ok {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		b, err := json.Marshal(posts)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		io.WriteString(w, bytes.NewBuffer(b).String())
	}
}
コード例 #2
0
ファイル: layout.go プロジェクト: wickedchicken/blarg
func list(w http.ResponseWriter, req *http.Request, blog_config map[string]interface{}, url_stem string, offset int, limit int, keygen func(appengine.Context) ([]*datastore.Key, error)) {

	appcontext := appengine.NewContext(req)
	template_dir := "templates/"
	postchan := make(chan post.FullPost, 16)
	errchan := make(chan error)

	keys, err := keygen(appcontext)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	idx := len(keys)

	pages := idx / limit
	if (idx % limit) > 0 {
		pages += 1
	}

	curpage := offset / limit

	if idx < 1 {
		io.WriteString(w, fmt.Sprintf("<div class=\"entry\"><p>no posts found :(</p></div>"))
	} else {
		//go post.ExecuteQuery(appcontext, query, offset, limit, postchan, errchan)
		go post.GetPosts(appcontext, keys, offset, limit, postchan, errchan)

		myurl, err := realhostname(req, appcontext)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		scheme := "http://"
		if req.TLS != nil {
			scheme = "https://"
		}

		urlprefix := scheme + myurl + config.Stringify(blog_config["blog_root"])

		for p := range postchan {
			con := bytes.NewBuffer(blackfriday.MarkdownCommon(bytes.NewBufferString(p.Content).Bytes())).String()
			context := map[string]interface{}{"c": con, "labels": labels(p.Post.Tags, blog_config),
				"link_to_entry": urlprefix + "article/" + p.Post.StickyUrl,
				"post_date":     p.Post.Postdate.Format("Jan 02 2006"),
				"post_title":    p.Post.Title}
			total_con := config.Stringify_map(config.Merge(blog_config, context))
			c := mustache.RenderFile(template_dir+"list_entry.html.mustache", total_con)
			io.WriteString(w, c)
		}

		err, ok := <-errchan
		if ok {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		context := map[string]interface{}{}

		root := config.Stringify(blog_config["blog_root"])

		context["prev_page"] = "&lt;&lt; prev"
		context["next_page"] = "next &gt;&gt;"

		if pages > 1 {
			context["pb"] = "true"
		}
		if curpage > 0 {
			context["prev_page"] = fmt.Sprintf("<a href=\"%v%v/%v\">&lt;&lt; prev</a>", root, url_stem, curpage-1)
		}
		if curpage < (pages - 1) {
			context["next_page"] = fmt.Sprintf("<a href=\"%v%v/%v\">next &gt;&gt;</a>", root, url_stem, curpage+1)
		}

		total_con := config.Stringify_map(config.Merge(blog_config, context))
		c := mustache.RenderFile(template_dir+"list.html.mustache", total_con)
		io.WriteString(w, c)
	}
}
コード例 #3
0
ファイル: layout.go プロジェクト: wickedchicken/blarg
func GetRSS(blog_config map[string]interface{}) func(w http.ResponseWriter, req *http.Request) {
	template_dir := "templates/"
	l := func(w http.ResponseWriter, req *http.Request) {
		limit, err := getLimit(blog_config)
		if err != nil {
			panic(err)
		}

		appcontext := appengine.NewContext(req)

		myurl, err := realhostname(req, appcontext)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		scheme := "http://"
		if req.TLS != nil {
			scheme = "https://"
		}

		urlprefix := scheme + myurl + config.Stringify(blog_config["blog_root"])

		keys, err := post.GetPostsMatchingTag(appcontext, "visible")
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		idx := len(keys)

		offset := 0

		entries := new(bytes.Buffer)
		if idx >= 1 {
			postchan := make(chan post.FullPost, 16)
			errchan := make(chan error)

			go post.GetPosts(appcontext, keys, offset, limit, postchan, errchan)
			for p := range postchan {
				con := bytes.NewBuffer(blackfriday.MarkdownCommon(bytes.NewBufferString(p.Content).Bytes())).String()
				context := map[string]interface{}{"c": con,
					"link_to_entry": urlprefix + "article/" + p.Post.StickyUrl,
					"post_date":     p.Post.Postdate.Format("Jan 02 2006"),
					"post_title":    p.Post.Title}
				total_con := config.Stringify_map(config.Merge(blog_config, context))
				c := mustache.RenderFile(template_dir+"rss_entry.html.mustache", total_con)
				io.WriteString(entries, c)
			}
			err, ok := <-errchan
			if ok {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

		}

		context := map[string]interface{}{
			"lastmod_date": post.GetLatestDate(appcontext).Format("2006-01-02"),
			"content":      entries,
		}

		total_con := config.Stringify_map(config.Merge(blog_config, context))
		c := mustache.RenderFile(template_dir+"rss.mustache", total_con)
		io.WriteString(w, c)
	}
	return l
}