diff --git a/README.md b/README.md index b203b21..3493667 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ mastodon-markdown-archive \ ## Template -By default, this tool uses the [post.tmp](./templates/post.tmpl) template to create the markdown file. A different template can be used by passing its path to `--template`. +By default, this tool uses the [post.tmp](./files/templates/post.tmpl) template to create the markdown file. A different template can be used by passing its path to `--template`. -For information about variables and functions available in the template context, refer to the `Write` method in [files.go](files/files.go#L108-L117). +For information about variables and functions available in the template context, refer to the `Write` method in [files.go](files/files.go#L104-L110). diff --git a/files/files.go b/files/files.go index c9adbf2..55d77e1 100644 --- a/files/files.go +++ b/files/files.go @@ -1,6 +1,7 @@ package files import ( + "embed" "fmt" "io" "mime" @@ -13,6 +14,9 @@ import ( md "github.com/JohannesKaufmann/html-to-markdown" ) +//go:embed templates/post.tmpl +var templates embed.FS + type FileWriter struct { dir string repies map[string]client.Post @@ -44,18 +48,6 @@ func New(dir string) (FileWriter, error) { } func (f FileWriter) Write(post client.Post, templateFile string) error { - tmplFilename := "templates/post.tmpl" - - if templateFile != "" { - tmplFilename = templateFile - } - - tmplFile, err := filepath.Abs(tmplFilename) - - if err != nil { - return fmt.Errorf("error resolving template absolute path: %w", err) - } - if post.InReplyToId != "" { f.repies[post.InReplyToId] = post return nil @@ -65,6 +57,7 @@ func (f FileWriter) Write(post client.Post, templateFile string) error { f.getReplies(post.Id, &descendants) var file *os.File + var err error if len(post.MediaAttachments) == 0 { name := fmt.Sprintf("%s.md", post.Id) @@ -89,10 +82,6 @@ func (f FileWriter) Write(post client.Post, templateFile string) error { media.Path = imageFilename } - if err != nil { - return fmt.Errorf("error downloading media attachments: %w", err) - } - filename := filepath.Join(dir, "index.md") file, err = os.Create(filename) } @@ -103,18 +92,12 @@ func (f FileWriter) Write(post client.Post, templateFile string) error { defer file.Close() - converter := md.NewConverter("", true, nil) - - funcs := template.FuncMap{ - "tomd": converter.ConvertString, - } - - tmpl, err := template.New(filepath.Base(tmplFilename)).Funcs(funcs).ParseFiles(tmplFile) - + tmpl, err := resolveTemplate(templateFile) context := TemplateContext{ Post: post, Descendants: descendants, } + err = tmpl.Execute(file, context) if err != nil { @@ -182,3 +165,29 @@ func downloadAttachment(dir string, id string, url string) (string, error) { return filename, nil } + +func resolveTemplate(templateFile string) (*template.Template, error) { + converter := md.NewConverter("", true, nil) + + funcs := template.FuncMap{ + "tomd": converter.ConvertString, + } + + if templateFile == "" { + tmpl, err := template.New("post.tmpl").Funcs(funcs).ParseFS(templates, "templates/*.tmpl") + + if err != nil { + return tmpl, err + } + + return tmpl, nil + } + + tmpl, err := template.New(filepath.Base(templateFile)).Funcs(funcs).ParseGlob(templateFile) + + if err != nil { + return tmpl, err + } + + return tmpl, nil +} diff --git a/templates/post.tmpl b/files/templates/post.tmpl similarity index 100% rename from templates/post.tmpl rename to files/templates/post.tmpl