From 6b550e80d2892d5e4c56c002277a5e78446fe23d Mon Sep 17 00:00:00 2001 From: Ashish Bhatia Date: Sat, 23 Nov 2024 23:53:09 -0800 Subject: [PATCH] feat: Support for [gallery] shortcodes with no specific IDs In WordPress, a gallery shortcode with no files will create a gallery of all attached media. This commit mimics that behavior. Ref: https://github.com/ashishb/wp2hugo/issues/68 --- .../internal/hugogenerator/hugo_gen_setup.go | 3 ++- .../hugogenerator/hugopage/hugo_page.go | 17 +++++++++++---- .../hugogenerator/hugopage/hugo_page_test.go | 2 +- .../hugopage/wordpress_gallery_converter.go | 21 ++++++++++++------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go b/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go index 949d048..aebcc1e 100644 --- a/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go +++ b/src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go @@ -402,7 +402,8 @@ 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.PostFormat) + page.Categories, page.Tags, g.wpInfo.GetAttachmentsForPost(page.PostID), + page.Footnotes, page.Content, page.GUID, page.FeaturedImageID, page.PostFormat) } func (g Generator) downloadPageMedia(outputMediaDirPath string, p *hugopage.Page, pageURL *url.URL) error { diff --git a/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go b/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go index 9da5325..96f58b5 100644 --- a/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go +++ b/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go @@ -31,8 +31,10 @@ const ( type Page struct { // This is the original URL of the page from the WordPress site absoluteURL url.URL - metadata map[string]any - markdown string + attachments []wpparser.AttachmentInfo + + metadata map[string]any + markdown string } const _WordPressMoreTag = "" @@ -69,7 +71,8 @@ var _hugoAudioLinks = regexp.MustCompile(`{{< audio.*?src="([^\"]+?)".*? >}}`) 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, + isDraft bool, categories []string, tags []string, attachments []wpparser.AttachmentInfo, + footnotes []wpparser.Footnote, 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) @@ -79,6 +82,7 @@ func NewPage(provider ImageURLProvider, pageURL url.URL, author string, title st page := Page{ absoluteURL: pageURL, metadata: metadata, + attachments: attachments, } // htmlContent is the HTML content of the page that will be // transformed to Markdown @@ -259,11 +263,16 @@ func (page *Page) getMarkdown(provider ImageURLProvider, htmlContent string, foo msg := "" return &msg, nil } + attachmentIDs := make([]string, 0, len(page.attachments)) + for _, attachment := range page.attachments { + attachmentIDs = append(attachmentIDs, attachment.PostID) + } + converter := getMarkdownConverter() htmlContent = improvePreTagsWithCode(htmlContent) htmlContent = replaceCaptionWithFigure(htmlContent) htmlContent = replaceAudioShortCode(htmlContent) - htmlContent = replaceGalleryWithFigure(provider, htmlContent) + htmlContent = replaceGalleryWithFigure(provider, attachmentIDs, htmlContent) htmlContent = replaceAWBWithParallaxBlur(provider, htmlContent) htmlContent = strings.Replace(htmlContent, _WordPressMoreTag, _customMoreTag, 1) diff --git a/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page_test.go b/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page_test.go index 1cf1a4d..ccae948 100644 --- a/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page_test.go +++ b/src/wp2hugo/internal/hugogenerator/hugopage/hugo_page_test.go @@ -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, nil) + page, err := NewPage(nil, *url1, "author", "Title", nil, false, nil, nil, nil, nil, htmlInput, nil, nil, nil) assert.Nil(t, err) md, err := page.getMarkdown(nil, htmlInput, nil) assert.Nil(t, err) diff --git a/src/wp2hugo/internal/hugogenerator/hugopage/wordpress_gallery_converter.go b/src/wp2hugo/internal/hugogenerator/hugopage/wordpress_gallery_converter.go index 1c431f3..8b9de94 100644 --- a/src/wp2hugo/internal/hugogenerator/hugopage/wordpress_gallery_converter.go +++ b/src/wp2hugo/internal/hugogenerator/hugopage/wordpress_gallery_converter.go @@ -27,13 +27,13 @@ var galleryWithNoIDsErr = errors.New("no image IDs found in gallery shortcode") // Converts the WordPress's caption shortcode to Hugo shortcode "figure" // https://adityatelange.github.io/hugo-PaperMod/posts/papermod/papermod-faq/#centering-image-in-markdown -func replaceGalleryWithFigure(provider ImageURLProvider, htmlData string) string { +func replaceGalleryWithFigure(provider ImageURLProvider, attachmentIDs []string, htmlData string) string { log.Debug(). Msg("Replacing gallery with figures") htmlData = replaceAllStringSubmatchFunc(_GalleryRegEx, htmlData, func(groups []string) string { - info, err := galleryReplacementFunction(provider, groups[1]) + info, err := galleryReplacementFunction(provider, attachmentIDs, groups[1]) if err != nil { return fmt.Sprintf("[gallery %s]", info) // Return the original shortcode } @@ -43,7 +43,7 @@ func replaceGalleryWithFigure(provider ImageURLProvider, htmlData string) string return htmlData } -func galleryReplacementFunction(provider ImageURLProvider, galleryInfo string) (string, error) { +func galleryReplacementFunction(provider ImageURLProvider, attachmentIDs []string, galleryInfo string) (string, error) { var output strings.Builder // Find columns layout @@ -56,10 +56,17 @@ func galleryReplacementFunction(provider ImageURLProvider, galleryInfo string) ( // Find image IDs ids := _idRegEx.FindStringSubmatch(galleryInfo) if len(ids) == 0 { - log.Warn(). - Str("galleryInfo", galleryInfo). - Msg("No image IDs found in gallery shortcode") - return "", galleryWithNoIDsErr + if len(attachmentIDs) > 0 { + ids = []string{"", strings.Join(attachmentIDs, ",")} + log.Info(). + Str("galleryInfo", galleryInfo). + Strs("attachmentIDs", attachmentIDs). + Msg("No image IDs found in gallery shortcode, fallback to page attachments") + } else { + log.Warn(). + Msg("No image IDs found in gallery shortcode and no page attachments") + return "", galleryWithNoIDsErr + } } idsArray := strings.Split(ids[1], ",")