Skip to content

Commit

Permalink
chore: add post ID -> attachment mapping
Browse files Browse the repository at this point in the history
This will be used for #68
  • Loading branch information
ashishb committed Nov 24, 2024
1 parent 3a98678 commit b22995f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 39 deletions.
56 changes: 17 additions & 39 deletions src/wp2hugo/internal/wpparser/wp_parser_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,6 @@ func NewParser() *Parser {
return &Parser{}
}

type WebsiteInfo struct {
Title string
Link string
Description string

PubDate *time.Time
Language string

Categories []CategoryInfo
Tags []TagInfo

// Collecting attachments is mostly useless but we are doing it for completeness
// Only the ones that are actually used in posts/pages are useful
Attachments []AttachmentInfo
Pages []PageInfo
Posts []PostInfo
NavigationLinks []NavigationLink
CustomPosts []CustomPostInfo
}

type NavigationLink struct {
// Fallback to Label if title is empty
Title string
URL string
Type string
}

type CategoryInfo struct {
ID string
Name string
NiceName string
}

type TagInfo struct {
ID string
Name string
Slug string
}

type PublishStatus string

// See some discussion here https://github.com/ashishb/wp2hugo/issues/26
Expand Down Expand Up @@ -97,6 +58,10 @@ type CommonFields struct {
PostFormat *string
PostType *string // Custom post types, typically FAQ, portfolio, etc.

// 1. Only attachments seem to have this
// 2. "0" seems to be reserved for no parent, we replace that with nil
PostParentID *string // ID of the parent post, if any

Description string // how to use this?
Content string
Excerpt string // may be empty
Expand Down Expand Up @@ -283,6 +248,8 @@ func (p *Parser) getWebsiteInfo(feed *rss.Feed, authors []string) (*WebsiteInfo,
Posts: posts,
CustomPosts: customPosts,
NavigationLinks: navigationLinks,

postIDToAttachmentCache: getPostIDToAttachmentsMap(attachments),
}
log.Info().
Int("numAttachments", len(websiteInfo.Attachments)).
Expand Down Expand Up @@ -391,6 +358,16 @@ func getCommonFields(item *rss.Item) (*CommonFields, error) {
postType = &item.Extensions["wp"]["post_type"][0].Value
}

var postParent *string
tmp := item.Extensions["wp"]["post_parent"][0].Value
if tmp != "0" && tmp != "" {
log.Debug().
Str("link", item.Link).
Str("post_parent", tmp).
Msg("Item has a parent")
postParent = &tmp
}

return &CommonFields{
Author: getAuthor(item),
PostID: item.Extensions["wp"]["post_id"][0].Value,
Expand All @@ -402,6 +379,7 @@ func getCommonFields(item *rss.Item) (*CommonFields, error) {
PublishStatus: PublishStatus(publishStatus),
PostFormat: postFormat,
PostType: postType,
PostParentID: postParent,
Excerpt: item.Extensions["excerpt"]["encoded"][0].Value,

Description: item.Description,
Expand Down
64 changes: 64 additions & 0 deletions src/wp2hugo/internal/wpparser/wp_website_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package wpparser

import (
"time"
)

type WebsiteInfo struct {
Title string
Link string
Description string

PubDate *time.Time
Language string

Categories []CategoryInfo
Tags []TagInfo

// Collecting attachments is mostly useless but we are doing it for completeness
// Only the ones that are actually used in posts/pages are useful
Attachments []AttachmentInfo
Pages []PageInfo
Posts []PostInfo
NavigationLinks []NavigationLink
CustomPosts []CustomPostInfo

postIDToAttachmentCache map[string][]AttachmentInfo
}
type NavigationLink struct {
// Fallback to Label if title is empty
Title string
URL string
Type string
}

type CategoryInfo struct {
ID string
Name string
NiceName string
}

type TagInfo struct {
ID string
Name string
Slug string
}

func (w *WebsiteInfo) GetAttachmentsForPost(postID string) []AttachmentInfo {
return w.postIDToAttachmentCache[postID]
}

func getPostIDToAttachmentsMap(attachments []AttachmentInfo) map[string][]AttachmentInfo {
result := make(map[string][]AttachmentInfo)
for _, attachment := range attachments {
if attachment.PostParentID == nil {
continue
}
parentID := *attachment.PostParentID
if result[parentID] == nil {
result[parentID] = make([]AttachmentInfo, 0, 1)
}
result[parentID] = append(result[parentID], attachment)
}
return result
}

0 comments on commit b22995f

Please sign in to comment.