about summary refs log tree commit diff
path: root/usfm2gmi.py
diff options
context:
space:
mode:
Diffstat (limited to 'usfm2gmi.py')
-rwxr-xr-xusfm2gmi.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/usfm2gmi.py b/usfm2gmi.py
new file mode 100755
index 0000000..9b976ab
--- /dev/null
+++ b/usfm2gmi.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+import fileinput
+
+def printf(string):
+  print(string,end='')
+
+def convert(line):
+  """Convert a string to a list of tuples, each a token"""
+  split = line.split()
+  out = ''
+  if len(split) == 0:
+    return out
+  if split[0] == '\\s':
+    return '\n## ' + ' '.join(split[1:])
+  skip = 0
+  for word in split:
+    if skip > 0:
+      skip = skip - 1
+      continue
+    if word in ['\\v', '\\c']:
+      skip = 1
+    elif word == '\\p':
+      out += '\n'
+    else:
+      out += word + ' '
+  return out
+
+def main():
+  """Read usfm from stdin, output gemtext to stdout
+     ./usfm2gmi <in.usfm >out.md
+  """
+  for line in fileinput.input():
+    gmi = convert(line)
+    printf(gmi)
+
+if __name__ == '__main__':
+  main()