From 6a3d85477d4303fa9339d9cf5b604676b2a4f5fd Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Wed, 25 Nov 2020 14:51:54 -0800 Subject: [PATCH] Add rewrite rule replace for custom search and replace --- reader/rewrite/rewrite_functions.go | 9 +++++++++ reader/rewrite/rewriter.go | 16 +++++++++++++++- reader/rewrite/rewriter_test.go | 10 ++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index 17bff9f0900..6dce09c3cdb 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -221,3 +221,12 @@ func replaceTextLinks(input string) string { func replaceLineFeeds(input string) string { return strings.Replace(input, "\n", "
", -1) } + +func replaceCustom(entryContent string, searchTerm string, replaceTerm string) string { + re, err := regexp.Compile(searchTerm) + if err == nil { + return re.ReplaceAllString(entryContent, replaceTerm) + } + return entryContent +} + diff --git a/reader/rewrite/rewriter.go b/reader/rewrite/rewriter.go index 91955247190..158dcd209c0 100644 --- a/reader/rewrite/rewriter.go +++ b/reader/rewrite/rewriter.go @@ -6,11 +6,14 @@ package rewrite // import "miniflux.app/reader/rewrite" import ( "strings" + "regexp" "miniflux.app/logger" "miniflux.app/url" ) +var customReplaceRuleRegex = regexp.MustCompile(`replace\("(.*)"\|"(.*)"\)`) + // Rewriter modify item contents with a set of rewriting rules. func Rewriter(entryURL, entryContent, customRewriteRules string) string { rulesList := getPredefinedRewriteRules(entryURL) @@ -24,7 +27,8 @@ func Rewriter(entryURL, entryContent, customRewriteRules string) string { logger.Debug(`[Rewrite] Applying rules %v for %q`, rules, entryURL) for _, rule := range rules { - switch strings.TrimSpace(rule) { + rule := strings.TrimSpace(rule) + switch rule { case "add_image_title": entryContent = addImageTitle(entryURL, entryContent) case "add_mailto_subject": @@ -47,6 +51,16 @@ func Rewriter(entryURL, entryContent, customRewriteRules string) string { entryContent = fixMediumImages(entryURL, entryContent) case "use_noscript_figure_images": entryContent = useNoScriptImages(entryURL, entryContent) + default: + if strings.Contains(rule, "replace") { + // Format: replace("search-term"|"replace-term") + args := customReplaceRuleRegex.FindStringSubmatch(rule) + if len(args) >= 3 { + entryContent = replaceCustom(entryContent, args[1], args[2]) + } else { + logger.Debug("[Rewrite] Cannot find search and replace terms for replace rule %s", rule) + } + } } } diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index d8d78fd6de0..aebaf9f918c 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -230,3 +230,13 @@ func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) { t.Errorf(`Not expected output: %s`, output) } } + +func TestRewriteReplaceCustom(t *testing.T) { + content := `` + expected := `` + output := Rewriter("https://example.org/artcle", content, `replace("article/(.*).svg"|"article/$1.png")`) + + if expected != output { + t.Errorf(`Not expected output: %s`, output) + } +}