Package Pyblio :: Package Format :: Module HTML
[hide private]
[frames] | no frames]

Source Code for Module Pyblio.Format.HTML

 1  # This file is part of pybliographer 
 2  #  
 3  # Copyright (C) 1998-2006 Frederic GOBRY 
 4  # Email : gobry@pybliographer.org 
 5  #           
 6  # This program is free software; you can redistribute it and/or 
 7  # modify it under the terms of the GNU General Public License 
 8  # as published by the Free Software Foundation; either version 2  
 9  # of the License, or (at your option) any later version. 
10  #    
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details.  
15  #  
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the Free Software 
18  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
19  # 
20   
21  """ 
22  Transformation of the formatted record into an HTML representation. 
23  """ 
24   
25  from xml.sax.saxutils import escape 
26  from StringIO import StringIO 
27   
28  from Pyblio.Format.Generator import Generator as Base 
29   
30 -def _mkattrs(attrs):
31 # merge the attributes, handling the special case of attributes 32 # like _class -> class. 33 return ' '.join(['%s="%s"' % (k.lstrip('_'), v) 34 for k, v in attrs.items()])
35 36
37 -class Generator(Base):
38
39 - def __init__(self, fd):
40 self.fd = fd 41 return
42
43 - def do_string(self, t):
44 self.fd.write(escape(t))
45
46 - def do_i(self, t):
47 self.fd.write('<i>') 48 for s in t.children: self(s) 49 self.fd.write('</i>')
50
51 - def do_small(self, t):
52 self.fd.write('<small>') 53 for s in t.children: self(s) 54 self.fd.write('</small>')
55
56 - def do_span(self, t):
57 attrs = _mkattrs(t.attributes) 58 self.fd.write('<span %s>' % attrs) 59 for s in t.children: self(s) 60 self.fd.write('</span>')
61
62 - def do_b(self, t):
63 self.fd.write('<b>') 64 for s in t.children: self(s) 65 self.fd.write('</b>')
66
67 - def do_a(self, t):
68 attrs = _mkattrs(t.attributes) 69 self.fd.write('<a %s>' % attrs) 70 for s in t.children: self(s) 71 self.fd.write('</a>')
72
73 - def do_br(self, t):
74 self.fd.write('<br>')
75
76 - def begin_biblio(self):
77 self.fd.write('<table>\n')
78
79 - def end_biblio(self):
80 self.fd.write('</table>')
81
82 - def begin_reference(self, key):
83 self.fd.write('<tr><td>[%s]</td><td>' % escape(key))
84
85 - def end_reference(self, key):
86 self.fd.write('</td></tr>\n')
87 88
89 -def generate(t):
90 """ Convenience function that generates the HTML in a string """ 91 fd = StringIO() 92 g = Generator(fd) 93 g(t) 94 95 return fd.getvalue()
96