1 Star 0 Fork 2

myisen/ai-workshop-2024

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
convert_headings.py 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
powerfooI 提交于 2024-10-18 10:25 . init commit
import re
import os
import sys
def convert_headings(input_file_path, output_file_path=None):
"""
convert_headings converts markdown headings from format === and ---
to the standard format # and ## respectively.
"""
pattern_headline_1 = re.compile(r"^(.*)\n(\=+)$", re.MULTILINE)
pattern_headline_2 = re.compile(r"^(.*)\n(\-+)$", re.MULTILINE)
with open(input_file_path, "r", encoding="utf-8") as file:
content = file.read()
content = pattern_headline_1.sub(r"# \1", content)
content = pattern_headline_2.sub(r"## \1", content)
with open(output_file_path or input_file_path, "w", encoding="utf-8") as file:
file.write(content)
def walk_dir(dir_path):
for root, _, files in os.walk(dir_path):
for file in files:
if file.endswith(".md"):
convert_headings(os.path.join(root, file))
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python3 convert_headings.py <file or directory>...")
sys.exit(1)
for arg in sys.argv[1:]:
if not os.path.exists(arg):
print(f"File or directory {arg} does not exist.")
continue
if os.path.isdir(arg):
walk_dir(arg)
else:
convert_headings(arg)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/myisen/ai-workshop-2024.git
[email protected]:myisen/ai-workshop-2024.git
myisen
ai-workshop-2024
ai-workshop-2024
main

搜索帮助