python标记的wikilink扩展

mdx-wikilink-plus的Python项目详细描述


Build Status

将wikilinks([[wikilink]])转换为相对链接。绝对链接保持原样(如果没有显式地给出标签,则使用url中的文件路径部分生成的自动标签)。

您不应将markdown.extensions.wikilinks与此链接一起使用。此扩展旨在提供markdown.extensions.wikilinks的功能以及一些附加功能。选择其中一个。

安装

pip install mdx_wikilink_plus

WikiLink语法

通用格式为:

  1. 没有显式标签:[[wikilink]]
  2. 带有显式标签:[[ 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&amp;b=c">www.example.com</a></p>

配置

配置选项为:

Config paramDefaultDetails
base_url^{}Prepended to the file_path part of the URL. A ^{} at the end of the base_url will be handled intelligently.
end_url^{}Appended to the file_path part of the URL. If end_url is given (non-empty), then any ^{} at the end of the file_path part in the URL is removed. If the end_url matches the extension of the file_path part, it will be ignored, for example, if end_url is ^{} and the wikilink provided is ^{}, then the URL will be ^{} not ^{}.
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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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&amp;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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用无循环和乘法的递归调用查找值   java字符串大小冲突   在一组Java文件对象中查找唯一的超级目录   没有Eclipse控制台输出窗口(Java)?   java这怎么等于105而不是15?   java Adempiere列调出,用于不处理从(代码)选项导入和创建行的字段   java tomcat、2个webapps、2个log4js,但这两个应用都记录到一个文件中   lambda理解Java谓词   HotspotFX上的Java EOF问题   java google应用程序引擎:如何向连接/断开通道“ping”添加信息?   java如何使用VTDXML获取一个元素的所有名称空间声明?   java如何使用drawLine()获得一条线以随机方向拍摄?   java transactionManager应该使用哪个SessionFactory?   java在安卓上播放声音   在Mac上使用JBDC对SQL Server进行java Windows身份验证   java基本列表和字符串[]   java NamedParameterJdbcTemplate从中选择*   扩展上的java Android可扩展列表视图   使用ApacheAxis2的java SOAP附件