简体   繁体   English

如何使用 python-markdown 从 Markdown 文件中获取元数据?

[英]How to get the metadata from a Markdown file using python-markdown?

I am trying to use python-markdown to extract metadata from the following file:我正在尝试使用python-markdown从以下文件中提取元数据:

---
title: this is the title and it is compulsory
tags: this part is optional
something: this is not interesting, only 'title' and 'tags' is
---
some content

The documentation for metadata gives two examples:元数据的文档提供了两个示例:

markdown.markdown(some_text, extensions=['meta'])

and

>>> md = markdown.Markdown(extensions = ['meta'])
>>> html = md.convert(text)
>>> # Meta-data has been stripped from output
>>> print html
<p>This is the first paragraph of the document.</p>

>>> # View meta-data
>>> print md.Meta
{
'title' : ['My Document'],
'summary' : ['A brief description of my document.'],
'authors' : ['Waylan Limberg', 'John Doe'],
'date' : ['October 2, 2007'],
'blank-value' : [''],
'base_url' : ['http://example.com']
}

I cannot make out from these examples how to actually get the metadata:我无法从这些示例中弄清楚如何实际获取元数据:

  • the first example returns an str , which of course does not have the Meta property第一个示例返回一个str ,当然它没有Meta属性
  • the second example does not load text , except for html (which is not used to extract the metadata).第二个示例不加载text ,但html (不用于提取元数据)除外。

I found it: md.convert() works inplace (in other words, modifies md ).我找到了它: md.convert()就地工作(换句话说,修改md )。

The code编码

data = pathlib.Path(note).read_text(encoding='utf-8')
md = markdown.Markdown(extensions=['meta'])
md.convert(data)
print(md.Meta)

will correctly output the metadata from the file note将正确 output 文件note中的元数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM