예제 #1
0
파일: clipboard.py 프로젝트: thejeshgn/Zim
def _link_tree(links, notebook, path):
	# Convert a list of links (of any type) into a parsetree
	#~ print 'LINKS: ', links
	#~ print 'NOTEBOOK and PATH:', notebook, path
	builder = TreeBuilder()
	builder.start('zim-tree')
	for i in range(len(links)):
		if i > 0:
			builder.data(' ')

		link = links[i]
		type = link_type(link)
		isimage = False
		if type == 'file':
			try:
				file = File(link)
				isimage = file.isimage()
			except:
				pass

		logger.debug('Pasting link: %s (type: %s, isimage: %s)', link, type, isimage)

		if isimage:
			src = notebook.relative_filepath(file, path) or file.uri
			builder.start('img', {'src': src})
			builder.end('img')
		elif link.startswith('@'):
			# FIXME - is this ever used ??
			builder.start('tag', {'name': links[i][1:]})
			builder.data(links[i])
			builder.end('tag')
		else:
			if type == 'page':
				href = Path(notebook.cleanup_pathname(link)) # Assume links are always absolute
				link = notebook.relative_link(path, href) or link
			elif type == 'file':
				file = File(link) # Assume links are always URIs
				link = notebook.relative_filepath(file, path) or file.uri

			builder.start('link', {'href': link})
			builder.data(link)
			builder.end('link')

	builder.end('zim-tree')
	tree = ParseTree(builder.close())
	tree.resolve_images(notebook, path)
	tree.decode_urls()
	return tree
예제 #2
0
파일: templates.py 프로젝트: thejeshgn/Zim
	def __call__(self, dict, root=':', collapse=True, ignore_empty=True):
		builder = TreeBuilder()

		collapse = bool(collapse) and not collapse == 'False'
		ignore_empty = bool(ignore_empty) and not ignore_empty == 'False'

		if isinstance(root, PageProxy):
			# allow [% menu(page) %] vs [% menu(page.name) %]
			root = root.name

		expanded = [self._page] + list(self._page.parents())

		def add_namespace(path):
			builder.start('ul')

			pagelist = self._notebook.index.list_pages(path)
			for page in pagelist:
				if ignore_empty and not page.exists():
					continue
				builder.start('li')

				if page == self._page:
					# Current page is marked with the strong style
					builder.start('strong', {'_class': 'activepage'}) # HACK - used by Html output
					builder.data(page.basename)
					builder.end('strong')
				else:
					# links to other pages
					builder.start('link', {'type': 'page', 'href': ':'+page.name})
					builder.data(page.basename)
					builder.end('link')

				builder.end('li')
				if page.haschildren:
					if collapse:
						# Only recurs into namespaces that are expanded
						if page in expanded:
							add_namespace(page) # recurs
					else:
						add_namespace(page) # recurs

			builder.end('ul')

		builder.start('page')
		add_namespace(Path(root))
		builder.end('page')

		tree = ParseTree(builder.close())
		if not tree:
			return None

		#~ print "!!!", tree.tostring()

		format = self._format
		linker = self._linker

		dumper = format.Dumper(
			linker=linker,
			template_options=self._options )

		return ''.join(dumper.dump(tree))