2017-11-20 06:10:04 +01:00
|
|
|
// 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.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package rewrite // import "miniflux.app/reader/rewrite"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2020-11-25 23:51:54 +01:00
|
|
|
"regexp"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-12-03 05:51:06 +01:00
|
|
|
"miniflux.app/logger"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/url"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2020-11-25 23:51:54 +01:00
|
|
|
var customReplaceRuleRegex = regexp.MustCompile(`replace\("(.*)"\|"(.*)"\)`)
|
|
|
|
|
2017-12-12 07:16:32 +01:00
|
|
|
// Rewriter modify item contents with a set of rewriting rules.
|
|
|
|
func Rewriter(entryURL, entryContent, customRewriteRules string) string {
|
|
|
|
rulesList := getPredefinedRewriteRules(entryURL)
|
|
|
|
if customRewriteRules != "" {
|
|
|
|
rulesList = customRewriteRules
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-12-12 07:16:32 +01:00
|
|
|
rules := strings.Split(rulesList, ",")
|
2017-12-14 06:30:40 +01:00
|
|
|
rules = append(rules, "add_pdf_download_link")
|
|
|
|
|
2018-12-03 05:51:06 +01:00
|
|
|
logger.Debug(`[Rewrite] Applying rules %v for %q`, rules, entryURL)
|
|
|
|
|
2017-12-12 07:16:32 +01:00
|
|
|
for _, rule := range rules {
|
2020-11-25 23:51:54 +01:00
|
|
|
rule := strings.TrimSpace(rule)
|
|
|
|
switch rule {
|
2017-12-12 07:16:32 +01:00
|
|
|
case "add_image_title":
|
|
|
|
entryContent = addImageTitle(entryURL, entryContent)
|
2019-08-13 17:44:23 +02:00
|
|
|
case "add_mailto_subject":
|
|
|
|
entryContent = addMailtoSubject(entryURL, entryContent)
|
2018-07-09 07:22:48 +02:00
|
|
|
case "add_dynamic_image":
|
|
|
|
entryContent = addDynamicImage(entryURL, entryContent)
|
2017-12-12 07:16:32 +01:00
|
|
|
case "add_youtube_video":
|
|
|
|
entryContent = addYoutubeVideo(entryURL, entryContent)
|
2020-03-21 04:45:37 +01:00
|
|
|
case "add_invidious_video":
|
|
|
|
entryContent = addInvidiousVideo(entryURL, entryContent)
|
|
|
|
case "add_youtube_video_using_invidious_player":
|
|
|
|
entryContent = addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent)
|
2017-12-14 06:30:40 +01:00
|
|
|
case "add_pdf_download_link":
|
|
|
|
entryContent = addPDFLink(entryURL, entryContent)
|
2019-11-29 05:11:39 +01:00
|
|
|
case "nl2br":
|
|
|
|
entryContent = replaceLineFeeds(entryContent)
|
2019-11-29 20:17:14 +01:00
|
|
|
case "convert_text_link", "convert_text_links":
|
2019-11-29 05:11:39 +01:00
|
|
|
entryContent = replaceTextLinks(entryContent)
|
2020-09-30 07:22:25 +02:00
|
|
|
case "fix_medium_images":
|
|
|
|
entryContent = fixMediumImages(entryURL, entryContent)
|
2020-10-20 06:04:14 +02:00
|
|
|
case "use_noscript_figure_images":
|
|
|
|
entryContent = useNoScriptImages(entryURL, entryContent)
|
2020-11-25 23:51:54 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2017-12-12 07:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return entryContent
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 07:16:32 +01:00
|
|
|
func getPredefinedRewriteRules(entryURL string) string {
|
|
|
|
urlDomain := url.Domain(entryURL)
|
|
|
|
for domain, rules := range predefinedRules {
|
|
|
|
if strings.Contains(urlDomain, domain) {
|
|
|
|
return rules
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 07:16:32 +01:00
|
|
|
return ""
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|