-
Notifications
You must be signed in to change notification settings - Fork 181
/
htmlx.go
165 lines (151 loc) · 4.33 KB
/
htmlx.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package core
import "strings"
var defaultCol = `<colgroup>
<col width="40%">
<col width="60%">
</colgroup>`
var tableHtml = `<html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;">
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Dotweb</title>
<style>
.overtable {
width: 100%;
overflow: hidden;
overflow-x: auto;
}
body {
max-width: 780px;
margin:0 auto;
font-family: 'trebuchet MS', 'Lucida sans', Arial;
font-size: 1rem;
color: #444;
}
table {
font-family: 'trebuchet MS', 'Lucida sans', Arial;
*border-collapse: collapse;
/* IE7 and lower */
border-spacing: 0;
width: 100%;
border-collapse: collapse;
overflow-x: auto
}
caption {
font-family: 'Microsoft Yahei', 'trebuchet MS', 'Lucida sans', Arial;
text-align: left;
padding: .5rem;
font-weight: bold;
font-size: 110%;
color: #666;
}
tr {
border-top: 1px solid #dfe2e5
}
tr:nth-child(2n) {
background-color: #f6f8fa
}
td,
th {
border: 1px solid #dfe2e5;
padding: .6em 1em;
}
.bordered tr:hover {
background: #fbf8e9;
}
.bordered td,
.bordered th {
border: 1px solid #ccc;
padding: 10px;
text-align: left;
}
</style>
<script>
(function(doc, win) {
window.MPIXEL_RATIO = (function () {
var Mctx = document.createElement("canvas").getContext("2d"),
Mdpr = window.devicePixelRatio || 1,
Mbsr = Mctx.webkitBackingStorePixelRatio ||
Mctx.mozBackingStorePixelRatio ||
Mctx.msBackingStorePixelRatio ||
Mctx.oBackingStorePixelRatio ||
Mctx.backingStorePixelRatio || 1;
return Mdpr/Mbsr;
})();
function addEventListeners(ele,type,callback){
try{ // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
ele.addEventListener(type,callback,false);
}catch(e){
try{ // IE8.0及其以下版本
ele.attachEvent('on' + type,callback);
}catch(e){ // 早期浏览器
ele['on' + type] = callback;
}
}
}
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
window.recalc = function() {
var clientWidth = docEl.clientWidth < 768 ? docEl.clientWidth : 768;
if (!clientWidth) return;
docEl.style.fontSize = 10 * (clientWidth / 320) * window.MPIXEL_RATIO + 'px';
};
window.recalc();
addEventListeners(win, resizeEvt, recalc);
})(document, window);
</script>
</head>
<body>
<div class="overtable">
{{tableBody}}
</div>
</body>
</html>
`
// CreateTablePart create a table part html by replacing flags
func CreateTablePart(col, title, header, body string) string {
template := `<br><table class="bordered">
{{col}}
<caption>{{title}}</caption>
<thead>
{{header}}
</thead>
{{body}}
</table>`
if col == "" {
col = defaultCol
}
data := strings.Replace(template, "{{col}}", col, -1)
data = strings.Replace(data, "{{title}}", title, -1)
data = strings.Replace(data, "{{header}}", header, -1)
data = strings.Replace(data, "{{body}}", body, -1)
return data
}
// CreateTableHtml create a complete page html by replacing {{tableBody}} and table part html
func CreateTableHtml(col, title, header, body string) string {
template := `<br><table class="bordered">
{{col}}
<caption>{{title}}</caption>
<thead>
{{header}}
</thead>
{{body}}
</table>`
if col == "" {
col = defaultCol
}
data := strings.Replace(template, "{{col}}", col, -1)
data = strings.Replace(data, "{{title}}", title, -1)
data = strings.Replace(data, "{{header}}", header, -1)
data = strings.Replace(data, "{{body}}", body, -1)
return CreateHtml(data)
}
// CreateHtml create a complete page html by replacing {{tableBody}}
func CreateHtml(tableBody string) string {
return strings.Replace(tableHtml, "{{tableBody}}", tableBody, -1)
}