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 { type Account struct {
Id string `json:"id"` 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 MediaAttachment struct {
Type string `json:"type"` Type string `json:"type"`
Url string `json:"url"` URL string `json:"url"`
Description string `json:"description"` Description string `json:"description"`
Id string `json:"id"` Id string `json:"id"`
Path string 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 { type Post struct {
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
Id string `json:"id"` Id string `json:"id"`
Visibility string `json:"visibility"` Visibility string `json:"visibility"`
InReplyToId string `json:"in_reply_to_id"` 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"` URI string `json:"uri"`
URL string `json:"url"`
Application Application `json:"application"`
Content string `json:"content"` Content string `json:"content"`
MediaAttachments []MediaAttachment `json:"media_attachments"` 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 { type PostsFilter struct {
@ -133,6 +178,22 @@ func (c Client) getAccount() (Account, error) {
return account, nil 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 { func get(requestUrl string, variable interface{}) error {
res, err := http.Get(requestUrl) res, err := http.Get(requestUrl)

View file

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

View file

@ -1,26 +1,36 @@
--- ---
date: {{ .Post.CreatedAt }} date: {{ .Post.CreatedAt }}
post_url: {{ .Post.URI }} {{- if .Post.InReplyToId }}
post_ids: in_reply_to: {{ .Post.InReplyToId }}
- {{ .Post.Id }} {{- end }}
post_uri: {{ .Post.URI }}
post_id: {{ .Post.Id }}
{{- if len .Tags }}
tags:
{{- range .Tags }}
- {{ .Name }}
{{- end }}
{{- end }}
{{- if len .Descendants }}
descendants:
{{- range .Descendants }} {{- range .Descendants }}
- {{ .Id }} - {{ .Id }}
{{- end }} {{- end }}
{{- end }}
--- ---
{{ .Post.Content | tomd }} {{ .Post.Content | tomd }}
{{ range .Post.MediaAttachments }} {{- range .Post.MediaAttachments }}
{{- if eq .Type "image" -}} {{ if eq .Type "image" }}
![{{ .Description }}]({{ .Path }}) ![{{ .Description }}]({{ .Path }})
{{ end }} {{ end }}
{{- end -}} {{- end -}}
{{- range .Descendants }} {{- range .Descendants }}
{{ .Content | tomd -}} {{ .Content | tomd -}}
{{ range .MediaAttachments }} {{- range .MediaAttachments }}
{{- if eq .Type "image" -}} {{ if eq .Type "image" }}
![{{ .Description }}]({{ .Path }}) ![{{ .Description }}]({{ .Path }})
{{ end }} {{ end }}
{{- end -}} {{- end -}}
{{- 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") 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") 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") 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() flag.Parse()
@ -58,7 +59,7 @@ func main() {
continue 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) log.Panicln("error writing post to file: %w", err)
break break
} }