1# Copyright 2010 Google Inc. All Rights Reserved. 2"""Utilities for generating html.""" 3 4 5def GetPageHeader(page_title): 6 return """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 7"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 8<html> 9<head> 10<style type="text/css"> 11table 12{ 13border-collapse:collapse; 14} 15table, td, th 16{ 17border:1px solid black; 18} 19</style> 20<script type="text/javascript"> 21function displayRow(id){ 22 var row = document.getElementById("group_"+id); 23 if (row.style.display == '') row.style.display = 'none'; 24 else row.style.display = ''; 25 } 26</script> 27<title>%s</title> 28</head> 29<body> 30 31""" % page_title 32 33 34def GetListHeader(): 35 return '<ul>' 36 37 38def GetListItem(text): 39 return '<li>%s</li>' % text 40 41 42def GetListFooter(): 43 return '</ul>' 44 45 46def GetList(items): 47 return '<ul>%s</ul>' % ''.join(['<li>%s</li>' % item for item in items]) 48 49 50def GetParagraph(text): 51 return '<p>%s</p>' % text 52 53 54def GetFooter(): 55 return '</body>\n</html>' 56 57 58def GetHeader(text, h=1): 59 return '<h%s>%s</h%s>' % (h, text, h) 60 61 62def GetTableHeader(headers): 63 row = ''.join(['<th>%s</th>' % header for header in headers]) 64 return '<table><tr>%s</tr>' % row 65 66 67def GetTableFooter(): 68 return '</table>' 69 70 71def FormatLineBreaks(text): 72 return text.replace('\n', '<br/>') 73 74 75def GetTableCell(text): 76 return '<td>%s</td>' % FormatLineBreaks(str(text)) 77 78 79def GetTableRow(columns): 80 return '<tr>%s</tr>' % '\n'.join([GetTableCell(column) for column in columns]) 81 82 83def GetTable(headers, rows): 84 table = [GetTableHeader(headers)] 85 table.extend([GetTableRow(row) for row in rows]) 86 table.append(GetTableFooter()) 87 return '\n'.join(table) 88 89 90def GetLink(link, text): 91 return "<a href='%s'>%s</a>" % (link, text) 92