Update link posts to support !nomad entries.

This commit is contained in:
Kyle Isom 2023-08-29 14:14:40 -07:00
parent baf36dbaf8
commit 5e0a725deb
1 changed files with 36 additions and 6 deletions

View File

@ -4,14 +4,22 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"net/url"
"path/filepath" "path/filepath"
"git.sr.ht/~thrrgilag/woodstock" "git.sr.ht/~thrrgilag/woodstock"
"git.wntrmute.dev/kyle/goutils/log"
"github.com/anaskhan96/soup" "github.com/anaskhan96/soup"
) )
func nomadLink(url Source) string { func nomadLink(src Source) string {
return fmt.Sprintf("[nomad.wntrmute.net](%s)", url.ID()) u, err := url.Parse(src.ID())
if err != nil {
log.Errf("nomadLink: %s", err)
return fmt.Sprintf("[%s](%s)", src.ID(), src.ID())
}
return fmt.Sprintf("[%s](%s)", u.Host, src.ID())
} }
type sel struct { type sel struct {
@ -22,6 +30,23 @@ func selector(selectors ...string) sel {
return sel{selectors: selectors} return sel{selectors: selectors}
} }
type imageFinder func(soup.Root) (string, bool)
var imageFinders = []imageFinder{
func(root soup.Root) (string, bool) {
return find(
root, "src",
selector("div", "class", "entry-image"),
selector("img"))
},
func(root soup.Root) (string, bool) {
return find(
root, "src",
selector("div", "class", "gh-inner"),
selector("img"))
},
}
func find(root soup.Root, attr string, selectors ...sel) (string, bool) { func find(root soup.Root, attr string, selectors ...sel) (string, bool) {
result := root result := root
for _, selector := range selectors { for _, selector := range selectors {
@ -65,10 +90,15 @@ func (p *Post) Fetch() error {
p.Body = body + " " + p.Body p.Body = body + " " + p.Body
} }
imageURL, hasImageURL := find( var imageURL string
root, "src", var hasImageURL bool
selector("div", "class", "entry-image"),
selector("img")) for _, finder := range imageFinders {
imageURL, hasImageURL = finder(root)
if hasImageURL {
break
}
}
if hasImageURL { if hasImageURL {
p.Image = NewURLSource(imageURL) p.Image = NewURLSource(imageURL)