Updates to threading, structs, and template

* Extend Mastodon structs
* Make threading optional
* Include tags in default template
* Add function to get all tags
* Update spacing in template
* Update default template front-matter
This commit is contained in:
Gabriel Garrido 2024-05-12 11:46:31 +02:00
parent 56e217ee2a
commit 5af659241f
4 changed files with 98 additions and 24 deletions

View file

@ -19,24 +19,69 @@ type Client struct {
type Account struct {
Id string `json:"id"`
Username string `json:"username"`
Acct string `json:"acct"`
DisplayName string `json:"display_name"`
Locked bool `json:"locked"`
Bot bool `json:"bot"`
Discoverable bool `json:"discoverable"`
Group bool `json:"group"`
CreatedAt time.Time `json:"created_at"`
Note string `json:"note"`
URL string `json:"url"`
URI string `json:"uri"`
Avatar string `json:"avatar"`
AvatarStatic string `json:"avatar_static"`
Header string `json:"header"`
HeaderStatic string `json:"header_static"`
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
StatusesCount int `json:"statuses_count"`
LastStatusAt string `json:"last_status_at"`
}
type MediaAttachment struct {
Type string `json:"type"`
Url string `json:"url"`
URL string `json:"url"`
Description string `json:"description"`
Id string `json:"id"`
Path string
}
type Application struct {
Name string `json:"name"`
Website string `json:"website"`
}
type Tag struct {
Name string `json:"name"`
URL string `json:"url"`
}
type Post struct {
CreatedAt time.Time `json:"created_at"`
Id string `json:"id"`
Visibility string `json:"visibility"`
InReplyToId string `json:"in_reply_to_id"`
InReplyToAccountId string `json:"in_reply_to_account_id"`
Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"`
Language string `json:"language"`
URI string `json:"uri"`
URL string `json:"url"`
Application Application `json:"application"`
Content string `json:"content"`
MediaAttachments []MediaAttachment `json:"media_attachments"`
RepliesCount int `json:"replies_count"`
ReblogsCount int `json:"reblogs_count"`
FavoritesCount int `json:"favourites_count"`
Pinned bool `json:"pinned"`
Tags []Tag `json:"tags"`
Favourited bool `json:"favourited"`
Reblogged bool `json:"reblogged"`
Muted bool `json:"muted"`
Bookmarked bool `json:"bookmarked"`
Account Account `json:"account"`
}
type PostsFilter struct {
@ -133,6 +178,22 @@ func (c Client) getAccount() (Account, error) {
return account, nil
}
func TagsForPost(post Post, descendants []Post) []Tag {
var tags []Tag
for _, tag := range post.Tags {
tags = append(tags, tag)
}
for _, descendant := range descendants {
for _, tag := range descendant.Tags {
tags = append(tags, tag)
}
}
return tags
}
func get(requestUrl string, variable interface{}) error {
res, err := http.Get(requestUrl)

View file

@ -25,6 +25,7 @@ type FileWriter struct {
type TemplateContext struct {
Post client.Post
Descendants []client.Post
Tags []client.Tag
}
func New(dir string) (FileWriter, error) {
@ -47,8 +48,8 @@ func New(dir string) (FileWriter, error) {
}, nil
}
func (f FileWriter) Write(post client.Post, templateFile string) error {
if post.InReplyToId != "" {
func (f FileWriter) Write(post client.Post, threaded bool, templateFile string) error {
if threaded && post.InReplyToId != "" {
f.repies[post.InReplyToId] = post
return nil
}
@ -73,7 +74,7 @@ func (f FileWriter) Write(post client.Post, templateFile string) error {
continue
}
imageFilename, err := downloadAttachment(dir, media.Id, media.Url)
imageFilename, err := downloadAttachment(dir, media.Id, media.URL)
if err != nil {
return err
@ -96,6 +97,7 @@ func (f FileWriter) Write(post client.Post, templateFile string) error {
context := TemplateContext{
Post: post,
Descendants: descendants,
Tags: client.TagsForPost(post, descendants),
}
err = tmpl.Execute(file, context)

View file

@ -1,25 +1,35 @@
---
date: {{ .Post.CreatedAt }}
post_url: {{ .Post.URI }}
post_ids:
- {{ .Post.Id }}
{{- if .Post.InReplyToId }}
in_reply_to: {{ .Post.InReplyToId }}
{{- end }}
post_uri: {{ .Post.URI }}
post_id: {{ .Post.Id }}
{{- if len .Tags }}
tags:
{{- range .Tags }}
- {{ .Name }}
{{- end }}
{{- end }}
{{- if len .Descendants }}
descendants:
{{- range .Descendants }}
- {{ .Id }}
{{- end }}
{{- end }}
---
{{ .Post.Content | tomd }}
{{ range .Post.MediaAttachments }}
{{- if eq .Type "image" -}}
{{- range .Post.MediaAttachments }}
{{ if eq .Type "image" }}
![{{ .Description }}]({{ .Path }})
{{ end }}
{{- end -}}
{{- range .Descendants }}
{{ .Content | tomd -}}
{{ range .MediaAttachments }}
{{- if eq .Type "image" -}}
{{- range .MediaAttachments }}
{{ if eq .Type "image" }}
![{{ .Description }}]({{ .Path }})
{{ end }}
{{- end -}}

View file

@ -23,6 +23,7 @@ func main() {
persistFirst := flag.String("persist-first", "", "Location to persist the post id of the first post returned")
persistLast := flag.String("persist-last", "", "Location to persist the post id of the last post returned")
templateFile := flag.String("template", "", "Template to use for post rendering, if passed")
threaded := flag.Bool("threaded", true, "Thread replies for a post in a single file")
flag.Parse()
@ -58,7 +59,7 @@ func main() {
continue
}
if err := fileWriter.Write(post, *templateFile); err != nil {
if err := fileWriter.Write(post, *threaded, *templateFile); err != nil {
log.Panicln("error writing post to file: %w", err)
break
}