Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate WP post_format to Hugo type #47

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ $ make build_prod
1. [x] WordPress page author
1. [x] Ability to filter by author(s), useful for [WordPress multi-site](https://www.smashingmagazine.com/2020/01/complete-guide-wordpress-multisite/) migrations
1. [x] Featured images - export featured image associations with pages and posts correctly
1. [x] WordPoress [Post formats](https://developer.wordpress.org/advanced-administration/wordpress/post-formats/)

### Why existing tools don't work

Expand Down
2 changes: 1 addition & 1 deletion src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (g Generator) newHugoPage(pageURL *url.URL, page wpparser.CommonFields) (*h
g.imageURLProvider,
*pageURL, page.Author, page.Title, page.PublishDate,
page.PublishStatus == wpparser.PublishStatusDraft || page.PublishStatus == wpparser.PublishStatusPending,
page.Categories, page.Tags, page.Footnotes, page.Content, page.GUID, page.FeaturedImageID)
page.Categories, page.Tags, page.Footnotes, page.Content, page.GUID, page.FeaturedImageID, page.PostFormat)
}

func (g Generator) downloadPageMedia(outputMediaDirPath string, p *hugopage.Page, pageURL *url.URL) error {
Expand Down
11 changes: 8 additions & 3 deletions src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ var _hugoParallaxBlurLinks = regexp.MustCompile(`{{< parallaxblur.*?src="(.+?)".

func NewPage(provider ImageURLProvider, pageURL url.URL, author string, title string, publishDate *time.Time,
isDraft bool, categories []string, tags []string, footnotes []wpparser.Footnote,
htmlContent string, guid *rss.GUID, featuredImageID *string) (*Page, error) {
metadata, err := getMetadata(provider, pageURL, author, title, publishDate, isDraft, categories, tags, guid, featuredImageID)
htmlContent string, guid *rss.GUID, featuredImageID *string, postFormat *string) (*Page, error) {
metadata, err := getMetadata(provider, pageURL, author, title, publishDate, isDraft, categories, tags, guid,
featuredImageID, postFormat)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -119,7 +120,8 @@ func getMarkdownLinks(regex *regexp.Regexp, markdown string) []string {
}

func getMetadata(provider ImageURLProvider, pageURL url.URL, author string, title string, publishDate *time.Time,
isDraft bool, categories []string, tags []string, guid *rss.GUID, featuredImageID *string) (map[string]any, error) {
isDraft bool, categories []string, tags []string, guid *rss.GUID, featuredImageID *string,
postFormat *string) (map[string]any, error) {
metadata := make(map[string]any)
metadata["url"] = pageURL.Path // Relative URL
metadata["author"] = author
Expand Down Expand Up @@ -163,6 +165,9 @@ func getMetadata(provider ImageURLProvider, pageURL url.URL, author string, titl
metadata["cover"] = coverInfo
}
}
if postFormat != nil {
metadata["type"] = *postFormat
}
return metadata, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestManualLineBreaks(t *testing.T) {
func testMarkdownExtractor(t *testing.T, htmlInput string, markdownOutput string) {
url1, err := url.Parse("https://example.com")
assert.Nil(t, err)
page, err := NewPage(nil, *url1, "author", "Title", nil, false, nil, nil, nil, htmlInput, nil, nil)
page, err := NewPage(nil, *url1, "author", "Title", nil, false, nil, nil, nil, htmlInput, nil, nil, nil)
assert.Nil(t, err)
md, err := page.getMarkdown(nil, htmlInput, nil)
assert.Nil(t, err)
Expand Down
23 changes: 17 additions & 6 deletions src/wp2hugo/internal/wpparser/wp_parser_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/mmcdole/gofeed/extensions"
"github.com/mmcdole/gofeed/rss"
"github.com/rs/zerolog/log"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"io"
"regexp"
"strings"
"time"
"unicode"

ext "github.com/mmcdole/gofeed/extensions"
"github.com/mmcdole/gofeed/rss"
"github.com/rs/zerolog/log"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)

var (
Expand Down Expand Up @@ -92,6 +93,7 @@ type CommonFields struct {
LastModifiedDate *time.Time
PublishStatus PublishStatus // "publish", "draft", "pending" etc. may be make this a custom type
GUID *rss.GUID
PostFormat *string

Description string // how to use this?
Content string
Expand Down Expand Up @@ -307,12 +309,16 @@ func getCommonFields(item *rss.Item) (*CommonFields, error) {
}
pageCategories := make([]string, 0, len(item.Categories))
pageTags := make([]string, 0, len(item.Categories))
var postFormat *string

for _, category := range item.Categories {
if isCategory(category) {
pageCategories = append(pageTags, NormalizeCategoryName(category.Value))
} else if isTag(category) {
pageTags = append(pageTags, NormalizeCategoryName(category.Value))
} else if isPostFormat(category) {
tmp := NormalizeCategoryName(category.Value)
postFormat = &tmp
} else {
log.Warn().
Str("link", item.Link).
Expand Down Expand Up @@ -358,6 +364,7 @@ func getCommonFields(item *rss.Item) (*CommonFields, error) {
GUID: item.GUID,
LastModifiedDate: lastModifiedDate,
PublishStatus: PublishStatus(publishStatus),
PostFormat: postFormat,
Excerpt: item.Extensions["excerpt"]["encoded"][0].Value,

Description: item.Description,
Expand Down Expand Up @@ -471,6 +478,10 @@ func isCategory(category *rss.Category) bool {
return category.Domain == "category"
}

func isPostFormat(category *rss.Category) bool {
return category.Domain == "post_format"
}

func isTag(tag *rss.Category) bool {
return tag.Domain == "post_tag"
}
Expand Down
Loading