This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
220 lines (189 loc) · 6.5 KB
/
index.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'use strict';
;(function() {
// Establish the root object, `window` in the browser, or `exports` on the server.
let root = this;
class HTMLGen {
constructor(options={}) {
let {title, tags, pretty} = options;
this._title = title || 'Default title';
this.pretty = pretty || false;
// initialize tags
this.tags = {};
if (tags && typeof tags === 'object') {
for (let tag in tags) {
this.tags[tag] = tags[tag] || '';
}
}
for (let tag of ['head', 'body', 'header', 'footer']) {
if (!this.tags[tag]) this.tags[tag] = '';
}
// A trap for getting a property value
// like __noSuchMethod__ in firefox
// like method_missing in ruby
const handler = {
get (target, key) {
return Reflect.has(target, key) ? Reflect.get(target, key) : function methodMissing(attrhash, content) {
if (typeof attrhash === 'string' || typeof attrhash === 'function') {
content = content ? content : attrhash;
attrhash = {};
}
if (typeof content === 'string') {
content = content;
} else if (typeof content === 'function') {
content = content() && content().toString() ? content().toString() : null;
} else {
content = null;
}
return this.gentag(key, attrhash, content);
}
}
}
return new Proxy(this, handler);
}
gentag(m, attrhash={}, content) {
let origm, html;
m = m.toString();
if (this.shorttags[m]) {
origm = m;
m = this.shorttags[m]['tag'];
attrhash = Object.assign(attrhash, this.shorttags[origm]);
delete attrhash['tag'];
if (attrhash['!append']) {
content += attrhash['!append'];
delete attrhash['!append'];
}
}
let nl = this.newlinetags.includes(m) && this.pretty ? '\n' : '';
let attribs = '';
let singletag = this.singletags.includes(m);
if (Object.keys(attrhash).length != 0) {
for (let k in attrhash) {
let singleattr = this.singleattribs.includes(k);
let v = attrhash[k];
if (v) attribs += singleattr ? ` ${k}` : ` ${k}="${this.entities(v.toString())}"`;
}
}
if (singletag) {
html = `<${m}${attribs}>` + nl;
} else if (!singletag && content) {
if (content[-1] != 10) content += nl;
if (content[0] != 10) content = nl + content;
html = `<${m}${attribs}>` + content + `</${m}>`;
} else {
html = `<${m}${attribs}></${m}>` + nl;
}
return html;
}
list(l) {
return this.ul(() => {
});
}
entities(s) {
return escape(s);
}
unentities(s) {
return unescape(s);
}
urlencode(s) {
return encodeURIComponent(s);
}
urldecode(s) {
return decodeURIComponent(s);
}
getTitle () {
return this._title;
}
setTitle(t) {
return this._title = t;
}
append (content, tag='head') {
let tags = this.tags;
if (typeof content === 'string') return tags[tag] += content;
if (typeof content === 'function') return tags[tag] += content() ? content() : '';
}
page(content='') {
return typeof content === 'string' || typeof content === 'function' ?
'<!DOCTYPE html>' +
this.html(() => {
return this.head(() => {
return this.meta({charset: 'utf-8'}) +
this.title(`${this.entities(this._title)}`) +
this.meta({content: 'width=device-width, initial-scale=1, maximum-scale=1', name: 'viewport'}) +
this.meta({content: 'index', name: 'robots'}) +
this.tags.head || '';
}) +
this.body(() => {
return this.div({class: 'container'}, () => {
return this.tags.header + this.section({id: 'content'}, typeof content === 'string' ? content : content()) + this.tags.footer;
}) + this.tags.body;
});
}) :
'';
}
}
HTMLGen.prototype.newlinetags = 'html body div br ul hr title link head fieldset label legend option table li select td tr meta'.split(' ');
HTMLGen.prototype.singletags = 'base meta link br hr img input'.split(' ');
HTMLGen.prototype.singleattribs = 'async defer novalidate required'.split(' ');
HTMLGen.prototype.shorttags = {
'css': {tag: 'link', rel: 'stylesheet'},
'js': {tag: 'script'},
'text': {tag: 'input', type: 'text'},
'password': {tag: 'input', type: 'password'},
'file': {tag: 'input', type: 'file'},
'hidden': {tag: 'input', type: 'hidden'},
'button': {tag: 'input', type: 'button'},
'submit': {tag: 'input', type: 'submit'},
'checkbox': {tag: 'input', type: 'checkbox'},
'radio': {tag: 'input', type: 'radio'}
}
// Invert the keys and values of an object. The values must be serializable.
function invert(obj) {
let result = {};
let keys = Object.keys(obj);
for (let i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result;
};
// List of HTML entities for escaping.
let escapeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
let unescapeMap = invert(escapeMap);
// Functions for escaping and unescaping strings to/from HTML interpolation.
function createEscaper(map) {
let escaper = function(match) {
return map[match];
};
// Regexes for identifying a key that needs to be escaped
let source = '(?:' + Object.keys(map).join('|') + ')';
let testRegexp = RegExp(source);
let replaceRegexp = RegExp(source, 'g');
return function(string) {
string = string == null ? '' : '' + string;
return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
};
};
let escape = createEscaper(escapeMap);
let unescape = createEscaper(unescapeMap);
// Export the HTMLGen class for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `HTMLGen` as a global object.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = HTMLGen;
}
exports.HTMLGen = HTMLGen;
} else if (typeof define === 'function' && define.amd) {
define(function() {
return HTMLGen;
});
} else {
root.HTMLGen = HTMLGen;
}
}.call(this));