rewrite_functions.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. "encoding/base64"
  7. "fmt"
  8. "html"
  9. "net/url"
  10. "regexp"
  11. "strings"
  12. "miniflux.app/config"
  13. "github.com/PuerkitoBio/goquery"
  14. )
  15. var (
  16. youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  17. youtubeIdRegex = regexp.MustCompile(`youtube_id"?\s*[:=]\s*"([a-zA-Z0-9_-]{11})"`)
  18. invidioRegex = regexp.MustCompile(`https?:\/\/(.*)\/watch\?v=(.*)`)
  19. imgRegex = regexp.MustCompile(`<img [^>]+>`)
  20. textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
  21. )
  22. func addImageTitle(entryURL, entryContent string) string {
  23. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  24. if err != nil {
  25. return entryContent
  26. }
  27. matches := doc.Find("img[src][title]")
  28. if matches.Length() > 0 {
  29. matches.Each(func(i int, img *goquery.Selection) {
  30. altAttr := img.AttrOr("alt", "")
  31. srcAttr, _ := img.Attr("src")
  32. titleAttr, _ := img.Attr("title")
  33. img.ReplaceWithHtml(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + html.EscapeString(titleAttr) + `</p></figcaption></figure>`)
  34. })
  35. output, _ := doc.Find("body").First().Html()
  36. return output
  37. }
  38. return entryContent
  39. }
  40. func addMailtoSubject(entryURL, entryContent string) string {
  41. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  42. if err != nil {
  43. return entryContent
  44. }
  45. matches := doc.Find(`a[href^="mailto:"]`)
  46. if matches.Length() > 0 {
  47. matches.Each(func(i int, a *goquery.Selection) {
  48. hrefAttr, _ := a.Attr("href")
  49. mailto, err := url.Parse(hrefAttr)
  50. if err != nil {
  51. return
  52. }
  53. subject := mailto.Query().Get("subject")
  54. if subject == "" {
  55. return
  56. }
  57. a.AppendHtml(" [" + html.EscapeString(subject) + "]")
  58. })
  59. output, _ := doc.Find("body").First().Html()
  60. return output
  61. }
  62. return entryContent
  63. }
  64. func addDynamicImage(entryURL, entryContent string) string {
  65. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  66. if err != nil {
  67. return entryContent
  68. }
  69. // Ordered most preferred to least preferred.
  70. candidateAttrs := []string{
  71. "data-src",
  72. "data-original",
  73. "data-orig",
  74. "data-url",
  75. "data-orig-file",
  76. "data-large-file",
  77. "data-medium-file",
  78. "data-2000src",
  79. "data-1000src",
  80. "data-800src",
  81. "data-655src",
  82. "data-500src",
  83. "data-380src",
  84. }
  85. candidateSrcsetAttrs := []string{
  86. "data-srcset",
  87. }
  88. changed := false
  89. doc.Find("img,div").Each(func(i int, img *goquery.Selection) {
  90. // Src-linked candidates
  91. for _, candidateAttr := range candidateAttrs {
  92. if srcAttr, found := img.Attr(candidateAttr); found {
  93. changed = true
  94. if img.Is("img") {
  95. img.SetAttr("src", srcAttr)
  96. } else {
  97. altAttr := img.AttrOr("alt", "")
  98. img.ReplaceWithHtml(`<img src="` + srcAttr + `" alt="` + altAttr + `"/>`)
  99. }
  100. break
  101. }
  102. }
  103. // Srcset-linked candidates
  104. for _, candidateAttr := range candidateSrcsetAttrs {
  105. if srcAttr, found := img.Attr(candidateAttr); found {
  106. changed = true
  107. if img.Is("img") {
  108. img.SetAttr("srcset", srcAttr)
  109. } else {
  110. altAttr := img.AttrOr("alt", "")
  111. img.ReplaceWithHtml(`<img srcset="` + srcAttr + `" alt="` + altAttr + `"/>`)
  112. }
  113. break
  114. }
  115. }
  116. })
  117. if !changed {
  118. doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
  119. matches := imgRegex.FindAllString(noscript.Text(), 2)
  120. if len(matches) == 1 {
  121. changed = true
  122. noscript.ReplaceWithHtml(matches[0])
  123. }
  124. })
  125. }
  126. if changed {
  127. output, _ := doc.Find("body").First().Html()
  128. return output
  129. }
  130. return entryContent
  131. }
  132. func fixMediumImages(entryURL, entryContent string) string {
  133. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  134. if err != nil {
  135. return entryContent
  136. }
  137. doc.Find("figure.paragraph-image").Each(func(i int, paragraphImage *goquery.Selection) {
  138. noscriptElement := paragraphImage.Find("noscript")
  139. if noscriptElement.Length() > 0 {
  140. paragraphImage.ReplaceWithHtml(noscriptElement.Text())
  141. }
  142. })
  143. output, _ := doc.Find("body").First().Html()
  144. return output
  145. }
  146. func useNoScriptImages(entryURL, entryContent string) string {
  147. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  148. if err != nil {
  149. return entryContent
  150. }
  151. doc.Find("figure").Each(func(i int, figureElement *goquery.Selection) {
  152. imgElement := figureElement.Find("img")
  153. if imgElement.Length() > 0 {
  154. noscriptElement := figureElement.Find("noscript")
  155. if noscriptElement.Length() > 0 {
  156. figureElement.PrependHtml(noscriptElement.Text())
  157. imgElement.Remove()
  158. noscriptElement.Remove()
  159. }
  160. }
  161. })
  162. output, _ := doc.Find("body").First().Html()
  163. return output
  164. }
  165. func addYoutubeVideo(entryURL, entryContent string) string {
  166. matches := youtubeRegex.FindStringSubmatch(entryURL)
  167. if len(matches) == 2 {
  168. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  169. return video + `<br>` + entryContent
  170. }
  171. return entryContent
  172. }
  173. func addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent string) string {
  174. matches := youtubeRegex.FindStringSubmatch(entryURL)
  175. if len(matches) == 2 {
  176. video := `<iframe width="650" height="350" frameborder="0" src="https://` + config.Opts.InvidiousInstance() + `/embed/` + matches[1] + `" allowfullscreen></iframe>`
  177. return video + `<br>` + entryContent
  178. }
  179. return entryContent
  180. }
  181. func addYoutubeVideoFromId(entryContent string) string {
  182. matches := youtubeIdRegex.FindAllStringSubmatch(entryContent, -1)
  183. if matches == nil {
  184. return entryContent
  185. }
  186. sb := strings.Builder{}
  187. for _, match := range matches {
  188. if len(match) == 2 {
  189. sb.WriteString(`<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/`)
  190. sb.WriteString(match[1])
  191. sb.WriteString(`" allowfullscreen></iframe><br>`)
  192. }
  193. }
  194. sb.WriteString(entryContent)
  195. return sb.String()
  196. }
  197. func addInvidiousVideo(entryURL, entryContent string) string {
  198. matches := invidioRegex.FindStringSubmatch(entryURL)
  199. if len(matches) == 3 {
  200. video := `<iframe width="650" height="350" frameborder="0" src="https://` + matches[1] + `/embed/` + matches[2] + `" allowfullscreen></iframe>`
  201. return video + `<br>` + entryContent
  202. }
  203. return entryContent
  204. }
  205. func addPDFLink(entryURL, entryContent string) string {
  206. if strings.HasSuffix(entryURL, ".pdf") {
  207. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  208. }
  209. return entryContent
  210. }
  211. func replaceTextLinks(input string) string {
  212. return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
  213. }
  214. func replaceLineFeeds(input string) string {
  215. return strings.Replace(input, "\n", "<br>", -1)
  216. }
  217. func replaceCustom(entryContent string, searchTerm string, replaceTerm string) string {
  218. re, err := regexp.Compile(searchTerm)
  219. if err == nil {
  220. return re.ReplaceAllString(entryContent, replaceTerm)
  221. }
  222. return entryContent
  223. }
  224. func removeCustom(entryContent string, selector string) string {
  225. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  226. if err != nil {
  227. return entryContent
  228. }
  229. doc.Find(selector).Remove()
  230. output, _ := doc.Find("body").First().Html()
  231. return output
  232. }
  233. func addCastopodEpisode(entryURL, entryContent string) string {
  234. player := `<iframe width="650" frameborder="0" src="` + entryURL + `/embed/light"></iframe>`
  235. return player + `<br>` + entryContent
  236. }
  237. func applyFuncOnTextContent(entryContent string, selector string, repl func(string) string) string {
  238. var treatChildren func(i int, s *goquery.Selection)
  239. treatChildren = func(i int, s *goquery.Selection) {
  240. if s.Nodes[0].Type == 1 {
  241. s.ReplaceWithHtml(repl(s.Nodes[0].Data))
  242. } else {
  243. s.Contents().Each(treatChildren)
  244. }
  245. }
  246. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  247. if err != nil {
  248. return entryContent
  249. }
  250. doc.Find(selector).Each(treatChildren)
  251. output, _ := doc.Find("body").First().Html()
  252. return output
  253. }
  254. func decodeBase64Content(entryContent string) string {
  255. if ret, err := base64.StdEncoding.DecodeString(strings.TrimSpace(entryContent)); err != nil {
  256. return entryContent
  257. } else {
  258. return html.EscapeString(string(ret))
  259. }
  260. }