Beispiel #1
0
func GetPostText(blog_config map[string]interface{}) func(w http.ResponseWriter, req *http.Request) {
	l := func(w http.ResponseWriter, req *http.Request) {
		appcontext := appengine.NewContext(req)
		query := post.GetPostsMatchingUrl(req.URL.Query().Get(":name"))
		idx, err := post.GetCount(appcontext, query)

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

		if idx < 1 {
			http.Error(w, err.Error(), http.StatusNotFound)
			io.WriteString(w, fmt.Sprintf("<div class=\"entry\"><p>no posts found :(</p></div>"))
		} else {
			var ps []post.Post
			_, err := query.GetAll(appcontext, &ps)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			p := ps[0]

			content, err := post.GetPostContent(appcontext, p)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				appcontext.Errorf("post.GetPostContent: %v", err)
				return
			}
			con := bytes.NewBuffer(blackfriday.MarkdownCommon(bytes.NewBufferString(content).Bytes())).String()
			io.WriteString(w, con)
		}
	}
	return l
}
Beispiel #2
0
func EditPost(blog_config map[string]interface{}) func(w http.ResponseWriter, req *http.Request) {
	template_dir := "templates/"
	l := func(w http.ResponseWriter, req *http.Request) {
		appcontext := appengine.NewContext(req)

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

		name := req.URL.Query().Get(":name")
		if name != "" {
			query := post.GetPostsMatchingUrl(req.URL.Query().Get(":name"))
			idx, err := post.GetCount(appcontext, query)

			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}

			if idx < 1 {
				context["articlesource"] = "Enter your post here!"
				context["labels"] = "visible"
				context["title"] = "No post with that URL found, making new post!"
			} else {
				var ps []post.Post
				keys, err := query.GetAll(appcontext, &ps)
				if err != nil {
					http.Error(w, err.Error(), http.StatusInternalServerError)
				}

				p := ps[0]
				tags, err := post.GetTagSlice(appcontext, keys[0])
				if err != nil {
					http.Error(w, err.Error(), http.StatusInternalServerError)
				}

				content, err := post.GetPostContent(appcontext, p)
				if err != nil {
					http.Error(w, err.Error(), http.StatusInternalServerError)
				}

				context["articlesource"] = content
				context["labels"] = strings.Join(tags, ", ")
				context["title"] = p.Title
			}
		} else {
			context["articlesource"] = "Enter your post here!"
			context["labels"] = "visible, enter, labels, here"
			context["title"] = "enter title here"
		}

		key := fmt.Sprintf("%v", rand.Int())
		tok, err := channel.Create(appcontext, key)
		if err != nil {
			http.Error(w, "Couldn't create Channel", http.StatusInternalServerError)
			appcontext.Errorf("channel.Create: %v", err)
			return
		}

		posturl := "/admin/post?g=" + key
		uploadurl, err := blobstore.UploadURL(appcontext, posturl, nil)
		if err != nil {
			http.Error(w, "Couldn't create blob URL", http.StatusInternalServerError)
			appcontext.Errorf("blobstore.UploadURL: %v", err)
			return
		}

		context["token"] = tok
		context["session"] = key
		context["uploadurl"] = uploadurl

		total_con := config.Stringify_map(config.Merge(blog_config, context))
		c := mustache.RenderFile(template_dir+"edit.html.mustache", total_con)
		io.WriteString(w, c)

		if err != nil {
			appcontext.Errorf("mainTemplate: %v", err)
		}
	}
	return admin_layout(blog_config, l)
}
Beispiel #3
0
func GetArticle(blog_config map[string]interface{}, url_stem string) func(w http.ResponseWriter, req *http.Request) {
	template_dir := "templates/"
	l := func(w http.ResponseWriter, req *http.Request) {
		appcontext := appengine.NewContext(req)
		query := post.GetPostsMatchingUrl(req.URL.Query().Get(":name"))
		idx, err := post.GetCount(appcontext, query)

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

		if idx < 1 {
			http.Error(w, err.Error(), http.StatusNotFound)
			io.WriteString(w, fmt.Sprintf("<div class=\"entry\"><p>no posts found :(</p></div>"))
			return
		} else {

			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"])

			var ps []post.Post
			keys, err := query.GetAll(appcontext, &ps)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			p := ps[0]
			tags, err := post.GetTagSlice(appcontext, keys[0])
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			content, err := post.GetPostContent(appcontext, p)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				appcontext.Errorf("post.GetPostContent: %v", err)
				return
			}
			con := bytes.NewBuffer(blackfriday.MarkdownCommon(bytes.NewBufferString(content).Bytes())).String()
			context := map[string]interface{}{"c": con, "labels": labels(tags, blog_config),
				"link_to_entry":   urlprefix + "article/" + p.StickyUrl,
				"comment_display": "none",
				"post_date":       p.Postdate.Format("Oct 02 2006"),
				"post_title":      p.Title}
			total_con := config.Stringify_map(config.Merge(blog_config, context))
			c := mustache.RenderFile(template_dir+"entry.html.mustache", total_con)
			io.WriteString(w, c)
		}
	}
	return std_layout(blog_config, l)
}