from Pelican to Hugo

2023-03-27
1 min read

Migration code

#!/usr/bin/python3
# Author: rexoen
# https://muio.cn

import os
import sys
import re
import platform

path = sys.argv[1]
if not os.path.exists(path):
    sys.stderr.write(f"path does not exist!")
    sys.exit(2)

# Do backup
if "Linux" in platform.platform():
    os.popen(f"cp -r {path} {path}_backup").read()

metafields = ["title","date","slug","modified","status","category"]
meta_match = re.compile("("+"|".join(metafields)+"):",flags=re.I)
date_match = re.compile("(\d{4}-\d{1,2}-\d{1,2})\s+(\d{1,2}:\d{1,2})")

for dirpath,dirnames,filenames in os.walk(path):
    for filename in filenames:
        if not filename.endswith(".md"):
            continue
        full_path = os.path.join(dirpath,filename)
        print(f"converting {full_path}")
        with open(full_path,"r",encoding="UTF8") as rf:
            file_content = rf.read().strip()
            lines = file_content.splitlines()
            meta_line_idx = []
            for idx,line in enumerate(lines):
                match_meta = meta_match.match(line)
                if match_meta is not None:
                    print(match_meta.group(0))
                    # translate dateformat
                    if match_meta.group(0) == "date:" or "modified:":
                        search_date = date_match.search(line)
                        if search_date is not None:
                            line = f"{match_meta.group(0)} {search_date.group(1)}T{search_date.group(2)}:00+08:00"
                            lines[idx] = f"{match_meta.group(0)} {search_date.group(1)}T{search_date.group(2)}:00+08:00"
                    # translate keywords 
                    if match_meta.group(0) == "modified:":
                        line = line.replace("modified", "lastmod")
                        lines[idx] = line.replace("modified", "lastmod")
                    metafield,*metaval = line.split(":")
                    metaval = ":".join(metaval)
                    lines[idx] = f"{metafield.strip()}: {metaval.strip()}"
                    meta_line_idx.append(idx)
                else:
                    break
            print(meta_line_idx)
            lines.insert(meta_line_idx[-1]+1,"---")
            lines.insert(0,"---")
        converted_content = "\n".join(lines)
        with open(full_path,"w",encoding="UTF8") as wf:
            wf.write(converted_content)

Possible issue

  • Date format error