Beispiel #1
0
def custom_page(rel_path):
    """
    Render custom page
    """
    if '..' in rel_path or './' in rel_path \
            or '/.' in rel_path or rel_path.startswith('.') \
            or '//' in rel_path:
        # the path is not safe
        return page_not_found()

    file_path = os.path.join('pages', rel_path)

    if os.path.exists(file_path) and os.path.isfile(file_path):
        # the exact file exists, so return it
        return send_file(file_path)

    # try different type of file

    # try directory
    if os.path.isdir(file_path):
        # is a directory
        if not file_path.endswith('/'):
            return redirect(rel_path + '/', code=302)
        return custom_page(os.path.join(rel_path, 'index.html'))

    # # try html
    # if os.path.exists(file_path + '.html'):
    #     # html file exists
    #     return send_file(file_path + '.html')

    # try markdown
    # remove "html" extension if accessing this page through "xxx.html"
    file_path = os.path.splitext(file_path)[0]
    md_ext = extension_of_markdown_file(file_path)
    if md_ext is not None:
        # markdown file exists
        content = parse_custom_page('.'.join((file_path, md_ext)))
        content['id_key'] = rel_path
        content['absolute_url'] = make_abs_url(C.root_url, rel_path)
        if should_return_json():
            return jsonify(dict(ok=True, site=C, page=content))
        else:
            return render_template(content['layout'] + '.html',
                                   site=C,
                                   page=content)

    return page_not_found()
Beispiel #2
0
def custom_page(rel_path):
    """
    Render custom page
    """
    if '..' in rel_path or './' in rel_path \
            or '/.' in rel_path or rel_path.startswith('.') \
            or '//' in rel_path:
        # the path is not safe
        return page_not_found()

    file_path = os.path.join('pages', rel_path)

    if os.path.exists(file_path) and os.path.isfile(file_path):
        # the exact file exists, so return it
        return send_file(file_path)

    # try different type of file

    # try directory
    if os.path.isdir(file_path):
        # is a directory
        if not file_path.endswith('/'):
            return redirect(rel_path + '/', code=302)
        return custom_page(os.path.join(rel_path, 'index.html'))

    # # try html
    # if os.path.exists(file_path + '.html'):
    #     # html file exists
    #     return send_file(file_path + '.html')

    # try markdown
    # remove "html" extension if accessing this page through "xxx.html"
    file_path = os.path.splitext(file_path)[0]
    md_ext = extension_of_markdown_file(file_path)
    if md_ext is not None:
        # markdown file exists
        content = parse_custom_page('.'.join((file_path, md_ext)))
        content['id_key'] = rel_path
        content['absolute_url'] = make_abs_url(C.root_url, rel_path)
        if should_return_json():
            return jsonify(dict(ok=True, site=C, page=content))
        else:
            return render_template(content['layout'] + '.html', site=C, page=content)

    return page_not_found()
Beispiel #3
0
def post(year, month, day, name):
    """
    Render post
    """
    file_name = '-'.join((year, month, day, name))

    ext = extension_of_markdown_file(os.path.join('posts', file_name))
    if ext is None:
        return page_not_found()

    file_name_with_ext = '.'.join((file_name, ext))

    article = parse_posts(f_list=(file_name_with_ext,))[0]
    article['id_key'] = file_name
    article['absolute_url'] = make_abs_url(C.root_url, '/'.join(('post', year, month, day, name)))

    if should_return_json():
        return jsonify(dict(ok=True, site=C, page=article))
    else:
        return render_template(article['layout'] + '.html', site=C, page=article)
Beispiel #4
0
def post(year, month, day, name):
    """
    Render post
    """
    file_name = '-'.join((year, month, day, name))

    ext = extension_of_markdown_file(os.path.join('posts', file_name))
    if ext is None:
        return page_not_found()

    file_name_with_ext = '.'.join((file_name, ext))

    article = parse_posts(f_list=(file_name_with_ext, ))[0]
    article['id_key'] = file_name
    article['absolute_url'] = make_abs_url(
        C.root_url, '/'.join(('post', year, month, day, name)))

    if should_return_json():
        return jsonify(dict(ok=True, site=C, page=article))
    else:
        return render_template(article['layout'] + '.html',
                               site=C,
                               page=article)