From 3acdb782f65b40f36ff9222285b7c505daa8741b Mon Sep 17 00:00:00 2001 From: Freeyorp Date: Fri, 25 May 2018 12:23:24 +1200 Subject: Add RSS to news generation This also: - Changes the generated html for news-feed.php to provide anchors based on the input fragment name. - Changes newlines input to produce
rather than

. Browsers treat self closing syntax on a non-void HTML element as a start tag. - Makes a minor refactor to provide the name of the fragment in put() - Changes to python3. We were already using the print function from __future__, and it's 2018 now. Python 3 was released a decade ago. Kept try/except fallbacks for python2 compatibility anyway. --- news.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/news.py b/news.py index 7c9056e..fa66d78 100755 --- a/news.py +++ b/news.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- encoding: utf-8 -*- ## news.py - Generates news. @@ -27,8 +27,25 @@ 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') __metaclass__ = ABCMeta @@ -40,7 +57,7 @@ class BasicWriter(object): pass @abstractmethod - def put(self, entry): + def put(self, entry, path): pass @abstractmethod @@ -54,9 +71,9 @@ class HtmlWriter(BasicWriter): #self.stream.write('

\n')
         pass
 
-    def put(self, entry):
-        self.stream.write('
\n

\n') - entry = entry.replace('\n\n', '\n

\n') + def put(self, entry, path): + self.stream.write('

\n
\n') + entry = entry.replace('\n\n', '\n
\n') entry = entry.format(**colors.make_html_colors_dict()) self.stream.write(entry) self.stream.write('
\n') @@ -65,11 +82,36 @@ class HtmlWriter(BasicWriter): #self.stream.write('
\n') pass +class NOPFmt: + def __format__(self, s): + return s + +class RSSWriter(BasicWriter): + __slots__ = () + def start(self): + self.stream.write('\nThe Mana World NewsTMW Newshttps://www.themanaworld.org/news-feed.php\n') + + def put(self, entry, path): + self.stream.write('') + 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('\n\n') + entry = entry.replace('\n\n', '\n
\n') + entry = entry.format(**colors.make_html_colors_dict()) + entry = html_escape(entry) + self.stream.write(entry) + self.stream.write('
\nhttps://www.themanaworld.org/news-feed.php#' + url_escape(path) + '\n') + self.stream.write(path) + self.stream.write('\n
\n') + + def finish(self): + self.stream.write('
\n
\n') + class TxtWriter(BasicWriter): __slots__ = () def start(self): pass - def put(self, entry): + def put(self, entry, path): entry = entry.replace('\n\n', '\n \n') entry = entry.format(**colors.make_txt_colors_dict()) self.stream.write(entry) @@ -84,7 +126,7 @@ class ForumWriter(BasicWriter): __slots__ = ('done') def start(self): self.done = False - def put(self, entry): + def put(self, entry, path): if self.done: return entry = entry.format(**colors.make_forum_colors_dict()) @@ -96,6 +138,7 @@ class ForumWriter(BasicWriter): 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 ForumWriter(os.path.join(outdir, 'news.phpbb.txt')) def main(outdir, indir=None): @@ -106,11 +149,12 @@ def main(outdir, indir=None): for s in out: s.start() for entry in sorted(os.listdir(indir), reverse=True): - if not entry.endswith('.txt'): + suffix = '.txt' + if not entry.endswith(suffix): continue e = open(os.path.join(indir, entry)).read() for s in out: - s.put(e) + s.put(e, entry[:-len(suffix)]) for s in out: s.finish() -- cgit v1.2.3-60-g2f50