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

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