mirror of
https://github.com/adulau/mastodon-markdown-archive.git
synced 2024-11-22 01:47:05 +00:00
Support filename customization
This commit is contained in:
parent
7e9b4aa208
commit
b820e10996
3 changed files with 66 additions and 8 deletions
|
@ -23,8 +23,10 @@ Usage of mastodon-markdown-archive:
|
||||||
Whether or not to exclude reblogs
|
Whether or not to exclude reblogs
|
||||||
-exclude-replies
|
-exclude-replies
|
||||||
Whether or not exclude replies to other users
|
Whether or not exclude replies to other users
|
||||||
|
-filename string
|
||||||
|
Template for post filename
|
||||||
-limit int
|
-limit int
|
||||||
Maximum number of posts to fetch (default 40, maximum 40)
|
Maximum number of posts to fetch (default 40)
|
||||||
-max-id string
|
-max-id string
|
||||||
Fetch posts lesser than this id
|
Fetch posts lesser than this id
|
||||||
-min-id string
|
-min-id string
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package files
|
package files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -8,6 +9,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"git.garrido.io/gabriel/mastodon-markdown-archive/client"
|
"git.garrido.io/gabriel/mastodon-markdown-archive/client"
|
||||||
|
@ -25,6 +27,17 @@ type TemplateContext struct {
|
||||||
Post *client.Post
|
Post *client.Post
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FilenameDate struct {
|
||||||
|
Year int
|
||||||
|
Month string
|
||||||
|
Day string
|
||||||
|
}
|
||||||
|
|
||||||
|
type FilenameTemplateContext struct {
|
||||||
|
Post *client.Post
|
||||||
|
Date FilenameDate
|
||||||
|
}
|
||||||
|
|
||||||
type PostFile struct {
|
type PostFile struct {
|
||||||
Dir string
|
Dir string
|
||||||
Name string
|
Name string
|
||||||
|
@ -50,9 +63,9 @@ 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, templateFile, filenameTemplate string) error {
|
||||||
hasMedia := len(post.AllMedia()) > 0
|
hasMedia := len(post.AllMedia()) > 0
|
||||||
postFile, err := f.createFile(post, hasMedia)
|
postFile, err := f.createFile(post, hasMedia, filenameTemplate)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -89,18 +102,60 @@ func (f FileWriter) Write(post *client.Post, templateFile string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileWriter) createFile(post *client.Post, shouldBundle bool) (PostFile, error) {
|
func formatFilename(post *client.Post, filenameTemplate string) (string, error) {
|
||||||
|
tmplString := "{{.Post.Id}}"
|
||||||
|
|
||||||
|
if filenameTemplate != "" {
|
||||||
|
tmplString = filenameTemplate
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl := template.Must(template.New("filename").Parse(tmplString))
|
||||||
|
|
||||||
|
year, month, day := post.CreatedAt.Date()
|
||||||
|
|
||||||
|
filenameData := FilenameTemplateContext{
|
||||||
|
Post: post,
|
||||||
|
Date: FilenameDate{
|
||||||
|
Year: year,
|
||||||
|
Month: fmt.Sprintf("%02d", int(month)),
|
||||||
|
Day: fmt.Sprintf("%02d", day),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var nameBuffer bytes.Buffer
|
||||||
|
|
||||||
|
if err := tmpl.Execute(&nameBuffer, filenameData); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nameBuffer.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f FileWriter) createFile(post *client.Post, shouldBundle bool, filenameTemplate string) (PostFile, error) {
|
||||||
var postFile PostFile
|
var postFile PostFile
|
||||||
|
|
||||||
|
outputFilename, err := formatFilename(post, filenameTemplate)
|
||||||
|
extension := filepath.Ext(outputFilename)
|
||||||
|
|
||||||
|
if extension == "" {
|
||||||
|
extension = ".md"
|
||||||
|
} else {
|
||||||
|
outputFilename = strings.TrimSuffix(outputFilename, extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return postFile, err
|
||||||
|
}
|
||||||
|
|
||||||
if shouldBundle {
|
if shouldBundle {
|
||||||
dir := filepath.Join(f.dir, post.Id)
|
dir := filepath.Join(f.dir, outputFilename)
|
||||||
|
|
||||||
_, err := os.Stat(dir)
|
_, err := os.Stat(dir)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
os.Mkdir(dir, os.ModePerm)
|
os.Mkdir(dir, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
name := filepath.Join(dir, "index.md")
|
name := filepath.Join(dir, fmt.Sprintf("index%s", extension))
|
||||||
file, err := os.Create(name)
|
file, err := os.Create(name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -116,7 +171,7 @@ func (f FileWriter) createFile(post *client.Post, shouldBundle bool) (PostFile,
|
||||||
return postFile, nil
|
return postFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
name := filepath.Join(f.dir, fmt.Sprintf("%s.md", post.Id))
|
name := filepath.Join(f.dir, fmt.Sprintf("%s%s", outputFilename, extension))
|
||||||
file, err := os.Create(name)
|
file, err := os.Create(name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
3
main.go
3
main.go
|
@ -24,6 +24,7 @@ func main() {
|
||||||
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")
|
threaded := flag.Bool("threaded", true, "Thread replies for a post in a single file")
|
||||||
|
filenameTemplate := flag.String("filename", "", "Template for post filename")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -51,7 +52,7 @@ func main() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := fileWriter.Write(post, *templateFile); err != nil {
|
if err := fileWriter.Write(post, *templateFile, *filenameTemplate); 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