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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
import os import time import shutil
RAW_PATH="用你的\source目录!" TARGET_PATH="用你的\public目录!"
posts={} for p in os.listdir(os.path.join(RAW_PATH,"_posts")): if p.endswith(".md"): with open(os.path.join(RAW_PATH,"_posts",p),encoding="utf-8") as f: lines=f.readlines() title=p[:-3] posts[title]={"date":[],"categories":[],"tags":[],"cover":False} date="" for line in lines: if line.startswith("date: "): date=line[6:16] elif line.startswith("categories: "): posts[title]["categories"]=line[12:].strip()[1:-1].split(",") elif line.startswith("tags: "): posts[title]["tags"]=line[6:].strip()[1:-1].split(",") elif line.startswith("cover: "): posts[title]["cover"]=True if not date: date=str(os.path.getctime(os.path.join(RAW_PATH,"_posts",p)))[:10] date=time.localtime(int(date)) date=time.strftime("%Y-%m-%d",date) posts[title]["date"]=[date[:4],date[5:7],date[8:10]]
for p in posts: if not os.path.exists(os.path.join(RAW_PATH,"_posts",p,f"hexo_bg.png")): bg_path=os.path.join(RAW_PATH,"hexo_bg.png") if os.path.exists(bg_path): shutil.copy(bg_path,os.path.join(RAW_PATH,"_posts",p,"hexo_bg.png"))
path=os.path.join(TARGET_PATH,"archives") for p in posts: date=posts[p]["date"] raw_path=os.path.join(RAW_PATH,"_posts",p) target_path=os.path.join(path,date[0],date[1]) bg_path=os.path.join(RAW_PATH,"hexo_bg.png") if not os.path.exists(os.path.join(target_path,"hexo_bg.png")): if os.path.exists(bg_path): shutil.copy(bg_path,target_path) if not os.path.exists(raw_path): os.makedirs(raw_path) continue raw_path=os.path.join(raw_path,f"{p}.png") if not os.path.exists(raw_path): print(f"{p}.md 没有封面图,请记得加上") continue if not os.path.exists(os.path.join(target_path,f"{p}.png")): shutil.copy(raw_path,target_path) print("archives完成")
path=os.path.join(TARGET_PATH,"categories") for p in posts: categories=posts[p]["categories"] raw_path=os.path.join(RAW_PATH,"_posts",p,f"{p}.png") target_path=path for c in categories: target_path=os.path.join(target_path,c) bg_path=os.path.join(RAW_PATH,"hexo_bg.png") if not os.path.exists(os.path.join(target_path,"hexo_bg.png")): if os.path.exists(bg_path): shutil.copy(bg_path,target_path) if not os.path.exists(raw_path): continue if os.path.exists(os.path.join(target_path,f"{p}.png")): continue shutil.copy(raw_path,target_path) print("categories完成")
path=os.path.join(TARGET_PATH,"tags") for p in posts: tags=posts[p]["tags"] raw_path=os.path.join(RAW_PATH,"_posts",p,f"{p}.png") for t in tags: target_path=os.path.join(path,t) bg_path=os.path.join(RAW_PATH,"hexo_bg.png") if not os.path.exists(os.path.join(target_path,"hexo_bg.png")): if os.path.exists(bg_path): shutil.copy(bg_path,target_path) if not os.path.exists(raw_path): continue if os.path.exists(os.path.join(target_path,f"{p}.png")): continue shutil.copy(raw_path,target_path) print("tags完成")
for p in posts: if not posts[p]["cover"]: bg_path=os.path.join(RAW_PATH,"hexo_bg.png") if os.path.exists(bg_path): shutil.copy(bg_path,os.path.join(TARGET_PATH,posts[p]["date"][0],posts[p]["date"][1],posts[p]["date"][2],p,"hexo_bg.png")) if not os.path.exists(os.path.join(TARGET_PATH,posts[p]["date"][0],posts[p]["date"][1],posts[p]["date"][2],p,"hexo_bg.png")): bg_path=os.path.join(RAW_PATH,"hexo_bg.png") if os.path.exists(bg_path): shutil.copy(bg_path,os.path.join(TARGET_PATH,posts[p]["date"][0],posts[p]["date"][1],posts[p]["date"][2],p,"hexo_bg.png")) print("杂项处理完成") print("可以 npm run deploy 上传啦!")
|