You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
3.4 KiB
Go
143 lines
3.4 KiB
Go
2 years ago
|
package obspipeline
|
||
2 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/fs"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
2 years ago
|
func (p *Pipeline) Walk() error {
|
||
|
notesRoot := os.DirFS(p.Source)
|
||
|
blogRoot := os.DirFS(p.Target)
|
||
2 years ago
|
|
||
2 years ago
|
err := fs.WalkDir(notesRoot, ".", p.findAttachments)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return fmt.Errorf("error scanning for attachments: %w", err)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
err = fs.WalkDir(notesRoot, ".", p.findNotes)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return fmt.Errorf("error scanning vault for posts: %w", err)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
err = fs.WalkDir(blogRoot, ".", p.findPosts)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return fmt.Errorf("error scanning blog for posts: %w", err)
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (p *Pipeline) findNotes(path string, d fs.DirEntry, err error) error {
|
||
2 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if d.IsDir() {
|
||
|
return nil
|
||
|
}
|
||
2 years ago
|
walkLogger := p.L.Named("FindNotes").With(zap.String("path", path))
|
||
2 years ago
|
|
||
2 years ago
|
if strings.HasSuffix(path, ".md") && strings.Contains(path, p.BlogDir) {
|
||
2 years ago
|
walkLogger.Info("found blog post to publish, adding to index")
|
||
2 years ago
|
p.Notes = append(p.Notes, path)
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (p *Pipeline) findAttachments(path string, d fs.DirEntry, err error) error {
|
||
2 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if d.IsDir() {
|
||
|
return nil
|
||
|
}
|
||
2 years ago
|
walkLogger := p.L.Named("FindAttachments").With(zap.String("path", path))
|
||
2 years ago
|
|
||
2 years ago
|
if strings.Contains(path, p.AttachmentsDir) {
|
||
2 years ago
|
walkLogger.Info("found attachment file, adding to index")
|
||
2 years ago
|
absPath, err := filepath.Abs(filepath.Join(p.Source, path))
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return fmt.Errorf("error generating absolute path for attachment %q: %w", path, err)
|
||
2 years ago
|
}
|
||
2 years ago
|
walkLogger.Info("adding Attachment",
|
||
|
zap.String("key", filepath.Base(absPath)),
|
||
|
zap.String("value", absPath),
|
||
|
)
|
||
|
p.Attachments[filepath.Base(absPath)] = absPath
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (p *Pipeline) findPosts(path string, d fs.DirEntry, err error) error {
|
||
2 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if d.IsDir() {
|
||
|
return nil
|
||
|
}
|
||
2 years ago
|
walkLogger := p.L.Named("FindPosts").With(zap.String("path", path))
|
||
2 years ago
|
|
||
|
if strings.HasSuffix(path, "index.md") {
|
||
2 years ago
|
walkLogger.Info("found index.md, adding to index")
|
||
2 years ago
|
p.Posts = append(p.Posts, path)
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func (p *Pipeline) Move() error {
|
||
|
moveLogger := p.L.Named("Move")
|
||
|
moveLogger.Info("scanning posts", zap.Strings("posts", p.Posts))
|
||
|
for _, post := range p.Notes {
|
||
2 years ago
|
// log.Printf("scanning %q for attachment links", post)
|
||
2 years ago
|
linkedAttachments, err := extractAttachments(filepath.Join(p.Source, post))
|
||
2 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("could not extract attachment links from %q: %w", post, err)
|
||
|
}
|
||
|
for _, attachment := range linkedAttachments {
|
||
2 years ago
|
att, ok := p.Attachments[attachment]
|
||
|
if !ok {
|
||
|
return fmt.Errorf("Attachment is linked by post %q but doesn't exist in attachments directory %q", post, p.AttachmentsDir)
|
||
|
}
|
||
|
moveAttachment(post, att, p.L.Named("moveAttachment"))
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func moveAttachment(post, attachment string, l *zap.Logger) error {
|
||
|
l.Info("moving attachment",
|
||
|
zap.String("post", post),
|
||
|
zap.String("attachment", attachment),
|
||
|
)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func extractAttachments(post string) ([]string, error) {
|
||
2 years ago
|
|
||
|
pat := regexp.MustCompile(`\[\[Resources\/attachments\/(.*)?\]\]`)
|
||
|
|
||
|
attachments := make([]string, 0)
|
||
|
postBody, err := ioutil.ReadFile(post)
|
||
|
if err != nil {
|
||
2 years ago
|
return attachments, fmt.Errorf("error opening post to scan for attachment links: %w", err)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
for _, att := range pat.FindAllSubmatch(postBody, -1) {
|
||
2 years ago
|
filename := string(att[1])
|
||
|
attachments = append(attachments, filename)
|
||
2 years ago
|
|
||
|
}
|
||
|
|
||
|
return attachments, nil
|
||
|
}
|