moin_utils/md_cleanup.py

32 lines
941 B
Python
Raw Normal View History

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)
2017-04-20 13:48:02 +02:00
content = re.sub(r"\* ", r"* ", content)
file.seek(0)
file.truncate()
file.write(content)
file.close()
else:
print("Error: the path specified is invalid")