# 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/comments.py
# class CommentsDB(object):
#     def __init__(self, storage_dir):
#         self.storage_dir = storage_dir
#         if not os.path.exists(storage_dir):
#             os.makedirs(storage_dir)
# 
#     def _get_comment(self, uuid):
#         thread = comment = None
#         for file in os.listdir(self.storage_dir):
#             if not file.startswith('thread_'):
#                 continue
#             filename = os.path.join(self.storage_dir, file)
#             thread = Thread.loadfromfile(filename)
#             try:
#                 comment = thread.get_comment(uuid)
#                 break
#             except KeyError:
#                 pass
#         if comment is None:
#             thread = None
#         return thread, comment
# 
#     def activate_comment(self, uuid):
#         thread, comment = self._get_comment(uuid)
#         if thread is not None:
#             thread.activate_comment(comment.uuid)
#             thread.save()
# 
#     def reject_comment(self, uuid):
#         thread, comment = self._get_comment(uuid)
#         if thread is not None:
#             thread.reject_comment(comment.uuid)
#             thread.save()
# 
#     def _get_comments(self, inactive=True, active=False,
#                       article_uuid=None):
#         for file in os.listdir(self.storage_dir):
#             if not file.startswith('article_'):
#                 continue
#             filename = os.path.join(self.storage_dir, file)
#             article_thread = ArticleThread.loadfromfile(filename)
#             thread = article_thread.thread
# 
#             if (article_uuid is not None and
#                     article_thread.article_uuid != article_uuid):
#                 continue
# 
#             for comment in thread.get_comments(include_inactive=inactive,
#                                                include_active=active):
#                 yield comment
# 
#     def get_moderation_queue(self):
#         return self._get_comments()
# 
#     def get_comments(self, article_uuid=None):
#         return self._get_comments(inactive=False, active=True,
#                                   article_uuid=article_uuid)


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



# XXX batch by year!
@route("/category/<name>")
@app.view("category")
@csrf_token
def category(name):
    page = int(request.params.get('page', 0))
    data = dict(app.vars['categories'])[name]
    cache_dir = app._config['henet']['cache_dir']
    site_url = app._config['henet']['site_url']
    article_url = app._config['henet']['article_url']

# 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/comments.py
# class CommentsDB(object):
#     def __init__(self, storage_dir):
#         self.storage_dir = storage_dir
#         if not os.path.exists(storage_dir):
#             os.makedirs(storage_dir)
# 
#     def _get_comment(self, uuid):
#         thread = comment = None
#         for file in os.listdir(self.storage_dir):
#             if not file.startswith('thread_'):
#                 continue
#             filename = os.path.join(self.storage_dir, file)
#             thread = Thread.loadfromfile(filename)
#             try:
#                 comment = thread.get_comment(uuid)
#                 break
#             except KeyError:
#                 pass
#         if comment is None:
#             thread = None
#         return thread, comment
# 
#     def activate_comment(self, uuid):
#         thread, comment = self._get_comment(uuid)
#         if thread is not None:
#             thread.activate_comment(comment.uuid)
#             thread.save()
# 
#     def reject_comment(self, uuid):
#         thread, comment = self._get_comment(uuid)
#         if thread is not None:
#             thread.reject_comment(comment.uuid)
#             thread.save()
# 
#     def _get_comments(self, inactive=True, active=False,
#                       article_uuid=None):
#         for file in os.listdir(self.storage_dir):
#             if not file.startswith('article_'):
#                 continue
#             filename = os.path.join(self.storage_dir, file)
#             article_thread = ArticleThread.loadfromfile(filename)
#             thread = article_thread.thread
# 
#             if (article_uuid is not None and
#                     article_thread.article_uuid != article_uuid):
#                 continue
# 
#             for comment in thread.get_comments(include_inactive=inactive,
#                                                include_active=active):
#                 yield comment
# 
#     def get_moderation_queue(self):
#         return self._get_comments()
# 
#     def get_comments(self, article_uuid=None):
#         return self._get_comments(inactive=False, active=True,
#                                   article_uuid=article_uuid)


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




# XXX batch by year!
@route("/category/<name>")
@app.view("category")
@csrf_token
def category(name):
    page = int(request.params.get('page', 0))
    data = dict(app.vars['categories'])[name]
    cache_dir = app._config['henet']['cache_dir']
    site_url = app._config['henet']['site_url']
    article_url = app._config['henet']['article_url']

    article_url = site_url + '/' + article_url