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"
"errors"
"fmt"
"net/url"
"path/filepath"
"git.sr.ht/~thrrgilag/woodstock"
"git.wntrmute.dev/kyle/goutils/log"
"github.com/anaskhan96/soup"
)
func nomadLink(url Source) string {
return fmt.Sprintf("[nomad.wntrmute.net](%s)", url.ID())
func nomadLink(src Source) string {
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 {
@ -22,6 +30,23 @@ func selector(selectors ...string) sel {
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) {
result := root
for _, selector := range selectors {
@ -65,10 +90,15 @@ func (p *Post) Fetch() error {
p.Body = body + " " + p.Body
}
imageURL, hasImageURL := find(
root, "src",
selector("div", "class", "entry-image"),
selector("img"))
var imageURL string
var hasImageURL bool
for _, finder := range imageFinders {
imageURL, hasImageURL = finder(root)
if hasImageURL {
break
}
}
if hasImageURL {
p.Image = NewURLSource(imageURL)