aboutsummaryrefslogtreecommitdiff
path: root/scripts/generate_c_data.py
blob: 3afad0e3d064b88e292f2ed887032a23839ed929 (plain)
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
#!/usr/bin/env python3
"""
Generate src/pms_data.c from data/pms.tsv and data/pms_notes.tsv.
"""

import os

REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TSV_PATH = os.path.join(REPO_DIR, "data", "pms.tsv")
NOTES_TSV_PATH = os.path.join(REPO_DIR, "data", "pms_notes.tsv")
OUT_C_PATH = os.path.join(REPO_DIR, "src", "pms_data.c")

BOOK_ALIASES = {
    1: "gn,gen,genesis",
    2: "ex,exo,exodo",
    3: "lv,lev,levitico",
    4: "nm,num,numeros",
    5: "dt,deut,deuteronomio",
    6: "js,jos,josue",
    7: "jz,juiz,juizes",
    8: "rt,rut,rute",
    9: "1rs,1reis,ireis,1sm,1sam,1samuel,isamuel",
    10: "2rs,2reis,iireis,2sm,2sam,2samuel,iisamuel",
    11: "3rs,3reis,iiireis",
    12: "4rs,4reis,ivreis",
    13: "1par,1paralipomenos,iparalipomenos,1cr,1cron,1cronicas,icronicas",
    14: "2par,2paralipomenos,iiparalipomenos,2cr,2cron,2cronicas,iicronicas",
    15: "1esd,1esdras,iesdras,esd,esdras",
    16: "2esd,2esdras,iiesdras,ne,nee,neemias",
    17: "tb,tob,tobias",
    18: "jt,jdt,judit,judite",
    19: "est,ester",
    20: "job,jo",
    21: "sl,sal,salmo,salmos,ps,psalmos",
    22: "pr,prov,proverbios",
    23: "ecl,eclesiastes,qo,coelet",
    24: "ct,cant,cantico,canticos,canticodoscanticos",
    25: "sb,sab,sabedoria",
    26: "eclo,eclesiastico,sir,siracide,bensira",
    27: "is,isa,isaias",
    28: "jr,jer,jeremias",
    29: "lm,lam,lamentacoes",
    30: "br,bar,baruc",
    31: "ez,eze,ezeq,ezequiel",
    32: "dn,dan,daniel",
    33: "os,ose,oseas,oseias",
    34: "jl,joel",
    35: "am,amos",
    36: "abd,ab,abdias,ob,obadias",
    37: "jn,jon,jonas",
    38: "mq,miq,miqueas,miqueias",
    39: "na,nau,naum",
    40: "hab,hc,habacuc,habacuque",
    41: "sf,sof,sofonias",
    42: "ag,ageu",
    43: "zc,zac,zacarias",
    44: "ml,mal,malaquias",
    45: "1mc,1mac,1macabeus,imacabeus",
    46: "2mc,2mac,2macabeus,iimacabeus",
    47: "mt,mat,mateus,smateus,saomateus",
    48: "mc,mar,marcos,smarcos,saomarcos",
    49: "lc,luc,lucas,slucas,saolucas",
    50: "jo,joao,sjoao,saojoao",
    51: "at,act,atos,actos,actosdosapostolos,atosdosapostolos",
    52: "rm,rom,romanos",
    53: "1cor,1co,1corintios,icorintios",
    54: "2cor,2co,2corintios,iicorintios",
    55: "gl,gal,galatas",
    56: "ef,efe,efesios",
    57: "fp,flp,fil,filipenses",
    58: "cl,col,colossenses",
    59: "1ts,1tes,1tessalonicenses,itessalonicenses",
    60: "2ts,2tes,2tessalonicenses,iitessalonicenses",
    61: "1tm,1tim,1timoteo,itimoteo",
    62: "2tm,2tim,2timoteo,iitimoteo",
    63: "tt,tit,tito",
    64: "fm,flm,filemon",
    65: "hb,heb,hebreus",
    66: "tg,tiago,stiago,saotiago",
    67: "1pd,1pe,1ped,1pedro,1spedro,ipedro",
    68: "2pd,2pe,2ped,2pedro,2spedro,iipedro",
    69: "1jo,1joao,1sjoao,ijoao",
    70: "2jo,2joao,2sjoao,iijoao",
    71: "3jo,3joao,3sjoao,iiijoao",
    72: "jd,jud,judas,sjudas,saojudas",
    73: "ap,apoc,apocalipse",
}

def c_escape(s):
    return s.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n").replace("\r", "")

def main():
    books = {}
    verses = []
    with open(TSV_PATH, "r", encoding="utf-8") as f:
        for line in f:
            line = line.rstrip("\n")
            if not line:
                continue
            parts = line.split("\t")
            if len(parts) < 6:
                continue
            bname, babbr, bnum_s, cnum_s, vnum_s, vtext = parts[:6]
            bnum = int(bnum_s)
            cnum = int(cnum_s)
            vnum = int(vnum_s)
            if bnum not in books:
                books[bnum] = (bname, babbr)
            verses.append((bnum, cnum, vnum, vtext))

    notes = []
    if os.path.exists(NOTES_TSV_PATH):
        with open(NOTES_TSV_PATH, "r", encoding="utf-8") as f:
            for line in f:
                line = line.rstrip("\n")
                if not line:
                    continue
                parts = line.split("\t")
                if len(parts) < 6:
                    continue
                bnum_s, cnum_s, vs_s, ve_s, label, ntext = parts[:6]
                notes.append((int(bnum_s), int(cnum_s), int(vs_s), int(ve_s), label, ntext))

    with open(OUT_C_PATH, "w", encoding="utf-8") as out:
        out.write('/* Auto-generated from data/pms.tsv and data/pms_notes.tsv. Do not edit manually. */\n')
        out.write('#include "pms_data.h"\n\n')

        out.write("pms_book pms_books[] = {\n")
        for bnum in sorted(books.keys()):
            bname, babbr = books[bnum]
            aliases = BOOK_ALIASES.get(bnum, babbr.lower())
            out.write(f'    {{{bnum}, "{c_escape(bname)}", "{c_escape(babbr)}", "{c_escape(aliases)}"}},\n')
        out.write("};\n")
        out.write(f"int pms_books_length = {len(books)};\n\n")

        out.write("pms_verse pms_verses[] = {\n")
        for bnum, cnum, vnum, vtext in verses:
            out.write(f'    {{{bnum}, {cnum}, {vnum}, "{c_escape(vtext)}"}},\n')
        out.write("};\n")
        out.write(f"int pms_verses_length = {len(verses)};\n\n")

        out.write("pms_note pms_notes[] = {\n")
        for bnum, cnum, vs, ve, label, ntext in notes:
            out.write(f'    {{{bnum}, {cnum}, {vs}, {ve}, "{c_escape(label)}", "{c_escape(ntext)}"}},\n')
        out.write("};\n")
        out.write(f"int pms_notes_length = {len(notes)};\n")

    print(f"Generated {OUT_C_PATH} ({len(books)} books, {len(verses)} verses, {len(notes)} notes).")

if __name__ == "__main__":
    main()