spanskiblog/scripts/move.py

50 lines
1.5 KiB
Python
Raw Normal View History

2022-12-24 17:42:40 +01:00
import os
import time
header = {
"author": "spanskiduh",
}
MD_NOTES_DIR = "../../md-notes/"
POSTS_DIR = "../content/posts/"
def main():
# List all files in md-notes directory
for filename in os.listdir(MD_NOTES_DIR):
# Check if file is md file
if filename.endswith(".md"):
# Opent the file and read it
with open(MD_NOTES_DIR + filename, "r") as f:
# Read the file
file = f.read()
# Find --- in the file and remove it and content inside ---
splited_file = file.split("---")
if(len(splited_file) > 2):
splited_file = splited_file[2:]
# Join the list back to a string
file = "".join(splited_file)
header["title"] = filename[:-3]
header["description"] = f"click to read about {filename[:-3]}"
date_modified = os.path.getmtime(MD_NOTES_DIR + filename)
header["date"] = time.strftime("%Y-%m-%d", time.localtime(date_modified))
# Add the header to the file for hugo to parse
file = f"""\r
+++
date="{header["date"]}"
author="{header["author"]}"
title="{header["title"]}"
description="{header["description"]}"
+++
{file}"""
# Write the file to proc directory
with open(POSTS_DIR + filename, "w") as f:
f.write(file)
if __name__ == '__main__':
main()