Skip to content

Commit

Permalink
hackernews: custom html parser
Browse files Browse the repository at this point in the history
closes #11
  • Loading branch information
azimut committed May 5, 2023
1 parent f80f239 commit 222da73
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions internal/hackernews/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package hackernews

import (
"fmt"
"html"
"net/url"
"regexp"
"strings"

text "github.com/MichaelMure/go-term-text"
"github.com/azimut/cli-view/internal/format"
"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
"github.com/jaytaylor/html2text"
)

func (thread Thread) String() (ret string) {
Expand Down Expand Up @@ -65,15 +67,32 @@ func (c Comment) String() (ret string) {
return
}

func fixupComment(html string, leftPad int, width int) string {
plainText, err := html2text.FromString(
html,
html2text.Options{OmitLinks: false, PrettyTables: true, CitationStyleLinks: true},
var reAnchor = regexp.MustCompile(`<a href="([^"]+)"( rel="nofollow")?>([^<]+)</a>`)
var reItalic = regexp.MustCompile(`<i>([^<]+)</i>`)

var italicStyle = lipgloss.NewStyle().Italic(true)

func fixupComment(htmlText string, leftPad int, width int) string {
plainText := strings.ReplaceAll(htmlText, "<p>", "\n\n")
plainText = strings.TrimSpace(plainText)
plainText = reItalic.ReplaceAllStringFunc(plainText, func(s string) string {
return italicStyle.Render(s[3 : len(s)-4])
})
plainText = reAnchor.ReplaceAllStringFunc(plainText, func(s string) string {
matches := reAnchor.FindAllStringSubmatch(s, -1)
href := matches[0][1]
desc := strings.TrimSuffix(matches[0][3], "...")
if strings.HasPrefix(href, desc) {
return href
} else {
return fmt.Sprintf("[%s](%s)", desc, href)
}
})
wrapped, _ := text.WrapLeftPadded(
format.GreenTextIt(html.UnescapeString(plainText)),
width,
leftPad,
)
if err != nil {
panic(err)
}
wrapped, _ := text.WrapLeftPadded(format.GreenTextIt(plainText), width, leftPad)
return wrapped
}

Expand Down

0 comments on commit 222da73

Please sign in to comment.