00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 Dependencies = ["time"]
00018
00019 from MoinMoin import util, wikiutil, config
00020 from MoinMoin.Page import Page
00021
00022 class RSStoWiki:
00023 def __init__(self, macro, url, allow_html):
00024 self.macro = macro
00025 self.fmt = macro.formatter
00026 self.allow_html = allow_html
00027
00028 import feedparser
00029 self.f = feedparser.parse(url)
00030 self.result = []
00031 if self.f.feed == {}:
00032 self.result.append (self.fmt.icon('info') + \
00033 self.fmt.strong(1) + \
00034 self.fmt.text(' Unable to retreive feed %s' % url) + \
00035 self.fmt.strong(0))
00036 self.valid = False
00037 else:
00038 self.valid = True
00039
00040
00041 def get_title(self):
00042 if not self.f.feed.has_key('title'):
00043 return
00044 self.result.append(self.fmt.heading(on=1, depth=1) + \
00045 self.fmt.text(self.f.feed.title) + \
00046 self.fmt.heading(on=0, depth=1))
00047
00048 def get_subtitle(self):
00049 if not self.f.feed.has_key('subtitle'):
00050 return
00051 self.result.append(self.fmt.heading(on=1, depth=2) + \
00052 self.fmt.text(self.f.feed.subtitle) + \
00053 self.fmt.heading(on=0, depth=2))
00054
00055 def get_paragraph(self, text):
00056 self.result.append(self.fmt.paragraph(on=1) + \
00057 self.fmt.text(text) + \
00058 self.fmt.paragraph(on=0))
00059
00060 def get_link(self, link):
00061 self.result.append(self.fmt.url(on=1, href=link) + \
00062 self.fmt.icon('www') + \
00063 self.fmt.text(" "+link) + \
00064 self.fmt.url(on=0))
00065
00066 def get_feedlink(self):
00067 if not self.f.feed.has_key('link'):
00068 return
00069 self.get_link(self.f.feed.link)
00070
00071 def get_description(self):
00072 if not self.f.feed.has_key('description'):
00073 return
00074 self.get_paragraph(self.f.feed.description)
00075
00076 def get_rule(self):
00077 self.result.append(self.fmt.rule(size=1))
00078
00079 def get_entry_header(self, title):
00080 self.result.append(self.fmt.heading(on=1, depth=3) + \
00081 self.fmt.text(title) + \
00082 self.fmt.heading(on=0, depth=3))
00083
00084 def get_entry_body(self, body):
00085 self.result.append(self.fmt.paragraph(on=1))
00086 if (self.allow_html):
00087 self.result.append(self.fmt.rawHTML(body))
00088 else:
00089 self.result.append(self.fmt.text(body))
00090 self.result.append(self.fmt.paragraph(on=0))
00091
00092 def get_entries(self):
00093 for entry in self.f.entries:
00094 if entry.has_key('title'):
00095 self.get_entry_header(entry.title)
00096 if entry.has_key('updated'):
00097 self.get_paragraph(entry.updated)
00098 if entry.has_key('description'):
00099 self.get_entry_body(entry.description)
00100 if entry.has_key('link'):
00101 self.get_link(entry.link)
00102
00103 def get_output(self):
00104 if self.valid:
00105 self.get_title()
00106 self.get_subtitle()
00107 self.get_description()
00108 self.get_feedlink()
00109 self.get_rule()
00110 self.get_entries()
00111 self.get_rule()
00112 return ''.join(self.result)
00113
00114 def execute(macro, args):
00115 macro_args = args.split(",")
00116 try:
00117 if macro_args[1].strip() == "allow_html":
00118 allow_html = True
00119 else:
00120 allow_html = False
00121 except:
00122 allow_html = False
00123
00124 rss = RSStoWiki(macro, macro_args[0], allow_html)
00125 return rss.get_output()