def initialize(self, config): """ Configure content options from a given 'config' dictionary. """ self._pages_metafiles = MetaFiles( root = config['PAGE_ROOT'], extensions = config['PAGE_EXTENSIONS'], encoding = config['PAGE_ENCODING'], meta_render = config['PAGE_META_RENDERER'], body_render = config['PAGE_BODY_RENDERER']) self._posts_metafiles = MetaFiles( root = config['POST_ROOT'], extensions = config['POST_EXTENSIONS'], encoding = config['POST_ENCODING'], meta_render = config['POST_META_RENDERER'], body_render = config['POST_BODY_RENDERER'])
class Content(object): """ Maintains the collection of pages and posts in the blog. """ def __init__(self): self.pages = [] self.pages_by_path = {} self.posts = [] self.posts_by_path = {} self.posts_by_tag = {} self._pages_metafiles = None self._posts_metafiles = None def initialize(self, config): """ Configure content options from a given 'config' dictionary. """ self._pages_metafiles = MetaFiles( root = config['PAGE_ROOT'], extensions = config['PAGE_EXTENSIONS'], encoding = config['PAGE_ENCODING'], meta_render = config['PAGE_META_RENDERER'], body_render = config['PAGE_BODY_RENDERER']) self._posts_metafiles = MetaFiles( root = config['POST_ROOT'], extensions = config['POST_EXTENSIONS'], encoding = config['POST_ENCODING'], meta_render = config['POST_META_RENDERER'], body_render = config['POST_BODY_RENDERER']) def load_pages(self): """ Load all the pages in the blog. Can be called multiple times to reload. On errors, the previous content is preserved. """ self._pages_metafiles.load() pages = [] pages_by_path = {} for page in Target.from_metafiles(self._pages_metafiles): pages.append(page) pages_by_path[page.path] = page self.pages = pages self.pages_by_path = pages_by_path def load_posts(self): """ Load all the posts in the blog, sorting by date and grouping by tag. Posts without date are skipped (considered drafts). Posts without tags are put in a default ['untagged']. Can be called multiple times to reload. On errors, the previous content is preserved. """ self._posts_metafiles.load() posts = [] posts_by_path = {} posts_by_tag = {} for post in Target.from_metafiles(self._posts_metafiles): if 'date' in post.meta: posts.append(post) posts_by_path[post.path] = post # sort first so that tags have their posts sorted too: posts.sort(key = lambda post: post.meta['date'], reverse = True) for post in posts: post.meta.setdefault('tags', ['untagged']) for tag in post.meta['tags']: posts_by_tag.setdefault(tag, []) posts_by_tag[tag].append(post) self.posts = posts self.posts_by_path = posts_by_path self.posts_by_tag = posts_by_tag def load(self): """ Load all the pages and posts in the blog. """ self.load_pages() self.load_posts() @property def environment(self): """ Return the content as a dict suitable for template rendering. """ return { 'pages' : self.pages, 'pages_by_path' : self.pages_by_path, 'posts' : self.posts, 'posts_by_path' : self.posts_by_path, 'posts_by_tag' : self.posts_by_tag }