Add cleantitle plugin + Story support
authorJack Miller <jack@codezen.org>
Wed, 8 Jul 2015 16:50:00 +0000 (11:50 -0500)
committerJack Miller <jack@codezen.org>
Wed, 8 Jul 2015 16:53:05 +0000 (11:53 -0500)
This plugin adds the ability to do simple text replacement on story
titles before they're displayed. Useful for stripping out content that
(according to the feed) should be there, but (according to the user) is
crap. Default case, newlines and some HTML tags that seem to get left
behind occasionally.

canto_curses/story.py
plugins/cleantitle.py [new file with mode: 0644]

index 6099862..0ca1a0f 100644 (file)
@@ -315,6 +315,15 @@ class Story(PluginHandler):
                 self.lns = 1
                 return self.lns
 
+        for attr in list(self.plugin_attrs.keys()):
+            if not attr.startswith("edit_"):
+                continue
+            try:
+                getattr(self, attr)()
+            except:
+                log.error("Error running story editing plugin")
+                log.error(traceback.format_exc())
+
         self.evald_string = self.eval()
 
         taglist_conf = self.callbacks["get_opt"]("taglist")
diff --git a/plugins/cleantitle.py b/plugins/cleantitle.py
new file mode 100644 (file)
index 0000000..2314a19
--- /dev/null
@@ -0,0 +1,39 @@
+# Clean Title Plugin
+# by Jack Miller
+# v1.0
+
+# This plugin will strip some annoying content out of story titles. Even though
+# most of it should technically still be in there (we already try to parse
+# HTML, etc.) some feeds are poorly defined and will double escape HTML and
+# insert annoying newlines.
+
+# For now, just do a simple string replace, regexen are probably a bit heavy
+# for this.
+
+replacements = [
+    ("\n", ""),
+    ("<em>",""),
+    ("</em>",""),
+    ("<strong>",""),
+    ("</strong>",""),
+    ("<nobr />", ""),
+]
+
+from canto_next.plugins import check_program
+
+check_program("canto-curses")
+
+from canto_curses.story import StoryPlugin
+
+class CleanTitle(StoryPlugin):
+    def __init__(self, story):
+        self.story = story
+        self.plugin_attrs = { "edit_clean" : self.edit_clean }
+
+    def edit_clean(self):
+        t = self.story.content["title"]
+
+        for o,n in replacements:
+            t = t.replace(o, n)
+
+        self.story.content["title"] = t