75 lines
1.9 KiB
Python
Executable file
75 lines
1.9 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import datetime
|
|
import zoneinfo
|
|
|
|
SWEDEN = zoneinfo.ZoneInfo("Europe/Stockholm")
|
|
|
|
parser = argparse.ArgumentParser(
|
|
prog="nikola_to_zola.py",
|
|
description="Convert Nikola frontmatter to Zola",
|
|
)
|
|
parser.add_argument("filename")
|
|
|
|
args = parser.parse_args()
|
|
|
|
def parse_frontmatter_line(line: str) -> tuple[str, str]:
|
|
"""Parse one line from Nikola frontmatter
|
|
|
|
Returns a tuple with (key, value).
|
|
|
|
Parses dates into ISO8601 format.
|
|
"""
|
|
frontmatter = tuple(line.strip()[3:].split(": ", 1))
|
|
key = frontmatter[0]
|
|
if len(frontmatter) < 2:
|
|
val = ""
|
|
else:
|
|
val = frontmatter[1]
|
|
|
|
# Parse dateformat used in our frontmatter
|
|
if key == 'date':
|
|
try:
|
|
time_format = "%Y-%m-%d %H:%M:%S %Z"
|
|
val = datetime.datetime.strptime(val, time_format)
|
|
except ValueError:
|
|
time_format2 = "%Y-%m-%d %H:%M %Z"
|
|
val = datetime.datetime.strptime(val, time_format2)
|
|
|
|
val = val.astimezone(SWEDEN).isoformat()
|
|
|
|
return key, val
|
|
|
|
|
|
def read_file():
|
|
frontmatter = {}
|
|
text = []
|
|
with open(args.filename, 'r') as file:
|
|
reading_frontmatter = True
|
|
for line in file:
|
|
if line == "<!--\n":
|
|
continue
|
|
if line.startswith("-->"):
|
|
reading_frontmatter = False
|
|
continue
|
|
|
|
# Strip out teaser comments
|
|
if line.startswith("<!-- TEASER"):
|
|
continue
|
|
|
|
if reading_frontmatter and line.startswith(".. "):
|
|
fkey, fval = parse_frontmatter_line(line)
|
|
frontmatter[fkey] = fval
|
|
else:
|
|
text.append(line)
|
|
|
|
print("+++")
|
|
for key, val in frontmatter.items():
|
|
print(f'{key} = "{val}"')
|
|
print("+++")
|
|
print("".join(text).strip())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
read_file()
|