2 Commits 9b2765efcd ... 5f1bb51af4

Author SHA1 Message Date
  likhy 5f1bb51af4 displays rich content on atom feeds 5 months ago
  likhy 52faa2c3c2 properly terminates HTML tags 5 months ago
1 changed files with 53 additions and 11 deletions
  1. 53 11
      src/rss.py

+ 53 - 11
src/rss.py

@@ -1,4 +1,7 @@
 
+from microblog import My_Html_Parser
+# from html.parser import HTMLParser
+from html import escape
 import sys, traceback, dateutil.parser
 try:
     import feedgenerator
@@ -17,7 +20,7 @@ def line2words(lines, limit):
         words = line.split()
         for word in words:
             l = len(word)
-            if l + char_count > limit:
+            if limit > 0 and (l + char_count > limit):
                 break_outer_loop = True
                 break;
             output.append(word)
@@ -32,6 +35,33 @@ def line2words(lines, limit):
         output.append("...")
     return output
 
+# this is similar tot he markup function in microblog
+def enrich_msg(lines, is_atom=True):
+    if not is_atom:
+        return string
+    content = []
+    parser = My_Html_Parser([])
+    for line in lines:
+        parser.feed(line)
+        if parser.stack == [] \
+        and (parser.completed_by == "" or parser.completed_by not in line):
+            words = line.split()
+            for i in range(len(words)):
+                word  = words[i]
+                if word.find("src=") == 0 \
+                or word.find("href=") == 0:
+                    continue
+                elif word.find("https://") != -1: 
+                    w = escape(word)
+                    new_word = ("<a href=\"%s\">%s</a>") % (w, w)
+                    words[i] = new_word
+            words.insert(0,"<p>")
+            words.append("</p>")
+            content.append(" ".join(words))
+        else:
+            content.append(line)
+    return content
+
 def write_feed(posts, filename, params):
     feed = None
     t = params["title"]
@@ -50,23 +80,35 @@ def write_feed(posts, filename, params):
             description = d,
         )
     base_url = l
+    TITLE_LEN_LIMIT = 60
+    DESC_LEN_LIMIT = -1 if params["use_atom"] else 300
     for post in posts:
         # len of post.message is number of lines
-        TITLE_LEN_LIMIT = 60
-        DESC_LEN_LIMIT = 300
         msg = post.message
         ti = " ".join(
             line2words(msg,TITLE_LEN_LIMIT))
-        de = " ".join(
-            line2words(msg,DESC_LEN_LIMIT))
+        if params["use_atom"]:
+            de = " ".join(enrich_msg(msg))
+        else:
+            de = " ".join(
+                line2words(msg,DESC_LEN_LIMIT))
         li = base_url + ("#%i" % post.num)
         p = dateutil.parser.parse(post.timestamp)
-        feed.add_item(
-            title = ti,
-            link = li,
-            description = de,
-            pubdate = p
-        )
+        if params["use_atom"]:
+            feed.add_item(
+                title = ti,
+                link = li,
+                description = str(),
+                content = de,
+                pubdate = p
+            )
+        else:
+            feed.add_item(
+                title = ti,
+                link = li,
+                description = de,
+                pubdate = p
+            )
     with open(filename, 'w') as f:
         f.write(feed.writeString('utf-8'))
     del feed