summaryrefslogtreecommitdiffstats
path: root/generate_reference.py
blob: f8595dfc278ae970fdca6036c5c712030a7556b2 (plain) (blame)
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
#!/usr/bin/env python3

import collections
import logging
import re
import sys
import xml.sax

from xml.sax.saxutils import XMLFilterBase, XMLGenerator
logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
                    level=logging.DEBUG)

ENGL_BOOKS = ('Gen', 'Exod', 'Lev', 'Num', 'Deut', 'Josh', 'Judg', 'Ruth',
              '1Sam', '2Sam', '1Kgs', '2Kgs', '1Chr', '2Chr', 'Ezra', 'Neh',
              'Esth', 'Job', 'Ps', 'Prov', 'Eccl', 'Song', 'Isa', 'Jer', 'Lam',
              'Ezek', 'Dan', 'Hos', 'Joel', 'Amos', 'Obad', 'Jonah', 'Mic',
              'Nah', 'Hab', 'Zeph', 'Hag', 'Zech', 'Mal', 'Matt', 'Mark',
              'Luke', 'John', 'Acts', 'Rom', '1Cor', '2Cor', 'Gal', 'Eph',
              'Phil', 'Col', '1Thess', '2Thess', '1Tim', '2Tim', 'Titus',
              'Phlm', 'Heb', 'Jas', '1Pet', '2Pet', '1John', '2John', '3John',
              'Jude', 'Rev')

# 1Mak, 2Mak, 3Mak, 4Mak, ditto s Ma

TranslDict = collections.OrderedDict([
    ("Gn", "Gen"),
    ("Ex", "Exod"),
    ("Lv", "Lev"),
    ("Nu", "Num"),
    ("Dt", "Deut"),
    ("Joz", "Josh"),
    ("Sd", "Judg"),
    ("Rt", "Ruth"),
    ("1S", "1Sam"),
    ("2S", "2Sam"),
    ("1Kr", "1Kgs"),
    ("2Kr", "2Kgs"),
    ("1Pa", "1Chr"),
    ("2Pa", "2Chr"),
    ("Ezd", "Ezra"),
    ("Neh", "Neh"),
    ("Est", "Esth"),
    ("Jb", "Job"),
    ("Ž", "Ps"),
    ("Př", "Prov"),
    ("Kaz", "Eccl"),
    ("Pís", "Song"),
    ("Iz", "Isa"),
    ("Jr", "Jer"),
    ("Pl", "Lam"),
    ("Ez", "Ezek"),
    ("Da", "Dan"),
    ("Oz", "Hos"),
    ("Jl", "Joel"),
    ("Am", "Amos"),
    ("Obad", "Obad"),
    ("Jon", "Jonah"),
    ("Mi", "Mic"),
    ("Na", "Nah"),
    ("Abk", "Hab"),
    ("Sof", "Zeph"),
    ("Ag", "Hag"),
    ("Za", "Zech"),
    ("Mal", "Mal"),
    ("Mt", "Matt"),
    ("Mk", "Mark"),
    ("L", "Luke"),
    ("J", "John"),  # nebo Jn
    ("Sk", "Acts"),
    ("Ř", "Rom"),
    ("1K", "1Cor"),
    ("2K", "2Cor"),
    ("Ga", "Gal"),
    ("Ef", "Eph"),
    ("Fp", "Phil"),
    ("Ko", "Col"),
    ("1Te", "1Thess"),
    ("2Te", "2Thess"),
    ("1Tm", "1Tim"),  # nebo 1Ti
    ("2Tm", "2Tim"),
    ("Tt", "Titus"),
    ("Flm", "Phlm"),
    ("He", "Heb"),
    ("Jk", "Jas"),
    ("1Pt", "1Pet"),
    ("2Pt", "2Pet"),
    ("1J", "1John"),
    ("2J", "2John"),
    ("3J", "3John"),
    ("Jud", "Jude"),
    ("Zj", "Rev")])

CZ_BOOKS = tuple(TranslDict.keys())
EN_BOOKS = tuple(TranslDict.values())

BIBLE_REF_PARSE_RE = re.compile(r'(%s)?\s*([0-9]+),([0-9—.-]+)'
                                % '|'.join(CZ_BOOKS))
SPLIT_VERSE_RE = re.compile('[—.-]')

def parse_notes_test(instr):
    logging.debug('%s\ninstr = %s' % ('-' * 30, instr))
    matches = BIBLE_REF_PARSE_RE.findall(instr)
    logging.debug('matches = %s', matches)
    out_list = []

    for match in matches:
        logging.debug('match = %s', match)
        if match[0]:
            book = match[0]
        chapter = int(match[1])
        verse = match[2]
        if SPLIT_VERSE_RE.search(verse):
            verse_split = SPLIT_VERSE_RE.split(verse)
            verse = int(verse_split[0])
            next_verse = int(verse_split[1])
            logging.debug('verse, next_verse = %s, %s' % (verse, next_verse))
            out_list.append((book, chapter, verse, next_verse))
        else:
            out_list.append((book, chapter, int(verse)))
    return tuple(out_list)

class GenerateReferencesFilter(XMLFilterBase):
    def __init__(self, upstream, downstream):
        XMLFilterBase.__init__(self, upstream)
        self._downstream = downstream
        self._cur_book = None
        self._cur_chapter = 0
        self._cur_verse = 0
        self._in_note = True
        self._note_content = ""

#    def startDocument(self):
#        pass

#    def endDocument(self):
#        pass

    def startElement(self, name, attrs):
        if name == "verse" and 'sID' in attrs:
            ref_elements = attrs['sID'].split('.')
            self._cur_book, self._cur_chapter, self._cur_verse = \
                ref_elements[0], int(ref_elements[1]), int(ref_elements[2])
        elif name == "note":
            self._in_note = True
            self._note_content = ""
        self._downstream.startElement(name, attrs)

    def endElement(self, name):
        if name == 'note' and self._in_note:
            logging.debug('content:\n%s', self._note_content)
            self._in_note = False
            self._note_content = ""
        self._downstream.endElement(name)

    def characters(self, content):
        if self._in_note and len(content.strip()) > 0:
            self._note_content += content
        self._downstream.characters(content)


if __name__ == "__main__":
    downstream_handler = XMLGenerator(encoding="utf-8",
                                      short_empty_elements=True)
    parser = GenerateReferencesFilter(xml.sax.make_parser(),
                                      downstream_handler)
    parser.parse(sys.argv[1])