//printPageNavi renders page_navi.txt, part for paging.
func (t *threadCGI) printPageNavi(path string, page int, ca *thread.Cache, id string) {
	len := ca.Len()
	first := len / cfg.ThreadPageSize
	if len%cfg.ThreadPageSize == 0 {
		first++
	}
	pages := make([]int, first+1)
	for i := 0; i <= first; i++ {
		pages[i] = i
	}
	s := struct {
		Page           int
		CacheLen       int
		Path           string
		ID             string
		First          int
		ThreadCGI      string
		Message        cgi.Message
		ThreadPageSize int
		Pages          []int
	}{
		page,
		len,
		path,
		id,
		first,
		cfg.ThreadURL,
		t.M,
		cfg.ThreadPageSize,
		pages,
	}
	cgi.RenderTemplate("page_navi", s, t.WR)
}
//printRecord renders record.txt , with records in cache ca.
func (t *threadCGI) printRecord(ca *thread.Cache, rec *record.Record) {
	thumbnailSize := ""
	var suffix string
	var attachSize int64
	if at := rec.GetBodyValue("attach", ""); at != "" {
		suffix = rec.GetBodyValue("suffix", "")
		attachFile := rec.AttachPath("")
		attachSize = int64(len(at)*57/78) + 1000
		reg := regexp.MustCompile("^[0-9A-Za-z]+")
		if !reg.MatchString(suffix) {
			suffix = cfg.SuffixTXT
		}
		typ := mime.TypeByExtension("." + suffix)
		if typ == "" {
			typ = "text/plain"
		}
		if util.IsValidImage(typ, attachFile) {
			thumbnailSize = cfg.DefaultThumbnailSize
		}
	}
	body := rec.GetBodyValue("body", "")
	body = t.HTMLFormat(body, cfg.ThreadURL, t.Path(), false)
	removeID := rec.GetBodyValue("remove_id", "")
	if len(removeID) > 8 {
		removeID = removeID[:8]
	}
	resAnchor := t.ResAnchor(removeID, cfg.ThreadURL, t.Path(), false)

	id8 := rec.ID
	if len(id8) > 8 {
		id8 = id8[:8]
	}
	s := struct {
		Datfile    string
		Rec        *record.Record
		RecHead    record.Head
		Sid        string
		AttachSize int64
		Suffix     string
		Body       template.HTML
		Thumbnail  string
		RemoveID   string
		ResAnchor  string
		cgi.Defaults
	}{
		ca.Datfile,
		rec,
		rec.CopyHead(),
		id8,
		attachSize,
		suffix,
		template.HTML(body),
		thumbnailSize,
		removeID,
		resAnchor,
		*t.Defaults(),
	}
	cgi.RenderTemplate("record", s, t.WR)
}
//printTag renders thread_tags.txt , part for displayng tags.
func (t *threadCGI) printTag(ca *thread.Cache) {
	s := struct {
		Datfile   string
		Tags      []string
		Classname string
		Target    string
		cgi.Defaults
	}{
		ca.Datfile,
		user.GetStrings(ca.Datfile),
		"tags",
		"changes",
		*t.Defaults(),
	}
	cgi.RenderTemplate("thread_tags", s, t.WR)
}
//printPostForm renders post_form.txt,page for posting attached file.
func (t *threadCGI) printPostForm(ca *thread.Cache) {
	mimes := []string{
		".css", ".gif", ".htm", ".html", ".jpg", ".js", ".pdf", ".png", ".svg",
		".txt", ".xml",
	}
	s := struct {
		Cache    *thread.Cache
		Suffixes []string
		Limit    int
		cgi.Defaults
	}{
		ca,
		mimes,
		cfg.RecordLimit * 3 >> 2,
		*t.Defaults(),
	}
	cgi.RenderTemplate("post_form", s, t.WR)
}
//PrintTitle renders list of newer thread in the disk for the top page
func PrintTitle(w http.ResponseWriter, r *http.Request) {
	g, err := new(w, r)
	if err != nil {
		log.Println(err)
		return
	}
	if r.FormValue("cmd") != "" {
		g.jumpNewFile()
		return
	}
	all := thread.AllCaches()
	sort.Sort(sort.Reverse(thread.NewSortByStamp(all, false)))
	outputCachelist := make([]*thread.Cache, 0, thread.Len())
	for _, ca := range all {
		if time.Now().Unix() <= ca.Stamp()+cfg.TopRecentRange {
			outputCachelist = append(outputCachelist, ca)
		}
	}

	g.Header(g.M["logo"]+" - "+g.M["description"], "", nil, false)
	s := struct {
		Target        string
		Taglist       tag.Slice
		MchURL        string
		MchCategories []*mchCategory
		Types         string
		NoList        bool
		cgi.ListItem
		cgi.Defaults
	}{
		"changes",
		user.Get(),
		g.mchURL(""),
		g.mchCategories(),
		"thread",
		len(outputCachelist) == 0,
		*cgi.NewListItem(outputCachelist, false, "changes", false, g.Filter, g.Tag),
		*g.Defaults(),
	}
	cgi.RenderTemplate("top", s, g.WR)
	g.PrintNewElementForm()
	g.Footer(nil)
}
//printThread renders whole thread list page.
func (t *threadCGI) printThread(path, id string, nPage int) {
	if id != "" && t.Req.FormValue("ajax") != "" {
		t.printThreadAjax(id)
		return
	}
	filePath := util.FileEncode("thread", path)
	ca := thread.NewCache(filePath)
	rss := cfg.GatewayURL + "/rss"
	if t.printThreadHead(path, id, nPage, ca, rss) != nil {
		return
	}
	tags := strings.Fields(strings.TrimSpace(t.Req.FormValue("tag")))
	if t.IsAdmin() && len(tags) > 0 {
		user.Add(ca.Datfile, tags)
	}
	t.printTag(ca)
	t.printThreadTop(path, id, nPage, ca)
	t.printPageNavi(path, nPage, ca, id)
	t.printThreadBody(id, nPage, ca)

	escapedPath := html.EscapeString(path)
	escapedPath = strings.Replace(escapedPath, "  ", "&nbsp;&nbsp;", -1)
	ss := struct {
		Cache   *thread.Cache
		Message cgi.Message
	}{
		ca,
		t.M,
	}
	cgi.RenderTemplate("thread_bottom", ss, t.WR)

	if ca.HasRecord() {
		t.printPageNavi(path, nPage, ca, id)
		fmt.Fprintf(t.WR, "</p>")
	}
	t.printPostForm(ca)
	t.printTag(ca)
	t.RemoveFileForm(ca, escapedPath)
	t.Footer(t.MakeMenubar("bottom", rss))
}
//printThreadTop renders toppart of thread page.
func (t *threadCGI) printThreadTop(path, id string, nPage int, ca *thread.Cache) {
	var lastrec *record.Record
	var resAnchor string
	recs := ca.LoadRecords(record.Alive)
	ids := recs.Keys()
	if ca.HasRecord() && nPage == 0 && id == "" && len(ids) > 0 {
		lastrec = recs[ids[len(ids)-1]]
		resAnchor = t.ResAnchor(lastrec.ID[:8], cfg.ThreadURL, t.Path(), false)
	}
	s := struct {
		Path      string
		Cache     *thread.Cache
		Lastrec   *record.Record
		ResAnchor template.HTML
		cgi.Defaults
	}{
		path,
		ca,
		lastrec,
		template.HTML(resAnchor),
		*t.Defaults(),
	}
	cgi.RenderTemplate("thread_top", s, t.WR)
}
Example #8
0
//errorResp render erro page with cp932 code.
func (m *mchCGI) errorResp(msg string, info map[string]string) {
	m.WR.Header().Set("Content-Type", "text/html; charset=Shift_JIS")
	info["message"] = msg
	cgi.RenderTemplate("2ch_error", info, m.WR)
}