def link_emit(self, node): target = node.content m = self.rules.addr_re.match(target) if m: if m.group("page_name"): # link to a page word = m.group("page_name") if word.startswith(wikiutil.PARENT_PREFIX): word = word[wikiutil.PARENT_PREFIX_LEN :] elif word.startswith(wikiutil.CHILD_PREFIX): word = "%s/%s" % (self.formatter.page.page_name, word[wikiutil.CHILD_PREFIX_LEN :]) word, anchor = wikiutil.split_anchor(word) return "".join( [ self.formatter.pagelink(1, word, anchor=anchor), self.emit_children(node) or self.formatter.text(target), self.formatter.pagelink(0, word), ] ) elif m.group("extern_addr"): # external link address = m.group("extern_addr") proto = m.group("extern_proto") return "".join( [ self.formatter.url(1, address, css=proto), self.emit_children(node) or self.formatter.text(target), self.formatter.url(0), ] ) elif m.group("inter_wiki"): # interwiki link wiki = m.group("inter_wiki") page = m.group("inter_page") page, anchor = wikiutil.split_anchor(page) return "".join( [ self.formatter.interwikilink(1, wiki, page, anchor=anchor), self.emit_children(node) or self.formatter.text(page), self.formatter.interwikilink(0), ] ) elif m.group("attach_scheme"): # link to an attachment scheme = m.group("attach_scheme") attachment = m.group("attach_addr") url = wikiutil.url_unquote(attachment) text = self.get_text(node) return "".join( [ self.formatter.attachment_link(1, url), self.formatter.text(text), self.formatter.attachment_link(0), ] ) return "".join(["[[", self.formatter.text(target), "]]"])
def link_emit(self, node): target = node.content m = self.rules.addr_re.match(target) if m: if m.group('page_name'): # link to a page word = m.group('page_name') if word.startswith(wikiutil.PARENT_PREFIX): word = word[wikiutil.PARENT_PREFIX_LEN:] elif word.startswith(wikiutil.CHILD_PREFIX): word = "%s/%s" % (self.formatter.page.page_name, word[wikiutil.CHILD_PREFIX_LEN:]) word, anchor = wikiutil.split_anchor(word) return ''.join([ self.formatter.pagelink(1, word, anchor=anchor), self.emit_children(node) or self.formatter.text(target), self.formatter.pagelink(0, word), ]) elif m.group('extern_addr'): # external link address = m.group('extern_addr') proto = m.group('extern_proto') return ''.join([ self.formatter.url(1, address, css=proto), self.emit_children(node) or self.formatter.text(target), self.formatter.url(0), ]) elif m.group('inter_wiki'): # interwiki link wiki = m.group('inter_wiki') page = m.group('inter_page') page, anchor = wikiutil.split_anchor(page) return ''.join([ self.formatter.interwikilink(1, wiki, page, anchor=anchor), self.emit_children(node) or self.formatter.text(page), self.formatter.interwikilink(0), ]) elif m.group('attach_scheme'): # link to an attachment scheme = m.group('attach_scheme') attachment = m.group('attach_addr') url = wikiutil.url_unquote(attachment) text = self.get_text(node) return ''.join([ self.formatter.attachment_link(1, url), self.formatter.text(text), self.formatter.attachment_link(0) ]) return "".join(["[[", self.formatter.text(target), "]]"])
def testsplit_anchor(): """ TODO: add the test for for split_anchor when we have better approach to deal wih problems like "#MoinMoin#" returning ("#MoinMoin", "") """ result = wikiutil.split_anchor('MoinMoin') expected = 'MoinMoin', '' assert result == expected result = wikiutil.split_anchor('MoinMoin#test_anchor|label|attr=val') expected = ['MoinMoin', 'test_anchor|label|attr=val'] assert result == expected result = wikiutil.split_anchor('#MoinMoin#') expected = ['#MoinMoin', ''] assert result == expected
def visit_reference(self, node): """ Pass links to MoinMoin to get the correct wiki space url. Extract the url and pass it on to the html4css1 writer to handle. Inline images are also handled by visit_image. Not sure what the "drawing:" link scheme is used for, so for now it is handled here. Also included here is a hack to allow MoinMoin macros. This routine checks for a link which starts with "<<". This link is passed to the MoinMoin formatter and the resulting markup is inserted into the document in the place of the original link reference. """ if 'refuri' in node.attributes: refuri = node['refuri'] prefix = '' link = refuri if ':' in refuri: prefix, link = refuri.lstrip().split(':', 1) # First see if MoinMoin should handle completely. Exits through add_wiki_markup. if refuri.startswith('<<') and refuri.endswith('>>'): # moin macro self.process_wiki_text(refuri) self.wiki_text = self.fixup_wiki_formatting(self.wiki_text) self.add_wiki_markup() if prefix == 'drawing': self.process_wiki_text("[[%s]]" % refuri) self.wiki_text = self.fixup_wiki_formatting(self.wiki_text) self.add_wiki_markup() # From here down, all links are handled by docutils (except # missing attachments), just fixup node['refuri']. if prefix == 'attachment': if not AttachFile.exists(self.request, self.request.page.page_name, link): # Attachment doesn't exist, give to MoinMoin to insert upload text. self.process_wiki_text("[[%s]]" % refuri) self.wiki_text = self.fixup_wiki_formatting(self.wiki_text) self.add_wiki_markup() # Attachment exists, just get a link to it. node['refuri'] = AttachFile.getAttachUrl( self.request.page.page_name, link, self.request) if not [ i for i in node.children if i.__class__ == docutils.nodes.image ]: node['classes'].append(prefix) elif prefix == 'wiki': wiki_name, page_name = wikiutil.split_interwiki(link) wikitag, wikiurl, wikitail, err = wikiutil.resolve_interwiki( self.request, wiki_name, page_name) wikiurl = wikiutil.mapURL(self.request, wikiurl) node['refuri'] = wikiutil.join_wiki(wikiurl, wikitail) # Only add additional class information if the reference does # not have a child image (don't want to add additional markup # for images with targets). if not [ i for i in node.children if i.__class__ == docutils.nodes.image ]: node['classes'].append('interwiki') elif prefix == 'javascript': # is someone trying to do XSS with javascript? node['refuri'] = 'javascript:alert("it does not work")' elif prefix != '': # Some link scheme (http, file, https, mailto, etc.), add class # information if the reference doesn't have a child image (don't # want additional markup for images with targets). # Don't touch the refuri. if not [ i for i in node.children if i.__class__ == docutils.nodes.image ]: node['classes'].append(prefix) else: # Default case - make a link to a wiki page. pagename, anchor = wikiutil.split_anchor(refuri) page = Page( self.request, wikiutil.AbsPageName(self.formatter.page.page_name, pagename)) node['refuri'] = page.url(self.request, anchor=anchor) if not page.exists(): node['classes'].append('nonexistent') html4css1.HTMLTranslator.visit_reference(self, node)
def visit_reference(self, node): """ Pass links to MoinMoin to get the correct wiki space url. Extract the url and pass it on to the html4css1 writer to handle. Inline images are also handled by visit_image. Not sure what the "drawing:" link scheme is used for, so for now it is handled here. Also included here is a hack to allow MoinMoin macros. This routine checks for a link which starts with "<<". This link is passed to the MoinMoin formatter and the resulting markup is inserted into the document in the place of the original link reference. """ if 'refuri' in node.attributes: refuri = node['refuri'] prefix = '' link = refuri if ':' in refuri: prefix, link = refuri.lstrip().split(':', 1) # First see if MoinMoin should handle completely. Exits through add_wiki_markup. if refuri.startswith('<<') and refuri.endswith('>>'): # moin macro self.process_wiki_text(refuri) self.wiki_text = self.fixup_wiki_formatting(self.wiki_text) self.add_wiki_markup() if prefix == 'drawing': self.process_wiki_text("[[%s]]" % refuri) self.wiki_text = self.fixup_wiki_formatting(self.wiki_text) self.add_wiki_markup() # From here down, all links are handled by docutils (except # missing attachments), just fixup node['refuri']. if prefix == 'attachment': if not AttachFile.exists(self.request, self.request.page.page_name, link): # Attachment doesn't exist, give to MoinMoin to insert upload text. self.process_wiki_text("[[%s]]" % refuri) self.wiki_text = self.fixup_wiki_formatting(self.wiki_text) self.add_wiki_markup() # Attachment exists, just get a link to it. node['refuri'] = AttachFile.getAttachUrl(self.request.page.page_name, link, self.request) if not [i for i in node.children if i.__class__ == docutils.nodes.image]: node['classes'].append(prefix) elif prefix == 'wiki': wiki_name, page_name = wikiutil.split_interwiki(link) wikitag, wikiurl, wikitail, err = wikiutil.resolve_interwiki(self.request, wiki_name, page_name) wikiurl = wikiutil.mapURL(self.request, wikiurl) node['refuri'] = wikiutil.join_wiki(wikiurl, wikitail) # Only add additional class information if the reference does # not have a child image (don't want to add additional markup # for images with targets). if not [i for i in node.children if i.__class__ == docutils.nodes.image]: node['classes'].append('interwiki') elif prefix != '': # Some link scheme (http, file, https, mailto, etc.), add class # information if the reference doesn't have a child image (don't # want additional markup for images with targets). # Don't touch the refuri. if not [i for i in node.children if i.__class__ == docutils.nodes.image]: node['classes'].append(prefix) else: # Default case - make a link to a wiki page. pagename, anchor = wikiutil.split_anchor(refuri) page = Page(self.request, wikiutil.AbsPageName(self.formatter.page.page_name, pagename)) node['refuri'] = page.url(self.request, anchor=anchor) if not page.exists(): node['classes'].append('nonexistent') html4css1.HTMLTranslator.visit_reference(self, node)