使用 python-creole 更改内部链接渲染

发布于 2024-12-12 18:29:05 字数 535 浏览 0 评论 0原文

我的目标是创建 bitbucket wiki 浏览器的本地可浏览克隆。 页面使用 creole 语法编写。

我正在使用 python-creole 将文件渲染为 html。它工作得相对较好,但是 python-creole 和 bitbucket 渲染内部链接的方式存在差异。

在 Bitbucket 站点上,带有诸如 [[systemprogramming]] 之类的空格的内部链接将呈现为类似 systemprogramming (空格被 _ 替换)在使用 python-creole 时,这将渲染为 system编程

我可以调整 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疏忽 2024-12-19 18:29:05

Ascobol 的答案有效,但使用类继承更干净。

这是我正在制作的 wiki 应用程序的摘录(略有改动)。它更改链接和表格的输出。如果您想查看可以重写哪些方法,可以查看 python-creole 的源代码。

class WikiLinkHtmlEmitter(HtmlEmitter):
    def link_emit(self, node):
        target = node.content
        if node.children:
            inside = self.emit_children(node)
        else:
            inside = self.html_escape(target)
        m = self.link_rules.addr_re.match(target)
        if m:
            if m.group('extern_addr'):
                return u'<a href="%s">%s</a>' % (
                    self.attr_escape(target), inside)
            elif m.group('inter_wiki'):
                raise NotImplementedError
        if re.match(r'^\S+@\S+
, target):
            target = 'mailto:%s' % target
            return u'<a href="%s">%s</a>' % (
                self.attr_escape(target), inside)

        target = target.lower()
        target = slugify(target)

        target = '/wiki/' + target

        return u'<a href="%s" class="%s">%s</a>' % (
            self.attr_escape(target), classes, inside)

    def table_emit(self, node):
        return u'''
        <table class="table table-bordered table-striped">
            \n%s
        </table>\n''' % self.emit_children(node)

def creole_markup(value):
    document = Parser(value).parse()
    return mark_safe(WikiLinkHtmlEmitter(document).emit())

Ascobol's answer works, but it is cleaner to use class inheritance.

This is an (slightly changed) extract from a wiki-application I am making. It changes the output from links and tables. If you want to see which methods you can override, you can look at the source-code of python-creole.

class WikiLinkHtmlEmitter(HtmlEmitter):
    def link_emit(self, node):
        target = node.content
        if node.children:
            inside = self.emit_children(node)
        else:
            inside = self.html_escape(target)
        m = self.link_rules.addr_re.match(target)
        if m:
            if m.group('extern_addr'):
                return u'<a href="%s">%s</a>' % (
                    self.attr_escape(target), inside)
            elif m.group('inter_wiki'):
                raise NotImplementedError
        if re.match(r'^\S+@\S+
, target):
            target = 'mailto:%s' % target
            return u'<a href="%s">%s</a>' % (
                self.attr_escape(target), inside)

        target = target.lower()
        target = slugify(target)

        target = '/wiki/' + target

        return u'<a href="%s" class="%s">%s</a>' % (
            self.attr_escape(target), classes, inside)

    def table_emit(self, node):
        return u'''
        <table class="table table-bordered table-striped">
            \n%s
        </table>\n''' % self.emit_children(node)

def creole_markup(value):
    document = Parser(value).parse()
    return mark_safe(WikiLinkHtmlEmitter(document).emit())
尽揽少女心 2024-12-19 18:29:05

我想我找到了一种相当肮脏的方法来做到这一点。查看克里奥尔语源代码,将链接转换为 html 的代码在这里:

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)

在 python shell 中,我尝试了以下代码:

>>> 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>'

用 '_' 替换空格的确切代码留给读者作为练习...

我我仍在寻找一种更正确的方法来以“官方方式”执行此操作。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文