WoJ
WoJ

Reputation: 30005

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:

---
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:

Upvotes: 2

Views: 3086

Answers (1)

WoJ
WoJ

Reputation: 30005

I found it: md.convert() works inplace (in other words, modifies 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

Upvotes: 7

Related Questions