rewrite_functions.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. )
  14. func addImageTitle(entryURL, entryContent string) string {
  15. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  16. if err != nil {
  17. return entryContent
  18. }
  19. imgTag := doc.Find("img").First()
  20. if titleAttr, found := imgTag.Attr("title"); found {
  21. return entryContent + `<blockquote cite="` + entryURL + `">` + titleAttr + "</blockquote>"
  22. }
  23. return entryContent
  24. }
  25. func addYoutubeVideo(entryURL, entryContent string) string {
  26. matches := youtubeRegex.FindStringSubmatch(entryURL)
  27. if len(matches) == 2 {
  28. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  29. return video + "<p>" + entryContent + "</p>"
  30. }
  31. return entryContent
  32. }
  33. func addPDFLink(entryURL, entryContent string) string {
  34. if strings.HasSuffix(entryURL, ".pdf") {
  35. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  36. }
  37. return entryContent
  38. }