mirror of
https://github.com/miniflux/v2.git
synced 2025-07-22 17:18:37 +00:00
Add rewrite rule to remove dom elements
This commit is contained in:
parent
9fbcfc213b
commit
93596c1218
3 changed files with 112 additions and 38 deletions
|
@ -5,14 +5,18 @@
|
|||
package rewrite // import "miniflux.app/reader/rewrite"
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/scanner"
|
||||
|
||||
"miniflux.app/logger"
|
||||
"miniflux.app/url"
|
||||
)
|
||||
|
||||
var customReplaceRuleRegex = regexp.MustCompile(`replace\("(.*)"\|"(.*)"\)`)
|
||||
type rule struct {
|
||||
name string
|
||||
args []string
|
||||
}
|
||||
|
||||
// Rewriter modify item contents with a set of rewriting rules.
|
||||
func Rewriter(entryURL, entryContent, customRewriteRules string) string {
|
||||
|
@ -21,46 +25,78 @@ func Rewriter(entryURL, entryContent, customRewriteRules string) string {
|
|||
rulesList = customRewriteRules
|
||||
}
|
||||
|
||||
rules := strings.Split(rulesList, ",")
|
||||
rules = append(rules, "add_pdf_download_link")
|
||||
rules := parseRules(rulesList)
|
||||
rules = append(rules, rule{name: "add_pdf_download_link"})
|
||||
|
||||
logger.Debug(`[Rewrite] Applying rules %v for %q`, rules, entryURL)
|
||||
|
||||
for _, rule := range rules {
|
||||
rule := strings.TrimSpace(rule)
|
||||
switch 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)
|
||||
case "add_pdf_download_link":
|
||||
entryContent = addPDFLink(entryURL, entryContent)
|
||||
case "nl2br":
|
||||
entryContent = replaceLineFeeds(entryContent)
|
||||
case "convert_text_link", "convert_text_links":
|
||||
entryContent = replaceTextLinks(entryContent)
|
||||
case "fix_medium_images":
|
||||
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)
|
||||
}
|
||||
entryContent = applyRule(entryURL, entryContent, rule)
|
||||
}
|
||||
|
||||
return entryContent
|
||||
}
|
||||
|
||||
func parseRules(rulesText string) (rules []rule) {
|
||||
scan := scanner.Scanner{Mode: scanner.ScanIdents | scanner.ScanStrings}
|
||||
scan.Init(strings.NewReader(rulesText))
|
||||
|
||||
for {
|
||||
switch scan.Scan() {
|
||||
case scanner.Ident:
|
||||
rules = append(rules, rule{name: scan.TokenText()})
|
||||
|
||||
case scanner.String:
|
||||
if l := len(rules) - 1; l >= 0 {
|
||||
text := scan.TokenText()
|
||||
text, _ = strconv.Unquote(text)
|
||||
|
||||
rules[l].args = append(rules[l].args, text)
|
||||
}
|
||||
|
||||
case scanner.EOF:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func applyRule(entryURL, entryContent string, rule rule) string {
|
||||
switch rule.name {
|
||||
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)
|
||||
case "add_pdf_download_link":
|
||||
entryContent = addPDFLink(entryURL, entryContent)
|
||||
case "nl2br":
|
||||
entryContent = replaceLineFeeds(entryContent)
|
||||
case "convert_text_link", "convert_text_links":
|
||||
entryContent = replaceTextLinks(entryContent)
|
||||
case "fix_medium_images":
|
||||
entryContent = fixMediumImages(entryURL, entryContent)
|
||||
case "use_noscript_figure_images":
|
||||
entryContent = useNoScriptImages(entryURL, entryContent)
|
||||
case "replace":
|
||||
// Format: replace("search-term"|"replace-term")
|
||||
if len(rule.args) >= 2 {
|
||||
entryContent = replaceCustom(entryContent, rule.args[0], rule.args[1])
|
||||
} else {
|
||||
logger.Debug("[Rewrite] Cannot find search and replace terms for replace rule %s", rule)
|
||||
}
|
||||
case "remove":
|
||||
// Format: remove("#selector > .element, .another")
|
||||
if len(rule.args) >= 1 {
|
||||
entryContent = removeCustom(entryContent, rule.args[0])
|
||||
} else {
|
||||
logger.Debug("[Rewrite] Cannot find selector for remove rule %s", rule)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue