mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
First commit
This commit is contained in:
commit
8ffb773f43
2121 changed files with 1118910 additions and 0 deletions
47
reader/rewrite/rewriter.go
Normal file
47
reader/rewrite/rewriter.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
||||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package rewrite
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
var rewriteRules = []func(string, string) string{
|
||||
func(url, content string) string {
|
||||
re := regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
|
||||
matches := re.FindStringSubmatch(url)
|
||||
|
||||
if len(matches) == 2 {
|
||||
video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
|
||||
return video + "<p>" + content + "</p>"
|
||||
}
|
||||
return content
|
||||
},
|
||||
func(url, content string) string {
|
||||
if strings.HasPrefix(url, "https://xkcd.com") {
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
|
||||
if err != nil {
|
||||
return content
|
||||
}
|
||||
|
||||
imgTag := doc.Find("img").First()
|
||||
if titleAttr, found := imgTag.Attr("title"); found {
|
||||
return content + `<blockquote cite="` + url + `">` + titleAttr + "</blockquote>"
|
||||
}
|
||||
}
|
||||
return content
|
||||
},
|
||||
}
|
||||
|
||||
func Rewriter(url, content string) string {
|
||||
for _, rewriteRule := range rewriteRules {
|
||||
content = rewriteRule(url, content)
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue