bug fixes
[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
11 class XHTMLWriter:
12         def __init__(self, planet):
13                 self.planet     = planet
14                 self.maxitems   = 50
15                 self.parent     = None
16         
17         def __write_item__(self, item):
18                 output  =       ''
19                 output  +=      '<div class="item">\n'
20                 output  +=      '<h2><a href="%s" title="%s">%s</a>: <a href="%s">%s</a></h2>\n' % (item.blogURL, item.blogTitle, item.blogName,
21                                                                                                         item.itemURL, item.itemTitle)
22                 output  +=      '<p class="time">\n'
23                 if item.imageURL:
24                         output += '<a class="image" href="%s"><img class="image" src="%s" alt="%s" /></a>\n' % (item.imageLink, item.imageURL, item.blogName)
25                 output  +=      '(<a href="%s">%s</a>)\n' % (item.itemURL ,time.strftime('posted on %A %B %d, %Y at %H:%M AWST', time.localtime(item.itemDate)))
26                 output  +=      '</p>\n'
27                 output  +=      '<p class="body">\n'
28                 output  +=      item.contents
29                 output  +=      '\n</p>\n'
30                 output  +=      '</div>\n'
31                 return output
32         
33         def write(self):
34                 itemcount       = 0
35                 output  =       ''
36                 output  +=      '<?xml version="1.0" encoding="UTF-8"?>\n'
37                 output  +=      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n'
38                 output  +=      '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >\n'
39                 output  +=      '<head>\n'
40                 output  +=      '<title>Planet UCC</title>\n'
41                 output  +=      '<link rel="stylesheet" href="planet.css" type="text/css" media="screen" />\n'
42                 output  +=      '<link rel="icon" type="image/png" href="icon.png" />\n'
43                 output  +=      '</head>\n'
44                 output  +=      '<body>\n'
45                 output  +=      '<div class="header">\n'
46                 try:
47                         output  +=      open('header.html').read()
48                 except:
49                         pass
50                 output  +=      '</div>\n'
51                 output  +=      '<div class="sidebar">\n'
52                 try:
53                         output  +=      open('sidebar.html').read()
54                 except:
55                         pass
56                 output  +=      '<h2>Last Updated</h2>\n'
57                 output  +=      '<p>\n'
58                 output  +=      '%s\n' % time.strftime('%A %B %d, %Y %H:%M AWST', time.localtime())
59                 output  +=      '</p>\n'
60                 if self.parent:
61                         output  +=      '<h2>Feeds</h2>\n'
62                         output  +=      '<p>\n'
63                         blogscopy       = self.parent.blogs + []
64                         blogscopy.sort(self.__blog_sort__)
65                         for blog in blogscopy:
66                                 output  += '<a href="%s">%s</a> (<a href="%s">feed</a>)<br />\n' % (blog.blogURL, blog.blogName, blog.feedURL)
67                         output  +=      '</p>\n'
68                 output  +=      '</div>\n'
69                 output  +=      '<div class="items">\n'
70                 for date in self.planet:
71                         output  += '<h1>%s</h1>\n' % time.strftime('%A %B %d, %Y', time.localtime(date.planetDate))
72                         for item in date.items:
73                                 output  += self.__write_item__(item)
74                                 # see how many items we've written
75                                 itemcount += 1
76                                 if itemcount >= self.maxitems:
77                                         break
78                         # again, check to see if we've written the maximum number of items
79                         if itemcount >= self.maxitems:
80                                 break
81                 output  +=      '</div>\n'
82                 output  +=      '<div class="footer">\n'
83                 try:
84                         output  +=      open('footer.html').read()
85                 except:
86                         pass
87                 output  +=      '</div>\n'
88                 output  +=      '</body>\n'
89                 output  +=      '</html>'
90                 return output
91         
92         def __blog_sort__(self, blog1, blog2):
93                 name1, name2    = blog1.blogName.split(' ')[-1], blog2.blogName.split(' ')[-1]
94                 if name1 < name2: return -1
95                 if name1 == name2: return 0
96                 if name1 > name2: return 1

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