From 054aa15a178fc06a8b7b87bb676753c42fcb6330 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Tue, 22 Oct 2019 00:19:31 -0400 Subject: [PATCH] composition support for inline images --- activity.go | 9 ++------- docs/changelog.txt | 4 ++-- docs/honk.5 | 5 +++++ fun.go | 42 ++++++++++++++++++++++++++++++++++++------ web.go | 3 ++- 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/activity.go b/activity.go index 47adf5a..5f2095e 100644 --- a/activity.go +++ b/activity.go @@ -33,7 +33,6 @@ import ( "time" "humungus.tedunangst.com/r/webs/cache" - "humungus.tedunangst.com/r/webs/htfilter" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/image" "humungus.tedunangst.com/r/webs/junk" @@ -824,11 +823,6 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { content = content[:90001] } - // grab any inline imgs - imgfilt := htfilter.New() - imgfilt.Imager = inlineimgsfor(&xonk) - imgfilt.String(content) - // init xonk xonk.What = what xonk.XID = xid @@ -844,6 +838,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { xonk.Whofore = 1 } } + imaginate(&xonk) if isUpdate { log.Printf("something has changed! %s", xonk.XID) @@ -1023,7 +1018,7 @@ func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) { if !h.Public { jo["directMessage"] = true } - translate(h) + translate(h, true) h.Noise = re_memes.ReplaceAllString(h.Noise, "") jo["summary"] = html.EscapeString(h.Precis) jo["content"] = ontologize(mentionize(h.Noise)) diff --git a/docs/changelog.txt b/docs/changelog.txt index 753b218..15b24bb 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -2,6 +2,8 @@ changelog -- next ++ Inline images in posts. Send and receive. + + Somewhat functional admin console (TTY). + More JS free fallbacks for some basic functions. @@ -14,8 +16,6 @@ changelog +++ Add Honk Filtering and Censorship System (HFCS). -+ Inline images in received posts. - + Times for events. + Split media database into separate blob.db. diff --git a/docs/honk.5 b/docs/honk.5 index b0d4a63..19a0e6f 100644 --- a/docs/honk.5 +++ b/docs/honk.5 @@ -44,6 +44,11 @@ Inline `code fragments` with single ticks. int main() { return 0; } ``` .Ed +.It images +Inline images with img tags. +.Bd -literal +Lifecycle of a honk +.Ed .It links URLs beginning with .Dq http diff --git a/fun.go b/fun.go index 1be7618..151fc31 100644 --- a/fun.go +++ b/fun.go @@ -58,7 +58,7 @@ func reverbolate(userid int64, honks []*Honk) { if !h.Public { h.Style += " limited" } - translate(h) + translate(h, false) if h.Whofore == 2 || h.Whofore == 3 { h.URL = h.XID if h.What != "bonked" { @@ -92,7 +92,7 @@ func reverbolate(userid int64, honks []*Honk) { zap := make(map[string]bool) { filt := htfilter.New() - filt.Imager = replaceimgsand(zap) + filt.Imager = replaceimgsand(zap, false) filt.SpanClasses = allowedclasses p, _ := filt.String(h.Precis) n, _ := filt.String(h.Noise) @@ -145,7 +145,7 @@ func reverbolate(userid int64, honks []*Honk) { } } -func replaceimgsand(zap map[string]bool) func(node *html.Node) string { +func replaceimgsand(zap map[string]bool, absolute bool) func(node *html.Node) string { return func(node *html.Node) string { src := htfilter.GetAttr(node, "src") alt := htfilter.GetAttr(node, "alt") @@ -156,7 +156,11 @@ func replaceimgsand(zap map[string]bool) func(node *html.Node) string { d := finddonk(src) if d != nil { zap[d.XID] = true - return string(templates.Sprintf(`%s`, alt, alt, d.XID)) + base := "" + if absolute { + base = "https://" + serverName + } + return string(templates.Sprintf(`%s`, alt, alt, base, d.XID)) } return string(templates.Sprintf(`<img alt="%s" src="%s">`, alt, src, src)) } @@ -177,7 +181,13 @@ func inlineimgsfor(honk *Honk) func(node *html.Node) string { } } -func translate(honk *Honk) { +func imaginate(honk *Honk) { + imgfilt := htfilter.New() + imgfilt.Imager = inlineimgsfor(honk) + imgfilt.String(honk.Noise) +} + +func translate(honk *Honk, redoimages bool) { if honk.Format == "html" { return } @@ -197,9 +207,29 @@ func translate(honk *Honk) { noise = strings.TrimSpace(noise) noise = quickrename(noise, honk.UserID) noise = markitzero(noise) - honk.Noise = noise honk.Onts = oneofakind(ontologies(honk.Noise)) + + if redoimages { + zap := make(map[string]bool) + { + filt := htfilter.New() + filt.Imager = replaceimgsand(zap, true) + filt.SpanClasses = allowedclasses + p, _ := filt.String(honk.Precis) + n, _ := filt.String(honk.Noise) + honk.Precis = string(p) + honk.Noise = string(n) + } + j := 0 + for i := 0; i < len(honk.Donks); i++ { + if !zap[honk.Donks[i].XID] { + honk.Donks[j] = honk.Donks[i] + j++ + } + } + honk.Donks = honk.Donks[:j] + } } func shortxid(xid string) string { diff --git a/web.go b/web.go index 7450c94..3c3d576 100644 --- a/web.go +++ b/web.go @@ -1065,7 +1065,7 @@ func submithonk(w http.ResponseWriter, r *http.Request) { noise = hooterize(noise) honk.Noise = noise - translate(honk) + translate(honk, false) var convoy string if rid != "" { @@ -1189,6 +1189,7 @@ func submithonk(w http.ResponseWriter, r *http.Request) { } } memetize(honk) + imaginate(honk) placename := strings.TrimSpace(r.FormValue("placename")) placelat := strings.TrimSpace(r.FormValue("placelat"))