Commits

Fredrik Håård committed f564071 Draft

Better category/keyword support

  • Participants
  • Parent commits 4a6d434
  • Branches genshi

Comments (0)

Files changed (1)

 
 def sieve(*words):
     """Filter generator for RSS generation"""
+    if not words:
+        words = ['\S+']
+    regex = re.compile("<!-- keywords: [^>]*(%s) [^>]* -->" % ('|'.join(words)))
+
     def sieve_impl(parts):
-        for word in words:
-            if (word in parts['title'].lower() or
-                word in parts['body'].lower()):
-                return True
-        return False
+        #lines = [line.strip()[12:].lower() for line in parts['body'].split('\n') if 'keywords' in line]
+        #keywords = reduce(lambda x,y: x+y, [line.split(' ') for line in lines], [])
+        match = regex.search(parts['body'])
+        if match:
+            return match.groups()
+        elif words == ['\S+']:
+            return ('uncategorized',)
+        else:
+            return tuple()
     return sieve_impl
 
 
 FEEDS = {
-    'rss.xml': lambda x: True,
-    'python.xml': sieve('python')
+    'rss.xml': sieve(),
+    'python.xml': sieve('python', 'mercurial', 'pycon')
     }
 
 def generate_rss(data, sieve, title, description, base_url,
     Depends on that the parts['created'] contains UTC """
     items = []
     for parts in data:
-        if sieve(parts):
+        categories = sieve(parts)
+        if categories:
             items.append(PyRSS2Gen.RSSItem(
                     title = parts['title'],
                     link = get_url(parts),
                     description = parts['html_body'],
                     guid = PyRSS2Gen.Guid(get_url(parts)),
+                    categories = categories,
                     pubDate = datetime.fromtimestamp(parts['created'])))
 
     return PyRSS2Gen.RSS2(