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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
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]]
sort_posts=sorted(posts.keys(),key=lambda x:posts[x]["date"],reverse=True) n={"all":0,"tags":{},"categories":{}} page={"all":1,"tags":{},"categories":{}} for p in posts: for t in posts[p]["tags"]: if t not in page["tags"]: n["tags"][t]=0 page["tags"][t]=1 for c in posts[p]["categories"]: if c not in page["categories"]: n["categories"][c]=0 page["categories"][c]=1 for p in sort_posts: posts[p]["page"]={"all":page["all"],"tags":{},"categories":{}} for t in posts[p]["tags"]: posts[p]["page"]["tags"][t]=page["tags"][t] for c in posts[p]["categories"]: posts[p]["page"]["categories"][c]=page["categories"][c] n["all"]+=1 for t in posts[p]["tags"]: n["tags"][t]+=1 if n["tags"][t]%10==0: page["tags"][t]+=1 for c in posts[p]["categories"]: n["categories"][c]+=1 if n["categories"][c]%10==0: page["categories"][c]+=1 if n["all"]%10==0: page["all"]+=1
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(os.path.join(path,date[0],"hexo_bg.png")): if os.path.exists(bg_path): shutil.copy(bg_path,os.path.join(path,date[0])) if not os.path.exists(os.path.join(path,"page",str(posts[p]["page"]["all"]),"hexo_bg.png")) and posts[p]["page"]["all"]!=1: if os.path.exists(bg_path): shutil.copy(bg_path,os.path.join(path,"page",str(posts[p]["page"]["all"]))) 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) if not os.path.exists(os.path.join(path,date[0],f"{p}.png")): shutil.copy(raw_path,os.path.join(path,date[0])) if not os.path.exists(os.path.join(path,"page",str(posts[p]["page"]["all"]),f"{p}.png")) and posts[p]["page"]["all"]!=1: shutil.copy(raw_path,os.path.join(path,"page",str(posts[p]["page"]["all"]))) print("archives完成")
path=os.path.join(TARGET_PATH,"page") for p in posts: page=posts[p]["page"] if page==1: continue raw_path=os.path.join(RAW_PATH,"_posts",p,f"{p}.png") target_path=os.path.join(path,str(page["all"])) 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)
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 posts[p]["page"]["categories"][c]==1: 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) else: if not os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c]),"hexo_bg.png")): if os.path.exists(bg_path): shutil.copy(bg_path,os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c]))) if not os.path.exists(raw_path): continue if posts[p]["page"]["categories"][c]==1: if os.path.exists(os.path.join(target_path,f"{p}.png")): continue shutil.copy(raw_path,target_path) else: if os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c]),f"{p}.png")): continue shutil.copy(raw_path,os.path.join(target_path,"page",str(posts[p]["page"]["categories"][c]))) 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 posts[p]["page"]["tags"][t]==1: 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) else: if not os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t]),"hexo_bg.png")): if os.path.exists(bg_path): shutil.copy(bg_path,os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t]))) if not os.path.exists(raw_path): continue if posts[p]["page"]["tags"][t]==1: if os.path.exists(os.path.join(target_path,f"{p}.png")): continue shutil.copy(raw_path,target_path) else: if os.path.exists(os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t]),f"{p}.png")): continue shutil.copy(raw_path,os.path.join(target_path,"page",str(posts[p]["page"]["tags"][t]))) 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 上传啦!")
|