def subst(match): if (is_bible or match.group(1)): if match.group(2).startswith("%3F"): url = SW.URL("bible:" + SW.URL.decode(str(match.group(2))).c_str()) values = url.getParameterValue("values") references = VerseList([ url.getParameterValue("val%s" % value) for value in range(int(values)) ]) else: reference_text = SW.URL.decode(str(match.group(2))).c_str() references = VerseList(reference_text) else: # our closing </a>? if not is_bible and in_ref[0] and match.group(4): in_ref[0] = False return "</span>%s" % match.group(4) return match.group(0) for item in v: for i in item: if references.VerseInRange(i): if is_bible: # wrap the contents of the <a> in formatting return '%s<a name="highlight"></a><span class="search_highlight">%s</span>%s' % match.group(1, 4, 5) else: # start the formatting, to be completed with # the </a> tag handling above in_ref[0] = True return '%s<a name="highlight"></a><span class="search_highlight">' % \ match.group(1) return match.group(0)
def on_search_click(): """Search for the selected word in the Bible""" strongs_number = None text = None if href: match = re.match(u'strongs://(Greek|Hebrew)/(\d+)(!\w+)?', href) if match: prefix, number, extra = match.group(1, 2, 3) strongs_number = "HG"[prefix == "Greek"] + number if extra: strongs_number += extra[1:] text = "strongs:" + strongs_number if not text: match = re.match(u'morph://(\w*)((:|%3A)[^/]+)/([\w-]+)', href) if match: module, value = match.group(1, 4) if module == "Greek": module = "Robinson" text = "morph:%s:%s" % (module, value) ref = None if not text: match = (re.match( u'n?bible:([^#]*)(#.*)?', href) or re.match( u'passagestudy.jsp\?action=showRef&type=scripRef&' 'value=([^&]*)&module=.*', href)) if match: ref = match.group(1) if ref: ref = SW.URL.decode(str(ref)).c_str() vl = VerseList(ref) first_ref = VerseList([vl[0] ]).GetBestRange(userOutput=True) # only search on the first item or range text = 'ref:"%s"' % first_ref if not text: text = self.SelectionToText() if not text: text = self.get_clicked_cell_text() text = self.strip_text(text) # get rid of a few special characters text = re.sub(r'[\()"/:\-]', '', text) # if this is a phrase, put quotes around it. if " " in text: text = '"%s"' % text search_panel = frame.get_search_panel_for_frame() assert search_panel, "Search panel not found for %s" % self search_panel.search_and_show(text)
def _setupPassageLists(test): """Sets up the passage lists for the given test.""" test._verse_list1 = VerseList( [VK(("gen 2:3", "gen 2:5")), "gen 3:4", "gen 5:2"]) test._verse_list2 = VerseList( ["ex 2:2", "ex 3:5", VK(("ex 3:7", "ex 3:9"))]) test._verse_list3 = VerseList([VK(("lev 2:3", "lev 2:5")), "lev 2:7"]) test._verse_list4 = VerseList(["num 3:1", VK(("num 3:4", "num 3:5"))]) test._list = PassageList.create_from_verse_list("abc", test._verse_list1) test._list2 = PassageList.create_from_verse_list("def", test._verse_list2) test._list3 = PassageList.create_from_verse_list("ghi", test._verse_list3) test._list4 = PassageList.create_from_verse_list("jkl", test._verse_list4) test._list2.add_subtopic(test._list4) test._list.add_subtopic(test._list2) test._list.add_subtopic(test._list3) test._manager = PassageListManager() test._manager.add_subtopic(test._list)
def _parse_passage_str(self, passage): if not passage: return None passages = VerseList(passage) if len(passages) >= 1: return passages else: raise InvalidPassageError
def _is_valid_passage(self): passage_text = self.passage_text.Value passages = VerseList(passage_text, userInput=True) if len(passages) >= 1: self._passage_entry.set_passage(passages, new_passage=self.is_new_passage) return True else: wx.MessageBox( _(u"Unrecognised passage `%s'.") % passage_text, "", wx.OK | wx.ICON_INFORMATION, self) return False
def add_references(tk, references): self.mod.setKey(tk) entry = self.mod.getRawEntry() entry_references = '|'.join(re.findall('<harmonytable refs="([^"]*)"></harmonytable>', entry)) if entry_references: references.append(( tk.getText(), [VerseList(reference, userInput=False) for reference in entry_references.split('|')] )) for child_tk in tk: add_references(child_tk, references)
def create_from_verse_list(name, verse_list, description="", comment="", display_tag=True): """Creates a passage list with the given name. verse_list: The list of verses to create the passage list from. description: The description for the passage list. comment: The comment to attach to every passage entry. display_tag: Should the tag be displayed for this topic. """ passage_list = PassageList(name, description, display_tag=display_tag) for verse in verse_list: passage_list.add_passage(PassageEntry(VerseList([verse]), comment)) return passage_list
def SetReference(self, ref, reload=False, settings_changed=False): if not ref: return last_reference = verse_comparison_settings["reference"] if VerseList(last_reference).VerseInRange(ref): ref = last_reference if ref == self.reference and not reload: return text = "<h3>%s</h3>" % ref self.reference = ref verse_comparison_settings["reference"] = ref text_func = [self.get_compare_text, self.get_parallel_text ][verse_comparison_settings["parallel"]] self.SetPage(text_func(ref)) self.gui_reference.SetValue(GetBestRange(ref, userOutput=True)) self.gui_reference.currentverse = ref self.update_title()
def process_references(self, gospels): self.fulldescription += self.description self.name = self.fulldescription if not self.references: self.references = [[]] else: #merge all lines after first into first for refs in self.references[1:]: for idx, ref in enumerate(refs): self.references[0][idx] += ref self.references = self.references[0] #split each reflist at ; self.references = [ref.split(";") for ref in self.references] if not self.references: longest = 0 else: longest = max(len(ref) for ref in self.references) references = [[] for _ in range(longest)] for book_idx, refs in enumerate(self.references): for ref_idx, ref in enumerate(refs): if ref: references[ref_idx].append( VerseList(ref, gospels[book_idx])) self.references = references for idx, child in enumerate(self.children): child.process_references(gospels) # set forward and back links on child if idx: child.previous = self.children[idx - 1] if idx != len(self.children) - 1: child.next = self.children[idx + 1]
else: os.makedirs(module_dir) print module_dir result = driver.createModule(module_dir) assert result == '\x00', \ "Failed creating module" module = driver(module_dir) assert module.isWritable(), "MODULE MUST BE WRITABLE" for key in keys: from swlib.pysw import VerseList key = VerseList(key)[0] lower_bound = key.LowerBound().getText() key_text = "This is %s." % key.getRangeText() module.setKey(SW.VerseKey(lower_bound)) module.setEntry(key_text) for linked_key in [key_.getText() for key_ in key][1:]: module.setKey(SW.VerseKey(linked_key)) module.linkEntry(SW.VerseKey(lower_bound)) conf_file = items["bible"][4] f = open("mods.d/test_bad.conf", "w") f.write(conf_file + "\ntest") f.close() zip = zipfile.ZipFile("test_bad.zip", "w")
def prepare(cls, input): vl = VerseList(input, raiseError=True, userInput=True) if len(vl) != 1: dprint(WARNING, "Multiple results in search ref: verselist. Taking first", vl) if vl[0][0].getBookName() != vl[0][-1].getBookName(): from index import SearchException raise SearchException( _("In finding references, the reference " "must all be in one book")) def get_chapter_verses(versekey): return versekey.verseCount(ord(versekey.Testament()), ord(versekey.Book()), versekey.Chapter()) def get_book_chapters(versekey): return versekey.chapterCount( ord(versekey.Testament()), ord(versekey.Book()), ) start, end = vl[0][0], vl[0][-1] osisref = start.getOSISRef() bookname = osisref[:osisref.find(".")] if start.Chapter() == 1 and start.Verse() == 1 and \ end.Chapter() == get_book_chapters(end) and \ end.Verse() == get_chapter_verses(end): return r'\b%s\b' % bookname items = [] if start.Chapter() != end.Chapter(): # do all the verses in the start chapter individually sc = start.Chapter() for verse in range(start.Verse(), get_chapter_verses(start) + 1): items.append(r'(?:%d\.%d)' % (sc, verse)) # do all the verses in the end chapter individually ec = end.Chapter() for verse in range(1, end.Verse() + 1): items.append(r'(?:%d\.%d)' % (ec, verse)) # but do each chapter in the middle all in one for chapter in range(start.Chapter() + 1, end.Chapter()): items.append(r'(?:%d)' % (chapter)) else: sc = start.Chapter() if start.Verse() == 1 and \ end.Verse() == get_chapter_verses(end): items.append('(?:%d)' % (sc)) else: for verse in range(start.Verse(), end.Verse() + 1): items.append('(?:%d\.%d)' % (sc, verse)) regex = r'\b%s\.(?:%s)\b' % (bookname, '|'.join(items)) print regex return regex
def setUp(self): self._list = PassageList.create_from_verse_list( "name", VerseList(["gen 2:3", "gen 2:5", "gen 2:7"]), "description")
def highlight( string1, string2, is_bible, regexes, fields=(), start_tag='<a name="highlight"></a><span class="search_highlight">', end_tag='</span>'): """Highlight string2 with the regular expressions regexes, being matched on string1 string1 should be text processed through striptext string2 should be the same text, but with extra rendering niceties start_tag and end_tag give the tags for the start and end of highlighting """ results = unite(string1, string2, is_bible) # if we couldn't process it, return string2 intact if not results: return string2 for regex in regexes: matched = False for match in regex.finditer(string1): matched = True start, end = match.span() highlight_section(results, start, end, start_tag, end_tag) if not matched: dprint(ERROR, "Regular expression not matched against plain text", regex.pattern, string1) for key, value in fields: if key == "strongs": match = re.match("([GH])(\d+)(\w?)", value.upper()) if not match: continue prefix, number, extra = match.group(1, 2, 3) lang = ["Hebrew", "Greek"][prefix == "G"] number = str(int(number)) if extra: number += "!%s" % extra href = r"strongs://%s/0*%s(?:!\w)?" % (lang, number) link_matcher = re.compile( '^<a([^>]*)(href="%s")([^>]*)>([^<]*)</a>$' % href) for tokens in results: for idx, token in enumerate(tokens): # highlight strong's numbers... tokens[idx] = link_matcher.sub( r'<a name="highlight"></a><span class="search_highlight"><a \1\2\3>\4</a></span>', token) elif key == "morph": parts = value.split(":", 1) if len(parts) != 2: dprint(WARNING, "Not two parts in split. Skipping") continue k, v = parts if k == "Robinson": k = "(?:Robinson|Greek)" k = '%s:%s' % (k, v) k = SW.URL.encode(str(k)).c_str() v = SW.URL.encode(str(v)).c_str() d = r'^(<a class="morph" href="morph://%s/%s">)([^<]*)(</a>)$' % ( k, v) count = 0 morph_matcher = re.compile(d) for tokens in results: for idx, token in enumerate(tokens): tokens[idx], c = morph_matcher.subn( r'<a name="highlight"></a><span class="search_highlight">\1\2\3</span>', token) count += c if count == 0: dprint(WARNING, "Didn't highlight morph - are they on?", value) elif key == "ref": if is_bible: ref = re.compile( r'^(<a href="bible:([^#]*)(#.*)?">)(.*?)(</a>)$') else: # if we aren't in the bible, the start and end will be in # different tokens. So match start and then end ref = re.compile(r'^(<a href="bible:([^#]*)(#.*)?">)|(</a>)$') v = VerseList(value, userInput=True) in_ref = [False] def subst(match): if (is_bible or match.group(1)): if match.group(2).startswith("%3F"): url = SW.URL( "bible:" + SW.URL.decode(str(match.group(2))).c_str()) values = url.getParameterValue("values") references = VerseList([ url.getParameterValue("val%s" % value) for value in range(int(values)) ]) else: reference_text = SW.URL.decode(str( match.group(2))).c_str() references = VerseList(reference_text) else: # our closing </a>? if not is_bible and in_ref[0] and match.group(4): in_ref[0] = False return "</span>%s" % match.group(4) return match.group(0) for item in v: for i in item: if references.VerseInRange(i): if is_bible: # wrap the contents of the <a> in formatting return '%s<a name="highlight"></a><span class="search_highlight">%s</span>%s' % match.group( 1, 4, 5) else: # start the formatting, to be completed with # the </a> tag handling above in_ref[0] = True return '%s<a name="highlight"></a><span class="search_highlight">' % \ match.group(1) return match.group(0) for tokens in results: for idx, token in enumerate(tokens): tokens[idx] = ref.sub(subst, token) replacements = {'<': '<', '>': '>', '&': '&'} for x in results: for idx, y in enumerate(x): # don't let <, > or & by themselves go straight through, as they # will have been translated from <, ... if y in ('<', '>', '&'): x[idx] = replacements[y] return htmlify_unicode(''.join(''.join(x) for x in results))