about summary refs log tree commit diff
path: root/oeb.py
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@librem.one>2021-04-13 07:39:55 -0400
committerZach DeCook <zachdecook@librem.one>2021-04-13 07:39:55 -0400
commitebf3e79809ff84b9b2803aa874ad6028391a1533 (patch)
tree17b7be374fd4eec434c4142d9ad25284591c1afe /oeb.py
parentc7d0ca4262db79c8bdbcbe20540f72dbf86c7a0b (diff)
downloadcgi-bin-ebf3e79809ff84b9b2803aa874ad6028391a1533.tar.gz
oeb: add python script
Diffstat (limited to 'oeb.py')
-rwxr-xr-xoeb.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/oeb.py b/oeb.py
new file mode 100755
index 0000000..ce4718c
--- /dev/null
+++ b/oeb.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+# pip3 install bible-passage-reference-parser
+from bible import parse_string
+import usfm2gmi.usfm2gmi as usfm2gmi
+import os
+import sys
+from subprocess import check_output as sco
+from webp import printf
+from webp import eprint
+
+def main(qs):
+  if not qs:
+    print("10 Enter a scripture reference\r\n")
+    return
+
+  passages = parse_string(qs)
+  # TODO: Also handle "john 3, tomato 2" errors?
+  if type(passages[0]) == tuple:
+    print("51 " + str(passages[0][0]) + "'\r\n")
+    return
+  print("20 text/gemini\r\n")
+  print('=> https://github.com/openenglishbible/Open-English-Bible This content from OpenEnglishBible (public domain)')
+  print('=> gemini://gemini.zachdecook.com/usfm2gmi/ and rendered with usfm2gmi')
+  for passage in passages:
+    printing = 0
+    book=passage.start.format('b').upper()
+    eprint(book)
+    fname=sco(['find', 'oeb/', '-iname', f'*-{book}*']).strip()
+    eprint(fname)
+    if not fname:
+      # TODO: throw 51?
+      print(f"{book} doesn't exist yet in the OEB\r\n")
+      continue
+    print("# " + passage.format())
+    f = open(fname)
+    startc = passage.start.chapter
+    startv= passage.start.verse
+    endc = passage.end.chapter
+    endv= passage.end.verse
+    endmark = passage.end.format('a c:v')
+    for line in f:
+      if f'\\c {startc}' == line.strip():
+        printing = 1
+      if printing == 1 and (f'\\v {startv} ') in line:
+        printing =2
+      if (f'\\c {endc}') == line.strip():
+        printing = 3
+      if printing == 3 and (f'\\v {endv+1} ') in line:
+        printing =0
+      if (f'\\c {endc+1}') in line.strip():
+        printing =0
+
+      if printing>=2:
+        printf(line)
+    print('(OEB)') #end in newline
+
+if __name__ == '__main__':
+  qs = os.getenv("QUERY_STRING") or (sys.argv[1] if len(sys.argv) >= 2 else '')
+  main(qs)