diff --git a/attachments.go b/attachments.go deleted file mode 100644 index 29fae06..0000000 --- a/attachments.go +++ /dev/null @@ -1,11 +0,0 @@ -package obp - -func (p *Pipeline) FindAttachments() error { - - return nil -} - -func (p *Pipeline) MoveAttachments(post string) error { - - return nil -} diff --git a/pipeline.go b/pipeline.go index a875a2b..b890dff 100644 --- a/pipeline.go +++ b/pipeline.go @@ -55,6 +55,7 @@ func (p *Pipeline) Walk() error { if err != nil { return fmt.Errorf("error scanning blog for posts: %w", err) } + return nil } @@ -66,12 +67,15 @@ func (p *Pipeline) findNotes(path string, d fs.DirEntry, err error) error { if d.IsDir() { return nil } + walkLogger := p.L.Named("FindNotes").With(zap.String("path", path)) if strings.HasSuffix(path, ".md") && strings.Contains(path, p.BlogDir) { walkLogger.Info("found blog post to publish, adding to index") + p.Notes = append(p.Notes, path) } + return nil } @@ -83,20 +87,25 @@ func (p *Pipeline) findAttachments(path string, d fs.DirEntry, err error) error if d.IsDir() { return nil } + walkLogger := p.L.Named("FindAttachments").With(zap.String("path", path)) if strings.Contains(path, p.AttachmentsDir) { walkLogger.Info("found attachment file, adding to index") + absPath, err := filepath.Abs(filepath.Join(p.Source, path)) if err != nil { return fmt.Errorf("error generating absolute path for attachment %q: %w", path, err) } + walkLogger.Info("adding Attachment", zap.String("key", filepath.Base(absPath)), zap.String("value", absPath), ) + p.Attachments[filepath.Base(absPath)] = absPath } + return nil } @@ -108,29 +117,35 @@ func (p *Pipeline) findPosts(path string, d fs.DirEntry, err error) error { if d.IsDir() { return nil } + walkLogger := p.L.Named("FindPosts").With(zap.String("path", path)) if strings.HasSuffix(path, "index.md") { walkLogger.Info("found index.md, adding to index") + p.Posts = append(p.Posts, path) } + return nil } func (p *Pipeline) Move() error { moveLogger := p.L.Named("Move") moveLogger.Info("scanning posts", zap.Strings("posts", p.Posts)) + for _, post := range p.Notes { // log.Printf("scanning %q for attachment links", post) linkedAttachments, err := extractAttachments(filepath.Join(p.Source, post)) if err != nil { return fmt.Errorf("could not extract attachment links from %q: %w", post, err) } + for _, attachment := range linkedAttachments { 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) } + err := moveAttachment(post, att, p.L.Named("moveAttachment")) if err != nil { return fmt.Errorf("error moving attachments: %w", err) @@ -151,10 +166,10 @@ func moveAttachment(post, attachment string, l *zap.Logger) error { } func extractAttachments(post string) ([]string, error) { - pat := regexp.MustCompile(`\[\[Resources\/attachments\/(.*)?\]\]`) attachments := make([]string, 0) + postBody, err := ioutil.ReadFile(post) if err != nil { return attachments, fmt.Errorf("error opening post to scan for attachment links: %w", err) @@ -163,8 +178,17 @@ func extractAttachments(post string) ([]string, error) { for _, att := range pat.FindAllSubmatch(postBody, -1) { filename := string(att[1]) attachments = append(attachments, filename) - } return attachments, nil } + +func (p *Pipeline) FindAttachments() error { + + return nil +} + +func (p *Pipeline) MoveAttachments(post string) error { + + return nil +}