defmakeFirstLine(): res = "---\n" res += ("title: " + blogInfo["title"] + '\n') res += ("date: " + blogInfo["date"] + '\n') res += ("tags:\n") for tag in blogInfo["tags"]: res += ("- " + tag + '\n') res += ("categories: " + blogInfo["categories"] + '\n')
res += ("keywords:\n") for keyword in blogInfo["keywords"]: res += ("- " + keyword + '\n')
res += ("description: " + blogInfo["description"] + '\n') res += ("cover: " + blogInfo["cover"] + '\n') res += '---\n'
print(res) return res
defremoveFirstLine(path): withopen(path, mode="r", encoding="utf-8") as file: lines = file.readlines()
# 刪除第一行 lines = lines[1:]
withopen(path, mode="w", encoding="utf-8") as file: file.writelines(lines)
if __name__ == "__main__": basePath = "./target" fileNames = os.listdir(basePath) for fileName in fileNames: if fileName.endswith(".md"): os.rename(f"{basePath}/{fileName}", f"{basePath}/{blogInfo['title']}.md") else: os.rename(f"{basePath}/{fileName}", f"{basePath}/{blogInfo['title']}") folderPath = f"{basePath}/{blogInfo['title']}" blogMdPath = f"{basePath}/{blogInfo['title']}.md"
removeFirstLine(blogMdPath)
withopen(blogMdPath, mode="r", encoding="utf-8") as f: tmp = f.read()
idx = 0 targetImgLinks = re.findall(r"!\[.*?\]\(.*?/Untitled.*?\.png\)", tmp) for targetImgLink in targetImgLinks: if idx == 0: tmp = tmp.replace(targetImgLink, "![Untitled](Untitled.png)") else: tmp = tmp.replace(targetImgLink, f"![Untitled](Untitled{idx}.png)") idx += 1
tmp = makeFirstLine() + tmp
withopen(blogMdPath, mode="w", encoding="utf-8") as f: f.write(tmp)