Search code examples
pythonmarkupbitbucketcreole

Change internal links rendering using python-creole


My goal is to create a locally-browsable clone of the bitbucket's wiki browser. Pages are written using creole syntax.

I'm using python-creole to render the files into html. It works relatively fine, but there is a difference between the way python-creole and bitbucket render internal links.

On the Bitbucket site, an internal link with spaces like [[system programming]] will render to something like <a href="/wiki/system_programming">system programming</a> (spaces are replaced by _ ) while using python-creole this will render to <a href="system programming">system programming</a>.

Can I tweak python-creole into replacing spaces by _ and how?


Solution

  • I think I have found a quite dirty way to do this. Looking to creole source-code, the code which turns links to html is here:

    def link_emit(self, node):
        target = node.content
        if node.children:
            inside = self.emit_children(node)
        else:
            inside = self.html_escape(target)
    
        return '<a href="%s">%s</a>' % (
            self.attr_escape(target), inside)
    

    In a python shell I have tried the following code:

    >>> import creole
    >>> from creole.creole2html import emitter
    >>> def new_emitter(self, node):
    ...    return 'blah'
    >>> emitter.HtmlEmitter.link_emit = new_emitter
    >>> creole.creole2html(u"[[link]]")
    u'<p>blah</p>'
    

    The exact code to replace spaces by '_' is left as an exercice to the reader...

    I'm still looking for a more correct way to do this in "the official way".