1
0
Fork 0
miniflux/reader/rewrite/rewriter.go

62 lines
1.8 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05: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 00:51:50 -04:00
package rewrite // import "miniflux.app/reader/rewrite"
2017-11-20 00:10:04 -05:00
import (
"strings"
2018-12-02 23:51:06 -05:00
"miniflux.app/logger"
2018-08-25 00:51:50 -04:00
"miniflux.app/url"
2017-11-20 00:10:04 -05: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 00:10:04 -05:00
rules := strings.Split(rulesList, ",")
2017-12-14 00:30:40 -05:00
rules = append(rules, "add_pdf_download_link")
2018-12-02 23:51:06 -05:00
logger.Debug(`[Rewrite] Applying rules %v for %q`, rules, entryURL)
for _, rule := range rules {
switch strings.TrimSpace(rule) {
case "add_image_title":
entryContent = addImageTitle(entryURL, entryContent)
case "add_mailto_subject":
entryContent = addMailtoSubject(entryURL, entryContent)
case "add_dynamic_image":
entryContent = addDynamicImage(entryURL, entryContent)
case "add_youtube_video":
entryContent = addYoutubeVideo(entryURL, entryContent)
case "add_invidious_video":
entryContent = addInvidiousVideo(entryURL, entryContent)
case "add_youtube_video_using_invidious_player":
entryContent = addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent)
2017-12-14 00:30:40 -05:00
case "add_pdf_download_link":
entryContent = addPDFLink(entryURL, entryContent)
case "nl2br":
entryContent = replaceLineFeeds(entryContent)
case "convert_text_link", "convert_text_links":
entryContent = replaceTextLinks(entryContent)
2017-11-20 00:10:04 -05:00
}
}
return entryContent
2017-11-20 00:10:04 -05: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 00:10:04 -05:00
}
return ""
2017-11-20 00:10:04 -05:00
}