Commits

Ronny Pfannschmidt committed 1f2724a

load rst, grab basic metadata

Comments (0)

Files changed (4)

articledump/__init__.py

+from .restructuredtext import load_rst
+

articledump/restructuredtext.py

+
+from docutils.core import publish_doctree
+from docutils.nodes import docinfo
+from datetime import datetime
+
+class Info(object):
+    def __init__(self, document, source, root):
+        self.document = document
+        self.source = source
+        self.root = root
+        self.docinfo = next(c for c in document.children if c.__class__ is docinfo)
+
+    @property
+    def title(self):
+        return self.document['title']
+
+    @property
+    def slug(self):
+        for item in self.docinfo:
+            if item.tagname == 'slug':
+                return item.content
+        return 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()
+
+
+
+
+
+def load_rst(file, root):
+    document = publish_doctree(file.read(), source_path=root.bestrelpath(file))
+    return Info(document, file, root)

testing/__init__.py

Empty file added.

testing/test_load_rst.py

+from articledump import load_rst
+import datetime
+content = """\
+Test something
+================
+
+:date: 2011-10-10
+"""
+import pprint
+def test_load(tmpdir):
+    testfile = tmpdir/'test.rst'
+    testfile.write(content)
+    info = load_rst(testfile, root=tmpdir)
+    assert info.title == 'Test something'
+    assert info.slug == 'test-something'
+    assert info.date == datetime.date(2011, 10, 10)