-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosis2usfm.py
301 lines (269 loc) · 7.78 KB
/
osis2usfm.py
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
# -*- coding: utf-8 -*-
import xml.sax
import codecs
import sys
import re
reload(sys)
#sys.setdefaultencoding('iso-8859-1')
sys.setdefaultencoding('utf-8')
del sys.setdefaultencoding
EMIT_TRAILING_NEWLINE = True
NO_TRAILING_NEWLINE = False
UBS_book_ids = [
U"GEN",
U"EXO",
U"LEV",
U"NUM",
U"DEU",
U"JOS",
U"JDG",
U"RUT",
U"1SA",
U"2SA",
U"1KI",
U"2KI",
U"1CH",
U"2CH",
U"EZR",
U"NEH",
U"EST",
U"JOB",
U"PSA",
U"PRO",
U"ECC",
U"SNG",
U"ISA",
U"JER",
U"LAM",
U"EZK",
U"DAN",
U"HOS",
U"JOL",
U"AMO",
U"OBA",
U"JON",
U"MIC",
U"NAM",
U"HAB",
U"ZEP",
U"HAG",
U"ZEC",
U"MAL",
U"MAT",
U"MRK",
U"LUK",
U"JHN",
U"ACT",
U"ROM",
U"1CO",
U"2CO",
U"GAL",
U"EPH",
U"PHP",
U"COL",
U"1TH",
U"2TH",
U"1TI",
U"2TI",
U"TIT",
U"PHM",
U"HEB",
U"JAS",
U"1PE",
U"2PE",
U"1JN",
U"2JN",
U"3JN",
U"JUD",
U"REV",
]
def usage():
sys.stderr.write("Usage:\n python osis2usfm.py <OSIS-infilename.xml>\n\n")
def mangle_XML_entities(s):
r = s.replace("&", "&")
r = r.replace("<", "<")
r = r.replace(">", ">")
r = r.replace("\"", """)
return r
class OSIS2USFMHandler(xml.sax.ContentHandler):
def __init__(self):
self.elemstack = []
self.charstack = []
self.nixing_elements = set(['header', 'work', 'date', 'type', 'identifier', 'language', 'rights', 'refSystem', 'publisher'])
self.nixing_stack = []
self.books = []
self.book_number = -1
self.bInBible = False
def startDocument(self):
pass
def endDocument(self):
pass
def characters(self, data):
self.charstack.append(data)
def handle_chars(self, chars):
if len(self.nixing_stack) != 0:
pass
else:
self.addString(chars)
def addString(self, s):
outs = s.replace(u"\n", u" ")
if len(outs.strip()) == 0:
pass
else:
self.emit(outs)
def emit(self, outs):
if self.bInBible:
self.books[-1].append(outs)
def emitTag(self, tag, content, bNewlineAfter):
if len(self.books[-1]) == 1:
newlineBefore = u""
elif len(self.books[-1][-1]) == 0:
newlineBefore = u""
elif self.books[-1][-1][-1] == u"\n":
newlineBefore = u""
else:
newlineBefore = u"\n"
if bNewlineAfter:
newlineAfter = u"\n"
else:
newlineAfter = u""
if content == u"":
self.emit(u"%s\\%s%s" % (newlineBefore, tag, newlineAfter))
else:
self.emit(u"%s\\%s %s%s" % (newlineBefore, tag, content, newlineAfter))
def startElement(self, tag, attributes):
self.elemstack.append(tag)
chars = "".join(self.charstack)
del self.charstack
self.charstack = []
self.handle_chars(chars)
if tag == "verse":
if attributes.has_key("eID"):
pass
else:
osisID = attributes["osisID"]
arr = osisID.split(".")
verse = arr[2]
self.curverse = verse
chapter = arr[1]
self.emitTag(u"v", u"%s " % self.curverse, NO_TRAILING_NEWLINE)
elif tag == "reference":
pass
elif tag == "p":
self.emitTag(u"p", u" ", NO_TRAILING_NEWLINE)
elif tag == "chapter":
if attributes.has_key("eID"):
pass
else:
osisID = attributes["osisID"]
arr = osisID.split(".")
chapter = arr[-1]
self.curchapter = chapter
self.emitTag(u"c", self.curchapter, EMIT_TRAILING_NEWLINE)
self.mydivtype = "chapter"
elif tag == "note":
self.emit(u"\\f + \\fr %s,%s \\ft " % (self.curchapter, self.curverse))
elif tag == "div":
self.mydivtype = attributes["type"]
if self.mydivtype == u"book":
self.osisBookID = attributes[u"osisID"]
if self.osisBookID == "Gen":
self.book_number = 0
elif self.osisBookID == "Matt":
self.book_number = 39
else:
self.book_number += 1
UBS_id = UBS_book_ids[self.book_number]
self.UBS_number = self.book_number + 1
self.UBS_id = UBS_id
self.books.append([UBS_id])
self.bInBible = True
self.emitTag(u"id", self.UBS_id, EMIT_TRAILING_NEWLINE)
elif tag == "title":
if len(self.nixing_stack) != 0:
pass
else:
if self.mydivtype == "bookGroup":
pass
elif self.mydivtype == "book":
self.emitTag(u"mt", u" ", NO_TRAILING_NEWLINE)
elif self.mydivtype == "chapter":
self.emitTag(u"s", u" ", NO_TRAILING_NEWLINE)
else:
raise Exception((u"Unknown mydivtype: '%s'" % self.mydivtype).encode('utf-8'))
elif tag in self.nixing_elements:
self.nixing_stack.append(tag)
elif tag == "hi":
self.emit(u"\\it ")
elif tag == "osis":
pass
elif tag == "osisText":
pass
else:
raise Exception((u"Unknown start-tag <%s> with attributes %s" % (tag, str(attributes))).encode('utf-8'))
def endElement(self, tag):
poppedelem = self.elemstack.pop()
chars = "".join(self.charstack)
del self.charstack
self.charstack = []
self.handle_chars(chars)
if tag == "verse":
pass
elif tag == "reference":
pass
elif tag == "p":
pass
elif tag == "chapter":
pass
elif tag == "note":
self.emit(u"\\ft*\\f* ")
elif tag == "div":
pass
elif tag == "title":
if len(self.nixing_stack) != 0:
pass
else:
if self.mydivtype == "chapter":
self.emit(u"\n")
elif self.mydivtype == "book":
self.emit(u"\n")
else:
self.emit(u"\n")
self.bHasJustSeenTitle = True
elif tag in self.nixing_elements:
assert self.nixing_stack.pop() == tag
elif tag == "hi":
self.emit(u"\\it*")
elif tag == "osis":
pass
elif tag == "osisText":
pass
else:
raise Exception((u"Unknown end-tag </%s>" % tag).encode('utf-8'))
def transform_USFM(self, udoc):
udoc = re.sub(ur'(\\v \d+ )\s*', ur'\1', udoc)
udoc = re.sub(ur'(\\mt )\s*', ur'\1', udoc)
udoc = re.sub(ur'(\\s )\s*', ur'\1', udoc)
#udoc = re.sub(ur'', ur'', udoc)
return udoc
def dumpUSFM(self):
first_UBS_id = self.books[0][0]
offset = UBS_book_ids.index(first_UBS_id) + 1
for index in xrange(0, len(self.books)):
UBS_id = self.books[index][0]
filename = "USFM/%02d%s.SFM" % (index + offset, UBS_id)
sys.stderr.write("Now dumping: %s...\n" % filename)
udoc = u"".join(self.books[index][1:])
udoc = self.transform_USFM(udoc)
fout = open(filename, "wb")
fout.write(udoc.encode('utf-8'))
fout.close()
if len(sys.argv) > 1:
infilename = sys.argv[1]
else:
usage()
sys.exit(1)
fin = open(infilename, "r")
handler = OSIS2USFMHandler()
xml.sax.parse(fin, handler)
handler.dumpUSFM()