def GetBookChapter(string, context=""): chapter = i_vk.ParseVerseList( to_str(string), to_str(context), True ).getText()#.decode(locale_encoding) index = chapter.find(":") if index != -1: chapter = chapter[:index] return chapter
def GetKey(self, key_text=None): if not self.mod: return None mod_tk = SW.TreeKey.castTo(self.mod.getKey()) mod_tk.root() tk = TK(mod_tk, self.mod) if key_text is not None: tk.setText(to_str(key_text, self.mod)) return tk
def choose_item(self, text, update_text_entry_value=False): if not self.book.mod: return idx = self.topics.mod.getEntryForKey(to_str(text, self.topics.mod)) idx = min(len(self.topics) - 1, idx) if idx >= 0: self.EnsureVisible(idx) self.Select(idx) return idx
def snap_text(self, text, module=None): mod = module or self.mod if mod is None: return text k = mod.getKey() k.setText(to_str(text, mod)) mod.setKey(k) # snap to entry mod.getRawEntryBuf() return to_unicode(mod.getKeyText(), mod)
def chapter_move(self, amount): mod = self.book.mod if not mod: return key = mod.getKey() key.Persist(1) key.setText(to_str(self.reference, mod)) mod.setKey(key) mod.increment(amount) ref = to_unicode(mod.getKeyText(), mod) self.notify(ref, source=events.CHAPTER_MOVE)
def chapter_move(self, amount): mod = self.book.mod if not mod: return key = mod.getKey() key.Persist(1) key.setText(to_str(self.reference, mod)) mod.setKey(key) mod.increment(amount); ref = to_unicode(mod.getKeyText(), mod) self.notify(ref, source=events.CHAPTER_MOVE)
def GetChapter(self, ref, specialref="", specialtemplate=None, context="", raw=False): self.vk.setText(to_str(ref, self.mod)) #get first ref text = self.vk.getText() match = re.match("([\w\s]+) (\d+):(\d+)", text) if match: book, chapter, verse = match.group(1, 2, 3) # include introductions - book introduction if necessary ref = "%s %s" % (book, chapter) text = "%s %s:0-%s %s" % (book, chapter, book, chapter) vk = SW.VerseKey() vk.Headings(1) list = vk.ParseVerseList(text, "", True) if chapter == "1": vk.setText("%s 0:0" % book) list.add(vk) #text = "%s 0:0-%s %s" % (book, book, chapter) if book == "Genesis": vk.Testament(0) list.add(vk) vk.Testament(1) list.add(vk) elif book == "Matthew": # set it to 0 first so that we come back to the testament # heading vk.Testament(0) vk.Testament(2) list.add(vk) list.sort() else: dprint(ERROR, "Couldn't parse verse text", text) return "" return self.GetReference(ref, specialref, specialtemplate, context, raw=raw, headings=True, verselist=list)
def GetReference(self, ref, context=None, max_verses=500, stripped=False, raw=False, end_ref=None): if not self.mod: return None assert not end_ref, "Dictionaries don't support ranges" raw = raw or display_options.options["raw"] render_text, render_start, render_end = self.get_rendertext() #TODO: use render_start and render_end? template = self.templatelist[-1] key = self.mod.getKey() key.setText(to_str(ref, self.mod)) self.mod.setKey(key) # We have to get KeyText after RenderText, otherwise our # KeyText will be wrong if stripped: text = self.mod.StripText().decode("utf-8", "replace") else: text = render_text().decode("utf-8", "replace") d = dict( # render text so that we convert utf-8 into html range=to_unicode(self.mod.getKeyText(), self.mod), description=to_unicode(self.mod.Description(), self.mod), version=self.mod.Name(), reference_encoded=SW.URL.encode(self.mod.getKeyText()).c_str(), ) d["reference"] = self.format_ref(self.mod, d["range"], snap=False) verses = template.header.safe_substitute(d) d1 = d if raw: d1["text"] = self.process_raw(self.mod.getRawEntry(), text, self.mod.getKey(), self.mod) else: d1["text"] = text verses += template.body.safe_substitute(d1) verses += template.footer.safe_substitute(d) #dictionary name return verses
def update_preview(self): if self.mod is None: self.preview.SetPage(config.MODULE_MISSING_STRING()) return try: for frame in guiconfig.mainfrm.frames: book = frame.book if self.mod.Name() not in book.GetModuleList(): continue # if we are already set to that book, use it ### should we get a better key here? if self.mod == frame.mod or True: if isinstance(frame.reference, basestring): ref = frame.reference else: ref = frame.reference.text self.mod.KeyText(to_str(ref, self.mod)) text = self.mod.RenderText() # if there is no text here, look back and forth for text if not text: old = self.mod.getSkipConsecutiveLinks() self.mod.setSkipConsecutiveLinks(True) for direction in 1, -1: self.mod.increment(direction) text = self.mod.RenderText() if text: break self.mod.setSkipConsecutiveLinks(old) ref = to_unicode(self.mod.getKeyText(), self.mod) ref = frame.format_ref(self.mod, ref) preview_text = u'%s (%s)<br>%s' % ( ref, self.mod.Name().decode("utf8"), text.decode("utf8") ) preview_text = u'<span style="font-family: %s; font-size: %spt;">%s</span>' % \ (self.font_face.StringSelection, self.font_size.Value, preview_text) item_to_focus_on = wx.Window.FindFocus() self.preview.SetPage(preview_text) #self.preview.ForceKillFocus() self.preview.SetFocus() item_to_focus_on.SetFocus() finally: pass
def update_preview(self): if self.mod is None: self.preview.SetPage(config.MODULE_MISSING_STRING()) return try: for frame in guiconfig.mainfrm.frames: book = frame.book if self.mod.Name() not in book.GetModuleList(): continue # if we are already set to that book, use it ### should we get a better key here? if self.mod == frame.mod or True: if isinstance(frame.reference, basestring): ref = frame.reference else: ref = frame.reference.text self.mod.KeyText(to_str(ref, self.mod)) text = self.mod.RenderText() # if there is no text here, look back and forth for text if not text: old = self.mod.getSkipConsecutiveLinks() self.mod.setSkipConsecutiveLinks(True) for direction in 1, -1: self.mod.increment(direction) text = self.mod.RenderText() if text: break self.mod.setSkipConsecutiveLinks(old) ref = to_unicode(self.mod.getKeyText(), self.mod) ref = frame.format_ref(self.mod, ref) preview_text = u'%s (%s)<br>%s' % ( ref, self.mod.Name().decode("utf8"), text.decode("utf8")) preview_text = u'<span style="font-family: %s; font-size: %spt;">%s</span>' % \ (self.font_face.StringSelection, self.font_size.Value, preview_text) item_to_focus_on = wx.Window.FindFocus() self.preview.SetPage(preview_text) #self.preview.ForceKillFocus() self.preview.SetFocus() item_to_focus_on.SetFocus() finally: pass
def GetReference(self, ref, context = None, max_verses = 500, stripped=False, raw=False, end_ref=None): if not self.mod: return None assert not end_ref, "Dictionaries don't support ranges" raw = raw or display_options.options["raw"] render_text, render_start, render_end = self.get_rendertext() #TODO: use render_start and render_end? template = self.templatelist[-1] key = self.mod.getKey() key.setText(to_str(ref, self.mod)) self.mod.setKey(key) # We have to get KeyText after RenderText, otherwise our # KeyText will be wrong if stripped: text = self.mod.StripText().decode("utf-8", "replace") else: text = render_text().decode("utf-8", "replace") d = dict( # render text so that we convert utf-8 into html range=to_unicode(self.mod.getKeyText(), self.mod), description=to_unicode(self.mod.Description(), self.mod), version=self.mod.Name(), reference_encoded=SW.URL.encode(self.mod.getKeyText()).c_str(), ) d["reference"] = self.format_ref(self.mod, d["range"], snap=False) verses = template.header.safe_substitute(d) d1 = d if raw: d1["text"] = self.process_raw(self.mod.getRawEntry(), text, self.mod.getKey(), self.mod) else: d1["text"] = text verses += template.body.safe_substitute(d1) verses += template.footer.safe_substitute(d) #dictionary name return verses
def Search(self, string, options=0, scopestr=None, case_sensitive=False): self.mod.setKey(self.vk) scope = None if(scopestr): # TODO: this is VerseKey specific scope = self.vk.ParseVerseList(to_str(scopestr), "", True) verseslist = self.doSearch(string, options, (not case_sensitive)*REG_ICASE, scope) strings = verseslist.getRangeText() if not strings: return [] # TODO: ;'s don't cut it - in the ISBE, they are often used return strings.split("; ")
def GetChapter(self, ref, specialref="", specialtemplate = None, context="", raw=False): self.vk.setText(to_str(ref, self.mod)) #get first ref text = self.vk.getText() match = re.match("([\w\s]+) (\d+):(\d+)", text) if match: book, chapter, verse = match.group(1, 2, 3) # include introductions - book introduction if necessary ref = "%s %s" % (book, chapter) text = "%s %s:0-%s %s" % (book, chapter, book, chapter) vk = SW.VerseKey() vk.Headings(1) list = vk.ParseVerseList(text, "", True) if chapter == "1": vk.setText("%s 0:0" % book) list.add(vk) #text = "%s 0:0-%s %s" % (book, book, chapter) if book == "Genesis": vk.Testament(0) list.add(vk) vk.Testament(1) list.add(vk) elif book == "Matthew": # set it to 0 first so that we come back to the testament # heading vk.Testament(0) vk.Testament(2) list.add(vk) list.sort() else: dprint(ERROR, "Couldn't parse verse text", text) return "" return self.GetReference(ref, specialref, specialtemplate, context, raw=raw, headings=True, verselist=list)
def create_index_against_text(self, module, key): """Build an index against the text""" self.index = [] # reference, start, length if self.get_entries(): module.getKey().setText(to_str(self.start, module)) else: module.setPosition(TOP) # clear error module.Error() iterator = re.compile("^.*$", re.M).finditer(self.text) for match in iterator: error = ord(module.Error()) assert not error, \ "%r Text: %r Key: %s" % (error, match.group(), module.getKeyText()) ind = self.get_index(key) start, end = match.span() self.index.append((ind, start, end-start)) module.increment(1)
def create_index_against_text(self, module, key): """Build an index against the text""" self.index = [] # reference, start, length if self.get_entries(): module.getKey().setText(to_str(self.start, module)) else: module.setPosition(TOP) # clear error module.Error() iterator = re.compile("^.*$", re.M).finditer(self.text) for match in iterator: error = ord(module.Error()) assert not error, \ "%r Text: %r Key: %s" % (error, match.group(), module.getKeyText()) ind = self.get_index(key) start, end = match.span() self.index.append((ind, start, end - start)) module.increment(1)
def install_modules(modules, dest_dir): def callback(progress, text): continuing, skip = p.Update(progress, text) wx.GetApp().Yield() for module in modules: p = wx.ProgressDialog( _("Extracting %s") % to_unicode(module.Description(), module), _("Preparing"), style=wx.PD_APP_MODAL) # make it nice and long so that the status text will fit in p.Size = (640, -1) p.Show() try: module.extract_zipfile(dest_dir, callback) finally: p.Hide() p.Destroy() biblemgr.set_new_paths(path_changed=to_str(dest_dir))
def get_parallel_text(self, ref): vk = SW.VerseKey() verselist = vk.ParseVerseList(to_str(ref), "", True) items = [] text = ["<table class='parallel_view'>", "<tr>"] for item in self.book.GetModules(): name = item.Name() if name in verse_comparison_settings["comparison_modules"]: items.append((item, list( self.book.GetReference_yield( verselist, module=item, max_verses=176, skip_linked_verses=False)))) text.append(u"<th>%s</th>" % process_html_for_module( item, u"<b><a href='%s:%s'>" "%s</a></b>" % (BIBLE_VERSION_PROTOCOL, name, name))) text.append("</tr>") # if we have no bibles to compare, return the empty string if not items: return "" rows = [] was_clipped = False try: self.book.templatelist.append(config.parallel_template) while True: text.append("<tr>") for module, refs in items: if not refs: text.append("</tr>") break body_dict, headings = refs.pop(0) if not body_dict: was_clipped = True # remove opening row text.pop() break text_direction = get_module_css_text_direction(module) text.append( u'<td class="parallel_verse" module="%s" dir="%s">' % (module.Name(), text_direction)) t = "" for heading_dict in headings: t += biblemgr.bible.templatelist[-1].\ headings.safe_substitute(heading_dict) t += biblemgr.bible.templatelist[-1].\ body.safe_substitute(body_dict) t = process_html_for_module(module, t) text.append(t) text.append("</td>") else: text.append("</tr>") continue break finally: self.book.templatelist.pop() text.append("</table>") if was_clipped: text.append(config.MAX_VERSES_EXCEEDED() % 177) return ''.join(text)
def GetReference(self, ref, specialref="", specialtemplate=None, context="", max_verses=177, raw=False, stripped=False, template=None, display_tags=None, exclude_topic_tag=None, end_ref=None, headings=False, verselist=None, remove_extra_whitespace=False): """GetReference gets a reference from a Book. specialref is a ref (string) which will be specially formatted according to specialtemplate. exclude_topic_tag: If this is not None, then it is a topic that should not have a tag generated, because it is obvious from the context (for example, the topic window for that topic). """ #only for bible keyed books if not self.mod: return None raw = raw or display_options.options["raw"] if template is None and self.templatelist: template = self.templatelist[-1] if context: lastverse = context else: lastverse = "" if display_tags is None: # if we don't have tags in, don't calculate them as it can be # expensive if "$tags" not in template.body.template: display_tags = False else: display_tags = passage_list.settings.display_tags assert not (verselist and end_ref), \ "No end ref with a listkey!!!" if end_ref: ref += " - " + end_ref old_headings = self.vk.Headings(headings) if not verselist: verselist = self.vk.ParseVerseList(to_str(ref), to_str(lastverse), True) # if they pass in a verselist, they can also pass in the ref they # would like to go along with it. This can be useful if it also # includes headings that shouldn't be seen rangetext = GetBestRange(ref, context=context, userInput=False, userOutput=True, headings=headings) internal_rangetext = GetBestRange(ref, context=context, headings=headings) if rangetext == "": self.vk.Headings(old_headings) #if invalid reference, return empty string return u"" if specialref: specialref = GetVerseStr(specialref) description = to_unicode(self.mod.Description(), self.mod) d = dict(range=rangetext, internal_range=internal_rangetext, version=self.mod.Name(), description=description) text = template.header.safe_substitute(d) verses = [] for body_dict, headings in self.GetReference_yield( verselist, max_verses, raw, stripped, exclude_topic_tag=exclude_topic_tag, display_tags=display_tags, ): # if we have exceeded the verse limit, body_dict will be None if body_dict is None: verses.append(config.MAX_VERSES_EXCEEDED() % max_verses) break body_dict.update(d) t = template if specialref == body_dict["internal_reference"]: t = specialtemplate verse = t.preverse.safe_substitute(body_dict) for heading_dict in headings: verse += t.headings.safe_substitute(heading_dict) verse += t.body.safe_substitute(body_dict) verses.append(verse) self.vk.Headings(old_headings) text += template.finalize(u''.join(verses)) text += self.end_of_render text += template.footer.safe_substitute(d) return text
def is_bold(self, item): module = self.modules[item] key = to_str(self.key, module) k = SW.Key(key) return module.hasEntry(k)
def get_key(self, module): key = module.getKey() key.setText(to_str(self.start, module)) return TK(key)
def get_parallel_text(self, ref): vk = SW.VerseKey() verselist = vk.ParseVerseList(to_str(ref), "", True) items = [] text = ["<table class='parallel_view'>", "<tr>"] for item in self.book.GetModules(): name = item.Name() if name in verse_comparison_settings["comparison_modules"]: items.append(( item, list(self.book.GetReference_yield( verselist, module=item, max_verses=176, skip_linked_verses=False )) )) text.append(u"<th>%s</th>" % process_html_for_module(item, u"<b><a href='%s:%s'>" "%s</a></b>" % (BIBLE_VERSION_PROTOCOL, name, name)) ) text.append("</tr>") # if we have no bibles to compare, return the empty string if not items: return "" rows = [] was_clipped = False try: self.book.templatelist.append(config.parallel_template) while True: text.append("<tr>") for module, refs in items: if not refs: text.append("</tr>") break body_dict, headings = refs.pop(0) if not body_dict: was_clipped = True # remove opening row text.pop() break text_direction = get_module_css_text_direction(module) text.append(u'<td class="parallel_verse" module="%s" dir="%s">' % (module.Name(), text_direction)) t = "" for heading_dict in headings: t += biblemgr.bible.templatelist[-1].\ headings.safe_substitute(heading_dict) t += biblemgr.bible.templatelist[-1].\ body.safe_substitute(body_dict) t = process_html_for_module(module, t) text.append(t) text.append("</td>") else: text.append("</tr>") continue break finally: self.book.templatelist.pop() text.append("</table>") if was_clipped: text.append(config.MAX_VERSES_EXCEEDED() % 177) return ''.join(text)
def __init__(self, args=None, context="", expand=True, raiseError=False, userInput=False, headings=False, use_bpbible_locale=False): converted = False if(isinstance(args, (list, tuple))): assert not userInput args2 = [] for a in args: if(isinstance(a, SW.VerseKey)): args2.append(a) else: args2.append(VK(a)) list.__init__(self, args2) return locale_changed = False if isinstance(args, basestring): orig_args = args if userInput: for matcher, replacement in self.replacements: args = matcher.sub(replacement, args) args = process_digits(args, userInput=True) args = to_str(args) context = to_str(context) s = args if use_bpbible_locale: vk = b_vk locale = "bpbible" elif userInput: vk = u_vk locale = locale_lang else: vk = i_vk locale = locale_mgr.getDefaultLocaleName() old_headings = vk.Headings(headings) try: # make sure we have this set correctly # TestForError uses this, # as does ParseVerseList with expand on old = locale_mgr.getDefaultLocaleName() if old != locale: locale_mgr.setDefaultLocaleName(locale) locale_changed = True if not raiseError: args = vk.ParseVerseList(args, context, expand, userInput) else: an = vk.AutoNormalize(0) try: args = vk.ParseVerseList(args, context, expand, userInput) self.TestForError(s, context, orig_args) finally: vk.AutoNormalize(an) finally: vk.Headings(old_headings) if locale_changed: if old != locale: locale_mgr.setDefaultLocaleName(old) if(isinstance(args, SW.ListKey)): self.RefreshVKs(args, raiseError=raiseError, userInput=userInput, headings=headings) else: raise TypeError, `args` for a in self: if a[-1]== rev_22_21: #dprint(WARNING, "Possibly incorrect string. Result is", self) pass
def GetReference(self, ref, context = None, max_verses = 500, stripped=False, end_ref=None): """Get a reference from a genbook. ref should be either a TK or a string. If it is a TK, it is guaranteed not to change its position.""" if not self.mod: return None template = self.templatelist[-1] render_text, render_start, render_end = self.get_rendertext() #TODO: use render_start and render_end? module = self.mod if isinstance(ref, TK) and end_ref: # we will move, so take a copy to move ref = TK(ref) if isinstance(ref, basestring): key = TK(module.getKey(), module) key.setText(to_str(ref, module)) ref = key if isinstance(end_ref, basestring): key = TK(module.getKey(), module) key.setText(to_str(end_ref, module)) end_ref = key old_key = module.getKey() if not ord(old_key.Persist()): # if it wasn't a persistent key, the module owns it # so take a copy of it, and say we own it old_key = old_key.clone() old_key.thisown = True ref.Persist(1) module.setKey(ref) # snap to it entry = module.getRawEntry() # We have to get KeyText after getRawEntry, otherwise our # KeyText will be wrong d = dict(range = module.KeyText(), version = module.Name()) verses = template.header.substitute(d) d1 = d.copy() raw = display_options.options["raw"] while True: if stripped: text = module.StripText(entry).decode("utf-8", "replace") else: # we can't specify entry here for the same reasons as in # book.py text = render_text(#entry ).decode("utf-8", "replace") if raw: text = self.process_raw(entry, text, ref, module) d1["reference"] = to_unicode(module.getKeyText(), module) d1["reference_encoded"] = \ SW.URL.encode(module.getKeyText()).c_str() d1["text"] = text d1["breadcrumbed_reference"] = ref.breadcrumb(delimiter=" > ") d1["level"] = ref.getLevel() verses += template.body.substitute(d1) if not end_ref or end_ref == ref: break ref.increment(1) entry = module.getRawEntry() verses += template.footer.substitute(d) module.setKey(old_key) return verses
def BookName(text): #TODO: u_vk or i_vk? u_vk.setText(to_str(text)) if u_vk.Error(): return None return u_vk.getBookName()
def set_text(self, value): self.setText(to_str(value, self.module))