示例#1
0
def new_index_page(page_to_copy, page_num, count, total_posts, posts_per_page, path):
    index_page = Page()
    index_page.copy(page_to_copy)
    index_page.url = '/' + path + '/index-%s' % count
    index_page.template = template.get_template('post.html')

    apply_filter('page-head', index_page)
    apply_filter('page-meta', index_page)
    apply_filter('page-menu', index_page)
    apply_filter('page-foot', index_page)

    total_pages = math.ceil(total_posts / posts_per_page) - 1

    if page_num > 0:
        index_page.template.set_value('prevpage', '<< Newer posts')
        if page_num - 1 == 0:
            index_page.template.set_attribute('prevpage', 'href', 'index.html')
        else:
            index_page.template.set_attribute('prevpage', 'href', 'index-%s.html' % (page_num - 1))

    if page_num < total_pages:
        index_page.template.set_value('nextpage', 'Older posts >>')
        index_page.template.set_attribute('nextpage', 'href', 'index-%s.html' % (page_num + 1))

    if page_num > 0 and page_num < total_pages:
        index_page.template.set_value('pagelinksep', ' | ')

    index_page.template.repeat('posts', min(posts_per_page, total_posts - count))
    return index_page
示例#2
0
def process_path(path, output_path, pages):
    sorted_posts = sorted(filter_pages(path, pages.values()), key=lambda x: x.url[len(path)+1:len(path)+11], reverse=True)
    total_posts = len(sorted_posts)

    if total_posts == 0:
        return

    posts_per_page = get_posts_per_page(sorted_posts)

    count = 0
    page_num = 0
    index = 0

    if path.startswith('/'):
        path = path[1:]

    index_page = new_index_page(sorted_posts[0], page_num, count, total_posts, posts_per_page, path)

    do_action('post-meta-reset')
    for page in sorted_posts:
        cpage = Page()
        cpage.copy(page)
        cpage.template = index_page.template

        # TODO: this isn't right, need to fix later
        apply_filter('pre-markdown', cpage)

        content = apply_filter('markdown', page.content)

        cpage.template.set_value('content', content, index)
        apply_filter('post-meta', cpage, index)
        index += 1
        count += 1
        if index >= posts_per_page:
            write_index_page(index_page, output_path, path, page_num)
            do_action('post-meta-reset')
            index = 0
            page_num += 1
            index_page = new_index_page(sorted_posts[0], page_num, count, total_posts, posts_per_page, path)

    if index > 0:
        write_index_page(index_page, output_path, path, page_num)
示例#3
0
def process_pages(pages, output_path):
    root_page = get_root_page(pages)
    if not root_page:
        return pages

    index_title = root_page.config.get('tags', 'index_title')
    tag_title = root_page.config.get('tags', 'title')

    tags = {}

    print('Generating tag index pages')
    for page in pages.values():
        if 'tags' in page.headers:
            for tag in split_tags(page.headers['tags']):
                tag = sanitise_tag(tag)
                if tag not in tags:
                    tags[tag] = []
                tags[tag].append(page)

    tag_dir = os.path.join(output_path, 'tags')

    if not os.path.exists(tag_dir):
        os.makedirs(tag_dir)

    for fname in os.listdir(tag_dir):
        os.remove(os.path.join(tag_dir, fname))

    for tag in tags:
        tmp = template.get_template('tag.html')
        print('  - generating tag %s' % tag)

        output_name = os.path.join(tag_dir, '%s.html' % tag)

        tmp.repeat('posts', len(tags[tag]))
        x = 0
        do_action('post-meta-reset')
        for page in sorted(tags[tag], key=lambda x: x.last_modified, reverse=False):
            cpage = Page()
            cpage.copy(page)
            cpage.template = tmp
            tmp.set_value('content', page.get_html_content(include_title=True), x)
            apply_filter('post-meta', cpage, x)
            x += 1

        apply_filter('page-head', cpage)
        apply_filter('page-meta', cpage)
        apply_filter('page-menu', cpage)
        apply_filter('page-foot', cpage)

        tmp.set_value('page-title', tag_title % tag)
        tmp.set_value('tag', tag_title % tag)
        
        apply_filter('tag-page', cpage)

        out = str(tmp)
        f = open(output_name, 'w+')
        f.write(out)
        f.close()

    tag_counts = {k: len(v) for k, v in tags.items()}

    tag_classes = {}
    page = next(iter(pages.values()))
    for (name, value) in page.config.items('tags'):
        if name.isdigit():
            tag_classes[name] = value

    tmp = template.get_template('tags.html')
    if tmp is None:
        return

    tmp.repeat('tags', len(tags))
    x = 0
    for tag in sorted(tags):
        tmp.set_value('tag', tag, x)
        tmp.set_attribute('tag', 'href', '/tags/%s.html' % tag, x)
        count = tag_counts[tag]

        for key in sorted(tag_classes, reverse=True):
            if count >= int(key):
                tmp.set_attribute('tag', 'class', tag_classes[key], x)
        x += 1

    cpage = Page()
    cpage.copy(page)
    cpage.template = tmp
    apply_filter('page-head', cpage)
    apply_filter('page-meta', cpage)
    apply_filter('page-menu', cpage)
    apply_filter('page-foot', cpage)

    tmp.set_value('page-title', index_title)
    
    apply_filter('tags-index-page', cpage)

    output_name = os.path.join(tag_dir, 'index.html')
    f = open(output_name, 'w+')
    f.write(str(tmp))
    f.close()

    print('  - complete')

    return pages