You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rewrite_functions.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "fmt"
  7. "regexp"
  8. "strings"
  9. "github.com/PuerkitoBio/goquery"
  10. )
  11. var (
  12. youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  13. imgRegex = regexp.MustCompile(`<img [^>]+>`)
  14. textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
  15. )
  16. func addImageTitle(entryURL, entryContent string) string {
  17. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  18. if err != nil {
  19. return entryContent
  20. }
  21. matches := doc.Find("img[src][title]")
  22. if matches.Length() > 0 {
  23. matches.Each(func(i int, img *goquery.Selection) {
  24. altAttr := img.AttrOr("alt", "")
  25. srcAttr, _ := img.Attr("src")
  26. titleAttr, _ := img.Attr("title")
  27. img.ReplaceWithHtml(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + titleAttr + `</p></figcaption></figure>`)
  28. })
  29. output, _ := doc.Find("body").First().Html()
  30. return output
  31. }
  32. return entryContent
  33. }
  34. func addDynamicImage(entryURL, entryContent string) string {
  35. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  36. if err != nil {
  37. return entryContent
  38. }
  39. // Ordered most preferred to least preferred.
  40. candidateAttrs := []string{
  41. "data-src",
  42. "data-original",
  43. "data-orig",
  44. "data-url",
  45. "data-orig-file",
  46. "data-large-file",
  47. "data-medium-file",
  48. "data-2000src",
  49. "data-1000src",
  50. "data-800src",
  51. "data-655src",
  52. "data-500src",
  53. "data-380src",
  54. }
  55. changed := false
  56. doc.Find("img,div").Each(func(i int, img *goquery.Selection) {
  57. for _, candidateAttr := range candidateAttrs {
  58. if srcAttr, found := img.Attr(candidateAttr); found {
  59. changed = true
  60. if img.Is("img") {
  61. img.SetAttr("src", srcAttr)
  62. } else {
  63. altAttr := img.AttrOr("alt", "")
  64. img.ReplaceWithHtml(`<img src="` + srcAttr + `" alt="` + altAttr + `"/>`)
  65. }
  66. break
  67. }
  68. }
  69. })
  70. if !changed {
  71. doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
  72. matches := imgRegex.FindAllString(noscript.Text(), 2)
  73. if len(matches) == 1 {
  74. changed = true
  75. noscript.ReplaceWithHtml(matches[0])
  76. }
  77. })
  78. }
  79. if changed {
  80. output, _ := doc.Find("body").First().Html()
  81. return output
  82. }
  83. return entryContent
  84. }
  85. func addYoutubeVideo(entryURL, entryContent string) string {
  86. matches := youtubeRegex.FindStringSubmatch(entryURL)
  87. if len(matches) == 2 {
  88. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  89. return video + "<p>" + replaceLineFeeds(replaceTextLinks(entryContent)) + "</p>"
  90. }
  91. return entryContent
  92. }
  93. func addPDFLink(entryURL, entryContent string) string {
  94. if strings.HasSuffix(entryURL, ".pdf") {
  95. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  96. }
  97. return entryContent
  98. }
  99. func replaceTextLinks(input string) string {
  100. return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
  101. }
  102. func replaceLineFeeds(input string) string {
  103. return strings.Replace(input, "\n", "<br>", -1)
  104. }