Created
April 15, 2021 06:06
-
-
Save sspaeti/eab0faa8674cf19d92bd4a344857b0ba to your computer and use it in GitHub Desktop.
Moving images from WordPress /images/wp-content to /content/posts/my-post/images
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
""" | |
If you moved from Wordpress to GoHugo and all your images are in "/images/wp-content", but you want to use image processing or other featues of GoHugo | |
that only works in the content-folder (/content/posts/). | |
This script | |
1. searches all images from wordpress which are used in the content | |
2. creating a folder with the name of the content-file (*.md) | |
3. moving images to /content/posts/my-post-name/images | |
4. and creates and index.en.md with the content. | |
After you run it, I used regexp search/replace in visual studio to replace old images to new path inside the content (*.md) files. | |
Search: /images/wp-content/uploads/[0-9]{4}/[0-9]{2}/(.*?)(\.png|\.jpg) | |
replace: images/$1$2 | |
search in folder: ./content/posts | |
For old style featured image to resource fetured image | |
Search: featuredImage:(.*?$) | |
Replace: resources: | |
- name: "featured-image" | |
src: $1 | |
""" | |
import os | |
import re | |
from shutil import copyfile | |
from pathlib import Path | |
import ntpath | |
# grab git path from env variable | |
git = os.getenv('git') | |
git_location = f"{git}/sspaeti.com-hugo" | |
post_dir = os.path.join(git_location, "content/posts") | |
for file in os.listdir(post_dir): | |
if file.endswith(".md"): | |
# print(os.path.join("/mydir", file)) | |
print(file) | |
# my search pattern for images | |
pattern = re.compile("/images/wp-content/uploads/.*?(\.png|\.jpg)") | |
# create content name and images folder | |
folder_name = ntpath.basename(file).replace(".md", "") | |
current_folder = os.path.join(post_dir, folder_name) | |
print(current_folder) | |
current_folder_images = os.path.join(current_folder, "images") | |
print(current_folder_images) | |
Path(current_folder).mkdir(parents=True, exist_ok=True) | |
Path(current_folder_images).mkdir(parents=True, exist_ok=True) | |
md_file_path = os.path.join(post_dir, file) | |
for i, line in enumerate(open(md_file_path)): | |
for match in re.finditer(pattern, line): | |
print('Found on line %s: %s' % (i + 1, match.group())) | |
src = git_location + '/static' + match.group() | |
dest = os.path.join(current_folder_images, ntpath.basename(match.group())) | |
# move found image from static folder to content folder | |
print(f"src: {src} // dest: {dest}") | |
copyfile(src, dest) | |
dest_index = os.path.join(current_folder, 'index.en.md') | |
print(f"create index.en.md - src: {md_file_path} // dest: {dest_index}") | |
copyfile(md_file_path, dest_index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment