summaryrefslogtreecommitdiff
path: root/news.py
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2013-01-23 18:15:14 -0800
committerBen Longbons <b.r.longbons@gmail.com>2013-01-24 08:56:33 -0800
commit48c9a9b684609f467225e3c90d96f93ba2379616 (patch)
tree37158eb50557c446a7bf8cbb6afac861aaa75d46 /news.py
parent2a6616793bdcbe3ce645323b71defd820756bbfd (diff)
downloadtools-48c9a9b684609f467225e3c90d96f93ba2379616.tar.gz
tools-48c9a9b684609f467225e3c90d96f93ba2379616.tar.bz2
tools-48c9a9b684609f467225e3c90d96f93ba2379616.tar.xz
tools-48c9a9b684609f467225e3c90d96f93ba2379616.zip
Implement news generation
Diffstat (limited to 'news.py')
-rwxr-xr-xnews.py103
1 files changed, 103 insertions, 0 deletions
diff --git a/news.py b/news.py
new file mode 100755
index 0000000..53350ac
--- /dev/null
+++ b/news.py
@@ -0,0 +1,103 @@
+#!/usr/bin/env python
+# -*- 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
+
+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('<!-- Generated by tools/news.py for index.php -->\n')
+ #self.stream.write('<pre>\n')
+ pass
+
+ def put(self, entry):
+ self.stream.write('<div>\n')
+ entry = entry.replace('\n\n', '\n<p/>\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 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:])