aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/html_tools.py
blob: 202bef05a5ef52b22c188598374277081a6a9a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
# Copyright 2019 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Utilities for generating html."""


def GetPageHeader(page_title):
    return (
        """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
table
{
border-collapse:collapse;
}
table, td, th
{
border:1px solid black;
}
</style>
<script type="text/javascript">
function displayRow(id){
  var row = document.getElementById("group_"+id);
  if (row.style.display == '')  row.style.display = 'none';
    else row.style.display = '';
  }
</script>
<title>%s</title>
</head>
<body>

"""
        % page_title
    )


def GetListHeader():
    return "<ul>"


def GetListItem(text):
    return "<li>%s</li>" % text


def GetListFooter():
    return "</ul>"


def GetList(items):
    return "<ul>%s</ul>" % "".join(["<li>%s</li>" % item for item in items])


def GetParagraph(text):
    return "<p>%s</p>" % text


def GetFooter():
    return "</body>\n</html>"


def GetHeader(text, h=1):
    return "<h%s>%s</h%s>" % (h, text, h)


def GetTableHeader(headers):
    row = "".join(["<th>%s</th>" % header for header in headers])
    return "<table><tr>%s</tr>" % row


def GetTableFooter():
    return "</table>"


def FormatLineBreaks(text):
    return text.replace("\n", "<br/>")


def GetTableCell(text):
    return "<td>%s</td>" % FormatLineBreaks(str(text))


def GetTableRow(columns):
    return "<tr>%s</tr>" % "\n".join(
        [GetTableCell(column) for column in columns]
    )


def GetTable(headers, rows):
    table = [GetTableHeader(headers)]
    table.extend([GetTableRow(row) for row in rows])
    table.append(GetTableFooter())
    return "\n".join(table)


def GetLink(link, text):
    return "<a href='%s'>%s</a>" % (link, text)