From 8ffb773f43c8dc54801ca1d111854e7e881c93c9 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 19 Nov 2017 21:10:04 -0800 Subject: First commit --- reader/rewrite/rewriter.go | 47 +++++++++++++++++++++++++++++++++++++++++ reader/rewrite/rewriter_test.go | 34 +++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 reader/rewrite/rewriter.go create mode 100644 reader/rewrite/rewriter_test.go (limited to 'reader/rewrite') diff --git a/reader/rewrite/rewriter.go b/reader/rewrite/rewriter.go new file mode 100644 index 0000000..f23c0af --- /dev/null +++ b/reader/rewrite/rewriter.go @@ -0,0 +1,47 @@ +// Copyright 2017 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package rewrite + +import ( + "regexp" + "strings" + + "github.com/PuerkitoBio/goquery" +) + +var rewriteRules = []func(string, string) string{ + func(url, content string) string { + re := regexp.MustCompile(`youtube\.com/watch\?v=(.*)`) + matches := re.FindStringSubmatch(url) + + if len(matches) == 2 { + video := `` + return video + "

" + content + "

" + } + return content + }, + func(url, content string) string { + if strings.HasPrefix(url, "https://xkcd.com") { + doc, err := goquery.NewDocumentFromReader(strings.NewReader(content)) + if err != nil { + return content + } + + imgTag := doc.Find("img").First() + if titleAttr, found := imgTag.Attr("title"); found { + return content + `
` + titleAttr + "
" + } + } + return content + }, +} + +func Rewriter(url, content string) string { + for _, rewriteRule := range rewriteRules { + content = rewriteRule(url, content) + } + + return content +} diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go new file mode 100644 index 0000000..a666406 --- /dev/null +++ b/reader/rewrite/rewriter_test.go @@ -0,0 +1,34 @@ +// Copyright 2017 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package rewrite + +import "testing" + +func TestRewriteWithNoMatchingRule(t *testing.T) { + output := Rewriter("https://example.org/article", `Some text.`) + expected := `Some text.` + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + +func TestRewriteWithYoutubeLink(t *testing.T) { + output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`) + expected := `

Video Description

` + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + +func TestRewriteWithXkcdLink(t *testing.T) { + description := `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 + `
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.
` + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} -- cgit v1.2.3