mirror of
https://github.com/adulau/mastodon-markdown-archive.git
synced 2024-11-24 19:07:09 +00:00
Pass file customization options to constructor
This commit is contained in:
parent
b820e10996
commit
5b0f031ab0
2 changed files with 20 additions and 16 deletions
|
@ -21,6 +21,8 @@ var templates embed.FS
|
||||||
|
|
||||||
type FileWriter struct {
|
type FileWriter struct {
|
||||||
dir string
|
dir string
|
||||||
|
templateFile string
|
||||||
|
filenameTemplate string
|
||||||
}
|
}
|
||||||
|
|
||||||
type TemplateContext struct {
|
type TemplateContext struct {
|
||||||
|
@ -44,7 +46,7 @@ type PostFile struct {
|
||||||
File *os.File
|
File *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(dir string) (FileWriter, error) {
|
func New(dir string, templateFile string, filenameTemplate string) (FileWriter, error) {
|
||||||
var fileWriter FileWriter
|
var fileWriter FileWriter
|
||||||
_, err := os.Stat(dir)
|
_, err := os.Stat(dir)
|
||||||
|
|
||||||
|
@ -60,12 +62,13 @@ func New(dir string) (FileWriter, error) {
|
||||||
|
|
||||||
return FileWriter{
|
return FileWriter{
|
||||||
dir: absDir,
|
dir: absDir,
|
||||||
|
templateFile: templateFile,
|
||||||
|
filenameTemplate: filenameTemplate,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileWriter) Write(post *client.Post, templateFile, filenameTemplate string) error {
|
func (f *FileWriter) Write(post *client.Post) error {
|
||||||
hasMedia := len(post.AllMedia()) > 0
|
postFile, err := f.createFile(post)
|
||||||
postFile, err := f.createFile(post, hasMedia, filenameTemplate)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -88,7 +91,7 @@ func (f FileWriter) Write(post *client.Post, templateFile, filenameTemplate stri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := resolveTemplate(templateFile)
|
tmpl, err := resolveTemplate(f.templateFile)
|
||||||
context := TemplateContext{
|
context := TemplateContext{
|
||||||
Post: post,
|
Post: post,
|
||||||
}
|
}
|
||||||
|
@ -102,11 +105,11 @@ func (f FileWriter) Write(post *client.Post, templateFile, filenameTemplate stri
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatFilename(post *client.Post, filenameTemplate string) (string, error) {
|
func (f *FileWriter) formatFilename(post *client.Post) (string, error) {
|
||||||
tmplString := "{{.Post.Id}}"
|
tmplString := "{{.Post.Id}}"
|
||||||
|
|
||||||
if filenameTemplate != "" {
|
if f.filenameTemplate != "" {
|
||||||
tmplString = filenameTemplate
|
tmplString = f.filenameTemplate
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl := template.Must(template.New("filename").Parse(tmplString))
|
tmpl := template.Must(template.New("filename").Parse(tmplString))
|
||||||
|
@ -131,10 +134,11 @@ func formatFilename(post *client.Post, filenameTemplate string) (string, error)
|
||||||
return nameBuffer.String(), nil
|
return nameBuffer.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileWriter) createFile(post *client.Post, shouldBundle bool, filenameTemplate string) (PostFile, error) {
|
func (f FileWriter) createFile(post *client.Post) (PostFile, error) {
|
||||||
var postFile PostFile
|
var postFile PostFile
|
||||||
|
|
||||||
outputFilename, err := formatFilename(post, filenameTemplate)
|
shouldBundle := len(post.AllMedia()) > 0
|
||||||
|
outputFilename, err := f.formatFilename(post)
|
||||||
extension := filepath.Ext(outputFilename)
|
extension := filepath.Ext(outputFilename)
|
||||||
|
|
||||||
if extension == "" {
|
if extension == "" {
|
||||||
|
|
4
main.go
4
main.go
|
@ -41,7 +41,7 @@ func main() {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileWriter, err := files.New(*dist)
|
fileWriter, err := files.New(*dist, *templateFile, *filenameTemplate)
|
||||||
posts := c.Posts()
|
posts := c.Posts()
|
||||||
postsCount := len(posts)
|
postsCount := len(posts)
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ func main() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := fileWriter.Write(post, *templateFile, *filenameTemplate); err != nil {
|
if err := fileWriter.Write(post); err != nil {
|
||||||
log.Panicln("error writing post to file: %w", err)
|
log.Panicln("error writing post to file: %w", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue