Linkifying images in web site

This commit is contained in:
Chris Sexton 2013-06-01 17:24:05 -04:00
parent 79bec9fc21
commit be1739d316
2 changed files with 16 additions and 2 deletions

View File

@ -461,8 +461,22 @@ func (p *FactoidPlugin) RegisterWeb() *string {
return &tmp return &tmp
} }
func linkify(text string) template.HTML {
parts := strings.Split(text, " ")
for i, word := range parts {
if strings.HasPrefix(word, "http") {
parts[i] = fmt.Sprintf("<a href=\"%s\">%s</a>", word, word)
}
}
return template.HTML(strings.Join(parts, " "))
}
func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) {
context := make(map[string]interface{}) context := make(map[string]interface{})
funcMap := template.FuncMap{
// The name "title" is what the function will be called in the template text.
"linkify": linkify,
}
if e := r.FormValue("entry"); e != "" { if e := r.FormValue("entry"); e != "" {
var entries []Factoid var entries []Factoid
p.Coll.Find(bson.M{"trigger": bson.M{"$regex": e}}).All(&entries) p.Coll.Find(bson.M{"trigger": bson.M{"$regex": e}}).All(&entries)
@ -470,7 +484,7 @@ func (p *FactoidPlugin) serveQuery(w http.ResponseWriter, r *http.Request) {
context["Entries"] = entries context["Entries"] = entries
context["Search"] = e context["Search"] = e
} }
t, err := template.New("factoidIndex").Parse(factoidIndex) t, err := template.New("factoidIndex").Funcs(funcMap).Parse(factoidIndex)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }

View File

@ -76,7 +76,7 @@ var factoidIndex string = `
{{range .Entries}} {{range .Entries}}
<tr> <tr>
<td>{{.Trigger}}</td> <td>{{.Trigger}}</td>
<td>{{.FullText}}</td> <td>{{linkify .FullText}}</td>
<td>{{.CreatedBy}}</td> <td>{{.CreatedBy}}</td>
<td>{{.AccessCount}}</td> <td>{{.AccessCount}}</td>
</tr> </tr>