Ejemplo n.º 1
0
    def parse_meta_data(self):
        """ Parses the meta data from posts
        """
        self.posts = []
        for post in os.listdir(self.config.posts_dir):
            # ignore hidden files
            if post.startswith('.'):
                continue

            post = os.path.join(self.config.posts_dir, post)
            post_meta = template.get_meta_data(post)
            date_format = self.date_formats.get('-')
            if '.' in post_meta['date']: date_format = self.date_formats.get('.')
            if '/' in post_meta['date']: date_format = self.date_formats.get('/')
            post_meta['date'] = datetime.datetime.strptime(post_meta['date'], date_format)
            if 'title' in post_meta:
                post_id = self.get_post_id(post_meta)
            else:
                post_id = os.path.splitext(post_meta['filename'].split(self.config.posts_dir)[1][1:])[0]
            post_url = os.path.join(
                                    '/',
                                    post_meta['date'].strftime(
                                        self.config.post_url),
                                    post_id
                                    )
            post_meta['url'] = post_url
            self.posts.append(post_meta)

        # sort posts based on date.
        self.posts.sort(key=lambda x: x['date'], reverse=True)
        
        # include next and previous urls for posts.
        for post_num, post in enumerate(self.posts):
            post['id'] = post['url']
            previous = post_num + 1
            next = post_num - 1
            if previous < len(self.posts):
                post['previous'] = self.posts[previous]
            else:
                post['previous'] = None
            if next >= 0:
                post['next'] = self.posts[next]
            else:
                post['next'] = None

        # create paginator
        self.paginator = paginator.Paginator(self.posts, self.config.paginate)
        # create site information
        site = {}
        site['time'] = datetime.datetime.now()
        # extract site information from settings.yaml
        site.update(getattr(self.config, 'site', {}))
        # update the template global with posts info
        template.env.globals.update( {
                                        'posts': self.posts,
                                        'site' : site
                                     }
                                   )
Ejemplo n.º 2
0
    def generate_pages(self):
        """Generate HTML from all the other pages.
        """
        log.info('Generating pages')
        self.pages = []
        for root, dirs, files in os.walk(self.work_dir):
            # checks whether the directory is as subdirectory of root
            def is_a_subdirectory(sub):
                return sub in root
            # ignore all the subdirectories
            if any(map(is_a_subdirectory, self.ignored_items)):
                continue

            for filename in files:
                # ignore hidden files
                if filename.startswith('.'):
                    continue

                filename = os.path.join(root, filename)
                # ignore tag template
                if filename == self.tag_template:
                    continue

                _, extn = os.path.splitext(filename)
                if extn not in self.template_extensions:
                    dest = os.path.join(self.config.site_dir,
                        filename.split(self.work_dir)[1][1:])
                    self.move_to_site(filename, dest)
                    continue

                page_meta = template.get_meta_data(filename)
                page_url = os.path.join(
                    '/',
                    os.path.splitext(
                        page_meta['filename'].split(self.work_dir)[1][1:])[0])
                page_meta['url'] = page_url
                self.pages.append(page_meta)
                # paginate if needed
                if page_meta.get('paginate', False) == True:
                    self.paginate(filename, page_meta)
                    continue

                html = template.render(page_meta['raw'], {'page': page_meta})
                page_path = os.path.join(
                    self.config.site_dir,
                    filename.split(self.work_dir)[1][1:])
                page_path = self.get_page_name_for_site(page_path)
                log.debug('Generating page %s' %page_path)
                # write the rendered page
                self.write_html(page_path, html)
Ejemplo n.º 3
0
    def generate_pages(self):
        """Generate HTML from all the other pages.
        """
        log.info('Generating pages')
        self.pages = []
        for root, dirs, files in os.walk(self.work_dir):
            # checks whether the directory is as subdirectory of root
            def is_a_subdirectory(sub):
                return sub in root

            # ignore all the subdirectories
            if any(map(is_a_subdirectory, self.ignored_items)):
                continue

            for filename in files:
                # ignore hidden files
                if filename.startswith('.'):
                    continue

                filename = os.path.join(root, filename)
                # ignore tag template
                if filename == self.tag_template:
                    continue

                _, extn = os.path.splitext(filename)
                if extn not in self.template_extensions:
                    dest = os.path.join(self.config.site_dir,
                                        filename.split(self.work_dir)[1][1:])
                    self.move_to_site(filename, dest)
                    continue

                page_meta = template.get_meta_data(filename)
                page_url = os.path.join(
                    '/',
                    os.path.splitext(page_meta['filename'].split(
                        self.work_dir)[1][1:])[0])
                page_meta['url'] = page_url
                self.pages.append(page_meta)
                # paginate if needed
                if page_meta.get('paginate', False) == True:
                    self.paginate(filename, page_meta)
                    continue

                html = template.render(page_meta['raw'], {'page': page_meta})
                page_path = os.path.join(self.config.site_dir,
                                         filename.split(self.work_dir)[1][1:])
                page_path = self.get_page_name_for_site(page_path)
                log.debug('Generating page %s' % page_path)
                # write the rendered page
                self.write_html(page_path, html)
Ejemplo n.º 4
0
 def generate_tags(self):
     """Generate tag pages.
     """
     log.info('Generating tags')
     tag_template_meta = template.get_meta_data(
         os.path.join(self.work_dir, self.tag_template))
     for tagname, posts in self.tags.iteritems():
         render_vars = {'tag': {'name': tagname, 'posts': posts}, 
                        'page': tag_template_meta}
         html = template.render(tag_template_meta['raw'], render_vars)
         tag_page_path = os.path.join(
             self.config.site_dir,
             'tag',
             urllib.quote_plus(tagname.lower()),
             'index.html')
         log.debug('Generating tag %s: %s' % (tagname, tag_page_path))
         # write the html page
         self.write_html(tag_page_path, html)
Ejemplo n.º 5
0
 def generate_tags(self):
     """Generate tag pages.
     """
     log.info('Generating tags')
     tag_template_meta = template.get_meta_data(
         os.path.join(self.work_dir, self.tag_template))
     for tagname, posts in self.tags.iteritems():
         render_vars = {
             'tag': {
                 'name': tagname,
                 'posts': posts
             },
             'page': tag_template_meta
         }
         html = template.render(tag_template_meta['raw'], render_vars)
         tag_page_path = os.path.join(self.config.site_dir, 'tag',
                                      urllib.quote_plus(tagname.lower()),
                                      'index.html')
         log.debug('Generating tag %s: %s' % (tagname, tag_page_path))
         # write the html page
         self.write_html(tag_page_path, html)
Ejemplo n.º 6
0
    def parse_meta_data(self):
        """Parses the meta data from posts
        """
        self.posts = []
        self.tags = {}
        for post in os.listdir(self.config.posts_dir):
            # ignore hidden files
            if post.startswith('.'):
                continue

            post = os.path.join(self.config.posts_dir, post)
            post_meta = template.get_meta_data(post)
            post_meta['date'] = datetime.datetime.strptime(
                post_meta['date'], 
                self.date_format)
            post_url = os.path.join(
                '/',
                post_meta['date'].strftime(
                    self.config.post_url),
                os.path.splitext(
                    post_meta['filename'].split(
                        self.config.posts_dir)[1][1:])[0])
            post_meta['url'] = post_url
            self.posts.append(post_meta)

            # create tag cloud
            for tag in post_meta.get('tags', []):
                if self.tags.has_key(tag):
                    self.tags[tag].append(post_meta)
                else:
                    self.tags[tag] = [post_meta]

        # sort posts based on date.
        self.posts.sort(key=lambda x: x['date'], reverse=True)
        # sort tags based on date
        for tagname in self.tags:
            self.tags[tagname].sort(key=lambda x: x['date'], reverse=True)

        # include next and previous urls for posts. includes post tags
        for post_num, post in enumerate(self.posts):
            post_tags = []
            for tagname in post.get('tags', []):
                tag_url = os.path.join(
                    '/', 
                    'tag',
                    urllib.quote_plus(tagname.lower()))
                post_tags.append(
                    {'name': tagname, 
                     'url': tag_url, 
                     'posts': self.tags[tagname]})
            post['tags'] = post_tags
            post['id'] = post['url']
            previous = post_num + 1
            next = post_num - 1
            if previous < len(self.posts):
                post['previous'] = self.posts[previous]
            else:
                post['previous'] = None
            if next >= 0:
                post['next'] = self.posts[next]
            else:
                post['next'] = None

        # tags for env
        tags_info = []
        for tagname, post in self.tags.iteritems():
            tag_url = os.path.join(
                '/', 
                'tag',
                 urllib.quote_plus(tagname.lower()))
            tags_info.append(
                {'name': tagname,
                 'url': tag_url,
                 'posts': self.tags[tagname]})

        # create paginator
        self.paginator = paginator.Paginator(self.posts, self.config.paginate)
        # create site information
        site = {}
        site['time'] = datetime.datetime.now()
        # extract site information from settings.yaml
        site.update(getattr(self.config, 'site', {}))
        # update the template global with posts info
        template.env.globals.update(
            {'posts': self.posts,
             'site' : site,
             'tags' : tags_info})
Ejemplo n.º 7
0
    def parse_meta_data(self):
        """Parses the meta data from posts
        """
        self.posts = []
        self.tags = {}
        for post in os.listdir(self.config.posts_dir):
            # ignore hidden files
            if post.startswith('.'):
                continue

            post = os.path.join(self.config.posts_dir, post)
            post_meta = template.get_meta_data(post)
            post_meta['date'] = datetime.datetime.strptime(
                post_meta['date'], self.date_format)
            post_url = os.path.join(
                '/', post_meta['date'].strftime(self.config.post_url),
                os.path.splitext(post_meta['filename'].split(
                    self.config.posts_dir)[1][1:])[0])
            post_meta['url'] = post_url
            self.posts.append(post_meta)

            # create tag cloud
            for tag in post_meta.get('tags', []):
                if tag in self.tags:
                    self.tags[tag].append(post_meta)
                else:
                    self.tags[tag] = [post_meta]

        # sort posts based on date.
        self.posts.sort(key=lambda x: x['date'], reverse=True)
        # sort tags based on date
        for tagname in self.tags:
            self.tags[tagname].sort(key=lambda x: x['date'], reverse=True)

        # include next and previous urls for posts. includes post tags
        for post_num, post in enumerate(self.posts):
            post_tags = []
            for tagname in post.get('tags', []):
                tag_url = os.path.join('/', 'tag',
                                       urllib.quote_plus(tagname.lower()))
                post_tags.append({
                    'name': tagname,
                    'url': tag_url,
                    'posts': self.tags[tagname]
                })
            post['tags'] = post_tags
            post['id'] = post['url']
            previous = post_num + 1
            next = post_num - 1
            if previous < len(self.posts):
                post['previous'] = self.posts[previous]
            else:
                post['previous'] = None
            if next >= 0:
                post['next'] = self.posts[next]
            else:
                post['next'] = None

        # tags for env
        tags_info = []
        for tagname, post in self.tags.iteritems():
            tag_url = os.path.join('/', 'tag',
                                   urllib.quote_plus(tagname.lower()))
            tags_info.append({
                'name': tagname,
                'url': tag_url,
                'posts': self.tags[tagname]
            })

        # create paginator
        self.paginator = paginator.Paginator(self.posts, self.config.paginate)
        # create site information
        site = {}
        site['time'] = datetime.datetime.now()
        # extract site information from settings.yaml
        site.update(getattr(self.config, 'site', {}))
        # update the template global with posts info
        template.env.globals.update({
            'posts': self.posts,
            'site': site,
            'tags': tags_info
        })