echodict/typst_hlm
01k
1 2# see huggingface_echodict\typst-app-clone\public\chapters\ch01.typ 给它排版,校对3 4# 自动生成 typst 章节,放 auto_chapters 目录下5 6# see exinfo.py 先生成 .boxsnew ,再手动改 space is_menu is_menutitle 属性7 8if __name__ == '__main__':9 10 import glob11 import os12 import json13 import numpy as np14 from pathlib import Path15 import opencc16 import copy17 from collections import defaultdict, OrderedDict18 s2t = opencc.OpenCC('s2t.json') # 简繁转换19 t2s = opencc.OpenCC('t2s.json')20 21 chs = glob.glob('data/ch*', recursive=False)22 23 menus = []24 pages_all = []25 for i, pth in enumerate(chs):26 pth = pth.replace('\\', '/')27 parts = pth.split('/')28 chapter = parts[1]29 30 out2 = f'data/{chapter}/out2'31 32 33 with open(f"{out2}/page_texts.json", "r", encoding='UTF-8') as fp:34 page_texts = json.load(fp)35 36 with open(f"{out2}/pageIds_pageNames.json", "r", encoding='UTF-8') as fp:37 pageIds_pageNames = json.load(fp)38 39 pageNames_pageIds = {}40 for k, v in pageIds_pageNames.items():41 pageNames_pageIds[v] = k42 43 44 boxs = glob.glob(f"{out2}/*.boxsnew", recursive=False)45 46 47 menu = ''48 menu_title = ''49 50 pages = []51 52 for j, pt in enumerate(boxs):53 54 pageName = Path(pt).stem55 pageId = pageNames_pageIds[pageName]56 57 with open(pt, "r", encoding='UTF-8') as fp:58 box = json.load(fp)59 60 page_text = ''61 62 for k, bx in enumerate(box):63 charPoly = bx["charPoly"]64 charText = bx["charText"]65 sign = bx["sign"]66 space = bx["space"]67 is_menu = bx["is_menu"]68 is_menutitle = bx["is_menutitle"]69 70 if is_menu:71 menu = menu + charText + sign72 if is_menutitle:73 menu_title = menu_title + charText + sign74 75 if menu_title != '' and not is_menutitle:76 77 if len(pages) > 0:78 menus[-1]["pages"] = copy.deepcopy(pages)79 pages = []80 81 menus.append({ "menu": menu, "menu_title": menu_title })82 menu = ''83 menu_title = ''84 85 # if not is_menu and not is_menutitle:86 # page_text = page_text + charText + sign87 page_text = page_text + charText + sign88 pass89 90 pages.append({"pageName":pageName, "pageId":pageId, "chapter":chapter, "page_text": page_text, "box": box })91 pages_all.append({"pageName":pageName, "pageId":pageId, "chapter":chapter, "page_text": page_text, "box": box })92 93 pass94 95 # 不存在就写96 pth_pages_all = 'data/pages_all.json'97 if not os.path.exists(pth_pages_all):98 with open(pth_pages_all, 'w', encoding='utf-8') as fp:99 json.dump(pages_all, fp, indent=4, ensure_ascii=False)100 101 # 存在就读 pages_all.json ,生成 typst 章节102 if os.path.exists(pth_pages_all):103 with open(pth_pages_all, "r", encoding='UTF-8') as fp:104 pages_all = json.load(fp)105 106 107 dir_autochapters = 'auto_chapters'108 if not os.path.exists(dir_autochapters):109 os.makedirs(dir_autochapters)110 111 # py3.7+ 字典默认就是有序的112 grouped = defaultdict(list)113 for item in pages_all:114 grouped[item.get("chapter")].append(item)115 116 for ch, pages in grouped.items():117 pt_ch = f'{dir_autochapters}/{ch}.typ'118 menu = ''119 menu_title = ''120 page_text = ''121 ch_text = """#import "../../../lib.typ": *\n#heading(level: 1, outlined: true)"""122 menu_linkChar = ''123 menu_title_linkChar = ''124 for i, page in enumerate(pages):125 box = page["box"]126 pageName = page['pageName']127 for k, bx in enumerate(box):128 charPoly = bx["charPoly"]129 charText = bx["charText"]130 sign = bx["sign"]131 space = ' ' if bx["space"] else ''132 space_n = ' ' if bx["space"] else ''133 is_menu = bx["is_menu"]134 is_menutitle = bx["is_menutitle"]135 x0, y0, x1, y1, x2, y2, x3, y3 = charPoly['x0'], charPoly['y0'], charPoly['x1'], charPoly['y1'], charPoly['x2'], charPoly['y2'], charPoly['x3'], charPoly['y3']136 linkChar = f"""#c("{charText}", "{pageName},{x0},{y0},{x1},{y1},{x2},{y2},{x3},{y3}")"""137 if is_menu:138 menu = menu + charText + space_n139 menu_linkChar = menu_linkChar + linkChar + space140 if is_menutitle:141 menu_title = menu_title + charText + space_n142 menu_title_linkChar = menu_title_linkChar + linkChar + space143 144 if menu_title != '' and not is_menutitle:145 ch_text = ch_text + f"""[{menu} {menu_title}]\n #v(0.1em){menu_linkChar}#v(18em)\n {menu_title_linkChar}#v(18em)\n"""146 menu = ''147 menu_title = ''148 menu_linkChar = ''149 menu_title_linkChar = ''150 151 if not is_menu and not is_menutitle:152 page_text = page_text + linkChar + space + sign153 154 page_text += '\n'155 156 157 ch_text += page_text158 159 with open(pt_ch, 'w', encoding='utf-8') as f:160 f.write(ch_text)161 162 print(f"{ch} / {len(grouped)}")163 164 pass165 166 