示例#1
0
文件: post.py 项目: babalavident/oak
    def __init__(self, f, url, settings, processor=None):
        """The Post class __init__

        :param f: the path to the post file
        :param settings: the blog settings
        :param processor: the processor to render post's contents
        :processor type: class

        :raises: PostError
        """
        try:
            _f = codecs.open(f, mode='r', encoding='utf-8')
        except:
            raise PostError('Unable to open file. Hint: isn\'t it UTF-8 encoded?')
        
        metadata = settings.POST_DEFAULTS

        # Set metadata to the app defaults
        self['metadata'] = metadata.copy()
        self.f = _f.read()
        if not self.f.startswith(HEADER_MARK):
            raise PostError('Post file invalid, no header found.')
        _, metadata, self['raw'] = self.f.split(HEADER_MARK, 2)
        # update the metadata with the header's contents
        self['metadata'].update(yaml.load(metadata))
        # TODO auto determine processor based on metadata['markup']
        if processor:
            p = processor()
            self = p.process(self)

        # Partial refactoring
        filename = os.path.basename(f)
        name, extension = os.path.splitext(filename)

        # TODO add sanity check on source filename (count of - ...)
        self['output_path'] = self._post_path(name, settings.OUTPUT_PATH) 
        self['url'] = "%s%s" % (url, self._post_url(name))
        self['id'] = Atom.gen_id(self)
示例#2
0
文件: __init__.py 项目: angeldiaz/oak
 def _do_posts(self):
     """Do the posts generation.
     """
     self.logger.info("Rendering posts...")
     self.logger.info("Using %s as source of content." % (self.settings.CONTENT_PATH,))
     for f in glob.glob("%s/*.%s" % (self.settings.CONTENT_PATH,self.settings.SRC_EXT)):
         filename = os.path.basename(f)
         # TODO add sanity check on source filename (count of - ...)
         post_path = os.path.sep.join([self.settings.CONTENT_PATH] + filename.split('-')[:2])
         newfilename = "%s.html" % (os.path.splitext(filename)[0],)
         self.logger.info("Processing %s..." % (filename,))
         post = Post(f, self.settings.POST_DEFAULTS, processor.MarkdownProcessor)
         post['url'] = "%s%s" % (self.blog_url, self._post_url(newfilename))
         post['id'] = Atom.gen_id(post)
         self.posts.append(post)
         # cache the tags of the current post
         for t in post['metadata']['tags']:
             if t not in self.tags.keys():
                 self.tags[t] = Tag(tag=t, url=self._tag_url(t), posts=[post])
             else:
                 self.tags[t]['posts'].append(post)
         # cache the author of the current post
         author = post['metadata']['author']
         if author not in self.authors.keys():
             self.authors[author] = Author(author=author,url=self._author_url(author), posts=[post])
         else:
             self.authors[author].append(post)
         
         # make sure we have the final path created
         if not os.path.exists(post_path) or not os.path.isdir(post_path):
             self.logger.debug("Output directory %s not found, creating" % (post_path,))
             os.makedirs(post_path)
         self.tpl_vars.update({'post': post})
         self.logger.debug("tpl_vars: %s" % (self.tpl_vars,))
         output = self.jenv.get_template(self.settings.TEMPLATES['post']).render(self.tpl_vars)
         self.logger.info("Generating output file in %s" % (self._post_path(filename),))
         self._write_file(self._post_path(filename), output)
         self.tpl_vars.pop('post') # remove the aded key