如何在降价中检查键值元数据
How to check for key value metadata in markdown
我需要检查我使用 markdown 格式化的输入是否在开头有键值对元数据,然后在整个元数据块之后插入文本。
我在第一行寻找 :
,如果找到,则在第一个换行符处拆分输入字符串并添加我的内容。
现在,if markdown_content.splitlines()[0].find(':') >= 0:
显然在开头没有元数据时失败,但其他内容包含 :
而不是。
例子
输入元数据:
page title: fancypagetitle
something else: another value
# Heading
Text
输入没有元数据,但有 :
This is a [link](http://www.whosebug.com)
# Heading
Text
我的问题是:如何检查元数据块是否存在,如果存在,则在元数据和剩余降价之间添加一些内容。
元数据的定义
The keywords are case-insensitive and may consist of letters, numbers, underscores and dashes and must end with a colon. The values consist of anything following the colon on the line and may even be blank.
If a line is indented by 4 or more spaces, that line is assumed to be an additional line of the value for the previous keyword. A keyword may have as many lines as desired.
The first blank line ends all meta-data for the document. Therefore, the first line of a document must not be blank. All meta-data is stripped from the document prior to any further processing by Markdown.
来源:https://pythonhosted.org/Markdown/extensions/meta_data.html
您是否考虑过查看元数据扩展的源代码以了解它是如何完成的?
使用的regex是:
META_RE = re.compile(r'^[ ]{0,3}(?P<key>[A-Za-z0-9_-]+):\s*(?P<value>.*)')
当然还有副线的regex:
META_MORE_RE = re.compile(r'^[ ]{4,}(?P<value>.*)')
如果您注意到,这些正则表达式比您的更具体,并且匹配误报的可能性要小得多。然后扩展将文档分成几行,loops 通过与那些正则表达式比较的每一行,并在不匹配的第一行(可能是也可能不是空行)处跳出循环。
如果您在该代码中注意到,已经添加了一个 new feature,它将在下一个版本中提供。正在添加对可选 YAML 样式分隔符的支持。如果您习惯使用最新的(未发布的)开发代码,您可以将元数据包装在 YAML 分隔符中,这可能会更容易找到元数据的结尾。
例如,您上面的示例文档将如下所示(注意我使用了可选的结束特定分隔符 (...
),它更清楚地标记结束):
---
page title: fancypagetitle
something else: another value
...
# Heading
Text
也就是说,您仍然需要注意不要得到错误的匹配项(例如 <hr>
)。我想无论哪种方式,您都需要根据自己的需要重新实现元数据扩展中的所有内容。当然,它是开源的,所以只要你尊重 license.
抱歉,我无法确定何时发布下一个版本。
哦,看看 MultiMarkdown 提供的此功能的描述可能也会有所帮助,它启发了 Python-Markdown 中的功能。这可能会让您更清楚地了解可能包含元数据的内容。
我需要检查我使用 markdown 格式化的输入是否在开头有键值对元数据,然后在整个元数据块之后插入文本。
我在第一行寻找 :
,如果找到,则在第一个换行符处拆分输入字符串并添加我的内容。
现在,if markdown_content.splitlines()[0].find(':') >= 0:
显然在开头没有元数据时失败,但其他内容包含 :
而不是。
例子
输入元数据:
page title: fancypagetitle
something else: another value
# Heading
Text
输入没有元数据,但有 :
This is a [link](http://www.whosebug.com)
# Heading
Text
我的问题是:如何检查元数据块是否存在,如果存在,则在元数据和剩余降价之间添加一些内容。
元数据的定义
The keywords are case-insensitive and may consist of letters, numbers, underscores and dashes and must end with a colon. The values consist of anything following the colon on the line and may even be blank.
If a line is indented by 4 or more spaces, that line is assumed to be an additional line of the value for the previous keyword. A keyword may have as many lines as desired.
The first blank line ends all meta-data for the document. Therefore, the first line of a document must not be blank. All meta-data is stripped from the document prior to any further processing by Markdown.
来源:https://pythonhosted.org/Markdown/extensions/meta_data.html
您是否考虑过查看元数据扩展的源代码以了解它是如何完成的?
使用的regex是:
META_RE = re.compile(r'^[ ]{0,3}(?P<key>[A-Za-z0-9_-]+):\s*(?P<value>.*)')
当然还有副线的regex:
META_MORE_RE = re.compile(r'^[ ]{4,}(?P<value>.*)')
如果您注意到,这些正则表达式比您的更具体,并且匹配误报的可能性要小得多。然后扩展将文档分成几行,loops 通过与那些正则表达式比较的每一行,并在不匹配的第一行(可能是也可能不是空行)处跳出循环。
如果您在该代码中注意到,已经添加了一个 new feature,它将在下一个版本中提供。正在添加对可选 YAML 样式分隔符的支持。如果您习惯使用最新的(未发布的)开发代码,您可以将元数据包装在 YAML 分隔符中,这可能会更容易找到元数据的结尾。
例如,您上面的示例文档将如下所示(注意我使用了可选的结束特定分隔符 (...
),它更清楚地标记结束):
---
page title: fancypagetitle
something else: another value
...
# Heading
Text
也就是说,您仍然需要注意不要得到错误的匹配项(例如 <hr>
)。我想无论哪种方式,您都需要根据自己的需要重新实现元数据扩展中的所有内容。当然,它是开源的,所以只要你尊重 license.
抱歉,我无法确定何时发布下一个版本。
哦,看看 MultiMarkdown 提供的此功能的描述可能也会有所帮助,它启发了 Python-Markdown 中的功能。这可能会让您更清楚地了解可能包含元数据的内容。