2015-07-11 23:44:24 +08:00
|
|
|
showdown.subParser('tables', function (text, options, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var table = function () {
|
|
|
|
|
|
|
|
var tables = {},
|
|
|
|
filter;
|
|
|
|
|
2015-07-12 01:21:43 +08:00
|
|
|
tables.th = function (header, style) {
|
|
|
|
var id = '';
|
|
|
|
header = header.trim();
|
|
|
|
if (header === '') {
|
2015-07-11 23:44:24 +08:00
|
|
|
return '';
|
|
|
|
}
|
2015-07-12 01:21:43 +08:00
|
|
|
if (options.tableHeaderId) {
|
|
|
|
id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
|
|
|
|
}
|
|
|
|
if (style.trim() === '') {
|
|
|
|
style = '';
|
|
|
|
} else {
|
|
|
|
style = ' style="' + style + '"';
|
|
|
|
}
|
|
|
|
return '<th' + id + style + '>' + header + '</th>';
|
2015-07-11 23:44:24 +08:00
|
|
|
};
|
|
|
|
|
2015-07-12 01:21:43 +08:00
|
|
|
tables.td = function (cell, style) {
|
|
|
|
var subText = showdown.subParser('spanGamut')(cell.trim(), options, globals);
|
|
|
|
if (style.trim() === '') {
|
|
|
|
style = '';
|
|
|
|
} else {
|
|
|
|
style = ' style="' + style + '"';
|
|
|
|
}
|
|
|
|
return '<td' + style + '>' + subText + '</td>';
|
2015-07-11 23:44:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
tables.ths = function () {
|
|
|
|
var out = '',
|
2015-07-12 01:21:43 +08:00
|
|
|
i = 0,
|
|
|
|
hs = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
for (i; i < hs.length; i += 1) {
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.th(hs[i], style[i]) + '\n';
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
tables.tds = function () {
|
2015-07-12 01:21:43 +08:00
|
|
|
var out = '',
|
|
|
|
i = 0,
|
|
|
|
ds = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
for (i; i < ds.length; i += 1) {
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.td(ds[i], style[i]) + '\n';
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
tables.thead = function () {
|
|
|
|
var out,
|
2015-07-12 01:21:43 +08:00
|
|
|
hs = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
out = '<thead>\n';
|
|
|
|
out += '<tr>\n';
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.ths.apply(this, [hs, style]);
|
2015-07-11 23:44:24 +08:00
|
|
|
out += '</tr>\n';
|
|
|
|
out += '</thead>\n';
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
tables.tr = function () {
|
|
|
|
var out,
|
2015-07-12 01:21:43 +08:00
|
|
|
cs = [].slice.apply(arguments[0]),
|
|
|
|
style = [].slice.apply(arguments[1]);
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
out = '<tr>\n';
|
2015-07-12 01:21:43 +08:00
|
|
|
out += tables.tds.apply(this, [cs, style]);
|
2015-07-11 23:44:24 +08:00
|
|
|
out += '</tr>\n';
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
filter = function (text) {
|
|
|
|
var i = 0,
|
|
|
|
lines = text.split('\n'),
|
|
|
|
line,
|
|
|
|
hs,
|
|
|
|
out = [];
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
for (i; i < lines.length; i += 1) {
|
|
|
|
line = lines[i];
|
|
|
|
// looks like a table heading
|
|
|
|
if (line.trim().match(/^[|].*[|]$/)) {
|
|
|
|
line = line.trim();
|
2015-07-12 01:21:43 +08:00
|
|
|
|
|
|
|
var tbl = [],
|
|
|
|
align = lines[i + 1].trim(),
|
|
|
|
styles = [],
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
if (align.match(/^[|][-=|: ]+[|]$/)) {
|
|
|
|
styles = align.substring(1, align.length - 1).split('|');
|
|
|
|
for (j = 0; j < styles.length; ++j) {
|
|
|
|
styles[j] = styles[j].trim();
|
|
|
|
if (styles[j].match(/^[:][-=| ]+[:]$/)) {
|
|
|
|
styles[j] = 'text-align:center;';
|
|
|
|
|
|
|
|
} else if (styles[j].match(/^[-=| ]+[:]$/)) {
|
|
|
|
styles[j] = 'text-align:right;';
|
|
|
|
|
|
|
|
} else if (styles[j].match(/^[:][-=| ]+$/)) {
|
|
|
|
styles[j] = 'text-align:left;';
|
|
|
|
} else {
|
|
|
|
styles[j] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
tbl.push('<table>');
|
|
|
|
hs = line.substring(1, line.length - 1).split('|');
|
2015-07-12 01:21:43 +08:00
|
|
|
|
|
|
|
if (styles.length === 0) {
|
|
|
|
for (j = 0; j < hs.length; ++j) {
|
|
|
|
styles.push('text-align:left');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tbl.push(tables.thead.apply(this, [hs, styles]));
|
2015-07-11 23:44:24 +08:00
|
|
|
line = lines[++i];
|
|
|
|
if (!line.trim().match(/^[|][-=|: ]+[|]$/)) {
|
|
|
|
// not a table rolling back
|
|
|
|
line = lines[--i];
|
|
|
|
} else {
|
|
|
|
line = lines[++i];
|
|
|
|
tbl.push('<tbody>');
|
|
|
|
while (line.trim().match(/^[|].*[|]$/)) {
|
|
|
|
line = line.trim();
|
2015-07-12 01:21:43 +08:00
|
|
|
tbl.push(tables.tr.apply(this, [line.substring(1, line.length - 1).split('|'), styles]));
|
2015-07-11 23:44:24 +08:00
|
|
|
line = lines[++i];
|
|
|
|
}
|
|
|
|
tbl.push('</tbody>');
|
|
|
|
tbl.push('</table>');
|
|
|
|
// we are done with this table and we move along
|
|
|
|
out.push(tbl.join('\n'));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.push(line);
|
|
|
|
}
|
|
|
|
return out.join('\n');
|
|
|
|
};
|
|
|
|
return {parse: filter};
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.tables) {
|
|
|
|
var tableParser = table();
|
|
|
|
return tableParser.parse(text);
|
|
|
|
} else {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
});
|