about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@librem.one>2026-06-03 06:38:10 -0400
committerZach DeCook <zachdecook@librem.one>2026-06-03 06:38:10 -0400
commitf82b41b820f559890c7f5132e21e8d746a8d1b1d (patch)
treec92773a4c33bd6cc0f053b73af86f265d74c269e
parent639aa4a8e434309f061649b4db5d7d99375b8a85 (diff)
downloadusfm2gmi-f82b41b820f559890c7f5132e21e8d746a8d1b1d.tar.gz
Handle \ref tag
-rwxr-xr-xusfm2gmi.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/usfm2gmi.py b/usfm2gmi.py
index 665706a..ce283fe 100755
--- a/usfm2gmi.py
+++ b/usfm2gmi.py
@@ -35,10 +35,12 @@ def superscript(word):
 def convert(line, printStrongs=False):
   """Convert a string to a list of tuples, each a token"""
   # TODO: preserve the lack of whitespace before a backslash.
-  split = line.replace('\\', ' \\').replace('\\nd*','\\nd* ').replace('\\+nd*','\\+nd* ').replace('\\f*','\\f* ').replace('\\wj*','\\wj* ').replace('\\w*',' \\w* ').replace('\\+w*', '\\+w* ').split()
+  split = line.replace('\\', ' \\').replace('\\nd*','\\nd* ').replace('\\+nd*','\\+nd* ').replace('\\f*','\\f* ').replace('\\wj*','\\wj* ').replace('\\w*',' \\w* ').replace('\\+w*', '\\+w* ').replace('\\ref*', '\\ref* ').split()
   out = ''
   nd = False
   superS = False
+  ref = False
+  yesprint = True
   if len(split) == 0:
     return out
   elif split[0] in ['\\mt1','\\mt','\\ms','\\h']:
@@ -106,6 +108,17 @@ def convert(line, printStrongs=False):
       continue
     elif word == '\\x*':
       out += ')'
+    # https://docs.usfm.bible/usfm/3.1.1/char/features/ref.html
+    elif word == '\\ref':
+      ref = True
+    elif word == '\\ref*':
+      ref = False
+      yesprint = True
+    elif ref and '|' in word:
+      spl = word.split('|')
+      out += spl[0]
+      # Everything after the pipe is the link
+      yesprint = False
     # TODO: support Endnotes (\fe and \fe*)
     elif word in ['\\ft']:
       continue # TODO: fancy formatting of more types
@@ -139,7 +152,7 @@ def convert(line, printStrongs=False):
       out += smallcaps(word) + ' '
      if superS:
       out += superscript(word) + ' '
-     else:
+     elif yesprint:
       out += word + ' '
   return out