Python Markdown:将Markdown字符串转换为HTML时,如何为媒体配置基本URL [英] Python Markdown: How can I config Base URL for Media when markdown string into HTML
问题描述
我有一个字符串:
# 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"/>
推荐答案
You can use Python-Markdown's Extension API and develop a plugin which alters the src
attribute of all images.
在这种特定情况下,可以通过对ImageInlineProcessor
和ReferenceInlineProcessor
类进行子类化来覆盖image_link
和image_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:
- 在扩展名中添加配置设置以设置基本URL而不是对其进行硬编码.
- 在加入基础时进行一些错误检查,以确保现有的URL尚没有基础.
- 将其包装到Python包中,以与他人分发和共享.
- Add a configuration setting to the extension to set the base URL rather than hardcoding it.
- Do some error checking when joining the base to ensure the existing URL doesn't already have a base.
- Wrap it up into a Python package to distribute and share with others.
这篇关于Python Markdown:将Markdown字符串转换为HTML时,如何为媒体配置基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!