2

我正在尝试让元扩展在 Python 2.6 中与 markdown 一起使用。代码如下所示:

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

我的文件如下所示:

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

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

我想要得到的是像'title' : 'Chaleur', 'date' : '2010-07-11', 'author' : 'gui13'打印md.Meta.

我得到的是:

$ python test.py
Markdown 版本:2.1.0
注册扩展:[]
Meta:{}

所以看起来元扩展甚至没有加载,而它应该是(元应该包含在 Markdown 2.0 版中)。

你知道是什么问题吗?

4

1 回答 1

2

convert()需要文本。替换md.convert(file)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

输出:

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']}
于 2010-11-10T20:17:13.573 回答