plugins, parsing threads list with BeautifulSoup
[chanspy.git] / server / html_generation.py
blobc3c31c8d46911856036637c88e3fc4876e19f19a
1 # -*- coding: utf-8 -*-
3 import sys
4 import shutil
5 import os
6 from BeautifulSoup import BeautifulSoup
8 # some constants
9 htdocs_dir = 'htdocs'
10 template = os.path.join(htdocs_dir, 'list_template.html')
11 try:
12 f = open(template, 'r')
13 template_data = f.read()
14 f.close()
15 except:
16 template_data = ''
18 #---------------------------------------------#
19 def get_chans_html(chans_list):
20 '''Generate chans html.'''
22 soup = BeautifulSoup(template_data)
24 body = u'\n'
25 for (chan_name, uri) in chans_list:
26 body += u'<div class="chans_list"><a href="%s" target="BOARDS">%s</a></div>\n' %(uri, chan_name)
28 soup.body.replaceWith(u'<body><div class="border">\n%s\n</div></body>' %body)
29 return unicode(soup)[:-1]
31 #---------------------------------------------#
32 def get_boards_html(boards_list):
33 '''Generate boards html.'''
35 soup = BeautifulSoup(template_data)
37 body = u'\n'
38 for (board_name, uri) in boards_list:
39 body += u'<div class="boards_list"><a href="%s" target="THREADS">%s</a></div>\n' %(uri, board_name)
41 soup.body.replaceWith(u'<body><div class="border">\n%s\n</div></body>' %body)
42 return unicode(soup)[:-1]
44 #---------------------------------------------#
45 def get_threads_html(threads_list, chan_name, board_name):
46 '''Generate threads html.'''
48 soup = BeautifulSoup(template_data)
50 body = u'<h2 class="caption">%s %s</h1>\n' %(chan_name, board_name)
51 for thread_dict in threads_list:
52 body += u'<div class="threads_list"><a name="%s"> %s | %s </a></div>\n' %( thread_dict['id'], thread_dict['title'], thread_dict['date'])
54 soup.body.replaceWith(u'<body>%s</body>' %body)
55 return unicode(soup)[:-1]