Beispiel #1
0
func Read(c appengine.Context, w http.ResponseWriter, r *http.Request) error {
	w.Header().Set("Content-Type", "application/xhtml+xml; charset=UTF-8")

	var id string
	if strings.HasPrefix(r.URL.Path, "/read/") {
		id = r.URL.Path[len("/read/"):]
	} else {
		return NotFound(r.URL.Path)
	}

	type metadata struct {
		Label string
		Value string
	}
	type renderdata struct {
		Title string
		Meta  []metadata
		HTML  string
	}

	var data renderdata

	s, err := GetStory(c, id)
	if err != nil {
		return err
	}

	data.Title = html.EscapeString(s.Title)

	for name, prop := range s.Meta {
		if len(name) == 0 || len(prop.Name) == 0 {
			c.Warningf("Zero-length property name?")
			continue
		}
		data.Meta = append(data.Meta, metadata{
			Label: html.EscapeString(strings.ToUpper(prop.Name[:1]) + prop.Name[1:]),
			Value: html.EscapeString(prop.Value),
		})
	}

	if node, err := fictex.ParseBytes(s.Source); err == nil {
		b := new(bytes.Buffer)
		if err := fictex.HTMLRenderer.Render(b, node); err == nil {
			data.HTML = b.String()
		}
	} else {
		data.HTML = html.EscapeString(fmt.Sprintf("Error: %s", err))
	}

	return templates.ExecuteTemplate(w, "render.html", data)
}
Beispiel #2
0
func Edit(c appengine.Context, w http.ResponseWriter, r *http.Request) error {
	w.Header().Set("Content-Type", "application/xhtml+xml; charset=UTF-8")

	var id string
	if strings.HasPrefix(r.URL.Path, "/edit/") {
		id = r.URL.Path[len("/edit/"):]
	} else if r.URL.Path == "/" {
		id = "autosave"
	} else {
		return NotFound(r.URL.Path)
	}

	type metadata struct {
		Id    string
		Label string
		Value string
	}
	type maindata struct {
		// Story
		Id    string
		Title string
		Meta  []metadata

		// Story list
		Stories string

		// Preview
		Source        string
		PreviewHTML   string
		PreviewSource string
	}

	var data maindata

	_, k := UserKey(c)
	s := NewStory(c, id, k)

	if err := s.Get(c); err != nil {
		if id != "autosave" {
			return NotFound(r.URL.Path)
		}
	} else {
		data.Title = html.EscapeString(s.Title)
		data.Id = html.EscapeString(id)
	}

	for name, prop := range s.Meta {
		if len(name) == 0 || len(prop.Name) == 0 {
			c.Warningf("Zero-length property name?")
			continue
		}
		data.Meta = append(data.Meta, metadata{
			Id:    html.EscapeString(name),
			Label: html.EscapeString(strings.ToUpper(prop.Name[:1]) + prop.Name[1:]),
			Value: html.EscapeString(prop.Value),
		})
	}

	data.Source = html.EscapeString(string(s.Source))
	if node, err := fictex.ParseBytes(s.Source); err == nil {
		b := new(bytes.Buffer)
		if err := fictex.HTMLRenderer.Render(b, node); err == nil {
			data.PreviewHTML = b.String()
		}
	}
	data.PreviewSource = html.EscapeString(data.PreviewHTML)

	if js, err := JSONStoryList(c, k); err != nil {
		c.Warningf("Failed to load story list: %s", err)
	} else {
		data.Stories = string(js)
	}

	return templates.ExecuteTemplate(w, "edit.html", data)
}