-
Notifications
You must be signed in to change notification settings - Fork 114
/
render_html.go
136 lines (127 loc) · 3.15 KB
/
render_html.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package table
import "strings"
// RenderHTML renders the Table in HTML format. Example:
// <table class="go-pretty-table">
// <thead>
// <tr>
// <th align="right">#</th>
// <th>First Name</th>
// <th>Last Name</th>
// <th align="right">Salary</th>
// <th> </th>
// </tr>
// </thead>
// <tbody>
// <tr>
// <td align="right">1</td>
// <td>Arya</td>
// <td>Stark</td>
// <td align="right">3000</td>
// <td> </td>
// </tr>
// <tr>
// <td align="right">20</td>
// <td>Jon</td>
// <td>Snow</td>
// <td align="right">2000</td>
// <td>You know nothing, Jon Snow!</td>
// </tr>
// <tr>
// <td align="right">300</td>
// <td>Tyrion</td>
// <td>Lannister</td>
// <td align="right">5000</td>
// <td> </td>
// </tr>
// </tbody>
// <tfoot>
// <tr>
// <td align="right"> </td>
// <td> </td>
// <td>Total</td>
// <td align="right">10000</td>
// <td> </td>
// </tr>
// </tfoot>
// </table>
func (t *Table) RenderHTML() string {
t.initForRender()
var out strings.Builder
if t.numColumns > 0 {
out.WriteString("<table class=\"")
out.WriteString(t.htmlCSSClass)
out.WriteString("\">\n")
t.htmlRenderRows(&out, t.rowsHeader, true, false)
t.htmlRenderRows(&out, t.rows, false, false)
t.htmlRenderRows(&out, t.rowsFooter, false, true)
out.WriteString("</table>")
}
return t.render(&out)
}
func (t *Table) htmlRenderRow(out *strings.Builder, row RowStr, isHeader bool, isFooter bool) {
out.WriteString(" <tr>\n")
for colIdx := 0; colIdx < t.numColumns; colIdx++ {
var colStr string
if colIdx < len(row) {
colStr = row[colIdx]
}
// header uses "th" instead of "td"
colTagName := "td"
if isHeader {
colTagName = "th"
}
// determine the HTML "align"/"valign" property values
align := t.getAlign(colIdx).HTMLProperty()
vAlign := t.getVAlign(colIdx).HTMLProperty()
// write the row
out.WriteString(" <")
out.WriteString(colTagName)
if align != "" {
out.WriteRune(' ')
out.WriteString(align)
}
if vAlign != "" {
out.WriteRune(' ')
out.WriteString(vAlign)
}
out.WriteString(">")
if len(colStr) > 0 {
out.WriteString(strings.Replace(colStr, "\n", "<br/>", -1))
} else {
out.WriteString(" ")
}
out.WriteString("</")
out.WriteString(colTagName)
out.WriteString(">\n")
}
out.WriteString(" </tr>\n")
}
func (t *Table) htmlRenderRows(out *strings.Builder, rows []RowStr, isHeader bool, isFooter bool) {
if len(rows) > 0 {
// determine that tag to use based on the type of the row
rowsTag := "tbody"
if isHeader {
rowsTag = "thead"
} else if isFooter {
rowsTag = "tfoot"
}
var renderedTagOpen, shouldRenderTagClose bool
for _, row := range rows {
if len(row) > 0 {
if !renderedTagOpen {
out.WriteString(" <")
out.WriteString(rowsTag)
out.WriteString(">\n")
renderedTagOpen = true
}
t.htmlRenderRow(out, row, isHeader, isFooter)
shouldRenderTagClose = true
}
}
if shouldRenderTagClose {
out.WriteString(" </")
out.WriteString(rowsTag)
out.WriteString(">\n")
}
}
}