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

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