def createIndexPage(notebook, path, section=None): # TODO make more flexible - use pages iter itself instead of section of notebook if section is None: section = Path(':') title = notebook.name else: title = section.name builder = ParseTreeBuilder() def add_namespace(path): pagelist = notebook.pages.list_pages(path) builder.start(BULLETLIST) for page in pagelist: builder.start(LISTITEM) builder.append(LINK, { 'type': 'page', 'href': page.name }, page.basename) builder.end(LISTITEM) if page.haschildren: add_namespace(page) # recurs builder.end(BULLETLIST) builder.start(FORMATTEDTEXT) builder.append(HEADING, {'level': 1}, 'Index of %s\n' % title) add_namespace(section) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() #~ print "!!!", tree.tostring() indexpage = Page(path, False, MockFile('/index'), None) indexpage.set_parsetree(tree) return indexpage
def index_function(self, namespace=None, collapse=True, ignore_empty=True): '''Index function for export template @param namespace: the namespace to include @param collapse: if C{True} only the branch of the current page is shown, if C{False} the whole index is shown @param ignore_empty: if C{True} empty pages (placeholders) are not shown in the index ''' if not self._index_generator: return '' builder = ParseTreeBuilder() builder.start(FORMATTEDTEXT) builder.start(BULLETLIST) if self._index_page: expanded = [self._index_page] + list(self._index_page.parents()) else: expanded = [] stack = [] for path in self._index_generator(namespace): if self._index_page and collapse \ and not path.parent in expanded: continue # skip since it is not part of current path elif ignore_empty and not (path.hascontent or path.haschildren): continue # skip since page is empty if not stack: stack.append(path.parent) elif stack[-1] != path.parent: if path.ischild(stack[-1]): builder.start(BULLETLIST) stack.append(path.parent) else: while stack and stack[-1] != path.parent: builder.end(BULLETLIST) stack.pop() builder.start(LISTITEM) if path == self._index_page: # Current page is marked with the strong style builder.append(STRONG, text=path.basename) else: # links to other pages builder.append(LINK, {'type': 'page', 'href': ':'+path.name}, path.basename) builder.end(LISTITEM) for p in stack: builder.end(BULLETLIST) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() if not tree: return '' #~ print "!!!", tree.tostring() dumper = self.dumper_factory(None) return ''.join(dumper.dump(tree))
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 = ParseTreeBuilder() builder.start(FORMATTEDTEXT) for i in range(len(links)): if i > 0: builder.text(' ') link = links[i] type = link_type(link) isimage = False if type == 'interwiki': prefix = notebook.interwiki + '?' if link.startswith(prefix): link = link[len(prefix):] type = link_type(link) elif 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.append(IMAGE, {'src': src}) elif link.startswith('@'): # FIXME - is this ever used ?? builder.append(TAG, {'name': links[i][1:]}, links[i]) else: name = None if type == 'page': anchor = None if '#' in link: link, anchor = link.split('#', 1) target = Path(Path.makeValidPageName( link)) # Assume links are always absolute href = notebook.pages.create_link(path, target) href.anchor = anchor link = href.to_wiki_link() if notebook.config['Notebook']['short_links']: name = href.parts()[-1] if anchor: name += '#' + anchor elif type == 'file': file = File(link) # Assume links are always URIs link = notebook.relative_filepath(file, path) or file.uri builder.append(LINK, {'href': link}, name or link) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() tree.resolve_images(notebook, path) tree.decode_urls() return tree
class HeadingSplitter(Visitor): def __init__(self, max_level=None): self.max_level = max_level or 999 self._builder = ParseTreeBuilder() self.headings = [] def _split(self): self._builder.end(FORMATTEDTEXT) tree = self._builder.get_parsetree() if tree.hascontent: self.headings.append(tree) self._builder = ParseTreeBuilder() self._builder.start(FORMATTEDTEXT) def _close(self): tree = self._builder.get_parsetree() if tree.hascontent: self.headings.append(tree) def start(self, tag, attrib=None): if tag is HEADING and int(attrib['level']) <= self.max_level: self._split() self._builder.start(tag, attrib) def end(self, tag): self._builder.end(tag) if tag == FORMATTEDTEXT: self._close() def text(self, text): self._builder.text(text) def append(self, tag, attrib=None, text=None): if tag is HEADING and int(attrib['level']) <= self.max_level: self._split() self._builder.append(tag, attrib, text)
def get_data_as(self, targetid): if targetid == PAGELIST_TARGET_ID: link = "%s?%s" % (self.notebook.interwiki, self.path.name) if self.anchor: link += "#" + self.anchor return pack_urilist((link, )) elif targetid == TEXT_TARGET_ID: link = self.path.name if self.anchor: link += "#" + self.anchor return link elif targetid == PARSETREE_TARGET_ID: link = self.path.name if self.anchor: link += "#" + self.anchor if self.text: text = self.text elif self.notebook.config['Notebook']['short_links']: href = HRef.new_from_wiki_link(link) text = href.parts()[-1] if self.anchor: text += '#' + self.anchor else: text = link # Same logic as set_parsetree_attributes_to_resolve_links() but no need to resolve again builder = ParseTreeBuilder() builder.start(FORMATTEDTEXT) builder.append(LINK, {'href': link, '_href': self.path.name}, text) builder.end(FORMATTEDTEXT) parsetree = builder.get_parsetree() parsetree._set_root_attrib('notebook', self.notebook.interwiki) parsetree._set_root_attrib( 'page', '-') # force resolve on paste also on same page return parsetree.tostring() else: raise ValueError('Unknown target id %i' % targetid)
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 = ParseTreeBuilder() builder.start(FORMATTEDTEXT) for i in range(len(links)): if i > 0: builder.text(' ') 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.append(IMAGE, {'src': src}) elif link.startswith('@'): # FIXME - is this ever used ?? builder.append(TAG, {'name': links[i][1:]}, links[i]) else: if type == 'page': target = Path(Path.makeValidPageName( link)) # Assume links are always absolute href = notebook.pages.create_link(path, target) link = href.to_wiki_link() elif type == 'file': file = File(link) # Assume links are always URIs link = notebook.relative_filepath(file, path) or file.uri builder.append(LINK, {'href': link}, link) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() tree.resolve_images(notebook, path) tree.decode_urls() return tree
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 = ParseTreeBuilder() builder.start(FORMATTEDTEXT) for i in range(len(links)): if i > 0: builder.text(' ') 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.append(IMAGE, {'src': src}) elif link.startswith('@'): # FIXME - is this ever used ?? builder.append(TAG, {'name': links[i][1:]}, links[i]) 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.append(LINK, {'href': link}, link) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() tree.resolve_images(notebook, path) tree.decode_urls() return tree
def index_function(self, namespace=None, collapse=True, ignore_empty=True): '''Index function for export template @param namespace: the namespace to include @param collapse: if C{True} only the branch of the current page is shown, if C{False} the whole index is shown @param ignore_empty: if C{True} empty pages (placeholders) are not shown in the index ''' if not self._index_generator: return '' builder = ParseTreeBuilder() builder.start(FORMATTEDTEXT) if self._index_page: expanded = [self._index_page] + list(self._index_page.parents()) else: expanded = [] stack = [] if isinstance(namespace, PageProxy): namespace = Path(namespace.name) elif isinstance(namespace, str): namespace = Path(namespace) for path in self._index_generator(namespace): logger.info(path) if self._index_page and collapse \ and not path.parent in expanded: continue # skip since it is not part of current path #elif ignore_empty and not (path.hascontent or path.haschildren): - bug, should be page.hascontent, page.haschildren # continue # skip since page is empty if not stack: stack.append(path.parent) builder.start(BULLETLIST) elif stack[-1] != path.parent: if path.ischild(stack[-1]): builder.start(BULLETLIST) stack.append(path.parent) else: while stack and stack[-1] != path.parent: builder.end(BULLETLIST) stack.pop() builder.start(LISTITEM) if path == self._index_page: # Current page is marked with the strong style builder.append(STRONG, text=path.basename) else: # links to other pages builder.append(LINK, {'type': 'page', 'href': ':' + path.name}, path.basename) builder.end(LISTITEM) for p in stack: builder.end(BULLETLIST) builder.end(FORMATTEDTEXT) tree = builder.get_parsetree() if not tree: return '' #~ print("!!!", tree.tostring()) dumper = self.get_dumper(None) return ''.join(dumper.dump(tree))