File size: 5,742 Bytes
cac436a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# 原先是 typst_hlm 给 huggingface_echodict\typst-app-clone\public\chapters\ch01.typ 它排版,校对

# 自动生成 typst 章节,放 auto_chapters 目录下

# see exinfo.py 先生成 .boxsnew ,再手动改 space is_menu is_menutitle 属性

if __name__ == '__main__':

    import glob
    import os
    import json
    import numpy as np
    from pathlib import Path
    import opencc
    import copy
    from collections import defaultdict, OrderedDict
    s2t = opencc.OpenCC('s2t.json')  # 简繁转换
    t2s = opencc.OpenCC('t2s.json')

    chs = glob.glob('data/ch*', recursive=False)

    menus = []
    pages_all = []
    for i, pth in enumerate(chs):
        pth = pth.replace('\\', '/')
        parts = pth.split('/')
        chapter = parts[1]

        chapter_path = glob.glob(f'data/{chapter}/*', recursive=False)[0]
        
        out2 = f'{chapter_path}/out2'

        if not os.path.exists(out2):
            continue

        with open(f"{out2}/page_texts.json", "r", encoding='UTF-8') as fp:
            page_texts = json.load(fp)

        with open(f"{out2}/pageIds_pageNames.json", "r", encoding='UTF-8') as fp:
            pageIds_pageNames = json.load(fp)
        
        pageNames_pageIds = {}
        for k, v in pageIds_pageNames.items():
            pageNames_pageIds[v] = k
        

        boxs = glob.glob(f"{out2}/*.boxsnew", recursive=False)

        
        menu = ''
        menu_title = ''

        pages = []

        for j, pt in enumerate(boxs):

            pageName = Path(pt).stem
            pageId = pageNames_pageIds[pageName]

            with open(pt, "r", encoding='UTF-8') as fp:
                box = json.load(fp)

            page_text = ''

            for k, bx in enumerate(box):
                charPoly = bx["charPoly"]
                charText = bx["charText"]
                sign = bx["sign"]
                space = bx["space"]
                is_menu = bx["is_menu"]
                is_menutitle = bx["is_menutitle"]

                if is_menu:
                    menu = menu + charText + sign
                if is_menutitle:
                    menu_title = menu_title + charText + sign
                
                if menu_title != '' and not is_menutitle:
                    
                    if len(pages) > 0:
                        menus[-1]["pages"] = copy.deepcopy(pages)
                        pages = []

                    menus.append({ "menu": menu, "menu_title": menu_title })
                    menu = ''
                    menu_title = ''

                # if not is_menu and not is_menutitle:
                #     page_text = page_text + charText + sign
                page_text = page_text + charText + sign
                pass

            pages.append({"pageName":pageName, "pageId":pageId,  "chapter":chapter, "page_text": page_text, "box": box })
            pages_all.append({"pageName":pageName, "pageId":pageId,  "chapter":chapter, "page_text": page_text, "box": box })

            pass

    # 不存在就写
    pth_pages_all = 'data/pages_all.json'
    if not os.path.exists(pth_pages_all):
        with open(pth_pages_all, 'w', encoding='utf-8') as fp:
            json.dump(pages_all, fp, indent=4, ensure_ascii=False)

    # 存在就读 pages_all.json ,生成 typst 章节
    if os.path.exists(pth_pages_all):
        with open(pth_pages_all, "r", encoding='UTF-8') as fp:
            pages_all = json.load(fp)


    dir_autochapters = 'auto_chapters'
    if not os.path.exists(dir_autochapters):
        os.makedirs(dir_autochapters)

    # py3.7+ 字典默认就是有序的
    grouped = defaultdict(list)
    for item in pages_all:
        grouped[item.get("chapter")].append(item)

    for ch, pages in grouped.items():
        pt_ch = f'{dir_autochapters}/{ch}.typ'
        menu = ''
        menu_title = ''
        page_text = ''
        ch_text = """#import "../lib.typ": *\n#heading(level: 1, outlined: true)"""
        menu_linkChar = ''
        menu_title_linkChar = ''
        for i, page in enumerate(pages):
            box = page["box"]
            pageName = page['pageName']
            for k, bx in enumerate(box):
                charPoly = bx["charPoly"]
                charText = bx["charText"]
                sign = bx["sign"]
                space = ' ' if bx["space"] else ''
                space_n = ' ' if bx["space"] else ''
                is_menu = bx["is_menu"]
                is_menutitle = bx["is_menutitle"]
                x0, y0, x1, y1, x2, y2, x3, y3 = charPoly['x0'], charPoly['y0'], charPoly['x1'], charPoly['y1'], charPoly['x2'], charPoly['y2'], charPoly['x3'], charPoly['y3']
                linkChar = f"""#c("{charText}", "{pageName},{x0},{y0},{x1},{y1},{x2},{y2},{x3},{y3}")"""
                if is_menu:
                    menu = menu + charText + space_n
                    menu_linkChar = menu_linkChar + linkChar + space
                if is_menutitle:
                    menu_title = menu_title + charText + space_n
                    menu_title_linkChar = menu_title_linkChar + linkChar + space

                if menu_title != '' and not is_menutitle:
                    ch_text = ch_text + f"""[{menu} {menu_title}]\n #v(0.1em){menu_linkChar}#v(18em)\n {menu_title_linkChar}#v(18em)\n"""
                    menu = ''
                    menu_title = ''
                    menu_linkChar = ''
                    menu_title_linkChar = ''

                if not is_menu and not is_menutitle:
                    page_text = page_text + linkChar + space + sign
            
            page_text += '\n'

        
        ch_text += page_text

        with open(pt_ch, 'w', encoding='utf-8') as f:
            f.write(ch_text)

    pass