rewriter.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/url"
  11. )
  12. type rule struct {
  13. name string
  14. args []string
  15. }
  16. // Rewriter modify item contents with a set of rewriting rules.
  17. func Rewriter(entryURL, entryContent, customRewriteRules string) string {
  18. rulesList := getPredefinedRewriteRules(entryURL)
  19. if customRewriteRules != "" {
  20. rulesList = customRewriteRules
  21. }
  22. rules := parseRules(rulesList)
  23. rules = append(rules, rule{name: "add_pdf_download_link"})
  24. logger.Debug(`[Rewrite] Applying rules %v for %q`, rules, entryURL)
  25. for _, rule := range rules {
  26. entryContent = applyRule(entryURL, entryContent, rule)
  27. }
  28. return entryContent
  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, entryContent string, rule rule) string {
  49. switch rule.name {
  50. case "add_image_title":
  51. entryContent = addImageTitle(entryURL, entryContent)
  52. case "add_mailto_subject":
  53. entryContent = addMailtoSubject(entryURL, entryContent)
  54. case "add_dynamic_image":
  55. entryContent = addDynamicImage(entryURL, entryContent)
  56. case "add_youtube_video":
  57. entryContent = addYoutubeVideo(entryURL, entryContent)
  58. case "add_invidious_video":
  59. entryContent = addInvidiousVideo(entryURL, entryContent)
  60. case "add_youtube_video_using_invidious_player":
  61. entryContent = addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent)
  62. case "add_youtube_video_from_id":
  63. entryContent = addYoutubeVideoFromId(entryContent)
  64. case "add_pdf_download_link":
  65. entryContent = addPDFLink(entryURL, entryContent)
  66. case "nl2br":
  67. entryContent = replaceLineFeeds(entryContent)
  68. case "convert_text_link", "convert_text_links":
  69. entryContent = replaceTextLinks(entryContent)
  70. case "fix_medium_images":
  71. entryContent = fixMediumImages(entryURL, entryContent)
  72. case "use_noscript_figure_images":
  73. entryContent = useNoScriptImages(entryURL, entryContent)
  74. case "replace":
  75. // Format: replace("search-term"|"replace-term")
  76. if len(rule.args) >= 2 {
  77. entryContent = replaceCustom(entryContent, 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. entryContent = removeCustom(entryContent, rule.args[0])
  85. } else {
  86. logger.Debug("[Rewrite] Cannot find selector for remove rule %s", rule)
  87. }
  88. case "add_castopod_episode":
  89. entryContent = addCastopodEpisode(entryURL, entryContent)
  90. case "base64_decode":
  91. if len(rule.args) >= 1 {
  92. entryContent = applyFuncOnTextContent(entryContent, rule.args[0], decodeBase64Content)
  93. } else {
  94. entryContent = applyFuncOnTextContent(entryContent, "body", decodeBase64Content)
  95. }
  96. case "parse_markdown":
  97. entryContent = parseMarkdown(entryContent)
  98. }
  99. return entryContent
  100. }
  101. func getPredefinedRewriteRules(entryURL string) string {
  102. urlDomain := url.Domain(entryURL)
  103. for domain, rules := range predefinedRules {
  104. if strings.Contains(urlDomain, domain) {
  105. return rules
  106. }
  107. }
  108. return ""
  109. }