Python Markdown:将Markdown字符串转换为HTML时,如何为媒体配置基本URL [英] Python Markdown: How can I config Base URL for Media when markdown string into HTML

查看:104
本文介绍了Python Markdown:将Markdown字符串转换为HTML时,如何为媒体配置基本URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

# H1 tag
h1 content is here

![](/media/blog/1551266934_21_289.jpg)

如您所见,我有一张图像![](/media/blog/1551266934_21_289.jpg)(与![](mydomain.com/media/blog/1551266934_21_289.jpg)相同)

As you see, I have an image ![](/media/blog/1551266934_21_289.jpg) (Same as ![](mydomain.com/media/blog/1551266934_21_289.jpg))

降价时,它变为:<img src="/media/blog/1551266934_21_289.jpg"/>

但是我想结果是使用了不同的基本URL:

But I want result is using different Base url:

<img src="https://media.mydomain.com/media/blog/1551266934_21_289.jpg"/>

推荐答案

您可以使用Python-Markdown的

You can use Python-Markdown's Extension API and develop a plugin which alters the src attribute of all images.

在这种特定情况下,可以通过对ImageInlineProcessorReferenceInlineProcessor类进行子类化来覆盖image_linkimage_reference内联处理器.但是,无需更改解析器的行为.您只需要修改所有img标记的src属性.使用 TreeProcessor 最简单.

In this specific case, you could override the image_link and image_reference inline processors by subclassing the ImageInlineProcessor and ReferenceInlineProcessor classes. But there is no need to alter the parser's behavior. You only need to modify the src attribute of all img tags. This would be easiest with a TreeProcessor.

from markdown.treeprocessors import Treeprocessor
from urllib.parse import urljoin

BASE = 'https://media.mydomain.com/'

class ImgBaseTreeprocessor(Treeprocessor):
    def run(self, root):
        # Loop through all img elements
        for img in root.getiterator('img'):
            # Join base to the src URL
            img.set('src', urljoin(BASE, img.get('src'))

现在,您需要使用Extension子类向Markdown类介绍新的Treeprocessor:

Now you need to tell the Markdown class about your new Treeprocessor with an Extension subclass:

from markdown.extensions import Extension

class ImgBase(Extension):
    def extendMarkdown(self, md, md_globals):
        # register the new treeprocessor with priority 15 (run after 'inline')
        md.treeprocessors.register(ImgBaseTreeprocessor(md), 'imgbase', 15)

最后,您需要告诉Markdown使用您的新扩展名:

Finally, you need to tell Markdown to use your new extension:

from markdown import markdown

html = markdown(text, extensions=[ImgBase()])

您可以做一些改进扩展的事情,留给读者练习:

There are a few things you might do to improve the extension, which are left as an exercise for the reader:

  1. 在扩展名中添加配置设置以设置基本URL而不是对其进行硬编码.
  2. 在加入基础时进行一些错误检查,以确保现有的URL尚没有基础.
  3. 将其包装到Python包中,以与他人分发和共享.
  1. Add a configuration setting to the extension to set the base URL rather than hardcoding it.
  2. Do some error checking when joining the base to ensure the existing URL doesn't already have a base.
  3. Wrap it up into a Python package to distribute and share with others.

这篇关于Python Markdown:将Markdown字符串转换为HTML时,如何为媒体配置基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆