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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| import os import re import time import frontmatter
def update_front_matter(file):
with open(file, 'r', encoding='utf-8') as f: post = frontmatter.loads(f.read())
is_write = False
if not post.metadata.get('modify_date', None): timeArray = time.localtime((os.path.getmtime(file))) post['date'] = time.strftime("%Y-%m-%d %H:%M", timeArray) if not is_write: is_write = True
temp_content = re.sub(r'```([\s\S]*?)```[\s]?', '', post.content) tags = re.findall(r'\s#[\u4e00-\u9fa5a-zA-Z]+', temp_content, re.M | re.I) ret_tags = list(set(map(lambda x: x.strip(), tags))) print('tags in content: ', ret_tags) print('tags in front matter: ', post.get("tags", [])) if len(ret_tags) == 0: pass
elif post.get("tags", []) != set(ret_tags): post['tags'] = ret_tags if not is_write: is_write = True
if is_write: with open(file, 'w', encoding='utf-8') as f: f.write(frontmatter.dumps(post))
def list_all_files(root_path, ignore_dirs=[]): files = [] default_dirs = [".git", ".obsidian", ".config"] ignore_dirs.extend(default_dirs)
for parent, dirs, filenames in os.walk(root_path): dirs[:] = [d for d in dirs if not d in ignore_dirs] filenames = [f for f in filenames if not f[0] == '.'] for file in filenames: if file.endswith(".md"): files.append(os.path.join(parent, file)) return files
if __name__ == "__main__": ignore_dirs = ["Resource", "Write"] files = list_all_files('./_posts', ignore_dirs=ignore_dirs)
print("current dir: ", os.path.dirname(os.path.abspath(__file__))) for file in files: print("---------------------------------------------------------------") print('current file: ', file) update_front_matter(file) time.sleep(1)
|