summaryrefslogtreecommitdiffstats
path: root/generate_reference.py
diff options
context:
space:
mode:
Diffstat (limited to 'generate_reference.py')
-rwxr-xr-xgenerate_reference.py59
1 files changed, 45 insertions, 14 deletions
diff --git a/generate_reference.py b/generate_reference.py
index edc4703..e182205 100755
--- a/generate_reference.py
+++ b/generate_reference.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
import collections
import logging
@@ -91,15 +92,38 @@ TranslDict = collections.OrderedDict([
CZ_BOOKS = tuple(TranslDict.keys())
EN_BOOKS = tuple(TranslDict.values())
-BIBLE_REF_PARSE_RE = re.compile(r'(%s)?\s*([0-9]+),([0-9—.-]+)'
- % '|'.join(CZ_BOOKS))
-SPLIT_VERSE_RE = re.compile('[—.-]')
-
+BIBLE_REF_PARSE_RE = re.compile(r'''
+ (%s)? # Name of the Bible book (optional)
+ \s*([0-9]+), # chapter number separated by whitespace
+ ([0-9—.-]+) # verse number(s) separated by (optional) comma
+''' % '|'.join(CZ_BOOKS), flags=re.VERBOSE)
+ONLY_VERSE_PARSE_RE = re.compile(r'''
+ v\.\s+ # abbreviation of "verš" (verse)
+ ([0-9—.-]+) # verse number(s) separated by (optional) comma
+''', flags=re.VERBOSE)
+SPLIT_VERSE_RE = re.compile('''
+ (\d+)
+ [—.-]
+ (\d+)
+''', flags=re.VERBOSE)
+
+
+def verse_interval(verse):
+ match = SPLIT_VERSE_RE.search(verse)
+ logging.debug('match = %s', match)
+ if match:
+ matches = match.groups()
+ verse = int(match.group(1))
+ next_verse = int(match.group(2))
+ logging.debug('verse, next_verse = %s, %s', verse, next_verse)
+ return (verse, next_verse)
+ else:
+ return (int(verse),)
def parse_notes_test(instr):
logging.debug('%s\ninstr = %s', '-' * 30, instr)
matches = BIBLE_REF_PARSE_RE.findall(instr)
- logging.debug('matches = %s', matches)
+ logging.debug('BIBLE_REF_PARSE_RE matches = %s', matches)
out_list = []
book = None
@@ -109,16 +133,23 @@ def parse_notes_test(instr):
book = match[0]
elif book is None:
book = ''
- chapter = int(match[1])
- verse = match[2]
- if SPLIT_VERSE_RE.search(verse):
- verse_split = SPLIT_VERSE_RE.split(verse)
- verse = int(verse_split[0])
- next_verse = int(verse_split[1])
- logging.debug('verse, next_verse = %s, %s', verse, next_verse)
- out_list.append((book, chapter, verse, next_verse))
+ logging.debug('match[1] = %s', match[1])
+ if match[1]:
+ chapter = int(match[1].rstrip(','))
else:
- out_list.append((book, chapter, int(verse)))
+ chapter = 0
+ logging.debug('match[2] = %s', match[2])
+ verse = match[2]
+ out_list.append((book, chapter) + verse_interval(match[2]))
+ matches = ONLY_VERSE_PARSE_RE.findall(instr)
+ logging.debug('ONLY_VERSE_PARSE_RE matches = %s', matches)
+
+ for match in matches:
+ logging.debug('match = %s', match)
+ if match:
+ out = ('', 0) + verse_interval(match)
+ out_list.append(out)
+
return tuple(out_list)