moin_utils/md_cleanup.py

32 lines
941 B
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from os import listdir
from os.path import isfile, join
import os
import sys
import re
import subprocess
# argv[1] - markdown files directory
if len(sys.argv) < 2:
print("Argument missing. Usage: python3 md_cleanup.py <md content dir>")
else:
path = sys.argv[1]
if os.path.exists(path):
files = [ f for f in listdir(path) if isfile(join(path, f)) ]
for filename in files:
name, file_extension = os.path.splitext(filename)
if file_extension == ".md":
print("Cleaning up %s/%s" % (path, filename))
with open(join(path,filename), "r+", encoding="utf-8") as file:
content = file.read()
content = re.sub(r"`\*(.*)`[\\]?", r"*\g<1>", content)
content = re.sub(r"!(\w+)", r"\g<1>", content)
content = re.sub(r"\* ", r"* ", content)
file.seek(0)
file.truncate()
file.write(content)
file.close()
else:
print("Error: the path specified is invalid")