# Path: henet/util.py
# def parse_articles(path, cache_dir, page=-1, page_size=20):
#     if isinstance(path, unicode):
#         path = path.encode('utf8')
# 
#     articles = []
#     for root, dirs, files in os.walk(path):
#         for file_ in files:
#             if not file_.endswith('.rst'):
#                 continue
#             fullpath = os.path.join(root, file_)
#             articles.append(parse_article(fullpath, cache_dir, path))
# 
#     articles.sort(by_date)
#     total_size = len(articles)
#     total_pages, rest = divmod(total_size, page_size)
#     if rest > 0:
#         total_pages += 1
# 
#     if page != -1:
#         start = page * page_size
#         end = start + page_size - 1
#         return articles[start:end], total_pages
# 
#     return articles, total_pages


import datetime
from bottle import route, request, app
from bottle_utils.csrf import csrf_token
from pelican.utils import slugify
from henet.util import parse_articles


@route("/page/<name>")
@app.view("pages")
@csrf_token
def page(name):
    page = int(request.params.get('page', 0))
    data = dict(app.vars['pages'])[name]
    cache_dir = app._config['henet']['cache_dir']

# Path: henet/util.py
# def parse_articles(path, cache_dir, page=-1, page_size=20):
#     if isinstance(path, unicode):
#         path = path.encode('utf8')
# 
#     articles = []
#     for root, dirs, files in os.walk(path):
#         for file_ in files:
#             if not file_.endswith('.rst'):
#                 continue
#             fullpath = os.path.join(root, file_)
#             articles.append(parse_article(fullpath, cache_dir, path))
# 
#     articles.sort(by_date)
#     total_size = len(articles)
#     total_pages, rest = divmod(total_size, page_size)
#     if rest > 0:
#         total_pages += 1
# 
#     if page != -1:
#         start = page * page_size
#         end = start + page_size - 1
#         return articles[start:end], total_pages
# 
#     return articles, total_pages


# Path: henet/views/page.py
import datetime
from bottle import route, request, app
from bottle_utils.csrf import csrf_token
from pelican.utils import slugify
from henet.util import parse_articles



@route("/page/<name>")
@app.view("pages")
@csrf_token
def page(name):
    page = int(request.params.get('page', 0))
    data = dict(app.vars['pages'])[name]
    cache_dir = app._config['henet']['cache_dir']

    site_url = app._config['henet']['site_url']