# Path: henet/comments.py
# class ArticleThread(object):
#     def __init__(self, storage_dir, article_uuid=None, thread_uuid=None):
#         self.storage_dir = storage_dir
#         if article_uuid is None:
#             article_uuid = str(uuid4())
#         self.article_uuid = article_uuid
#         self.thread_uuid = thread_uuid
#         self.thread = None
#         hashed_uuid = hashlib.md5(self.article_uuid).hexdigest()
#         self.filename = os.path.join(self.storage_dir,
#                                      'article_' + hashed_uuid + '.rst')
#         self.load()
# 
#     def add_comment(self, *args, **kw):
#         return self.thread.add_comment(*args, **kw)
# 
#     def asjson(self):
#         comments = [comment.asjson() for comment in
#                     self.thread.get_comments()]
# 
#         return {'article_uuid': self.article_uuid,
#                 'thread_uuid': self.thread_uuid,
#                 'comments': comments}
# 
#     def save(self):
#         with open(self.filename, 'w') as f:
#             f.write(self.render().encode('utf8'))
#         self.thread.save()
# 
#     def load(self):
#         if not os.path.exists(self.filename):
#             self.thread = Thread(self.storage_dir, self.thread_uuid)
#             self.thread_uuid = self.thread.uuid
#         else:
#             with open(self.filename) as f:
#                 uuids = f.read().split(':')
#             self.article_uuid = uuids[0]
#             self.thread_uuid = uuids[1]
#             self.thread = Thread(self.storage_dir, self.thread_uuid)
# 
#         self.thread.link_to_article(self.article_uuid)
# 
#     def render(self):
#         return u'%s:%s' % (self.article_uuid, self.thread_uuid)
# 
#     @classmethod
#     def loadfromfile(cls, filename):
#         storage_dir = os.path.dirname(filename)
#         klass = ArticleThread(storage_dir)
#         klass.filename = filename
#         klass.load()
#         return klass
# 
# Path: henet/rst/rst2html.py
# def rst2html(rst, theme=None, opts=None, body_only=False):
#     rst_opts = default_rst_opts.copy()
#     rst_opts['warning_stream'] = StringIO()
# 
#     if body_only:
#         out = publish_parts(rst, writer_name='html',
#                             settings_overrides=rst_opts)['html_body']
# 
#         rst_opts['warning_stream'].seek(0)
#         warnings = rst_opts['warning_stream'].read()
#         return out, warnings
# 
#     if opts:
#         rst_opts.update(opts)
#     rst_opts['template'] = os.path.join(THEMES, 'template.txt')
# 
#     stylesheets = ['basic.css']
#     if theme:
#         stylesheets.append('%s/%s.css' % (theme, theme))
#     rst_opts['stylesheet'] = ','.join([J(THEMES, p) for p in stylesheets])
# 
#     out = publish_string(rst, writer_name='html', settings_overrides=rst_opts)
# 
#     rst_opts['warning_stream'].seek(0)
#     warnings = rst_opts['warning_stream'].read()
#     return out, warnings


import traceback
from pelican import signals
from henet.comments import ArticleThread
from henet.rst.rst2html import rst2html
# -*- coding: utf-8 -*-




# xxx read config
storage_dir = '/Users/tarek/Dev/github.com/acr-dijon.org/comments/'


# xxx cache
def add_comments(generator, content):
    try:
        # the article unique id is its relative source path,
        # so the comments are not dependant on the URL.
        source_path = content.get_relative_source_path()
        article_uuid = source_path.encode('utf8')
        thread = ArticleThread(storage_dir, article_uuid)
        thread = thread.asjson()

        for comment in thread['comments']:
            html = rst2html(comment['text'], theme='acr', body_only=True)
            comment['html'] = html

        content.metadata["comments"] = thread
    except:
        # XXX for some reason Pelican does not print plugins exceptions

# Path: henet/comments.py
# class ArticleThread(object):
#     def __init__(self, storage_dir, article_uuid=None, thread_uuid=None):
#         self.storage_dir = storage_dir
#         if article_uuid is None:
#             article_uuid = str(uuid4())
#         self.article_uuid = article_uuid
#         self.thread_uuid = thread_uuid
#         self.thread = None
#         hashed_uuid = hashlib.md5(self.article_uuid).hexdigest()
#         self.filename = os.path.join(self.storage_dir,
#                                      'article_' + hashed_uuid + '.rst')
#         self.load()
# 
#     def add_comment(self, *args, **kw):
#         return self.thread.add_comment(*args, **kw)
# 
#     def asjson(self):
#         comments = [comment.asjson() for comment in
#                     self.thread.get_comments()]
# 
#         return {'article_uuid': self.article_uuid,
#                 'thread_uuid': self.thread_uuid,
#                 'comments': comments}
# 
#     def save(self):
#         with open(self.filename, 'w') as f:
#             f.write(self.render().encode('utf8'))
#         self.thread.save()
# 
#     def load(self):
#         if not os.path.exists(self.filename):
#             self.thread = Thread(self.storage_dir, self.thread_uuid)
#             self.thread_uuid = self.thread.uuid
#         else:
#             with open(self.filename) as f:
#                 uuids = f.read().split(':')
#             self.article_uuid = uuids[0]
#             self.thread_uuid = uuids[1]
#             self.thread = Thread(self.storage_dir, self.thread_uuid)
# 
#         self.thread.link_to_article(self.article_uuid)
# 
#     def render(self):
#         return u'%s:%s' % (self.article_uuid, self.thread_uuid)
# 
#     @classmethod
#     def loadfromfile(cls, filename):
#         storage_dir = os.path.dirname(filename)
#         klass = ArticleThread(storage_dir)
#         klass.filename = filename
#         klass.load()
#         return klass
# 
# Path: henet/rst/rst2html.py
# def rst2html(rst, theme=None, opts=None, body_only=False):
#     rst_opts = default_rst_opts.copy()
#     rst_opts['warning_stream'] = StringIO()
# 
#     if body_only:
#         out = publish_parts(rst, writer_name='html',
#                             settings_overrides=rst_opts)['html_body']
# 
#         rst_opts['warning_stream'].seek(0)
#         warnings = rst_opts['warning_stream'].read()
#         return out, warnings
# 
#     if opts:
#         rst_opts.update(opts)
#     rst_opts['template'] = os.path.join(THEMES, 'template.txt')
# 
#     stylesheets = ['basic.css']
#     if theme:
#         stylesheets.append('%s/%s.css' % (theme, theme))
#     rst_opts['stylesheet'] = ','.join([J(THEMES, p) for p in stylesheets])
# 
#     out = publish_string(rst, writer_name='html', settings_overrides=rst_opts)
# 
#     rst_opts['warning_stream'].seek(0)
#     warnings = rst_opts['warning_stream'].read()
#     return out, warnings


# Path: pelican/plugins/henet_comments.py
import traceback
from pelican import signals
from henet.comments import ArticleThread
from henet.rst.rst2html import rst2html

# -*- coding: utf-8 -*-




# xxx read config
storage_dir = '/Users/tarek/Dev/github.com/acr-dijon.org/comments/'


# xxx cache
def add_comments(generator, content):
    try:
        # the article unique id is its relative source path,
        # so the comments are not dependant on the URL.
        source_path = content.get_relative_source_path()
        article_uuid = source_path.encode('utf8')
        thread = ArticleThread(storage_dir, article_uuid)
        thread = thread.asjson()

        for comment in thread['comments']:
            html = rst2html(comment['text'], theme='acr', body_only=True)
            comment['html'] = html

        content.metadata["comments"] = thread
    except:
        # XXX for some reason Pelican does not print plugins exceptions

        traceback.print_exc()