add sizing to img

This commit is contained in:
zongor 2024-09-15 22:48:02 -04:00
parent 87a01d3df0
commit d55d21eb99
3 changed files with 46 additions and 3 deletions

4
go.mod
View File

@ -10,6 +10,6 @@ require (
github.com/mmcdole/goxpp v1.1.1-0.20240225020742-a0c311522b23 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/text v0.18.0 // indirect
)

4
go.sum
View File

@ -22,10 +22,14 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

41
main.go
View File

@ -4,6 +4,7 @@ import (
"bufio"
"context"
"fmt"
"regexp"
"log"
"os"
"sort"
@ -11,6 +12,7 @@ import (
"time"
"github.com/mmcdole/gofeed"
"golang.org/x/net/html"
)
type RSSInfo struct {
@ -66,10 +68,47 @@ func main() {
sort.Sort(info)
for _, item := range info.Items {
fmt.Printf("<li><a href=\"%s\">%s</a><p>%s</p></li>\n", item.Link, item.Title, item.Description)
description := fixHtml(item.Description)
content := fixHtml(item.Content)
fmt.Printf("<li>\n<h3>\n<a href=\"%s\">\n%s\n</a>\n</h3>\n<h5>\n%s\n</h5>\n<p>\n%s\n</p>\n</li>\n", item.Link, item.Title, description, content)
}
fmt.Println("</ul>")
fmt.Println("</body>")
fmt.Println("</html>")
}
func fixHtml(rawHtml string) string {
content := ""
// Parse the HTML string
doc, err := html.Parse(strings.NewReader(rawHtml))
if err != nil {
content = rawHtml
} else {
// Modify img and video elements to have static height and width
var f func(*html.Node)
f = func(n *html.Node) {
switch n.Data {
case "img", "video", "iframe", "object", "embed", "canvas":
n.Attr = append(n.Attr, html.Attribute{Key: "height", Val: "400px"})
// n.Attr = append(n.Attr, html.Attribute{Key: "width", Val: "100px"})
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
// Convert the modified doc back to a string
buf := &strings.Builder{}
err = html.Render(buf, doc)
if err != nil {
log.Println(err)
} else {
content = buf.String()
}
}
return content
}