Rework fixers to decide what to use and move dehtmlify process in them.
Added githubv fixer.
This commit is contained in:
+7
-4
@@ -1,17 +1,20 @@
|
||||
package fixers
|
||||
|
||||
type fixerFunc func(title, body string) (string, string)
|
||||
import "github.com/mmcdole/gofeed"
|
||||
|
||||
type fixerFunc func(feedItem *gofeed.Item) (string, string)
|
||||
|
||||
var fixers = map[string]fixerFunc{
|
||||
"github.com": githubFixer,
|
||||
"www.opennet.ru": opennetFixer,
|
||||
}
|
||||
|
||||
// Fix fixes title and body depending on site parsed.
|
||||
func Fix(domain, title, body string) (string, string) {
|
||||
func Fix(domain string, feedItem *gofeed.Item) (string, string) {
|
||||
fixer, found := fixers[domain]
|
||||
if !found {
|
||||
return title, body
|
||||
return feedItem.Title, feedItem.Description
|
||||
}
|
||||
|
||||
return fixer(title, body)
|
||||
return fixer(feedItem)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package fixers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"github.com/prodigeris/html2text"
|
||||
)
|
||||
|
||||
func githubFixer(item *gofeed.Item) (string, string) {
|
||||
plainTextBody, err := html2text.FromString(item.Content, html2text.Options{
|
||||
PrettyTables: true,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("Failed to de-HTMLify github body!", "error", err.Error())
|
||||
|
||||
return item.Title, item.Content
|
||||
}
|
||||
|
||||
return item.Title, plainTextBody
|
||||
}
|
||||
+19
-4
@@ -1,11 +1,26 @@
|
||||
package fixers
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"github.com/prodigeris/html2text"
|
||||
)
|
||||
|
||||
func opennetFixer(item *gofeed.Item) (string, string) {
|
||||
oldBody, err := html2text.FromString(item.Description, html2text.Options{
|
||||
PrettyTables: true,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("Failed to de-HTMLify opennet body!", "error", err.Error())
|
||||
|
||||
return item.Title, item.Description
|
||||
}
|
||||
|
||||
func opennetFixer(title, body string) (string, string) {
|
||||
newBody := &strings.Builder{}
|
||||
|
||||
for line := range strings.Lines(body) {
|
||||
for line := range strings.Lines(oldBody) {
|
||||
if strings.HasPrefix(line, "Источник") {
|
||||
continue
|
||||
}
|
||||
@@ -13,5 +28,5 @@ func opennetFixer(title, body string) (string, string) {
|
||||
_, _ = newBody.WriteString(line)
|
||||
}
|
||||
|
||||
return title, newBody.String()
|
||||
return item.Title, newBody.String()
|
||||
}
|
||||
|
||||
+1
-9
@@ -7,7 +7,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"github.com/prodigeris/html2text"
|
||||
"go.dev.pztrn.name/rsser/fixers"
|
||||
)
|
||||
|
||||
@@ -61,14 +60,7 @@ func (r *RSS) Parse() {
|
||||
continue
|
||||
}
|
||||
|
||||
description, err := html2text.FromString(item.Description, html2text.Options{
|
||||
PrettyTables: true,
|
||||
})
|
||||
if err != nil {
|
||||
description = item.Description
|
||||
}
|
||||
|
||||
title, fixedDescription := fixers.Fix(feedURL.Host, item.Title, description)
|
||||
title, fixedDescription := fixers.Fix(feedURL.Host, item)
|
||||
|
||||
for _, output := range r.outputs {
|
||||
if feed.Output == output.Name() {
|
||||
|
||||
Reference in New Issue
Block a user