Update toot printing
This commit is contained in:
parent
cd7c29c302
commit
8aad570ab4
7
main.go
7
main.go
|
@ -36,9 +36,10 @@ type Config struct {
|
|||
}
|
||||
|
||||
type Account struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"display_name"`
|
||||
Url string `json:"url"`
|
||||
Id string `json:"id"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Name string `json:"username"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type Thread struct {
|
||||
|
|
22
server.go
22
server.go
|
@ -103,7 +103,7 @@ func handleConn(conn *tls.Conn, baseURL, title, home_message string) {
|
|||
|
||||
// home
|
||||
if path == "" || path == "/" {
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# " + title + "\n\n" + home_message + "\n\n=> /tag Search for a tag\n=> /about About MastoGem")
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# %s\n\n%s\n\n=> /tag Search for a tag\n=> /about About MastoGem", title, home_message)
|
||||
if err != nil {
|
||||
log.Println("send error: %s", err)
|
||||
return
|
||||
|
@ -201,25 +201,21 @@ func printProfile(conn *tls.Conn, baseURL, profileID string) {
|
|||
return
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# Toots for " + account.Name + " account\n")
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# Toots for %s account\n", account.Name)
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, blog := range blogs {
|
||||
date := "\n```\n* Posted at " + blog.Date + " *\n```\n"
|
||||
|
||||
text := removeHTMLTags(blog.Content) + "\n"
|
||||
|
||||
_, err = fmt.Fprintf(conn, date + text + "=> /thread/" + blog.Id + " View the thread\n")
|
||||
_, err = fmt.Fprintf(conn, "\n%s\n=> /thread/%s View the thread\n", formatBlog(blog), blog.Id)
|
||||
if err != nil {
|
||||
log.Println("read blogs: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintf(conn, "=> " + account.Url + " Go to " + account.Name + " account")
|
||||
_, err = fmt.Fprintf(conn, "\n=> %s Go to %s account", account.Url, account.Name)
|
||||
if err != nil {
|
||||
log.Println("add link: %s", err)
|
||||
return
|
||||
|
@ -256,7 +252,7 @@ func printThread(conn *tls.Conn, baseURL, tootID string) {
|
|||
|
||||
// Print each anscestor
|
||||
for _, toot := range thread.Ancestors {
|
||||
_, err = fmt.Fprintf(conn, "\n```\n* Posted on " + toot.Date + " by " + toot.Author.Name + " *\n```\n" + removeHTMLTags(toot.Content) + "\n=> /profile/" + toot.Author.Id + " More toots from " + toot.Author.Name + "\n")
|
||||
_, err = fmt.Fprintf(conn, "\n%s\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Author.Id, toot.Author.Name)
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
|
@ -264,7 +260,7 @@ func printThread(conn *tls.Conn, baseURL, tootID string) {
|
|||
}
|
||||
|
||||
// Print original toot
|
||||
_, err = fmt.Fprintf(conn, "\n# Toot\n\n```\n* Posted on " + originalToot.Date + " by "+ originalToot.Author.Name +" *\n```\n" + removeHTMLTags(originalToot.Content) + "\n=> /profile/" + originalToot.Author.Id + " More toots from " + originalToot.Author.Name + "\n")
|
||||
_, err = fmt.Fprintf(conn, "\n# Toot\n\n%s\n=> /profile/%s More toots from %s\n", formatBlog(originalToot), originalToot.Author.Id, originalToot.Author.Name)
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
|
@ -277,7 +273,7 @@ func printThread(conn *tls.Conn, baseURL, tootID string) {
|
|||
return
|
||||
}
|
||||
for _, toot := range thread.Descendants {
|
||||
_, err = fmt.Fprintf(conn, "\n```\n* Posted on " + toot.Date + " by " + toot.Author.Name + " *\n```\n" + removeHTMLTags(toot.Content) + "\n=> /profile/" + toot.Author.Id + " More toots from " + toot.Author.Name + "\n")
|
||||
_, err = fmt.Fprintf(conn, "\n%s\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Author.Id, toot.Author.Name)
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
|
@ -297,7 +293,7 @@ func printTag(conn *tls.Conn, baseURL, tag string) {
|
|||
}
|
||||
|
||||
// Print header
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# Toots for " + tag + "\n")
|
||||
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# Toots for %s\n", tag)
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
|
@ -305,7 +301,7 @@ func printTag(conn *tls.Conn, baseURL, tag string) {
|
|||
|
||||
// Print toots
|
||||
for _, toot := range toots {
|
||||
_, err = fmt.Fprintf(conn, "\n```\n* Posted on " + toot.Date + " by " + toot.Author.Name + " *\n```\n" + removeHTMLTags(toot.Content) + "\n=> /profile/" + toot.Author.Id + " More toots from " + toot.Author.Name + "\n")
|
||||
_, err = fmt.Fprintf(conn, "\n%s\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Author.Id, toot.Author.Name)
|
||||
if err != nil {
|
||||
log.Println("handleConn: %s", err)
|
||||
return
|
||||
|
|
20
util.go
20
util.go
|
@ -81,3 +81,23 @@ func removeHTMLTags(content string) string {
|
|||
|
||||
return text
|
||||
}
|
||||
|
||||
func formatBlog(toot Blog) string {
|
||||
content := toot.Content
|
||||
content = removeHTMLTags(content)
|
||||
content = strings.Trim(content, " \n\r")
|
||||
content = strings.ReplaceAll(content, "\n#", "\n[#]")
|
||||
if strings.HasPrefix(content, "#") {
|
||||
content = "[#]" + content[1:]
|
||||
}
|
||||
|
||||
var author string
|
||||
|
||||
if toot.Author.DisplayName == "" {
|
||||
author = toot.Author.Name
|
||||
} else {
|
||||
author = toot.Author.DisplayName
|
||||
}
|
||||
|
||||
return "## Written by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16] + "\n" + content
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue