XMLParse2: add basic support for content:encoded extension attributes
[planet-ucc.git] / XHTMLWriter.py
1 #
2 # XHTMLWriter
3 #
4 # A plugin to XMLWriter to output XHTML
5 #
6 # (c) 2004, Davyd Madeley <[email protected]>
7 #
8
9 import time
10 import sys
11
12 class XHTMLWriter:
13         def __init__(self, planet):
14                 self.planet     = planet
15                 self.maxitems   = 50
16                 self.parent     = None
17         
18         def __write_item__(self, item):
19                 try:
20                         blogTitle, blogName, itemTitle = map(unicode, [item.blogTitle, item.blogName, item.itemTitle])
21                 except UnicodeDecodeError, e:
22                         print >> sys.stderr, "Unicode error in %s" % item.itemURL
23                         print >> sys.stderr, e
24                         return ''
25
26                 output  =       u''
27                 output  +=      u'<div class="item">\n'
28                 output  +=      u'<h2><a href="%s" title="%s">%s</a>: <a href="%s">%s</a></h2>\n' % (item.blogURL, blogTitle, blogName, item.itemURL, itemTitle)
29                 output  +=      u'<p class="time">\n'
30                 if item.imageURL:
31                         output += u'<a class="image" href="%s"><img class="image" src="%s" alt="%s" /></a>\n' % (item.imageLink, item.imageURL, item.blogName)
32                 output  +=      u'(<a href="%s">%s</a>)\n' % (item.itemURL ,time.strftime('posted on %A %B %d, %Y at %H:%M AWST', time.localtime(item.itemDate)))
33                 output  +=      u'</p>\n'
34                 output  +=      u'<div class="itembody">\n'
35                 output  +=      item.contents
36                 output  +=      u'\n</div>\n'
37                 if item.commentsURL:
38                         output += u'<p class="comments"><a href="%s">Comments</a></p>' % item.commentsURL
39                 output  +=      u'</div>\n'
40                 return output
41         
42         def write(self):
43                 itemcount       = 0
44                 output  =       ''
45                 output  +=      '<?xml version="1.0" encoding="UTF-8"?>\n'
46                 output  +=      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n'
47                 output  +=      '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >\n'
48                 output  +=      '<head>\n'
49                 output  +=      '<title>Planet UCC</title>\n'
50                 output  +=      '<link rel="stylesheet" href="planet.css" type="text/css" media="screen" />\n'
51                 output  +=      '<link rel="icon" type="image/png" href="icon.png" />\n'
52                 output  +=      '<link rel="SHORTCUT ICON" type="image/png" href="icon.png" />\n'
53                 output  +=      '<link rel="alternate" type="application/rss+xml" title="RSS 1.0" href="rss1.xml" />\n'
54                 output  +=      '<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="rss2.xml" />\n'
55                 output  +=      '</head>\n'
56                 output  +=      '<body>\n'
57                 output  +=      '<div class="header">\n'
58                 try:
59                         output  +=      open('header.html').read()
60                 except:
61                         pass
62                 output  +=      '</div>\n'
63                 output  +=      '<div class="items">\n'
64                 for date in self.planet:
65                         output  += '<h1>%s</h1>\n' % time.strftime('%A %B %d, %Y', time.localtime(date.planetDate))
66                         for item in date.items:
67                                 output  += self.__write_item__(item)
68                                 # see how many items we've written
69                                 itemcount += 1
70                                 if itemcount >= self.maxitems:
71                                         break
72                         # again, check to see if we've written the maximum number of items
73                         if itemcount >= self.maxitems:
74                                 break
75                 output  +=      '</div>\n'
76                 output  +=      '<div class="sidebar">\n'
77                 try:
78                         output  +=      open('sidebar.html').read()
79                 except:
80                         pass
81                 output  +=      '<h2>Last Updated</h2>\n'
82                 output  +=      '<p>\n'
83                 output  +=      '%s\n' % time.strftime('%A %B %d, %Y %H:%M AWST', time.localtime())
84                 output  +=      '</p>\n'
85                 if self.parent:
86                         output  +=      '<h2>Feeds</h2>\n'
87                         output  +=      '<p>\n'
88                         blogscopy       = self.parent.blogs + []
89                         blogscopy.sort(self.__blog_sort__)
90                         for blog in blogscopy:
91                                 output  += '<a href="%s">%s</a> (<a href="%s">feed</a>)<br />\n' % (blog.blogURL, blog.blogName, blog.feedURL)
92                         output  +=      '</p>\n'
93                 output  +=      '</div>\n'
94                 output  +=      '<div class="footer">\n'
95                 try:
96                         output  +=      open('footer.html').read()
97                 except:
98                         pass
99                 output  +=      '</div>\n'
100                 output  +=      '</body>\n'
101                 output  +=      '</html>'
102                 return output
103         
104         def __blog_sort__(self, blog1, blog2):
105                 # name1, name2  = blog1.blogName.split(' ')[-1], blog2.blogName.split(' ')[-1]
106                 name1, name2 = blog1.blogName, blog2.blogName
107                 if name1 < name2: return -1
108                 if name1 == name2: return 0
109                 if name1 > name2: return 1

UCC git Repository :: git.ucc.asn.au