Beispiel #1
0
	def format_combo(self, item):
		data = self.get_data(item)
		if isinstance(data, BookData):
			return "%s" % data

		elif isinstance(data, ChapterData):
			parent_data = self.get_data(self.tree.GetItemParent(item))
			if len(parent_data) == 1:
				return "%s" % parent_data

			return pysw.process_digits("%s %s" % (parent_data, data),
				userOutput=True)

		parent = self.tree.GetItemParent(item)
		parent_data = self.get_data(parent)
		if isinstance(parent_data, BookData):
			# if our parent is book data, and we aren't chapter data, then
			# this is a single chapter book
			return pysw.process_digits("%s %s" % (parent_data, data),
				userOutput=True)
			

		grandparent_data = self.get_data(self.tree.GetItemParent(parent))

		return pysw.process_digits(
			"%s %s:%s " % (grandparent_data, parent_data, data),
			userOutput=True)
Beispiel #2
0
    def format_combo(self, item):
        data = self.get_data(item)
        if isinstance(data, BookData):
            return "%s" % data

        elif isinstance(data, ChapterData):
            parent_data = self.get_data(self.tree.GetItemParent(item))
            if len(parent_data) == 1:
                return "%s" % parent_data

            return pysw.process_digits("%s %s" % (parent_data, data),
                                       userOutput=True)

        parent = self.tree.GetItemParent(item)
        parent_data = self.get_data(parent)
        if isinstance(parent_data, BookData):
            # if our parent is book data, and we aren't chapter data, then
            # this is a single chapter book
            return pysw.process_digits("%s %s" % (parent_data, data),
                                       userOutput=True)

        grandparent_data = self.get_data(self.tree.GetItemParent(parent))

        return pysw.process_digits("%s %s:%s " %
                                   (grandparent_data, parent_data, data),
                                   userOutput=True)
Beispiel #3
0
	def safe_substitute(self, dict):
		text = dict.pop("text")
		verse_0 = dict["versenumber"] == process_digits("0", userOutput=True)
		if verse_0 and not text or text == "<br />":
			return u""
		
		if verse_0:
			dict["versenumber"] = u""

		if dict["versenumber"] == process_digits("1", userOutput=True):
			dict["versenumber"] = dict["chapternumber"]
			dict["numbertype"] = "chapternumber"
		else:
			dict["numbertype"] = "versenumber"
		
		whitespace = []
		def collect(match):
			whitespace.append(match.group(0))
			return u""

		# float leading whitespace out to the front
		text = self.incl_whitespace_start.sub(collect, text)

		leading_whitespace = whitespace
		whitespace = []
		
		# float trailing whitespace to end
		text = self.incl_whitespace_end.sub(collect, text)
		
		dict["text"] = text

		verse_per_line = self.verse_per_line
		if verse_0:
			verse_per_line = False

		ret = u"%s%s%s%s\n" % (
			u''.join(leading_whitespace),
			self.body.safe_substitute(dict),
			u''.join(whitespace),
			self.vpl_text * verse_per_line
		)
		
		# remove empty verse number
		ret = self.empty_versenumber.sub(u"", ret)
		

		return ret
Beispiel #4
0
    def safe_substitute(self, dict):
        text = dict.pop("text")
        verse_0 = dict["versenumber"] == process_digits("0", userOutput=True)
        if verse_0 and not text or text == "<br />":
            return u""

        if verse_0:
            dict["versenumber"] = u""

        if dict["versenumber"] == process_digits("1", userOutput=True):
            dict["versenumber"] = dict["chapternumber"]
            dict["numbertype"] = "chapternumber"
        else:
            dict["numbertype"] = "versenumber"

        whitespace = []

        def collect(match):
            whitespace.append(match.group(0))
            return u""

        # float leading whitespace out to the front
        text = self.incl_whitespace_start.sub(collect, text)

        leading_whitespace = whitespace
        whitespace = []

        # float trailing whitespace to end
        text = self.incl_whitespace_end.sub(collect, text)

        dict["text"] = text

        verse_per_line = self.verse_per_line
        if verse_0:
            verse_per_line = False

        ret = u"%s%s%s%s\n" % (
            u''.join(leading_whitespace), self.body.safe_substitute(dict),
            u''.join(whitespace), self.vpl_text * verse_per_line)

        # remove empty verse number
        ret = self.empty_versenumber.sub(u"", ret)

        return ret
Beispiel #5
0
    def get_text(self):
        vk = pysw.UserVK(self.ref)
        html = u'<font size=+1><b><a href="nbible:%s">%s</a></b></font>' % (
            self.ref, vk.get_book_chapter())

        html += u": %s<br>" % (_("%s verses") % process_digits(str(
            vk.verseCount(ord(vk.Testament()), ord(vk.Book()), vk.Chapter())),
                                                               userOutput=True)
                               )

        html += u"<ul>"
        for vk, text in chapter_headings.get_chapter_headings(self.ref):
            html += u'<li><a href="nbible:%s">%s</a>' % (vk, text)
        html += u"</ul>"

        return html
Beispiel #6
0
	def get_text(self):
		vk = pysw.UserVK(self.ref)
		html = u'<font size=+1><b><a href="nbible:%s">%s</a></b></font>' % (
			self.ref, vk.get_book_chapter()
		)
	
		html += u": %s<br>" % (_("%s verses") % 
			process_digits(str(
				vk.verseCount(
					ord(vk.Testament()), 
					ord(vk.Book()), 
					vk.Chapter())
				), userOutput=True))
	
		html += u"<ul>"
		for vk, text in chapter_headings.get_chapter_headings(self.ref):
			html += u'<li><a href="nbible:%s">%s</a>' % (vk, text)
		html += u"</ul>"

		return html
Beispiel #7
0
    def get_tree_item(self):
        root = self.tree.GetRootItem()

        text = self.GetValue()
        was_book = False
        for book in UserVK.books:
            if ("%s" % book) == text:
                was_book = True
                self.currentverse = book.bookname
                break
        else:
            try:
                # try updating verse based on user text
                # if we fail, just use old text (assuming there is any)
                self.currentverse = GetVerseStr(text,
                                                self.currentverse,
                                                raiseError=True,
                                                userInput=True,
                                                userOutput=False)

            except VerseParsingError:
                if not self.currentverse:
                    return self.tree.GetFirstChild(root)[0]

        verse_key = UserVK(VK(self.currentverse))
        single_chapter_book = verse_key.getChapterMax() == 1

        book, chapter = verse_key.getBookName(), verse_key.Chapter()
        verse = verse_key.Verse()
        chapter = process_digits(str(chapter), userOutput=True)
        verse = process_digits(str(verse), userOutput=True)
        if single_chapter_book:
            chapter = verse

        item, cookie = self.tree.GetFirstChild(root)
        while item:
            if self.tree.GetItemText(item) == book:
                break

            item, cookie = self.tree.GetNextChild(root, cookie)

        assert item, book + " not found!"

        if was_book or (single_chapter_book and not self.with_verses):
            return item

        self.tree.Expand(item)

        item2, cookie = self.tree.GetFirstChild(item)

        while item2:
            data = unicode(self.get_data(item2))
            if data == chapter:
                # if : isn't in there, we take it as a chapter reference
                if not self.with_verses or  ":" not in text \
                  or single_chapter_book:
                    return item2
                else:
                    break

            item2, cookie = self.tree.GetNextChild(item, cookie)

        assert item2, "Chapter '%s' not found in %s" % (chapter, book)
        assert not single_chapter_book, "Single chapter books, but chapterMax == 1?!?"

        self.tree.Expand(item2)

        item3, cookie = self.tree.GetFirstChild(item2)

        while item3:
            data = unicode(self.get_data(item3))
            if data == verse:
                return item3

            item3, cookie = self.tree.GetNextChild(item2, cookie)

        assert item3, "Verse '%s' not found in %s %s" % (verse, book, chapter)
Beispiel #8
0
	def get_tree_item(self):
		root = self.tree.GetRootItem()
	
		text = self.GetValue()
		was_book = False
		for book in UserVK.books:
			if ("%s" % book) == text:
				was_book = True
				self.currentverse = book.bookname
				break
		else:
			try:
				# try updating verse based on user text
				# if we fail, just use old text (assuming there is any)
				self.currentverse = GetVerseStr(text, self.currentverse,
					raiseError=True, userInput=True, userOutput=False)

			except VerseParsingError:
				if not self.currentverse:
					return self.tree.GetFirstChild(root)[0]
					

		verse_key = UserVK(VK(self.currentverse))
		single_chapter_book = verse_key.getChapterMax() == 1

		book, chapter = verse_key.getBookName(), verse_key.Chapter()
		verse = verse_key.Verse()
		chapter = process_digits(str(chapter), userOutput=True)
		verse = process_digits(str(verse), userOutput=True)
		if single_chapter_book:
			chapter = verse
		

		item, cookie = self.tree.GetFirstChild(root)
		while item:
			if self.tree.GetItemText(item) == book:
				break

			item, cookie = self.tree.GetNextChild(root, cookie)

		assert item, book + " not found!"
		
		if was_book or (single_chapter_book and not self.with_verses): 
			return item

		self.tree.Expand(item)
		
		item2, cookie = self.tree.GetFirstChild(item)

		while item2:
			data = unicode(self.get_data(item2))
			if data == chapter:
				# if : isn't in there, we take it as a chapter reference
				if not self.with_verses or  ":" not in text \
						or single_chapter_book:
					return item2
				else:
					break

			item2, cookie = self.tree.GetNextChild(item, cookie)
		
		assert item2, "Chapter '%s' not found in %s" % (chapter, book)
		assert not single_chapter_book, "Single chapter books, but chapterMax == 1?!?"


		self.tree.Expand(item2)
		
		item3, cookie = self.tree.GetFirstChild(item2)

		while item3:
			data = unicode(self.get_data(item3))
			if data == verse:
				return item3

			item3, cookie = self.tree.GetNextChild(item2, cookie)
		
		assert item3, "Verse '%s' not found in %s %s" % (verse, book, chapter)
Beispiel #9
0
    def GetReference_yield(self,
                           verselist,
                           max_verses=177,
                           raw=False,
                           stripped=False,
                           module=None,
                           exclude_topic_tag=None,
                           display_tags=True,
                           skip_linked_verses=True):
        """GetReference_yield: 
			yield the body dictionary and headings dictinoary
			for each reference.

		Preconditions:
			one of module or self.mod is not None
			verselist is valid"""
        #only for bible keyed books
        verselist.setPosition(TOP)
        verselist.Persist(1)
        u_vk = pysw.UserVK()
        u_vk.Headings(1)
        versekey = SW.VerseKey()
        versekey.Headings(1)
        mod = module or self.mod
        old_mod_skiplinks = mod.getSkipConsecutiveLinks()
        mod.setSkipConsecutiveLinks(True)
        mod.SetKey(verselist)
        verses_left = max_verses

        render_text, render_start, render_end = self.get_rendertext(mod)
        if render_start: render_start()

        try:
            incrementer = mod if skip_linked_verses else verselist
            while not self.has_error(incrementer):
                if verses_left == 0:
                    yield None, None
                    break

                if not skip_linked_verses:
                    mod.SetKey(verselist)
                key = mod.getKey()
                #versekey = VK.castTo(key)
                versekey.setText(key.getText())
                #if(self.headings):
                #	versekey.Headings(1)
                osisRef = versekey.getOSISRef()
                internal_reference = versekey.getText()

                rawentry = mod.getRawEntryBuf()
                if skip_linked_verses and not rawentry.length():
                    # don't include empty text; typically this may be at the
                    # start of the chapter or something...
                    incrementer.increment(1)
                    continue

                start_verse = end_verse = versekey.Verse()

                # look forwards and backwards to see what the linked verse
                # number is (e.g. 3-5). Note: currently this won't cross
                # chapter boundaries
                vk = versekey.clone()
                vk = versekey.castTo(vk)
                vk.thisown = True
                vk.Headings(0)
                while (vk.Error() == '\x00'
                       and vk.Chapter() == versekey.Chapter()
                       and mod.isLinked(vk, versekey)):
                    end_verse = vk.Verse()
                    vk.increment(1)

                vk.copyFrom(versekey)
                vk.Headings(0)

                # hopefully we won't see anything backwards, but it is
                # possible (i.e. if we start in the middle of a linked
                # verse
                while (vk.Error() == '\x00'
                       and vk.Chapter() == versekey.Chapter()
                       and mod.isLinked(vk, versekey)):
                    start_verse = vk.Verse()

                    vk.decrement(1)

                if start_verse == end_verse:
                    verse = "%d" % start_verse
                else:
                    verse = "%d-%d" % (start_verse, end_verse)

                u_vk.setText(internal_reference)
                if internal_reference.endswith(":0"):
                    if start_verse != end_verse:
                        print "WARNING: unhandled linked verse with verse 0"

                    if versekey.Chapter() == 0:
                        reference = u_vk.getBookName()
                    else:
                        reference = u_vk.get_book_chapter()

                else:
                    reference = u_vk.get_book_chapter()
                    reference += ":" + verse

                body_dict = dict(
                    # text comes later
                    versenumber=process_digits(verse, userOutput=True),
                    chapternumber=process_digits(str(versekey.Chapter()),
                                                 userOutput=True),
                    booknumber=ord(versekey.Book()),
                    bookabbrev=versekey.getBookAbbrev(),
                    bookname=versekey.getBookName(),
                    reference=reference,
                    internal_reference=internal_reference,
                    osisRef=osisRef,
                )

                # usually RenderText flushes this out, but we haven't called
                # that yet - but we definitely don't want extraneous headings
                mod.getEntryAttributesMap().clear()

                # we want to do our pre-verse content first, but we can't
                # without running it through the optionFilter first.
                # we'll then have to run it through again after, otherwise our
                # entryattributes may go walkabout
                if raw: rawentry_str = rawentry.c_str()
                mod.optionFilter(rawentry, versekey)
                if raw: option_filtered = rawentry.c_str()

                headings = self.get_headings(internal_reference, mod)
                #versekey = VK.castTo(key)
                heading_dicts = []
                raw_headings = []
                for heading, canonical in headings:
                    # the new-style pre-verse content lives wrapped up in
                    # <div>'s - it will contain the <title>, but the div will
                    # be stripped out.
                    # the old-style pre-verse content lives in <title>'s,
                    # which will also be stripped out. Unfortunately, it isn't
                    # that easy to tell whether it did have a title, so we
                    # employ a heuristic - if it starts with an <, it is a new
                    # one...
                    nh = heading.startswith("<")
                    if stripped:
                        heading = mod.StripText(heading).decode(
                            "utf8", "replace")
                    else:
                        heading = render_text(heading).decode(
                            "utf8", "replace")

                    if not nh:
                        cls = " canonical" if (canonical
                                               and canonical == "true") else ""
                        heading = '<h2 class="heading%s">%s</h2>\n' % (cls,
                                                                       heading)

                    if raw:
                        raw_headings.append(heading)
                    heading_dict = dict(heading=heading, canonical=canonical)
                    heading_dict.update(body_dict)
                    heading_dicts.append(heading_dict)

                if stripped:
                    text = mod.StripText(rawentry.c_str(),
                                         rawentry.length()).decode(
                                             "utf-8", "replace")

                else:
                    # we can't use rawentry due to a static local buffer in
                    # swmodule.c; breaks gospel harmonies
                    text = (
                        render_text(  #rawentry.c_str(), rawentry.length()
                        ).decode("utf8", "replace"))

                # get our actual text
                if raw:
                    text = self.process_raw(rawentry_str, text, versekey, mod,
                                            raw_headings, option_filtered)

                user_comments = self.get_user_comments(osisRef, versekey)

                # XXX: This needs to be done better than this.  Move into
                # subclass somehow.
                if isinstance(self, Bible) and display_tags:
                    tags = self.insert_tags(osisRef, versekey,
                                            exclude_topic_tag)
                else:
                    tags = ""

                body_dict.update(
                    dict(text=text, tags=tags, usercomments=user_comments))

                yield body_dict, heading_dicts

                incrementer.increment(1)
                verses_left -= 1

        finally:
            mod.setKey(SW.Key())
            mod.setSkipConsecutiveLinks(old_mod_skiplinks)

        self.end_of_render = ""
        if render_end:
            self.end_of_render = render_end()
Beispiel #10
0
	def GetReference_yield(self, verselist, max_verses=177, 
			raw=False, stripped=False, module=None, exclude_topic_tag=None,
			display_tags=True, skip_linked_verses=True):
		"""GetReference_yield: 
			yield the body dictionary and headings dictinoary
			for each reference.

		Preconditions:
			one of module or self.mod is not None
			verselist is valid"""
		#only for bible keyed books
		verselist.setPosition(TOP)
		verselist.Persist(1)
		u_vk = pysw.UserVK()
		u_vk.Headings(1)
		versekey = SW.VerseKey()
		versekey.Headings(1)
		mod = module or self.mod
		old_mod_skiplinks = mod.getSkipConsecutiveLinks()
		mod.setSkipConsecutiveLinks(True)
		mod.SetKey(verselist)
		verses_left = max_verses

		render_text, render_start, render_end = self.get_rendertext(mod)
		if render_start: render_start()

		try:
			incrementer = mod if skip_linked_verses else verselist
			while not self.has_error(incrementer):
				if verses_left == 0:
					yield None, None
					break
				
				if not skip_linked_verses:
					mod.SetKey(verselist)
				key = mod.getKey()
				#versekey = VK.castTo(key)
				versekey.setText(key.getText())
				#if(self.headings):
				#	versekey.Headings(1)
				osisRef = versekey.getOSISRef()
				internal_reference = versekey.getText()
				
				rawentry = mod.getRawEntryBuf()
				if skip_linked_verses and not rawentry.length():
					# don't include empty text; typically this may be at the
					# start of the chapter or something...
					incrementer.increment(1)
					continue

				start_verse = end_verse = versekey.Verse()
				
				# look forwards and backwards to see what the linked verse
				# number is (e.g. 3-5). Note: currently this won't cross
				# chapter boundaries
				vk = versekey.clone()
				vk = versekey.castTo(vk)
				vk.thisown=True
				vk.Headings(0)
				while(vk.Error() == '\x00' 
					and vk.Chapter() == versekey.Chapter() 
					and mod.isLinked(vk, versekey)):
					end_verse = vk.Verse()
					vk.increment(1)
				
				vk.copyFrom(versekey)
				vk.Headings(0)

				# hopefully we won't see anything backwards, but it is
				# possible (i.e. if we start in the middle of a linked
				# verse
				while(vk.Error() == '\x00'
					and vk.Chapter() == versekey.Chapter() 
					and mod.isLinked(vk, versekey)):				
					start_verse = vk.Verse()

					vk.decrement(1)
				
				if start_verse == end_verse:
					verse = "%d" % start_verse
				else:
					verse = "%d-%d" % (start_verse, end_verse)
				
				u_vk.setText(internal_reference)				
				if internal_reference.endswith(":0"):
					if start_verse != end_verse:
						print "WARNING: unhandled linked verse with verse 0"
						
					if versekey.Chapter() == 0:
						reference = u_vk.getBookName()
					else:
						reference = u_vk.get_book_chapter()

				else:
					reference = u_vk.get_book_chapter()
					reference += ":" + verse
					
				body_dict = dict(
					# text comes later
					versenumber = process_digits(verse,
						userOutput=True), 
					chapternumber = process_digits(
						str(versekey.Chapter()),
						userOutput=True),
					booknumber = ord(versekey.Book()),
					bookabbrev = versekey.getBookAbbrev(),
					bookname = versekey.getBookName(),
					reference = reference,
					internal_reference = internal_reference,
					osisRef = osisRef,
				)	
				
				# usually RenderText flushes this out, but we haven't called
				# that yet - but we definitely don't want extraneous headings
				mod.getEntryAttributesMap().clear()

				# we want to do our pre-verse content first, but we can't
				# without running it through the optionFilter first.
				# we'll then have to run it through again after, otherwise our
				# entryattributes may go walkabout
				if raw: rawentry_str = rawentry.c_str()
				mod.optionFilter(rawentry, versekey)
				if raw: option_filtered = rawentry.c_str()
				
				headings = self.get_headings(internal_reference, mod)
				#versekey = VK.castTo(key)
				heading_dicts = []
				raw_headings = []
				for heading, canonical in headings:
					# the new-style pre-verse content lives wrapped up in
					# <div>'s - it will contain the <title>, but the div will
					# be stripped out.
					# the old-style pre-verse content lives in <title>'s,
					# which will also be stripped out. Unfortunately, it isn't
					# that easy to tell whether it did have a title, so we
					# employ a heuristic - if it starts with an <, it is a new
					# one...
					nh = heading.startswith("<")
					if stripped:
						heading = mod.StripText(heading).decode(
							"utf8",
							"replace"
						)
					else:
						heading = render_text(heading).decode("utf8", "replace")

					if not nh:
						cls = " canonical" if (canonical and canonical == "true") else ""
						heading = '<h2 class="heading%s">%s</h2>\n' % (cls, heading)

					if raw:
						raw_headings.append(heading)
					heading_dict = dict(heading=heading, canonical=canonical)
					heading_dict.update(body_dict)
					heading_dicts.append(heading_dict)
					



				
				if stripped:
					text = mod.StripText(rawentry.c_str(), rawentry.length()).decode("utf-8", "replace")			

				else:
					# we can't use rawentry due to a static local buffer in
					# swmodule.c; breaks gospel harmonies
					text = (render_text(#rawentry.c_str(), rawentry.length()
									   ).decode("utf8", "replace"))
				
				# get our actual text
				if raw:
					text = self.process_raw(rawentry_str, text, versekey, mod,
						raw_headings, option_filtered)

				user_comments = self.get_user_comments(osisRef, versekey)

				# XXX: This needs to be done better than this.  Move into
				# subclass somehow.
				if isinstance(self, Bible) and display_tags:
					tags = self.insert_tags(osisRef, versekey, exclude_topic_tag)
				else:
					tags = ""
				
				body_dict.update(dict(text=text,
									  tags=tags,
									  usercomments=user_comments))

						  
				yield body_dict, heading_dicts

				incrementer.increment(1)
				verses_left -= 1

		finally:
			mod.setKey(SW.Key())
			mod.setSkipConsecutiveLinks(old_mod_skiplinks)
		
		self.end_of_render = ""
		if render_end:
			self.end_of_render = render_end()