summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan MacKinlay <dan@possumpalace.org>2009-12-22 16:49:40 +1100
committerDan MacKinlay <dan@possumpalace.org>2009-12-22 16:49:40 +1100
commitbcea1a1d1c70120884905572066769a2c06c5aa8 (patch)
tree84a504d2a3e1c4761ee29da9e1a2e384becbf6ed
parent677b9bdcd30b01fe517b6412d210ed37e0acc817 (diff)
downloadsphinx-bcea1a1d1c70120884905572066769a2c06c5aa8.tar.gz
sphinx now preserves lots of useful document metadata
-rw-r--r--sphinx/environment.py11
-rw-r--r--tests/test_metadata.py22
2 files changed, 26 insertions, 7 deletions
diff --git a/sphinx/environment.py b/sphinx/environment.py
index d4f16819..64c745a7 100644
--- a/sphinx/environment.py
+++ b/sphinx/environment.py
@@ -757,6 +757,7 @@ class BuildEnvironment:
def process_metadata(self, docname, doctree):
"""
Process the docinfo part of the doctree as metadata.
+ Keep processing minimal - just return what docutils says.
"""
self.metadata[docname] = md = {}
try:
@@ -768,10 +769,12 @@ class BuildEnvironment:
# nothing to see here
return
for node in docinfo:
- if node.__class__ is nodes.author:
- # handled specially by docutils
- md['author'] = node.astext()
- elif node.__class__ is nodes.field:
+ # nodes are multiply inherited...
+ if isinstance(node, nodes.authors):
+ md['authors'] = [author.astext() for author in node]
+ elif isinstance(node, nodes.TextElement): #e.g. author
+ md[node.__class__.__name__] = node.astext()
+ else:
name, body = node
md[name.astext()] = body.astext()
del doctree[0]
diff --git a/tests/test_metadata.py b/tests/test_metadata.py
index 481e35af..ea41fc11 100644
--- a/tests/test_metadata.py
+++ b/tests/test_metadata.py
@@ -39,13 +39,29 @@ def teardown_module():
app.cleanup()
def test_docinfo():
+ """
+ inspect the 'docinfo' metadata stored in the first node of the document.
+ Note this doesn't give us access to data stored in subsequence blocks
+ that might be considered document metadata, such as 'abstract' or
+ 'dedication' blocks, or the 'meta' role. Doing otherwise is probably more
+ messing with the internals of sphinx than this rare use case merits.
+ """
exampledocinfo = env.metadata['metadata']
expected_metadata = {
'author': u'David Goodger',
- u'field name': u'This is a generic bibliographic field.',
- u'field name 2': u'Generic bibliographic fields may contain multiple body elements.\n\nLike this.'}
+ 'authors': [u'Me', u'Myself', u'I'],
+ 'address': u'123 Example Street\nExample, EX Canada\nA1B 2C3',
+ 'field name': u'This is a generic bibliographic field.',
+ 'field name 2': u'Generic bibliographic fields may contain multiple body elements.\n\nLike this.',
+ 'status': u'This is a "work in progress"',
+ 'version': u'1',
+ 'copyright': u"This document has been placed in the public domain. You\nmay do with it as you wish. You may copy, modify,\nredistribute, reattribute, sell, buy, rent, lease,\ndestroy, or improve it, quote it at length, excerpt,\nincorporate, collate, fold, staple, or mutilate it, or do\nanything else to it that your or anyone else's heart\ndesires.",
+ 'contact': u'goodger@python.org',
+ 'date': u'2006-05-21',
+ 'organization': u'humankind',
+ 'revision': u'4564'}
# I like this way of comparing dicts - easier to see the error.
for key in exampledocinfo:
- yield assert_equals, exampledocinfo[key], expected_metadata[key]
+ yield assert_equals, exampledocinfo.get(key), expected_metadata.get(key)
#but then we still have to check for missing keys
yield assert_equals, set(expected_metadata.keys()), set(exampledocinfo.keys())