rewriter.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package rewrite // import "miniflux.app/reader/rewrite"
  5. import (
  6. "strconv"
  7. "strings"
  8. "text/scanner"
  9. "miniflux.app/logger"
  10. "miniflux.app/model"
  11. "miniflux.app/url"
  12. )
  13. type rule struct {
  14. name string
  15. args []string
  16. }
  17. // Rewriter modify item contents with a set of rewriting rules.
  18. func Rewriter(entryURL string, entry *model.Entry, customRewriteRules string) {
  19. rulesList := getPredefinedRewriteRules(entryURL)
  20. if customRewriteRules != "" {
  21. rulesList = customRewriteRules
  22. }
  23. rules := parseRules(rulesList)
  24. rules = append(rules, rule{name: "add_pdf_download_link"})
  25. logger.Debug(`[Rewrite] Applying rules %v for %q`, rules, entryURL)
  26. for _, rule := range rules {
  27. applyRule(entryURL, entry, rule)
  28. }
  29. }
  30. func parseRules(rulesText string) (rules []rule) {
  31. scan := scanner.Scanner{Mode: scanner.ScanIdents | scanner.ScanStrings}
  32. scan.Init(strings.NewReader(rulesText))
  33. for {
  34. switch scan.Scan() {
  35. case scanner.Ident:
  36. rules = append(rules, rule{name: scan.TokenText()})
  37. case scanner.String:
  38. if l := len(rules) - 1; l >= 0 {
  39. text := scan.TokenText()
  40. text, _ = strconv.Unquote(text)
  41. rules[l].args = append(rules[l].args, text)
  42. }
  43. case scanner.EOF:
  44. return
  45. }
  46. }
  47. }
  48. func applyRule(entryURL string, entry *model.Entry, rule rule) {
  49. switch rule.name {
  50. case "add_image_title":
  51. entry.Content = addImageTitle(entryURL, entry.Content)
  52. case "add_mailto_subject":
  53. entry.Content = addMailtoSubject(entryURL, entry.Content)
  54. case "add_dynamic_image":
  55. entry.Content = addDynamicImage(entryURL, entry.Content)
  56. case "add_youtube_video":
  57. entry.Content = addYoutubeVideo(entryURL, entry.Content)
  58. case "add_invidious_video":
  59. entry.Content = addInvidiousVideo(entryURL, entry.Content)
  60. case "add_youtube_video_using_invidious_player":
  61. entry.Content = addYoutubeVideoUsingInvidiousPlayer(entryURL, entry.Content)
  62. case "add_youtube_video_from_id":
  63. entry.Content = addYoutubeVideoFromId(entry.Content)
  64. case "add_pdf_download_link":
  65. entry.Content = addPDFLink(entryURL, entry.Content)
  66. case "nl2br":
  67. entry.Content = replaceLineFeeds(entry.Content)
  68. case "convert_text_link", "convert_text_links":
  69. entry.Content = replaceTextLinks(entry.Content)
  70. case "fix_medium_images":
  71. entry.Content = fixMediumImages(entryURL, entry.Content)
  72. case "use_noscript_figure_images":
  73. entry.Content = useNoScriptImages(entryURL, entry.Content)
  74. case "replace":
  75. // Format: replace("search-term"|"replace-term")
  76. if len(rule.args) >= 2 {
  77. entry.Content = replaceCustom(entry.Content, rule.args[0], rule.args[1])
  78. } else {
  79. logger.Debug("[Rewrite] Cannot find search and replace terms for replace rule %s", rule)
  80. }
  81. case "remove":
  82. // Format: remove("#selector > .element, .another")
  83. if len(rule.args) >= 1 {
  84. entry.Content = removeCustom(entry.Content, rule.args[0])
  85. } else {
  86. logger.Debug("[Rewrite] Cannot find selector for remove rule %s", rule)
  87. }
  88. case "add_castopod_episode":
  89. entry.Content = addCastopodEpisode(entryURL, entry.Content)
  90. case "base64_decode":
  91. if len(rule.args) >= 1 {
  92. entry.Content = applyFuncOnTextContent(entry.Content, rule.args[0], decodeBase64Content)
  93. } else {
  94. entry.Content = applyFuncOnTextContent(entry.Content, "body", decodeBase64Content)
  95. }
  96. case "parse_markdown":
  97. entry.Content = parseMarkdown(entry.Content)
  98. case "remove_tables":
  99. entry.Content = removeTables(entry.Content)
  100. case "remove_clickbait":
  101. entry.Title = removeClickbait(entry.Title)
  102. }
  103. }
  104. func getPredefinedRewriteRules(entryURL string) string {
  105. urlDomain := url.Domain(entryURL)
  106. for domain, rules := range predefinedRules {
  107. if strings.Contains(urlDomain, domain) {
  108. return rules
  109. }
  110. }
  111. return ""
  112. }