-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_filenames.py
36 lines (32 loc) · 985 Bytes
/
_filenames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys
import glob
import os
import re
from pprint import pprint
from recipemd.data import RecipeParser
from unidecode import unidecode
root_path = '.'
rp = RecipeParser()
tt = str.maketrans({
"ä": "ae",
"ö": "oe",
"ü": "ue",
"Ä": "Ae",
"Ö": "Oe",
"Ü": "Ue",
"ß": "ss",
})
for path in glob.glob(os.path.join(root_path, '**/*.md'), recursive=True):
try:
with open(path, 'r', encoding='UTF-8') as file:
recipe = rp.parse(file.read())
filename = recipe.title
filename = filename.translate(tt)
filename = unidecode(filename)
filename = re.sub(r'[^a-zA-Z0-9]+', '_', filename)
filename = re.sub(r'^_+|_+$', '', filename)
new_path = os.path.join(os.path.dirname(path), f'{filename}.md')
os.rename(path, new_path)
except Exception as e:
print(f'[Filenames] Ignoring {path}', file=sys.stderr)
pprint(e, stream=sys.stderr)