-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for WP gallery shortcodes
1. Parse and convert markup from WP to Hugo 2. Add custom Hugo gallery shortcode with CSS for column layouts 3. Nest individual images with figure shortcode within galleries
- Loading branch information
1 parent
ce1f667
commit 358862e
Showing
6 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package hugogenerator | ||
|
||
import "path/filepath" | ||
|
||
const _customCSS = ` | ||
.gallery { | ||
display: flex; | ||
flex-wrap: wrap; | ||
} | ||
.gallery figure, | ||
.gallery figure img { | ||
text-align: center; | ||
} | ||
.gallery figure img { | ||
margin: 1rem auto; | ||
} | ||
.gallery-cols-1 figure { | ||
width: 100%; | ||
} | ||
.gallery-cols-2 figure { | ||
width: 50%; | ||
} | ||
.gallery-cols-3 figure { | ||
width: 33.3333333333%; | ||
} | ||
.gallery-cols-4 figure { | ||
width: 25%; | ||
} | ||
.gallery-cols-5 figure { | ||
width: 25%; | ||
} | ||
.gallery-cols-6 figure { | ||
width: 16.666666666%; | ||
} | ||
` | ||
|
||
func setupCSS(siteDir string) error { | ||
err := appendFile(filepath.Join(siteDir, _outputCssFile), _customCSS) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/wp2hugo/internal/hugogenerator/hugopage/wordpress_gallery_converter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package hugopage | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Example: [gallery size="medium" link="file" columns="4" ids="1710,1713,1712,1711"] | ||
// Full examples : https://codex.wordpress.org/Gallery_Shortcode | ||
// the important field to extract is "ids", but others might come in handy | ||
// `size` is legacy from pre-responsive design and should be discarded now | ||
// `link` is probably something to enforce in Hugo figure shortcode, | ||
// for us it's mostly "file" to handle, since "attachment_page" makes no sense for Hugo. | ||
var _GalleryRegEx = regexp.MustCompile(`\[gallery ([^\[\]]*)\]`) | ||
|
||
var _idRegEx = regexp.MustCompile(`ids="([^"]+)"`) | ||
var _colsRegEx = regexp.MustCompile(`columns="([^"]+)"`) | ||
|
||
// TODO: should we handle `order="ASC|DESC"` when `orderby="ID"` ? | ||
// Seems to me that people mostly order pictures in galleries arbitrarily. | ||
|
||
// 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 { | ||
log.Debug(). | ||
Msg("Replacing gallery with figures") | ||
|
||
htmlData = replaceAllStringSubmatchFunc(_GalleryRegEx, htmlData, | ||
func(groups []string) string { | ||
return galleryReplacementFunction(provider, groups) | ||
}) | ||
|
||
return htmlData | ||
} | ||
|
||
func galleryReplacementFunction(provider ImageURLProvider, groups []string) string { | ||
var output strings.Builder | ||
|
||
// Find columns layout | ||
cols := _colsRegEx.FindStringSubmatch(groups[1]) | ||
col_nb := "1" | ||
if cols != nil { | ||
col_nb = cols[1] | ||
} | ||
|
||
// Find image IDs | ||
ids := _idRegEx.FindStringSubmatch(groups[1]) | ||
ids_array := strings.Split(ids[1], ",") | ||
|
||
// TODO: maybe handle `order="ASC|DESC"` in conjunction with `orderby="..."`, so reorder ids_array here. | ||
|
||
// We will use `figure` shortcodes nested into a `gallery` shortcode for the main layout | ||
output.WriteString("<br>") // This will get converted to newline later on | ||
|
||
// Opening tag : | ||
output.WriteString(fmt.Sprintf(`{{%% gallery cols="%s" %%}}`, col_nb)) | ||
|
||
// For each image ID in WP gallery shortcode, get the URL | ||
for _, s := range ids_array { | ||
tmp, err := provider.GetImageInfo(s) | ||
if tmp != nil { | ||
src := tmp.ImageURL | ||
// These characters create problems in Hugo's markdown | ||
src = strings.ReplaceAll(src, " ", "%20") | ||
src = strings.ReplaceAll(src, "_", "%5F") | ||
|
||
title := tmp.Title | ||
|
||
output.WriteString("<br>") // This will get converted to newline later on | ||
output.WriteString(fmt.Sprintf(`{{< figure src="%s" title="%s" alt="%s" >}}`, src, title, title)) | ||
output.WriteString("<br>") // This will get converted to newline later on | ||
} else { | ||
log.Warn(). | ||
Err(err). | ||
Str("imageID", s). | ||
Msg("Image URL not found") | ||
} | ||
} | ||
|
||
// Closing tag for main gallery shortcode | ||
output.WriteString(`{{% /gallery %}}`) | ||
output.WriteString("<br>") // This will get converted to newline later on | ||
return output.String() | ||
} |