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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| #!/usr/bin/env python3
""" Hexo 博客字体压缩脚本 - 动态完整版 扫描汉字、英文、数字、标点 """
import os import re import sys import yaml import json
def extract_chars_from_file(filepath, skip_comments=False): """从文件提取所有字符(汉字、英文、数字、标点)""" try: with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() if skip_comments: lines = content.split('\n') content = '\n'.join([line for line in lines if not line.strip().startswith('#')]) chars = set() chars.update(re.findall(r'[\u4e00-\u9fa5]', content)) chars.update(re.findall(r'[a-zA-Z]', content)) chars.update(re.findall(r'[0-9]', content)) chars.update(re.findall(r'[,。!?、;:""''()《》【】…—~·|「」『』【】]', content)) chars.update(re.findall(r'[.,!?;:\'\"()\[\]{}@#$%^&*+=_\-\\/<>~`]', content)) return chars except Exception as e: print(f"读取文件失败 {filepath}: {e}") return set()
def extract_chars_from_json_yaml(filepath): """从 JSON/YAML 文件递归提取所有字符串中的字符""" chars = set() try: with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() # 尝试解析为 JSON 或 YAML data = None try: data = json.loads(content) except: try: data = yaml.safe_load(content) except: pass def extract_strings(obj): if isinstance(obj, str): result = set() # 汉字 result.update(re.findall(r'[\u4e00-\u9fa5]', obj)) # 英文 result.update(re.findall(r'[a-zA-Z]', obj)) # 数字 result.update(re.findall(r'[0-9]', obj)) # 标点 result.update(re.findall(r'[,。!?、;:""''()《》【】…—~·|「」『』【】.,!?;:\'\"()\[\]{}@#$%^&*+=_\-\\/<>~`]', obj)) return result elif isinstance(obj, list): result = set() for item in obj: result.update(extract_strings(item)) return result elif isinstance(obj, dict): result = set() for key, value in obj.items(): result.update(extract_strings(key)) result.update(extract_strings(value)) return result return set() if data: chars.update(extract_strings(data)) else: # 解析失败,直接正则提取 chars.update(extract_chars_from_file(filepath)) except Exception as e: print(f"读取失败 {filepath}: {e}") return chars
def extract_title_from_md(filepath): """只提取 Markdown 文件的 Front-matter title""" try: with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() # 匹配 Front-matter fm_match = re.match(r'^---\s*\n(.*?)\n---', content, re.DOTALL) title = '' if fm_match: try: fm = yaml.safe_load(fm_match.group(1)) title = str(fm.get('title', '')) except: pass # 备用:正则提取 title: xxx if not title: title_match = re.search(r'^title:\s*(.+)$', content, re.MULTILINE) if title_match: title = title_match.group(1).strip().strip('"\'') # 提取所有字符 chars = set() chars.update(re.findall(r'[\u4e00-\u9fa5]', title)) chars.update(re.findall(r'[a-zA-Z]', title)) chars.update(re.findall(r'[0-9]', title)) chars.update(re.findall(r'[,。!?、;:""''()《》【】…—~·|「」『』【】.,!?;:\'\"()\[\]{}@#$%^&*+=_\-\\/<>~`]', title)) return chars except Exception as e: print(f"读取失败 {filepath}: {e}") return set()
def get_dynamic_fixed_chars(hexo_root): """从配置文件动态读取固定字符""" chars = set() # 1. 从站点配置 _config.yml 读取 site_config = os.path.join(hexo_root, '_config.yml') if os.path.exists(site_config): print(f" 读取站点配置: _config.yml") config_chars = extract_chars_from_file(site_config, skip_comments=True) chars.update(config_chars) print(f" -> {len(config_chars)} 字符") # 2. 从主题配置 _config.anzhiyu.yml 读取(过滤注释) theme_config = os.path.join(hexo_root, '_config.anzhiyu.yml') if os.path.exists(theme_config): print(f" 读取主题配置: _config.anzhiyu.yml") config_chars = extract_chars_from_file(theme_config, skip_comments=True) chars.update(config_chars) print(f" -> {len(config_chars)} 字符") # 3. 从主题语言文件读取 theme_lang_dir = os.path.join(hexo_root, 'themes', 'anzhiyu', 'languages') if os.path.exists(theme_lang_dir): print(f" 读取主题语言文件:") for lang_file in os.listdir(theme_lang_dir): if lang_file.endswith(('.yml', '.yaml')): filepath = os.path.join(theme_lang_dir, lang_file) lang_chars = extract_chars_from_file(filepath, skip_comments=True) if lang_chars: chars.update(lang_chars) print(f" {lang_file} -> {len(lang_chars)} 字符") return chars
def scan_directory_full(directory, chars_set, skip_dirs=None, depth=0): """递归扫描目录下所有文件""" if skip_dirs is None: skip_dirs = ['node_modules', '.git', 'images', 'img', 'fonts', 'css', 'js'] if not os.path.exists(directory): return 0, 0 file_count = 0 total_chars = 0 prefix = " " * depth for root, dirs, files in os.walk(directory): dirs[:] = [d for d in dirs if d not in skip_dirs and not d.startswith('.')] for file in files: filepath = os.path.join(root, file) rel_path = os.path.relpath(filepath, directory) file_chars = set() if file.endswith('.md'): file_chars = extract_chars_from_file(filepath) elif file.endswith(('.json', '.yml', '.yaml')): file_chars = extract_chars_from_json_yaml(filepath) elif file.endswith(('.html', '.htm', '.txt')): file_chars = extract_chars_from_file(filepath) if file_chars: chars_set.update(file_chars) file_count += 1 total_chars += len(file_chars) print(f"{prefix} {rel_path} -> {len(file_chars)} 字符") return file_count, total_chars
def generate_char_file(output_path, hexo_root): """生成字符文件""" print("=" * 60) print("Hexo 博客字体压缩工具 - 动态完整版") print("=" * 60) chars = set() # 1. 文章目录:只扫描标题 posts_dir = os.path.join(hexo_root, 'source', '_posts') if os.path.exists(posts_dir): print(f"\n📁 扫描文章标题(仅title): {posts_dir}") count = 0 for root, dirs, files in os.walk(posts_dir): dirs[:] = [d for d in dirs if d not in ['node_modules', '.git']] for file in files: if file.endswith('.md'): filepath = os.path.join(root, file) title_chars = extract_title_from_md(filepath) chars.update(title_chars) count += 1 print(f" 扫描了 {count} 篇文章,提取 {len(chars)} 个独特字符") # 2. 其他页面目录:全内容扫描 source_dir = os.path.join(hexo_root, 'source') if os.path.exists(source_dir): print(f"\n📁 扫描页面目录(全内容递归):") for item in os.listdir(source_dir): if item in ['_posts', 'images', 'img', 'fonts', 'css', 'js']: continue item_path = os.path.join(source_dir, item) if os.path.isdir(item_path): print(f"\n 📂 {item}/") file_count, total_chars = scan_directory_full(item_path, chars, depth=1) print(f" 合计: {file_count} 个文件,{total_chars} 字符") # 3. 扫描 _data 目录 data_dir = os.path.join(hexo_root, 'source', '_data') if os.path.exists(data_dir): print(f"\n📁 扫描数据目录(_data):") file_count, total_chars = scan_directory_full(data_dir, chars, depth=1) print(f" 合计: {file_count} 个文件,{total_chars} 字符") # 4. 动态读取配置文件 print(f"\n📁 动态读取配置文件:") fixed_chars = get_dynamic_fixed_chars(hexo_root) chars.update(fixed_chars) print(f" 配置文件共提取: {len(fixed_chars)} 字符") # 5. 排序并保存 # 按字符类型排序:汉字 -> 英文 -> 数字 -> 标点 hanzi = sorted([c for c in chars if '\u4e00' <= c <= '\u9fa5']) english = sorted([c for c in chars if c.isalpha()]) digits = sorted([c for c in chars if c.isdigit()]) others = sorted([c for c in chars if not ('\u4e00' <= c <= '\u9fa5' or c.isalnum())]) sorted_chars = ''.join(hanzi + english + digits + others) with open(output_path, 'w', encoding='utf-8') as f: f.write(sorted_chars) print(f"\n" + "=" * 60) print(f"✅ 共收集 {len(chars)} 个独特字符") print(f" - 汉字: {len(hanzi)} 个") print(f" - 英文: {len(english)} 个") print(f" - 数字: {len(digits)} 个") print(f" - 标点/其他: {len(others)} 个") print(f"💾 已保存到: {output_path}") preview = sorted_chars[:100] print(f"\n📋 字符预览: {preview}...") return sorted_chars
def subset_font(input_font, output_font, chars_file): """使用 fonttools 子集化字体""" try: from fontTools.subset import main as subset_main except ImportError: print("\n❌ 缺少 fonttools,正在安装...") os.system(f"{sys.executable} -m pip install fonttools brotli zopfli") from fontTools.subset import main as subset_main print(f"\n🔧 开始压缩字体...") print(f" 输入: {input_font}") print(f" 输出: {output_font}") args = [ input_font, f"--text-file={chars_file}", f"--output-file={output_font}", "--flavor=woff2", "--desubroutinize", "--recalc-bounds", "--canonical-order", "--layout-features=*", ] ttf_output = output_font.replace('.woff2', '.ttf') args_ttf = [ input_font, f"--text-file={chars_file}", f"--output-file={ttf_output}", "--desubroutinize", "--recalc-bounds", ] try: subset_main(args) subset_main(args_ttf) original_size = os.path.getsize(input_font) woff2_size = os.path.getsize(output_font) ttf_size = os.path.getsize(ttf_output) print(f"\n✅ 压缩完成!") print(f" 原始大小: {original_size/1024:.2f} KB") print(f" WOFF2 大小: {woff2_size/1024:.2f} KB (压缩率: {woff2_size/original_size*100:.1f}%)") print(f" TTF 大小: {ttf_size/1024:.2f} KB (压缩率: {ttf_size/original_size*100:.1f}%)") return True except Exception as e: print(f"\n❌ 压缩失败: {e}") return False
def main(): """主函数""" HEXO_ROOT = "/www/wwwroot/myblog" WORK_DIR = "/www/wwwroot/myblog/font-spider-work" INPUT_FONT = os.path.join(WORK_DIR, "fonts", "BailuFeiYun.ttf") CHARS_FILE = os.path.join(WORK_DIR, "chars-full.txt") OUTPUT_WOFF2 = os.path.join(HEXO_ROOT, "source", "fonts", "BailuFeiYun.woff2") if not os.path.exists(INPUT_FONT): print(f"❌ 未找到原始字体: {INPUT_FONT}") sys.exit(1) os.makedirs(os.path.dirname(OUTPUT_WOFF2), exist_ok=True) chars = generate_char_file(CHARS_FILE, HEXO_ROOT) if len(chars) < 50: print("⚠️ 提取字符过少,请检查路径") sys.exit(1) success = subset_font(INPUT_FONT, OUTPUT_WOFF2, CHARS_FILE) if success: print(f"\n🎉 全部完成!") print(f" 压缩后的字体已输出到: {os.path.dirname(OUTPUT_WOFF2)}") else: sys.exit(1)
if __name__ == "__main__": main()
|