src/cm/converters/abi_converters.py
changeset 454 b7a092a52eae
parent 442 b6e443be2a9b
child 456 b2a7c015362b
equal deleted inserted replaced
453:1d314f629611 454:b7a092a52eae
   268     def add_html_header(self, body):
   268     def add_html_header(self, body):
   269         """ 
   269         """ 
   270         Add an HTML header to an HTML body
   270         Add an HTML header to an HTML body
   271         """
   271         """
   272 
   272 
   273         return """
   273         if '<html' in body and '<body' in body:
       
   274           full_html = body
       
   275         else:
       
   276           full_html = """
   274 <html xmlns="http://www.w3.org/1999/xhtml">
   277 <html xmlns="http://www.w3.org/1999/xhtml">
   275     <head>
   278     <head>
   276         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
   279         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
   277     </head>
   280     </head>
   278     <body>
   281     <body>
   279         %s
   282         %s
   280     </body>
   283     </body>
   281 </html>
   284 </html>
   282 """ %body
   285 """ %body
   283 
   286 
       
   287         # Adds some style to fix Abiword default margins for paragraphs.
       
   288         from BeautifulSoup import BeautifulSoup
       
   289         import cssutils
       
   290         soup = BeautifulSoup(full_html)
       
   291         for p in soup.findAll(['p', 'div', 'ul', 'ol', 'dl']):
       
   292           try:
       
   293             css = p['style']
       
   294             s = cssutils.parseStyle(css)
       
   295             if s.getProperty('margin') == None:
       
   296               if s.getProperty('margin-top') == None:
       
   297                 s.setProperty('margin-top', '10pt')
       
   298               if s.getProperty('margin-bottom') == None:
       
   299                 s.setProperty('margin-bottom', '10pt')
       
   300             p['style'] = s.cssText
       
   301 
       
   302           except KeyError:
       
   303             p['style'] = 'margin-top: 10pt; margin-bottom: 10pt;';
       
   304 
       
   305         # for some reason having DOCTYPE declaration makes soup unhappy
       
   306         output = re.sub(r'<!(<!DOCTYPE html[^>]*>)>', r'\1', unicode(soup))
       
   307         return output