#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

##    news.py - Generates news.
##
##    Copyright © 2012 Ben Longbons <b.r.longbons@gmail.com>
##
##    This file is part of The Mana World
##
##    This program is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program.  If not, see <http://www.gnu.org/licenses/>.


from __future__ import print_function

import sys
import os
from abc import ABCMeta, abstractmethod

try:
    # py3
    from urllib.parse import quote_plus
except ImportError:
    # py2
    from urllib import quote_plus
try:
    # py3
    from html import escape as html_escape
except ImportError:
    # py2
    from cgi import escape as html_escape

import _news_colors as colors


def url_escape(s):
    return quote_plus(s)

class BasicWriter(object):
    __slots__ = ('stream', 'isFirst')
    __metaclass__ = ABCMeta
    def __init__(self, outfile):
        self.stream = open(outfile, 'w', encoding='utf-8')
        self.isFirst = True

    @abstractmethod
    def start(self):
        pass

    @abstractmethod
    def put(self, entry, path):
        pass

    @abstractmethod
    def finish(self):
        pass

class HtmlWriter(BasicWriter):
    __slots__ = ()
    def start(self):
        self.stream.write('<!-- Generated by tools/news.py for index.php -->\n')
        #self.stream.write('<pre>\n')
        pass

    def put(self, entry, path):
        self.stream.write('<div>\n<br/><a name="' + path + '"></a>\n')
        entry = entry.replace('\n\n', '\n<br/>\n')
        entry = entry.format(**colors.make_html_colors_dict())
        self.stream.write(entry)
        self.stream.write('</div>\n')

    def finish(self):
        #self.stream.write('</pre>\n')
        pass

class NOPFmt:
    def __format__(self, s):
        return s

class RSSWriter(BasicWriter):
    __slots__ = ()
    def start(self):
        self.stream.write('<?xml version="1.0" encoding="UTF-8"?>\n<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><atom:link href="https://themanaworld.org/news.rss" rel="self" type="application/rss+xml" /><description>The Mana World News</description><title>TMW News</title><link>https://www.themanaworld.org/news-feed.php</link>\n')

    def put(self, entry, path):
        self.stream.write('<item><title>')
        title, entry = entry.lstrip('\n').split('\n', 1)
        self.stream.write(html_escape(title.format(**{'title':NOPFmt()}))) # FIXME: Assumes title is the first nonblank line
        self.stream.write('</title>\n<description>\n')
        entry = entry.replace('\n\n', '\n<br/>\n')
        entry = entry.format(**colors.make_html_colors_dict())
        entry = html_escape(entry)
        self.stream.write(entry)
        self.stream.write('</description>\n<link>https://www.themanaworld.org/news-feed.php#' + url_escape(path) + '</link>\n<guid isPermaLink="false">')
        self.stream.write(path)
        self.stream.write('</guid>\n</item>\n')

    def finish(self):
        self.stream.write('</channel>\n</rss>\n')

class JSONWriter(BasicWriter):
    __slots__ = ()
    def start(self):
        self.stream.write('[')

    def put(self, entry, path):
        if not self.isFirst:
            self.stream.write(',\n')
        self.isFirst = False
        self.stream.write('{')
        self.stream.write('"id":"%s",' % path)
        title, entry = entry.lstrip('\n').split('\n', 1)
        date, entry = entry.split('\n', 1)
        entry, author = entry.rstrip('\n').rsplit('\n', 1)
        self.stream.write('"title":"%s",' % title.format(**{'title':NOPFmt()})) # FIXME: assumes the first line is always the title
        self.stream.write('"date":"%s",' % date.format(**{'date':NOPFmt()})) # FIXME: assumes the second line is always the date
        self.stream.write('"author":"%s",' % author.format(**{'author':NOPFmt()})) # FIXME: assumes the second line is always the date
        self.stream.write('"html":"')
        entry = entry.lstrip('\n')
        entry = entry.replace('\n\n', '<br/>')
        entry = entry.format(**colors.make_html_colors_dict())
        entry = entry.replace('"', '\\"')
        entry = entry.replace('\n', ' ')
        entry = entry.rstrip('\n').rstrip(' ')
        self.stream.write(entry)
        self.stream.write('"')
        self.stream.write('}')

    def finish(self):
        self.stream.write(']\n')

class TxtWriter(BasicWriter):
    __slots__ = ()
    def start(self):
        pass
    def put(self, entry, path):
        entry = entry.replace('\n\n', '\n \n')
        entry = entry.format(**colors.make_txt_colors_dict())
        self.stream.write(entry)
        self.stream.write(' \n \n')
    def finish(self):
        # DO NOT REMOVE
        #self.stream.write('Did you really read down this far?\n')
        pass

class ForumWriter(BasicWriter):
    # unlike the other writers, the forum writer only generates the latest
    __slots__ = ('done')
    def start(self):
        self.done = False
    def put(self, entry, path):
        if self.done:
            return
        entry = entry.format(**colors.make_forum_colors_dict())
        self.stream.write(entry)
        self.done = True
    def finish(self):
        pass

def create_writers(outdir):
    yield TxtWriter(os.path.join(outdir, 'news.txt'))
    yield HtmlWriter(os.path.join(outdir, 'news.html'))
    yield RSSWriter(os.path.join(outdir, 'news.rss'))
    yield JSONWriter(os.path.join(outdir, 'news.json'))
    yield ForumWriter(os.path.join(outdir, 'news.phpbb.txt'))

def main(outdir, indir=None):
    if indir is None:
        indir = os.path.join(outdir, 'news.d')

    out = list(create_writers(outdir))
    for s in out:
        s.start()
    for entry in sorted(os.listdir(indir), reverse=True):
        suffix = '.txt'
        if not entry.endswith(suffix):
            continue
        e = open(os.path.join(indir, entry), encoding="utf-8").read()
        for s in out:
            s.put(e, entry[:-len(suffix)])
    for s in out:
        s.finish()

if __name__ == '__main__':
    main(*sys.argv[1:])
