Skip to content

Commit

Permalink
add base URL to output (#3)
Browse files Browse the repository at this point in the history
* add from url link

* fix method name

* add base url to list output
  • Loading branch information
nilic committed Aug 6, 2023
1 parent 511d90e commit e0f74c5
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cmd/hntop/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
ht "html/template"
"net/url"
tt "text/template"
"time"

Expand All @@ -13,14 +14,15 @@ import (
)

const (
fromBaseURL = "https://news.ycombinator.com/from?site="
itemBaseURL = "https://news.ycombinator.com/item?id="
userBaseURL = "https://news.ycombinator.com/user?id="
mailSubject = "[hntop] Top HN posts"

listTemplate = `{{if .FrontPage}}Displaying HN posts currently on the front page{{else}}Displaying top {{.ResultCount}} HN posts from {{.StartTime}} to {{.EndTime}}{{end -}}
{{if .Hits}}
{{range $i, $e := .Hits}}
{{increment $i}}. {{.Title}}
{{increment $i}}. {{.Title}}{{if ne .GetBaseExternalURL ""}} ({{.GetBaseExternalURL}}){{end}}
{{.GetExternalURL}}
{{- if ne .GetItemURL .GetExternalURL}}
{{.GetItemURL}}
Expand All @@ -33,7 +35,7 @@ const (
{{else}}Top {{.ResultCount}} HN posts from {{.StartTime}} to {{.EndTime}}{{end}}<br><br>
{{if .Hits}}
{{range $i, $e := .Hits}}
{{increment $i}}. <a href="{{.GetExternalURL}}">{{.Title}}</a><br>
{{increment $i}}. <a href="{{.GetExternalURL}}">{{.Title}}</a>{{if ne .GetBaseExternalURL ""}} (<a href="{{.GetFromURL}}">{{.GetBaseExternalURL}}</a>){{end}}<br>
{{.Points}} points by <a href="{{.GetUserURL}}">{{.Author}}</a> {{timeAgo .CreatedAt}} | <a href="{{.GetItemURL}}">{{.NumComments}} comments</a><br><br>
{{end}}
{{end}}`
Expand Down Expand Up @@ -146,6 +148,29 @@ func (h Hit) GetUserURL() string {
return userBaseURL + h.Author
}

func (h Hit) GetBaseExternalURL() string {
if h.URL == "" {
return ""
}

u, err := url.Parse(h.URL)
if err != nil {
return ""
}

return u.Hostname()
}

func (h Hit) GetFromURL() string {
b := h.GetBaseExternalURL()

if b == "" {
return ""
}

return fromBaseURL + b
}

func increment(i int) int {
return i + 1
}
Expand Down

0 comments on commit e0f74c5

Please sign in to comment.