miniflux/reader/rewrite/rewriter.go

50 lines
1.2 KiB
Go
Raw Normal View History

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.
package rewrite
import (
"strings"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/url"
2017-11-20 06:10:04 +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
rules := strings.Split(rulesList, ",")
2017-12-14 06:30:40 +01:00
rules = append(rules, "add_pdf_download_link")
for _, rule := range rules {
switch strings.TrimSpace(rule) {
case "add_image_title":
entryContent = addImageTitle(entryURL, entryContent)
case "add_dynamic_image":
entryContent = addDynamicImage(entryURL, entryContent)
case "add_youtube_video":
entryContent = addYoutubeVideo(entryURL, entryContent)
2017-12-14 06:30:40 +01:00
case "add_pdf_download_link":
entryContent = addPDFLink(entryURL, entryContent)
2017-11-20 06:10:04 +01:00
}
}
return entryContent
2017-11-20 06:10:04 +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
}
return ""
2017-11-20 06:10:04 +01:00
}