rewrite_functions.go 2.8 KB

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