Commits

Ronny Pfannschmidt  committed 0ded468

optional slug metadata

  • Participants
  • Parent commits 1f2724a

Comments (0)

Files changed (2)

File articledump/restructuredtext.py

 from docutils.core import publish_doctree
 from docutils.nodes import docinfo
 from datetime import datetime
+from logbook import Logger
+
+log = Logger('archivedump rst loader')
+
+def get_docinfo(docinfo, name, astext=True):
+    node = None
+    for item in docinfo:
+        if item.tagname == name:
+            node = item
+            break
+        elif item.tagname=='field':
+            if item[0].astext() == name:
+                node = item[1]
+                break
+    if node is not None:
+        return node if not astext else node.astext()
 
 class Info(object):
     def __init__(self, document, source, root):
         self.source = source
         self.root = root
         self.docinfo = next(c for c in document.children if c.__class__ is docinfo)
+        print list(self.docinfo)
 
     @property
     def title(self):
 
     @property
     def slug(self):
-        for item in self.docinfo:
-            if item.tagname == 'slug':
-                return item.content
-        return self.document['ids'][0]
+        return get_docinfo(self.docinfo, 'slug') or self.document['ids'][0]
 
     @property
     def date(self):
-        for item in self.docinfo:
-            if item.tagname == 'date':
-                return datetime.strptime(item.astext(), '%Y-%m-%d').date()
+        date = get_docinfo(self.docinfo, 'date')
+        if date:
+            return datetime.strptime(date, '%Y-%m-%d').date()
 
 
 

File testing/test_load_rst.py

     assert info.title == 'Test something'
     assert info.slug == 'test-something'
     assert info.date == datetime.date(2011, 10, 10)
+
+    testfile.write(content + ':slug: shortone\n')
+
+    info = load_rst(testfile, root=tmpdir)
+    assert info.title == 'Test something'
+    assert info.slug == 'shortone'
+    assert info.date == datetime.date(2011, 10, 10)