Files
NANOKVM-sipeed_wiki-MIRROR/docs/hardware/replace_metadata.py
2022-09-16 16:33:09 +08:00

46 lines
1.2 KiB
Python

import os, sys
from glob import glob
import re
path = sys.argv[1]
file_dirs = glob(os.path.join(path, "**"), recursive=True)
for path in file_dirs:
if os.path.isdir(path):
continue
if not path.endswith(".md"):
continue
print("deal:", path)
with open(path, encoding="utf-8") as f:
content = f.read()
title = ""
def re_replace_sharp_h1(c):
global title
title = c[0][2:-1]
return ""
def re_replace_h1(c):
global title
title = c[0].split("\n")[0]
return ""
# find title from:
# title
# ===
content = re.sub(r'.*\n===.*\n', re_replace_h1, content, flags=re.MULTILINE)
if not title:
# find tile from:
# # title
content = re.sub(r'^# .*\n', re_replace_sharp_h1, content)
header = '''---
title: {}
keywords: maixpy, k210, AIOT, 边缘计算
desc: maixpy {}
---
'''.format(title, title)
content = header + content
with open(path, "w", encoding="utf-8") as f:
f.write(content)