Attachment 'IRSS.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Inline RSS generator
   4 
   5     Copyright (c) 2003 by Conectiva, Inc.
   6     Copyright (c) 2000-2002 by Jürgen Hermann <jh@web.de>
   7     All rights reserved, see COPYING for details.
   8 
   9     Written by Gustavo Niemeyer <niemeyer@conectiva.com>, based on
  10     code from RecentChanges.
  11 
  12     This macro allows one to use a moin page as an RSS feed. To
  13     define an RSS feed, enclose the items you want to provide in the
  14     comment tags "##irss start" and "##irss stop", like this:
  15     
  16     ----
  17 
  18     This won't be considered
  19 
  20     ##irss start
  21 
  22     == Newest item ==
  23     This will be the newest RSS item.
  24 
  25     == Older item ==
  26     This is another RSS item.
  27 
  28     ##irss stop
  29 
  30     This won't be considered as well.
  31 
  32     ----
  33 
  34     Then, you can link to that feed in from any moin page using
  35     [[IRSS([PageName])]]. If linking from the same page, the pagename
  36     parameter is optional.
  37 
  38     You can also have more than one feed in the same page. To do that,
  39     use "##irss start yourfeedname", and link to it with
  40     [[IRSS([PageName], yourfeedname)]] (you can use an empty first
  41     parameter, if the feed is in the same page). Using the feedname in
  42     the stop tag, like "##irss stop yourfeedname", is optional and necessary
  43     only if you want to have one feed "contained" inside another one.
  44 
  45     The following tags can also be used between the start and stop tags
  46     to configure the feed.
  47 
  48     ##irss topic <rss topic>
  49 
  50         This will set the "title" of the whole RSS feed. Only the first
  51         topic found is considered. The default topic is the page name.
  52 
  53     ##irss descr <rss description>
  54 
  55         This will set the "description" of the whole RSS feed. Only the
  56         first description found is considered. The default description
  57         is "News from <pagename>".
  58 
  59     ##irss reverse 
  60 
  61         The default behavior is to have the newer items at the top of
  62         the list. If used, this tag will reverse this behavior.
  63 
  64     ##irss link <url>
  65 
  66         This tag should be used just after a Moin title, and will
  67         make the RSS item title link to that URL. The default behavior
  68         is to link the RSS item title to the Moin title in the page
  69         where the feed is defined.
  70 """
  71 
  72 import re, string, sys, StringIO, new, sha
  73 import pprint
  74 from MoinMoin import config, util, wikiutil, wikimacro
  75 from MoinMoin.Page import Page
  76 from MoinMoin.wikixml.util import RssGenerator
  77 from MoinMoin.parser.wiki import Parser
  78 from MoinMoin.formatter.text_html import Formatter
  79 
  80 def execute(pagename, request):
  81 
  82     page = Page(request, pagename)
  83 
  84     # Send a null page to make the page set its internal data (ugh!)
  85     out = StringIO.StringIO()
  86     backup = sys.stdout, request.write
  87     sys.stdout, request.write = out, out.write
  88     page.send_page(request, content_only=1)
  89     sys.stdout, request.write = backup
  90 
  91     # get params
  92     items_limit = 100
  93     try:
  94         max_items = int(request.form['items'].value)
  95         max_items = min(max_items, items_limit) # not more than `items_limit`
  96     except (KeyError, ValueError):
  97         # not more than 15 items in a RSS file by default
  98         max_items = 15
  99 
 100     # prepare output
 101     out = StringIO.StringIO()
 102     handler = RssGenerator(out)
 103 
 104     # get data
 105     interwiki = request.getBaseURL()
 106     if interwiki[-1] != "/": interwiki = interwiki + "/"
 107 
 108     logo = re.search(r'src="([^"]*)"', request.cfg.logo_string)
 109     if logo: logo = request.getQualifiedURL(logo.group(1))
 110 
 111     title_re = re.compile(r"^(={1,5}) (?P<title>.*) \1$")
 112     start_re = re.compile(r"^##irss\s+start(?:\s+(?P<name>\S+))?\s*$")
 113     stop_re = re.compile(r"^##irss stop(?:\s+(?P<name>\S+))?\s*$")
 114     topic_re = re.compile(r"^##irss\s+topic\s+(?P<topic>.*)$")
 115     descr_re = re.compile(r"^##irss\s+descr\s+(?P<descr>.*)$")
 116     reverse_re = re.compile(r"^##irss\s+reverse\s*$")
 117     link_re = re.compile(r"^##irss\s+link\s+(?P<link>.*)$")
 118     feeding = 0
 119     counter = 0
 120     rssdata = []
 121     rsstopic = None
 122     rssdescr = None
 123     rssreverse = 0
 124     class RssData:
 125         def __init__(self, title):
 126             self.title = title
 127             self.link = None
 128             self.lines = []
 129     if not request.user.may.read(pagename):
 130         rssdata.append(RssData("You are not allowed to view that page."))
 131     else:
 132         rssname = None
 133         titles = {}
 134         body = IncludeParser(request, page).parse(page.get_raw_body())
 135         for line in body.splitlines():
 136             m = start_re.match(line)
 137             if m:
 138                 if feeding:
 139                     continue
 140                 name = m.group("name")
 141                 try:
 142                     if name != request.form["name"].value: continue
 143                 except KeyError:
 144                     if name: continue
 145                 rssname = name
 146                 feeding = 1
 147                 continue
 148 
 149             m = title_re.match(line)
 150             if m:
 151                 if counter >= max_items:
 152                     break
 153                 # Based on parser/wiki.py
 154                 item_title = m.group("title")
 155                 try:
 156                     titles[item_title] += 1
 157                     unique_id = '-%d' % titles[item.title]
 158                 except KeyError:
 159                     titles[item_title] = 1
 160                     unique_id = ''
 161                 if not feeding:
 162                     continue
 163                 item = RssData(item_title)
 164                 rssdata.append(item)
 165                 item.link = "%s%s#head-%s%s" % \
 166                             (interwiki, wikiutil.quoteWikinameURL(pagename),
 167                              sha.new(item.title).hexdigest(), unique_id)
 168                 counter += 1
 169                 continue
 170 
 171             if not feeding:
 172                 continue
 173 
 174             m = stop_re.match(line)
 175             if m:
 176                 name = m.group("name")
 177                 if name and name != rssname:
 178                     continue
 179                 feeding = 0
 180                 continue
 181 
 182             m = topic_re.match(line)
 183             if m:
 184                 if not rsstopic:
 185                     rsstopic = m.group("topic").strip()
 186                 continue
 187 
 188             m = descr_re.match(line)
 189             if m:
 190                 if not rssdescr:
 191                     rssdescr = m.group("descr").strip()
 192                 continue
 193 
 194             m = link_re.match(line)
 195             if m:
 196                 if rssdata:
 197                     rssdata[-1].link = m.group("link")
 198                 continue
 199 
 200             m = reverse_re.match(line)
 201             if m:
 202                 rssreverse = 1
 203                 continue
 204 
 205             if rssdata and not line.startswith("##"):
 206                 rssdata[-1].lines.append(line)
 207 
 208     if not rsstopic:
 209         rsstopic = pagename
 210     if not rssdescr:
 211         rssdescr = "News from " + pagename
 212     if rssreverse:
 213         rssdata.reverse()
 214 
 215     # start SAX stream
 216     handler.startDocument()
 217 
 218     # emit channel description
 219     handler.startNode('channel', {
 220         (handler.xmlns['rdf'], 'about'): request.getBaseURL(),
 221     })
 222     handler.simpleNode('title', rsstopic)
 223     handler.simpleNode('link', interwiki +
 224                                wikiutil.quoteWikinameURL(pagename))
 225     handler.simpleNode('description', rssdescr)
 226     if logo:
 227         handler.simpleNode('image', None, {
 228             (handler.xmlns['rdf'], 'resource'): logo,
 229         })
 230     if request.cfg.interwikiname:
 231         handler.simpleNode(('wiki', 'interwiki'), request.cfg.interwikiname)
 232 
 233     handler.endNode('channel')
 234 
 235     # emit logo data
 236     if logo:
 237         handler.startNode('image', attr={
 238             (handler.xmlns['rdf'], 'about'): logo,
 239         })
 240         handler.simpleNode('title', rsstopic)
 241         handler.simpleNode('link', interwiki + 
 242                                    wikiutil.quoteWikinameURL(pagename))
 243         handler.simpleNode('url', logo)
 244         handler.endNode('image')
 245 
 246     # emit items
 247     for item in rssdata:
 248         handler.startNode('item')
 249         handler.simpleNode('title', item.title)
 250         handler.simpleNode('link', item.link)
 251 
 252         #handler.simpleNode(('dc', 'date'), util.W3CDate(item.time))
 253 
 254         desc_text = "\n".join(item.lines)
 255         if desc_text:
 256             parsed = StringIO.StringIO()
 257             request_write = request.write
 258             request.write = parsed.write
 259             Parser(desc_text, request).format(page.formatter)
 260             request.write = request_write
 261             handler.simpleNode('description', parsed.getvalue())
 262         handler.endNode('item')
 263 
 264     # end SAX stream
 265     handler.endDocument()
 266 
 267     # send the generated XML document
 268     request.http_headers(["Content-Type: text/xml; charset=%s" % config.charset]
 269                          + request.nocache)
 270     sys.stderr.write(out.getvalue().encode('utf-8'))
 271     sys.stderr.write("whatever works\n")
 272     request.write(out.getvalue())
 273     request.finish()
 274     request.no_closing_html_code = 1
 275 
 276 class IncludeParser:
 277     def __init__(self, request, page):
 278         self.parser = Parser("", request)
 279         # Format the empty string, making it set its internal data (ugh!).
 280         out = StringIO.StringIO()
 281         backup = sys.stdout, request.write
 282         sys.stdout, request.write = out, out.write
 283         self.parser.format(page.formatter)
 284         sys.stdout, request.write = backup
 285         self.include_re = re.compile("\[\[Include(?:\(.*?\))?\]\]")
 286         self.macro = wikimacro.Macro(self.parser)
 287 
 288         # This is really deep and cool black magic. Basically, it creates
 289         # a local copy of the function macro.Include.execute that behaves
 290         # exactly like the original one, but with a different "Page"
 291         # global. This allows us to follow changes in the Include macro
 292         # without much trouble.
 293         from MoinMoin.macro.Include import execute
 294         func_globals = {}
 295         func_globals.update(execute.func_globals)
 296         class IncludePage(Page):
 297             incparser = self
 298             def send_page(self, request, msg=None, **keywords):
 299                 request.write(self.incparser._parse(self.get_raw_body()))
 300         func_globals["Page"] = IncludePage
 301         self.execute = new.function(execute.func_code, func_globals,
 302                                     execute.func_name,
 303                                     execute.func_defaults)
 304 
 305     def _macro_repl(self, match):
 306         word = match.group(0)
 307         macro_name = word[2:-2]
 308         args = None
 309         if string.count(macro_name, "("):
 310             macro_name, args = string.split(macro_name, '(', 1)
 311             args = args[:-1]
 312         return self.execute(self.macro, args)
 313 
 314     def _parse(self, body):
 315         return self.include_re.sub(self._macro_repl, body)
 316 
 317     def parse(self, body):
 318         # Turn on some special settings, like disabling the "edit" link.
 319         #item = self.macro.form["action"]
 320         #pprint.pprint(self.macro.form, sys.stderr)
 321         #item_value = item.value
 322         #item.value = "print"
 323         body = self._parse(body)
 324         #item.value = item_value
 325         return body
 326 
 327 # vim:ts=4:sw=4:et

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-04-14 07:28:03, 4.7 KB) [[attachment:CreateNewPage.py]]
  • [get | view] (2011-04-14 07:26:24, 4.2 KB) [[attachment:CreateNewPage1.6.py]]
  • [get | view] (2006-09-10 21:29:29, 40.4 KB) [[attachment:CreatePdfDocument2_0_3.py]]
  • [get | view] (2006-09-12 06:05:06, 40.5 KB) [[attachment:CreatePdfDocument2_0_4.py]]
  • [get | view] (2006-09-12 12:00:09, 40.6 KB) [[attachment:CreatePdfDocument2_0_5.py]]
  • [get | view] (2006-11-14 21:56:11, 43.5 KB) [[attachment:CreatePdfDocument2_0_6.py]]
  • [get | view] (2006-11-15 17:00:47, 43.8 KB) [[attachment:CreatePdfDocument2_0_7.py]]
  • [get | view] (2006-11-16 22:06:18, 43.8 KB) [[attachment:CreatePdfDocument2_0_8.py]]
  • [get | view] (2006-12-17 15:54:21, 43.6 KB) [[attachment:CreatePdfDocument2_0_9.py]]
  • [get | view] (2007-08-20 09:10:23, 67.2 KB) [[attachment:CreatePdfDocument2_1_0.py]]
  • [get | view] (2007-08-21 07:39:49, 67.1 KB) [[attachment:CreatePdfDocument2_1_1.py]]
  • [get | view] (2007-09-11 19:16:37, 67.3 KB) [[attachment:CreatePdfDocument2_1_2.py]]
  • [get | view] (2007-09-18 20:17:58, 68.1 KB) [[attachment:CreatePdfDocument2_1_3.py]]
  • [get | view] (2007-09-21 13:32:54, 71.1 KB) [[attachment:CreatePdfDocument2_1_4.py]]
  • [get | view] (2007-09-23 20:56:30, 73.4 KB) [[attachment:CreatePdfDocument2_1_5.py]]
  • [get | view] (2007-09-25 20:54:48, 74.5 KB) [[attachment:CreatePdfDocument2_2_0.py]]
  • [get | view] (2008-06-23 21:08:49, 77.0 KB) [[attachment:CreatePdfDocument2_3_0.py]]
  • [get | view] (2008-06-26 19:25:07, 81.0 KB) [[attachment:CreatePdfDocument2_3_1.py]]
  • [get | view] (2008-07-06 05:50:38, 83.1 KB) [[attachment:CreatePdfDocument2_3_2.py]]
  • [get | view] (2008-07-09 17:42:02, 83.3 KB) [[attachment:CreatePdfDocument2_3_3.py]]
  • [get | view] (2008-09-07 11:11:01, 83.5 KB) [[attachment:CreatePdfDocument2_3_4.py]]
  • [get | view] (2009-01-11 15:53:09, 84.3 KB) [[attachment:CreatePdfDocument2_3_5.py]]
  • [get | view] (2009-02-16 06:52:06, 84.2 KB) [[attachment:CreatePdfDocument2_3_6.py]]
  • [get | view] (2010-01-29 11:53:21, 82.8 KB) [[attachment:CreatePdfDocument2_4_0.py]]
  • [get | view] (2010-01-31 14:10:03, 84.6 KB) [[attachment:CreatePdfDocument2_4_1.py]]
  • [get | view] (2010-09-18 16:23:23, 85.6 KB) [[attachment:CreatePdfDocument2_4_2.py]]
  • [get | view] (2006-06-16 20:56:53, 4.9 KB) [[attachment:FlashManager.py-1.5.3-1]]
  • [get | view] (2003-12-07 18:15:53, 3.9 KB) [[attachment:HTML2MoinMoin.py]]
  • [get | view] (2005-10-16 08:24:35, 4.9 KB) [[attachment:HelpOn-1.3.5-4.py]]
  • [get | view] (2006-02-03 19:21:04, 4.9 KB) [[attachment:HelpOn-1.5.1-5.py]]
  • [get | view] (2006-07-04 10:45:22, 4.8 KB) [[attachment:HelpOn-1.5.4-6.py]]
  • [get | view] (2006-07-04 22:39:14, 4.8 KB) [[attachment:HelpOn-1.6.0-7.py]]
  • [get | view] (2006-07-06 13:50:17, 4.0 KB) [[attachment:HelpOn-1.6.0-8.py]]
  • [get | view] (2008-01-10 17:43:24, 4.8 KB) [[attachment:HelpOn-1.6.0-9.py]]
  • [get | view] (2008-08-19 14:44:54, 5.0 KB) [[attachment:HelpOn-1.7.1-10.py]]
  • [get | view] (2005-02-20 18:28:34, 10.8 KB) [[attachment:IRSS.py]]
  • [get | view] (2005-03-09 22:46:23, 2.9 KB) [[attachment:ImportHtml-1.2.py]]
  • [get | view] (2003-12-07 18:15:53, 2.8 KB) [[attachment:ImportHtml.py]]
  • [get | view] (2003-12-07 18:15:53, 1.8 KB) [[attachment:IrcChat.py]]
  • [get | view] (2008-06-09 11:27:20, 4.4 KB) [[attachment:MoinCrypt.py]]
  • [get | view] (2010-11-29 12:08:27, 7.5 KB) [[attachment:PageActions.py]]
  • [get | view] (2006-08-07 15:12:19, 0.5 KB) [[attachment:PermanentLink.py]]
  • [get | view] (2003-12-07 18:15:53, 6.3 KB) [[attachment:PhoneDial.py]]
  • [get | view] (2005-04-17 14:21:47, 3.6 KB) [[attachment:RecommendPage-1.3.4-1.py]]
  • [get | view] (2005-04-19 18:21:52, 5.5 KB) [[attachment:RecommendPage-1.3.4-2.py]]
  • [get | view] (2005-05-02 19:53:09, 5.6 KB) [[attachment:RecommendPage-1.3.4-3.py]]
  • [get | view] (2005-09-03 07:33:35, 6.3 KB) [[attachment:RecommendPage-1.3.4-4.py]]
  • [get | view] (2005-09-05 17:44:03, 6.9 KB) [[attachment:RecommendPage-1.3.5-5.py]]
  • [get | view] (2005-09-07 16:42:26, 7.5 KB) [[attachment:RecommendPage-1.3.5-6.py]]
  • [get | view] (2005-09-08 16:06:28, 7.7 KB) [[attachment:RecommendPage-1.3.5-7.py]]
  • [get | view] (2005-11-01 11:31:51, 9.0 KB) [[attachment:RecommendPage-1.3.5-8.py]]
  • [get | view] (2006-02-03 19:40:51, 8.3 KB) [[attachment:RecommendPage-1.5.1-9.py]]
  • [get | view] (2008-01-11 09:14:35, 6.8 KB) [[attachment:RecommendPage-1.6.0-10.py]]
  • [get | view] (2008-08-19 14:44:59, 6.9 KB) [[attachment:RecommendPage-1.7.1-11.py]]
  • [get | view] (2008-06-09 11:27:40, 1.7 KB) [[attachment:ShowActions.py]]
  • [get | view] (2008-06-09 10:34:02, 5.3 KB) [[attachment:ShowDecrypted.py]]
  • [get | view] (2005-03-30 21:17:28, 7.7 KB) [[attachment:Slideshow.py]]
  • [get | view] (2004-02-02 20:48:31, 2.0 KB) [[attachment:SubscribeUser.py]]
  • [get | view] (2007-01-26 17:08:30, 2.2 KB) [[attachment:Subscribers-1.6.0.py]]
  • [get | view] (2003-12-07 18:15:53, 1.8 KB) [[attachment:Subscribers.py]]
  • [get | view] (2006-03-18 23:16:51, 0.8 KB) [[attachment:UserPreferences.py]]
  • [get | view] (2004-01-05 09:56:25, 8.1 KB) [[attachment:VisualSiteMap.py]]
  • [get | view] (2015-08-30 21:04:23, 11.1 KB) [[attachment:VisualSiteMap_1.10.py]]
  • [get | view] (2004-10-08 10:59:16, 9.3 KB) [[attachment:VisualSiteMap_1.2.py]]
  • [get | view] (2005-03-16 01:30:09, 9.8 KB) [[attachment:VisualSiteMap_1.3.py]]
  • [get | view] (2014-08-19 01:34:10, 10.8 KB) [[attachment:VisualSiteMap_1.9.py]]
  • [get | view] (2007-08-18 18:52:55, 1.0 KB) [[attachment:backlink.py]]
  • [get | view] (2007-03-15 05:53:49, 23.5 KB) [[attachment:findandreplace0.1Beta.py]]
  • [get | view] (2005-03-27 20:32:10, 3.6 KB) [[attachment:gallery2image-1.3.3-1.py]]
  • [get | view] (2005-08-03 20:14:56, 4.0 KB) [[attachment:gallery2image-1.3.3-2.py]]
  • [get | view] (2005-11-13 18:10:26, 20.7 KB) [[attachment:gallery2image-1.3.5-10.py]]
  • [get | view] (2005-11-25 22:03:50, 20.8 KB) [[attachment:gallery2image-1.3.5-11.py]]
  • [get | view] (2005-08-08 17:23:43, 8.4 KB) [[attachment:gallery2image-1.3.5-4.py]]
  • [get | view] (2005-08-13 15:15:45, 13.7 KB) [[attachment:gallery2image-1.3.5-5.py]]
  • [get | view] (2005-08-31 22:05:22, 15.5 KB) [[attachment:gallery2image-1.3.5-6.py]]
  • [get | view] (2005-10-29 20:23:50, 15.9 KB) [[attachment:gallery2image-1.3.5-8.py]]
  • [get | view] (2005-11-01 11:31:24, 17.6 KB) [[attachment:gallery2image-1.3.5-9.py]]
  • [get | view] (2006-01-27 20:52:32, 20.9 KB) [[attachment:gallery2image-1.5.1-12.py]]
  • [get | view] (2006-08-06 09:01:01, 22.1 KB) [[attachment:gallery2image-1.5.4-13.py]]
  • [get | view] (2006-08-11 18:21:40, 22.2 KB) [[attachment:gallery2image-1.5.4-14.py]]
  • [get | view] (2006-11-16 20:23:27, 22.6 KB) [[attachment:gallery2image-1.5.6-16.py]]
  • [get | view] (2006-08-11 18:30:22, 22.2 KB) [[attachment:gallery2image-1.6.0-15.py]]
  • [get | view] (2008-02-06 10:13:45, 22.3 KB) [[attachment:gallery2image-1.6.0-16.py]]
  • [get | view] (2008-05-20 15:51:09, 22.4 KB) [[attachment:gallery2image-1.6.3-17.py]]
  • [get | view] (2006-09-06 06:19:48, 1.3 KB) [[attachment:getmmap.py]]
  • [get | view] (2004-07-18 09:48:00, 1.5 KB) [[attachment:localnames.py]]
  • [get | view] (2005-03-25 15:02:31, 2.6 KB) [[attachment:newpageonly.py]]
  • [get | view] (2005-03-30 09:02:00, 3.5 KB) [[attachment:newpageonly_20050330.py]]
  • [get | view] (2006-06-06 19:12:27, 9.7 KB) [[attachment:pdf.py]]
  • [get | view] (2006-08-30 10:51:51, 36.0 KB) [[attachment:pdf2_0_0.py]]
  • [get | view] (2006-08-30 13:57:36, 36.5 KB) [[attachment:pdf2_0_1.py]]
  • [get | view] (2006-02-04 04:25:29, 1.0 KB) [[attachment:sisterindex.py]]
  • [get | view] (2004-10-28 07:33:10, 0.7 KB) [[attachment:xml.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.