Skip to content

Commit

Permalink
refactor html text
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyng committed Feb 2, 2024
1 parent 312addc commit 8b22d1d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 46 deletions.
50 changes: 19 additions & 31 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"time"

"github.com/charmbracelet/lipgloss"
"golang.org/x/net/html"
)

Expand Down Expand Up @@ -46,34 +45,38 @@ func HTMLText(t string) string {
var sb strings.Builder

type state struct {
attributes []html.Attribute
element string
attributes map[string]string
}

var fn func(*state, *html.Node)
fn = func(s *state, n *html.Node) {
var fn func(state, *html.Node)
fn = func(s state, n *html.Node) {
switch n.Type {
case html.TextNode:
text := n.Data
for _, attribute := range s.attributes {
switch attribute.Key {
case "href":
text = fmt.Sprintf("(%s %s)", n.Data, attribute.Val)
if n.Data == attribute.Val {
text = attribute.Val
} else if trim := strings.TrimSuffix(n.Data, "..."); strings.HasPrefix(attribute.Val, trim) {
switch s.element {
case "a":
if val, ok := s.attributes["href"]; ok {
text = fmt.Sprintf("(%s %s)", n.Data, val)
if n.Data == val {
text = val
} else if trim := strings.TrimSuffix(n.Data, "..."); strings.HasPrefix(val, trim) {
// HN truncates long links and appends "..."
text = attribute.Val
text = val
}

continue
}
}

sb.WriteString(text)
case html.ElementNode:
switch n.Data {
case "a":
s.attributes = append(s.attributes, n.Attr...)
s.element = n.Data
s.attributes = make(map[string]string)
for _, attr := range n.Attr {
// discard Namespace
s.attributes[attr.Key] = attr.Val
}
case "p":
sb.WriteString("\n\n")
}
Expand All @@ -82,11 +85,9 @@ func HTMLText(t string) string {
for child := n.FirstChild; child != nil; child = child.NextSibling {
fn(s, child)
}

s.attributes = nil
}

fn(&state{}, root)
fn(state{}, root)
return sb.String()
}

Expand Down Expand Up @@ -155,16 +156,3 @@ func NewComment(rank int) *Comment {
},
}
}

func (c Comment) Title() string {
style := lipgloss.NewStyle().Foreground(lipgloss.Color("#ff6600"))
return fmt.Sprintf(
"%s %s",
style.Render(c.By),
style.Copy().Faint(true).Render(humanize(time.Unix(c.Time, 0))),
)
}

func (c Comment) Description() string {
return strings.TrimSpace(HTMLText(c.Text))
}
37 changes: 22 additions & 15 deletions pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sort"
"strings"
"time"

"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/viewport"
Expand Down Expand Up @@ -50,9 +51,9 @@ type PaneView struct {

content strings.Builder

styleTitle lipgloss.Style
styleDescription lipgloss.Style
styleComment lipgloss.Style
styleTitle lipgloss.Style
styleDescription lipgloss.Style
styleCommentTitle lipgloss.Style
}

func NewPaneView() *PaneView {
Expand All @@ -63,12 +64,7 @@ func NewPaneView() *PaneView {
Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"}),
styleDescription: lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#a49fa5", Dark: "#777777"}),
styleComment: lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"}).
Border(lipgloss.NormalBorder(), false).
BorderLeft(true).
PaddingLeft(1).
MarginTop(1),
styleCommentTitle: lipgloss.NewStyle().Foreground(lipgloss.Color("#ff6600")),
}
}

Expand Down Expand Up @@ -141,20 +137,31 @@ func (p *PaneView) Render() {
if s.URL != "" {
fmt.Fprintln(&p.content, p.styleDescription.Copy().Underline(true).Italic(true).Render(s.URL))
} else if s.Text != "" {
fmt.Fprintln(&p.content)
fmt.Fprintln(&p.content, p.styleDescription.Copy().Width(p.style.GetWidth()).Render(HTMLText(s.Text)))
fmt.Fprintln(&p.content, p.styleDescription.Copy().MarginTop(1).Width(p.style.GetWidth()).Render(HTMLText(s.Text)))
}

h, _ := p.styleComment.GetFrameSize()
styleComment := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"}).
Border(lipgloss.NormalBorder(), false).
BorderLeft(true).
PaddingLeft(1).
MarginTop(1)

h, _ := styleComment.GetFrameSize()

var view func(lipgloss.Style, []*Comment) string
view = func(style lipgloss.Style, comments []*Comment) string {
var lines []string
for _, comment := range comments {
if comment.By != "" {
var sb strings.Builder
fmt.Fprintln(&sb, comment.Title())
fmt.Fprint(&sb, comment.Description())
fmt.Fprintln(
&sb,
p.styleCommentTitle.Render(comment.By),
p.styleCommentTitle.Copy().Faint(true).Render(humanize(time.Unix(comment.Time, 0))),
)

sb.WriteString(HTMLText(comment.Text))

if len(comment.Comments) > 0 {
fmt.Fprintln(&sb)
Expand All @@ -168,7 +175,7 @@ func (p *PaneView) Render() {
return strings.Join(lines, "\n")
}

fmt.Fprintln(&p.content, view(p.styleComment.Copy().Width(p.style.GetWidth()-h), s.Comments))
fmt.Fprintln(&p.content, view(styleComment.Copy().Width(p.style.GetWidth()-h), s.Comments))
}

p.viewport.SetContent(p.content.String())
Expand Down

0 comments on commit 8b22d1d

Please sign in to comment.