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
| import os import shutil from tqdm import tqdm import subprocess import yaml
ignore_folder = ['musics'] ignore_type = ['md', 'ejs']
def move_non_md_files(src, dst): os.makedirs(dst, exist_ok=True) for item in os.listdir(src): src_item = os.path.join(src, item) dst_item = os.path.join(dst, item) if os.path.isdir(src_item): move_non_md_files(src_item, dst_item) elif os.path.isfile(src_item) and src_item.split('.')[-1] not in ignore_type: shutil.move(src_item, dst)
print("处理文件……") if os.path.isdir('../temp'): shutil.rmtree('../temp')
for item in tqdm(os.listdir('../source')): if item not in ignore_folder: item_path = os.path.join('../source', item) if os.path.isdir(item_path): try: move_non_md_files(item_path, os.path.join('../temp',item)) except Exception as e: print(item_path + ":", e)
print("完成!")
print("hexo cl……") print(subprocess.run('hexo cl', shell=True, capture_output=True, text=True, encoding='utf-8').stdout) print("完成!") print("hexo g……") print(subprocess.run('hexo g', shell=True, capture_output=True, text=True, encoding='utf-8').stdout) print("完成!")
print("后处理 _post 文件……")
md_list = [] for item in os.listdir('../source/_posts'): if item.endswith('.md'): md_list.append(item)
for item in tqdm(md_list): try: with open(os.path.join('../source/_posts', item), 'r', encoding='utf-8') as file: content = file.read() yaml_header, body = content.split('---\n', 2)[1:] yaml_data = yaml.safe_load(yaml_header) source_folder = '../temp/_posts/' + item[:-3] destination_folder = ('../public/' + str(yaml_data['date'].year).zfill(2) + '/' + str(yaml_data['date'].month).zfill(2) + '/' + str(yaml_data['date'].day).zfill(2) + '/' + item[:-3]) shutil.copytree(source_folder, destination_folder, dirs_exist_ok=True) except Exception as e: print("shutil.copytree(source_folder, destination_folder, dirs_exist_ok=True): "+ item + ":", e) try: shutil.copytree(source_folder, os.path.join('../source/_posts', item[:-3]), dirs_exist_ok=True) except Exception as e: print("shutil.copytree(source_folder, '../source/_posts'): " + item + ":", e) print("完成!")
print("后处理其它文件……") for item in tqdm(os.listdir('../temp')): if item != '_posts' and item not in ignore_folder: item_path = os.path.join('../temp', item) if os.path.isdir(item_path): try: shutil.copytree(item_path, os.path.join('../public', item), dirs_exist_ok=True) except Exception as e: print(os.path.join('../public', item) + ":", e) try: shutil.copytree(item_path, os.path.join('../source', item), dirs_exist_ok=True) except Exception as e: print(os.path.join('../source', item) + ":", e)
if os.path.isdir('../temp'): shutil.rmtree('../temp') print("完成!")
|