changeset 157:55910ab4bfea

First draft of the `prose` page source.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 27 Dec 2014 18:17:30 -0800
parents 683baa977d97
children 1187739e5a19
files piecrust/plugins/builtin.py piecrust/sources/prose.py
diffstat 2 files changed, 53 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/plugins/builtin.py	Sat Dec 27 18:14:46 2014 -0800
+++ b/piecrust/plugins/builtin.py	Sat Dec 27 18:17:30 2014 -0800
@@ -23,6 +23,7 @@
 from piecrust.sources.posts import (FlatPostsSource, ShallowPostsSource,
         HierarchyPostsSource)
 from piecrust.sources.autoconfig import AutoConfigSource
+from piecrust.sources.prose import ProseSource
 from piecrust.templating.jinjaengine import JinjaTemplateEngine
 
 
@@ -58,7 +59,8 @@
                 FlatPostsSource,
                 ShallowPostsSource,
                 HierarchyPostsSource,
-                AutoConfigSource]
+                AutoConfigSource,
+                ProseSource]
 
     def getDataProviders(self):
         return [
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/piecrust/sources/prose.py	Sat Dec 27 18:17:30 2014 -0800
@@ -0,0 +1,50 @@
+import os
+import os.path
+import logging
+from piecrust.sources.base import (
+        SimplePageSource, SimplePaginationSourceMixin)
+
+
+logger = logging.getLogger(__name__)
+
+
+class ProseSource(SimplePageSource,
+                  SimplePaginationSourceMixin):
+    SOURCE_NAME = 'prose'
+
+    def __init__(self, app, name, config):
+        super(ProseSource, self).__init__(app, name, config)
+        self.config_recipe = config.get('config', {})
+
+    def buildPageFactories(self):
+        factories = super(ProseSource, self).buildPageFactories()
+        for f in factories:
+            f.metadata['config'] = self._makeConfig(f.path)
+            logger.debug(f.__dict__)
+            yield f
+
+    def findPagePath(self, metadata, mode):
+        rel_path, metadata = super(ProseSource, self).findPagePath(metadata, mode)
+        if rel_path:
+            metadata['config'] = self._makeConfig(self.resolveRef(rel_path))
+        return rel_path, metadata
+
+    def _makeConfig(self, path):
+        c = dict(self.config_recipe)
+        if c.get('title') == '%first_line%':
+            c['title'] = get_first_line(path)
+        return c
+
+
+def get_first_line(path):
+    with open(path, 'r') as f:
+        while True:
+            l = f.readline()
+            if not l:
+                break
+            l = l.strip()
+            if not l:
+                continue
+            return l
+    return None
+