Convert usfm bibles into gemtext (python library/utility)
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
#!/usr/bin/env python
"""Convert a usx file into gemtext"""
__author__ = "Zach DeCook"
__email__ = "zachdecook@librem.one"
__copyright__ = "Copyright (C) 2021 Zach DeCook"
__license__ = "AGPL"
__version__ = "3"

import sys
import xml.etree.ElementTree as ET

def printf(string):
    print(string,end='')

def main(argv):
    root = ET.parse(argv[1]).getroot()
    for thing in list(root):
        printf(convertBlock(thing))

def convertBlock(thing):
    if thing.tag == 'para':
        # TODO: use style attribute to affect the prefix.
        return "\n" + convertInline(thing)
    return ''

def convertInline(thing):
    # TODO: Implement this.
    out = ''
    # TODO: move note after the next word/phrase?
    if thing.tag == 'note':
      out += '['
    if thing.text:
        out = thing.text.replace("\n"," ")
    for t in thing:
        out += convertInline(t)
        if t.tail:
          out += t.tail.replace("\n"," ")
    if thing.tag == 'note':
      out += ']'

    return out

if __name__ == '__main__':
    main(sys.argv)