Ronny Pfannschmidt avatar 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)

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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.