rewrite_functions.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. "regexp"
  7. "strings"
  8. "github.com/PuerkitoBio/goquery"
  9. )
  10. var (
  11. youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  12. )
  13. func addImageTitle(entryURL, entryContent string) string {
  14. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  15. if err != nil {
  16. return entryContent
  17. }
  18. imgTag := doc.Find("img").First()
  19. if titleAttr, found := imgTag.Attr("title"); found {
  20. return entryContent + `<blockquote cite="` + entryURL + `">` + titleAttr + "</blockquote>"
  21. }
  22. return entryContent
  23. }
  24. func addYoutubeVideo(entryURL, entryContent string) string {
  25. matches := youtubeRegex.FindStringSubmatch(entryURL)
  26. if len(matches) == 2 {
  27. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  28. return video + "<p>" + entryContent + "</p>"
  29. }
  30. return entryContent
  31. }