python标记的wikilink扩展
mdx-wikilink-plus的Python项目详细描述
将wikilinks([[wikilink]]
)转换为相对链接。绝对链接保持原样(如果没有显式地给出标签,则使用url中的文件路径部分生成的自动标签)。
您不应将markdown.extensions.wikilinks与此链接一起使用。此扩展旨在提供markdown.extensions.wikilinks的功能以及一些附加功能。选择其中一个。
安装
pip install mdx_wikilink_plus
WikiLink语法
通用格式为:
- 没有显式标签:
[[wikilink]]
- 带有显式标签:
[[ link | label ]]
用法
text="[[wikilink]]"md=markdown.Markdown(extensions=['mdx_wikilink_plus'])html=md.convert(text)
快速示例
[[/path/to/file-name]]
将变成:
<p><aclass="wikilink"href="/path/to/file-name">File Name</a></p>
[[https://www.example.com/example-tutorial]]
将变成:
<p><aclass="wikilink"href="https://www.example.com/example-tutorial">Example Tutorial</a></p>
而[[https://www.example.com/?a=b&b=c]]
将变成:
<p><aclass="wikilink"href="https://www.example.com/?a=b&b=c">www.example.com</a></p>
配置
配置选项为:
Config param | Default | Details |
---|---|---|
base_url | ^{ | Prepended to the file_path part of the URL. A ^{ |
end_url | ^{ | Appended to the file_path part of the URL. If end_url is given (non-empty), then any ^{ |
url_whitespace | ^{ | Replace all whitespace in the file_path path with this character (string) when building the URL. |
label_case | ^{ | Choose case of the label. Available options: titlecase, capitalize, none. Capitalize will capitalize the first character only. |
html_class | ^{ | Set custom HTML classes on the anchor tag. It does not add classes rather it resets any previously set value. |
build_url | ^{ | A callable that returns the URL string. Default build_url callable |
除了html类和构建url之外,其他配置都不应用于绝对url。(是,标签盒也不起作用)
通过元数据进行配置
配置也可以通过元数据(markdown.extensions.meta)传递。
我们识别以下模板:
wiki_base_url: /static/ wiki_end_url: wiki_url_whitespace: _ wiki_label_case: capitalize wiki_html_class: wikilink The first line of the document
配置示例:
md_configs={'mdx_wikilink_plus':{'base_url':'/static','end_url':'.html','url_whitespace':'-','label_case':'titlecase','html_class':'a-custom-class',#'build_url': build_url, # A callable# all of the above config params are optional},}text="""[[/path/to/file-name]][[/path/to/file name/?a=b&b=c]]"""md=markdown.Markdown(extensions=['mdx_wikilink_plus'],extension_configs=md_configs)print(md.convert(text))
输出为:
<p><aclass="a-custom-class"href="/static/path/to/file-name.html">File Name</a></p><p><aclass="a-custom-class"href="/static/path/to/file-name.html?a=b&b=c">File Name</a></p>
啊!!!信息
end_url
被添加到url文件路径部分的末尾。
更多示例
我们的测试降价:
[[wikilink]] `[[wikilink]]`
[[/path/to/file name]]
[[/path/to/file_name]]
[[/path/to/file-name]]
[[/path/to/file name/?a=b&b=c]]
[[/path/to/file name.html]]
[[/path/to/file name.html?a=b&b=c]]
[[https://www.example.com/?]]
[[https://www.example.com/?a=b&b=c]]
[[https://www.example.com/example-tutorial]]
[[https://www.example.com/example-tutorial | Example Tutorial]]
不带任何配置的输出
<p><aclass="wikilink"href="wikilink">Wikilink</a><code>[[wikilink]]</code></p><p><aclass="wikilink"href="/path/to/file-name">File Name</a></p><p><aclass="wikilink"href="/path/to/file_name">File Name</a></p><p><aclass="wikilink"href="/path/to/file-name">File Name</a></p><p><aclass="wikilink"href="/path/to/file-name/?a=b&b=c">File Name</a></p><p><aclass="wikilink"href="/path/to/file-name.html">File Name</a></p><p><aclass="wikilink"href="/path/to/file-name.html?a=b&b=c">File Name</a></p><p><aclass="wikilink"href="https://www.example.com/">www.example.com</a></p><p><aclass="wikilink"href="https://www.example.com/?a=b&b=c">www.example.com</a></p><p><aclass="wikilink"href="https://www.example.com/example-tutorial">Example Tutorial</a></p><p><aclass="wikilink"href="https://www.example.com/example-tutorial">Example Tutorial</a></p>
使用配置
使用配置
'mdx_wikilink_plus':{'base_url':'/static','end_url':'.html','url_whitespace':'-','label_case':'titlecase','html_class':'a-custom-class',},
输出为:
<p><aclass="a-custom-class"href="/static/wikilink.html">Wikilink</a><code>[[wikilink]]</code></p><p><aclass="a-custom-class"href="/static/path/to/file-name.html">File Name</a></p><p><aclass="a-custom-class"href="/static/path/to/file_name.html">File Name</a></p><p><aclass="a-custom-class"href="/static/path/to/file-name.html">File Name</a></p><p><aclass="a-custom-class"href="/static/path/to/file-name.html?a=b&b=c">File Name</a></p><p><aclass="a-custom-class"href="/static/path/to/file-name.html">File Name</a></p><p><aclass="a-custom-class"href="/static/path/to/file-name.html?a=b&b=c">File Name</a></p><p><aclass="a-custom-class"href="https://www.example.com/">www.example.com</a></p><p><aclass="a-custom-class"href="https://www.example.com/?a=b&b=c">www.example.com</a></p><p><aclass="a-custom-class"href="https://www.example.com/example-tutorial">Example Tutorial</a></p><p><aclass="a-custom-class"href="https://www.example.com/example-tutorial">Example Tutorial</a></p>
使用meta(markdown.extensions.meta
)
将以下元添加到降价中:
wiki_base_url: /static/ wiki_end_url: wiki_url_whitespace: _ wiki_label_case: capitalize wiki_html_class: wikilink
输出为:
<p><aclass="wikilink"href="/static/wikilink">Wikilink</a><code>[[wikilink]]</code></p><p><aclass="wikilink"href="/static/path/to/file_name">File name</a></p><p><aclass="wikilink"href="/static/path/to/file_name">File name</a></p><p><aclass="wikilink"href="/static/path/to/file-name">File name</a></p><p><aclass="wikilink"href="/static/path/to/file_name/?a=b&b=c">File name</a></p><p><aclass="wikilink"href="/static/path/to/file_name.html">File name</a></p><p><aclass="wikilink"href="/static/path/to/file_name.html?a=b&b=c">File name</a></p><p><aclass="wikilink"href="https://www.example.com/">www.example.com</a></p><p><aclass="wikilink"href="https://www.example.com/?a=b&b=c">www.example.com</a></p><p><aclass="wikilink"href="https://www.example.com/example-tutorial">Example tutorial</a></p><p><aclass="wikilink"href="https://www.example.com/example-tutorial">Example Tutorial</a></p>
构建url可调用
默认的build_url
函数定义为:
defbuild_url(urlo,base,end,url_whitespace):""" Build and return a valid url. Parameters ---------- urlo A ParseResult object returned by urlparse base base_url from config end end_url from config url_whitespace url_whitespace from config Returns ------- URL string """ifnoturlo.netloc:ifnotend:clean_target=re.sub(r'\s+',url_whitespace,urlo.path)else:clean_target=re.sub(r'\s+',url_whitespace,urlo.path.rstrip('/'))ifclean_target.endswith(end):end=''ifbase.endswith('/'):path="%s%s%s"%(base,clean_target.lstrip('/'),end)elifbaseandnotclean_target.startswith('/'):path="%s/%s%s"%(base,clean_target,end)else:path="%s%s%s"%(base,clean_target,end)urlo=urlo._replace(path=path)returnurlunparse(urlo)