From ae6b9eb2e16b570c39666fb4dea2e9222a3c2d8d Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Wed, 23 Jan 2013 18:15:14 -0800 Subject: Implement news generation --- tools/.gitignore | 1 + tools/_news_colors.py | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/news.py | 103 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 tools/_news_colors.py create mode 100755 tools/news.py (limited to 'tools') diff --git a/tools/.gitignore b/tools/.gitignore index 00e2a6af..f11fb403 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -1 +1,2 @@ /aligncsv +*.pyc diff --git a/tools/_news_colors.py b/tools/_news_colors.py new file mode 100644 index 00000000..36f97b60 --- /dev/null +++ b/tools/_news_colors.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +## _news_colors.py - colors that can be used in news +## +## Copyright © 2012 Ben Longbons +## +## This file is part of The Mana World (Athena server) +## +## 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 . + +__all__ = ['make_html_colors_dict', 'make_txt_colors_dict'] + +class Color(object): + __slots__ = ('txt', 'rgb') + def __init__(self, txt, rgb): + self.txt = txt + self.rgb = rgb + +color_dict = dict( + black = Color(txt='##0', rgb=0x000000), + red = Color(txt='##1', rgb=0xff0000), + green = Color(txt='##2', rgb=0x009000), + blue = Color(txt='##3', rgb=0x0000ff), + orange = Color(txt='##4', rgb=0xe0980e), + yellow = Color(txt='##5', rgb=0xf1dc27), + pink = Color(txt='##6', rgb=0xff00d8), + purple = Color(txt='##7', rgb=0x8415e2), + gray = Color(txt='##8', rgb=0x919191), + brown = Color(txt='##9', rgb=0x8e4c17), +) + +class HtmlDate(object): + __slots__ = () + def __format__(self, date): + return '%s' % date + +class HtmlLink(object): + __slots__ = () + def __format__(self, target): + return '%s' % (target, target) + +class HtmlSignature(object): + __slots__ = () + def __format__(self, author): + return '-%s' % author + +def make_html_colors_dict(): + r = { + 'date': HtmlDate(), + 'link': HtmlLink(), + 'author': HtmlSignature(), + } + for k, v in color_dict.items(): + r[k] = '' % v.rgb + r['/' + k] = '' + return r + +# Here be dragons + +def make_txt_colors_dict(): + return dict(generate_txt_colors()) + +class StackPusher(object): + __slots__ = ('stack', 'txt') + def __init__(self, stack, txt): + self.stack = stack + self.txt = txt + def __format__(self, fmt): + assert fmt == '' + txt = self.txt + self.stack.append(txt) + return txt + +class StackPopper(object): + __slots__ = ('stack', 'txt') + def __init__(self, stack, txt): + self.stack = stack + self.txt = txt + def __format__(self, fmt): + assert fmt == '' + txt = self.txt + if len(self.stack) <= 1: + raise SyntaxError('Unmatched {/%s}' % txt) + prev = self.stack.pop() + if txt != prev: + raise SyntaxError('Mismatched {/%s} from {%s}' % (txt, prev)) + return self.stack[-1] + +class TxtDate(object): + __slots__ = ('stack') + def __init__(self, stack): + self.stack = stack + def __format__(self, date): + return '##3' + date + self.stack[-1] + +class TxtLink(object): + __slots__ = ('stack') + def __init__(self, stack): + self.stack = stack + def __format__(self, target): + return '##3' + target + self.stack[-1] + +class TxtSignature(object): + __slots__ = ('stack') + def __init__(self, stack): + self.stack = stack + def __format__(self, author): + return '-##2' + author + self.stack[-1] + +def generate_txt_colors(): + stack = ['##0'] # don't let stack become empty + for k,v in color_dict.items(): + yield k, StackPusher(stack, v.txt) + yield '/' + k, StackPopper(stack, v.txt) + yield 'date', TxtDate(stack) + yield 'link', TxtLink(stack) + yield 'author', TxtSignature(stack) diff --git a/tools/news.py b/tools/news.py new file mode 100755 index 00000000..53350ace --- /dev/null +++ b/tools/news.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +## news.py - Generates news. +## +## Copyright © 2012 Ben Longbons +## +## 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 . + + +from __future__ import print_function + +import sys +import os +from abc import ABCMeta, abstractmethod + +import _news_colors as colors + +class BasicWriter(object): + __slots__ = ('stream') + __metaclass__ = ABCMeta + def __init__(self, outfile): + self.stream = open(outfile, 'w') + + @abstractmethod + def start(self): + pass + + @abstractmethod + def put(self, entry): + pass + + @abstractmethod + def finish(self): + pass + +class HtmlWriter(BasicWriter): + __slots__ = () + def start(self): + self.stream.write('\n') + #self.stream.write('
\n')
+        pass
+
+    def put(self, entry):
+        self.stream.write('
\n') + entry = entry.replace('\n\n', '\n

\n') + entry = entry.format(**colors.make_html_colors_dict()) + self.stream.write(entry) + self.stream.write('

\n') + + def finish(self): + #self.stream.write('
\n') + pass + +class TxtWriter(BasicWriter): + __slots__ = () + def start(self): + pass + def put(self, entry): + 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 + +def create_writers(outdir): + yield TxtWriter(os.path.join(outdir, 'news.txt')) + yield HtmlWriter(os.path.join(outdir, 'news.html')) + +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): + if not entry.endswith('.txt'): + continue + e = open(os.path.join(indir, entry)).read() + for s in out: + s.put(e) + for s in out: + s.finish() + +if __name__ == '__main__': + main(*sys.argv[1:]) -- cgit v1.2.3-60-g2f50