Skip to content

Commit

Permalink
refactor: got rid of the createfakecontent method
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jan 3, 2023
1 parent e5a2909 commit b5713e1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 89 deletions.
4 changes: 2 additions & 2 deletions internal/backend/cache/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {
for _, item := range items {
result = append(result, simpleList.NewListItem(
item.Title,
item.Description,
rss.Glamourize(item),
rss.HTMLToText(item.Description),
rss.Markdownize(item),
))
}

Expand Down
82 changes: 8 additions & 74 deletions internal/backend/fake/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package fake

import (
"os"
"strings"
"time"
"path/filepath"

md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/TypicalAM/goread/internal/backend"
simpleList "github.com/TypicalAM/goread/internal/list"
"github.com/TypicalAM/goread/internal/rss"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/mmcdole/gofeed"
Expand Down Expand Up @@ -58,7 +57,8 @@ func (b Backend) FetchFeeds(catName string) tea.Cmd {
// Return some fake articles
func (b Backend) FetchArticles(feedName string) tea.Cmd {
return func() tea.Msg {
file, err := os.Open("rss.rss")
path := filepath.Join("test", "feeds", "test.xml")
file, err := os.Open(path)
if err != nil {
return backend.FetchErrorMessage{
Description: "Could not open file",
Expand All @@ -77,12 +77,11 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {
}

var result []list.Item
for i := range feed.Items {
content := CreateFakeContent(i, feed)
for _, item := range feed.Items {
result = append(result, simpleList.NewListItem(
content.Title,
strings.Join(content.Categories, ", "),
content.MoreContent(),
item.Title,
rss.HTMLToText(item.Description),
rss.Markdownize(*item),
))
}

Expand All @@ -91,71 +90,6 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {
}
}

// Fake backend logic
type Item struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Content string `json:"content,omitempty"`
Links []string `json:"links,omitempty"`
UpdatedParsed *time.Time `json:"updatedParsed,omitempty"`
PublishedParsed *time.Time `json:"publishedParsed,omitempty"`
Authors []*gofeed.Person `json:"authors,omitempty"`
Image *gofeed.Image `json:"image,omitempty"`
Categories []string `json:"categories,omitempty"`
}

// A function to create fake text content
func CreateFakeContent(id int, feed *gofeed.Feed) *Item {
item := &Item{}

item.Title = feed.Items[id].Title
item.Description = feed.Items[id].Description
item.Content = feed.Items[id].Content
item.Links = feed.Items[id].Links
item.UpdatedParsed = feed.Items[id].UpdatedParsed
item.PublishedParsed = feed.Items[id].PublishedParsed
item.Authors = feed.Items[id].Authors
item.Image = feed.Items[id].Image
item.Categories = feed.Items[id].Categories

return item
}

func (i Item) MoreContent() string {
var mdown string

// Add the title
mdown += "# " + i.Title + "\n "

// If there are no authors, then we don't want to add a newline
if i.Authors != nil {
mdown += i.Authors[0].Name + "\n"
}

// Show when the article was published if available
if i.PublishedParsed != nil {
mdown += "\n"
mdown += "Published: " + i.PublishedParsed.Format("2006-01-02 15:04:05")
}

mdown += "\n\n"
mdown += htmlToMarkdown(i.Description)

return mdown
}

// htmlToMarkdown converts html to markdown using the html-to-markdown library
func htmlToMarkdown(content string) string {
converter := md.NewConverter("", true, nil)

markdown, err := converter.ConvertString(content)
if err != nil {
panic(err)
}

return markdown
}

// Close the backend
func (b Backend) Close() error {
return nil
Expand Down
10 changes: 4 additions & 6 deletions internal/backend/web/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package web

import (
"github.com/TypicalAM/goread/internal/backend"
"github.com/TypicalAM/goread/internal/backend/fake"
simpleList "github.com/TypicalAM/goread/internal/list"
"github.com/TypicalAM/goread/internal/rss"
"github.com/charmbracelet/bubbles/list"
Expand Down Expand Up @@ -93,12 +92,11 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {

// Create the list of list items
var result []list.Item
for i := range feed.Items {
content := fake.CreateFakeContent(i, feed)
for _, item := range feed.Items {
result = append(result, simpleList.NewListItem(
content.Title,
content.Description,
content.MoreContent(),
item.Title,
rss.HTMLToText(item.Description),
rss.Markdownize(*item),
))
}

Expand Down
32 changes: 25 additions & 7 deletions internal/rss/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"errors"
"os"
"sort"
"strings"

md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
"github.com/mmcdole/gofeed"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -171,14 +173,14 @@ func (rss Rss) GetFeedURL(feedName string) (string, error) {
return "", ErrNotFound
}

// Glamourize will return a string that can be used to display the rss feeds
func Glamourize(item gofeed.Item) string {
// Markdownize will return a string that can be used to display the rss feeds
func Markdownize(item gofeed.Item) string {
var mdown string

// Add the title
mdown += "# " + item.Title + "\n "

// If there are no authors, then we don't want to add a newline
// If there are no authors, then don't add the author
if item.Authors != nil {
mdown += item.Authors[0].Name + "\n"
}
Expand All @@ -189,16 +191,32 @@ func Glamourize(item gofeed.Item) string {
mdown += "Published: " + item.PublishedParsed.Format("2006-01-02 15:04:05")
}

// Convert the html to markdown
mdown += "\n\n"
mdown += htmlToMarkdown(item.Description)
return mdown
}

// Convert the description html to markdown
// htmlToMarkdown converts html to markdown using the html-to-markdown library
func htmlToMarkdown(content string) string {
converter := md.NewConverter("", true, nil)
descMarkdown, err := converter.ConvertString(item.Description)

markdown, err := converter.ConvertString(content)
if err != nil {
panic(err)
}

mdown += descMarkdown
return markdown
}

return mdown
// HTMLToText converts html to text using the goquery library
func HTMLToText(content string) string {
// Create a new document
doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
if err != nil {
panic(err)
}

// Return the text
return doc.Text()
}
File renamed without changes.

0 comments on commit b5713e1

Please sign in to comment.