Exemple #1
0
//NewListItem returns ListItem struct from caches.
func NewListItem(caches []*thread.Cache, remove bool, target string, search bool, filter, tag string) *ListItem {
	li := &ListItem{Remove: remove}
	li.Caches = make([]*CacheInfo, 0, len(caches))
	if search {
		li.StrOpts = "?search_new_file=yes"
	}
	for _, ca := range caches {
		x, ok := checkCache(ca, target, filter, tag)
		if !ok {
			continue
		}
		ci := &CacheInfo{Cache: ca}
		li.Caches = append(li.Caches, ci)
		ci.Title = util.EscapeSpace(x)
		if target == "recent" {
			strTags := make([]string, user.Len(ca.Datfile))
			for i, v := range user.GetByThread(ca.Datfile) {
				strTags[i] = strings.ToLower(v.Tagstr)
			}
			for _, st := range suggest.Get(ca.Datfile, nil) {
				if !util.HasString(strTags, strings.ToLower(st.Tagstr)) {
					ci.Sugtags = append(ci.Sugtags, st)
				}
			}
		}
		ci.Tags = user.GetByThread(ca.Datfile)
	}
	return li
}
Exemple #2
0
//HTMLFormat converts plain text to html , including converting link string to <a href="link">.
func (c *CGI) HTMLFormat(plain, appli string, title string, absuri bool) string {
	if strings.HasPrefix(plain, "@markdown") {
		plain = strings.Replace(plain, "<br>", "\n", -1)
		plain = strings.Replace(plain, "&lt;", "<", -1)
		plain = strings.Replace(plain, "&gt;", ">", -1)
		return string(blackfriday.MarkdownCommon([]byte(plain[len("@markdown"):])))
	}
	buf := strings.Replace(plain, "<br>", "\n", -1)
	buf = strings.Replace(buf, "\t", "        ", -1)

	buf = util.Escape(buf)
	regLink := regexp.MustCompile(`https?://[^\x00-\x20"'\(\)<>\[\]\x7F-\xFF]{2,}`)
	if cfg.EnableEmbed {
		var strs []string
		for _, str := range strings.Split(buf, "<br>") {
			s := regLink.ReplaceAllString(str, `<a href="$0">$0</a>`)
			strs = append(strs, s)
			for _, link := range regLink.FindAllString(str, -1) {
				e := util.EmbedURL(link)
				if e != "" {
					strs = append(strs, e)
					strs = append(strs, "")
				}
			}
		}
		buf = strings.Join(strs, "<br>")
	}

	reg1 := regexp.MustCompile("&gt;&gt;[0-9a-f]{8}")
	buf = reg1.ReplaceAllStringFunc(buf, func(str string) string {
		regg := regexp.MustCompile("(&gt;&gt;)([0-9a-f]{8})")
		id := regg.ReplaceAllString(str, "$2")
		return regg.ReplaceAllString(str, c.ResAnchor(id, appli, title, absuri)+"$1$2</a>")
	})
	reg3 := regexp.MustCompile(`(:[a-z0-9_]+:)`)
	buf = reg3.ReplaceAllStringFunc(buf, func(str string) string {
		return util.Emoji(str)
	})
	reg2 := regexp.MustCompile(`\[\[([^<>]+?)\]\]`)
	tmp := reg2.ReplaceAllStringFunc(buf, func(str string) string {
		bl := c.bracketLink(str[2:len(str)-2], appli, absuri)
		return bl
	})
	return util.EscapeSpace(tmp)
}