示例#1
0
	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
示例#2
0
	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