diff options
author | dzaikos <you@example.com> | 2018-06-26 17:39:56 -0400 |
---|---|---|
committer | dzaikos <you@example.com> | 2018-06-26 17:50:18 -0400 |
commit | 45d7105ed15dd80c9fdd76f61cf239b0a6177a4c (patch) | |
tree | e0f028045a4f53ba1c6155e99affb0e58d73eefc | |
parent | c9131b0e89db2989dd5e2e6894d6bec6c0075cb6 (diff) |
Refactor AddImageTitle rewriter.
* Only processes images with `src` **and** `title` attributes (others are ignored).
* Processes **all** images in the document (not just the first one).
* Wraps the image and its title attribute in a `figure` tag with the title attribute's contents in a `figcaption` tag.
Updated xkcd rewriter unit test.
Added another xkcd rewriter unit test to check rendering of images without title tags.
-rw-r--r-- | reader/rewrite/rewrite_functions.go | 16 | ||||
-rw-r--r-- | reader/rewrite/rewriter_test.go | 10 |
2 files changed, 22 insertions, 4 deletions
diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index 6717deb..412266d 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -22,9 +22,19 @@ func addImageTitle(entryURL, entryContent string) string { return entryContent } - imgTag := doc.Find("img").First() - if titleAttr, found := imgTag.Attr("title"); found { - return entryContent + `<blockquote cite="` + entryURL + `">` + titleAttr + "</blockquote>" + matches := doc.Find("img[src][title]") + + if matches.Length() > 0 { + matches.Each(func(i int, img *goquery.Selection) { + altAttr := img.AttrOr("alt", "") + srcAttr, _ := img.Attr("src") + titleAttr, _ := img.Attr("title") + + img.ReplaceWithHtml(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + titleAttr + `</p></figcaption></figure>`) + }) + + output, _ := doc.Find("body").First().Html() + return output } return entryContent diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index 995508b..7a33b1f 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -35,7 +35,15 @@ func TestRewriteWithInexistingCustomRule(t *testing.T) { func TestRewriteWithXkcdLink(t *testing.T) { description := `<img src="https://imgs.xkcd.com/comics/thermostat.png" title="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />` output := Rewriter("https://xkcd.com/1912/", description, ``) - expected := description + `<blockquote cite="https://xkcd.com/1912/">Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.</blockquote>` + expected := `<figure><img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you."/><figcaption><p>Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.</p></figcaption></figure>` + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} +func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) { + description := `<img src="https://imgs.xkcd.com/comics/thermostat.png" alt="Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you." />` + output := Rewriter("https://xkcd.com/1912/", description, ``) + expected := description if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } |