Search code examples
pythonmarkdown

'meta' extension for Markdown not loading in Python2.6?


I'm trying to get the meta extension working with markdown in Python 2.6. The code looks like this:

import markdown as m

print "Markdown version: ", m.version
file = "file.md"
md = m.Markdown( extensions = ['meta']) # doesn't complain
print "Registered extensions: ", md.registeredExtensions
text = open(file)
try:
    md.convert(file)
except AttributeError as a:
    print "Error: ", a
print "Meta: ", md.Meta

And my file looks like this:

Title: Chaleur
Date: 2010-07-11
Author: Gui13

Simple md test
![Chaleur](../content/chaleur.jpg)

What I'd like to get is something like 'title' : 'Chaleur', 'date' : '2010-07-11', 'author' : 'gui13' when printing the md.Meta.

What I get is this:

$ python test.py
Markdown version: 2.1.0
Registered extensions: []
Meta: {}

So it looks like the meta extension is not even loaded, whereas it should be (meta is supposed to be included in Markdown since version 2.0).

Do you know what is the problem?


Solution

  • convert() expects text. Replace md.convert(file) by md.convert(open(file).read()).

    import markdown as m
    
    print "Markdown version: ", m.version
    file = "file.md"
    md = m.Markdown(extensions=['meta']) # doesn't complain
    
    print "Registered extensions: ", md.registeredExtensions
    print "Preprocessors:", md.preprocessors.keys()
    text = open(file).read()
    try:
        print md.convert(text)
    except AttributeError as a:
        print "Error: ", a
    print "Meta: ", md.Meta
    

    Output:

    Markdown version:  2.1.0
    Registered extensions:  []
    Preprocessors: ['meta', 'html_block', 'reference']
    <p>Simple md test
    <img alt="Chaleur" src="../content/chaleur.jpg" /></p>
    Meta:  {u'date': [u'2010-07-11'], u'author': [u'Gui13'], u'title': [u'Chaleur']}