2015-07-11 23:44:24 +08:00
|
|
|
showdown.subParser('tables', function (text, options, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
if (!options.tables) {
|
|
|
|
return text;
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2017-01-29 08:07:19 +08:00
|
|
|
var tableRgx = /^ {0,3}\|?.+\|.+\n[ \t]{0,3}\|?[ \t]*:?[ \t]*(?:-|=){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:-|=){2,}[\s\S]+?(?:\n\n|¨0)/gm;
|
2016-01-25 13:24:54 +08:00
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function parseStyles (sLine) {
|
2016-05-18 04:27:40 +08:00
|
|
|
if (/^:[ \t]*--*$/.test(sLine)) {
|
2016-01-25 13:24:54 +08:00
|
|
|
return ' style="text-align:left;"';
|
2016-05-18 04:27:40 +08:00
|
|
|
} else if (/^--*[ \t]*:[ \t]*$/.test(sLine)) {
|
2016-01-25 13:24:54 +08:00
|
|
|
return ' style="text-align:right;"';
|
2016-05-18 04:27:40 +08:00
|
|
|
} else if (/^:[ \t]*--*[ \t]*:$/.test(sLine)) {
|
2016-01-25 13:24:54 +08:00
|
|
|
return ' style="text-align:center;"';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function parseHeaders (header, style) {
|
2016-01-25 13:24:54 +08:00
|
|
|
var id = '';
|
|
|
|
header = header.trim();
|
2017-08-05 03:19:15 +08:00
|
|
|
// support both tablesHeaderId and tableHeaderId due to error in documention so we don't break backwards compatibility
|
|
|
|
if (options.tablesHeaderId || options.tableHeaderId) {
|
2016-01-25 13:24:54 +08:00
|
|
|
id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
|
|
|
|
}
|
|
|
|
header = showdown.subParser('spanGamut')(header, options, globals);
|
|
|
|
|
2016-02-02 08:29:06 +08:00
|
|
|
return '<th' + id + style + '>' + header + '</th>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function parseCells (cell, style) {
|
2016-01-25 13:24:54 +08:00
|
|
|
var subText = showdown.subParser('spanGamut')(cell, options, globals);
|
2016-02-02 08:29:06 +08:00
|
|
|
return '<td' + style + '>' + subText + '</td>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-31 13:46:25 +08:00
|
|
|
function buildTable (headers, cells) {
|
2016-01-25 13:24:54 +08:00
|
|
|
var tb = '<table>\n<thead>\n<tr>\n',
|
|
|
|
tblLgn = headers.length;
|
|
|
|
|
|
|
|
for (var i = 0; i < tblLgn; ++i) {
|
|
|
|
tb += headers[i];
|
|
|
|
}
|
|
|
|
tb += '</tr>\n</thead>\n<tbody>\n';
|
|
|
|
|
|
|
|
for (i = 0; i < cells.length; ++i) {
|
2016-02-02 08:29:06 +08:00
|
|
|
tb += '<tr>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
for (var ii = 0; ii < tblLgn; ++ii) {
|
|
|
|
tb += cells[i][ii];
|
2015-07-12 01:21:43 +08:00
|
|
|
}
|
2016-02-02 08:29:06 +08:00
|
|
|
tb += '</tr>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
2016-02-02 08:29:06 +08:00
|
|
|
tb += '</tbody>\n</table>\n';
|
2016-01-25 13:24:54 +08:00
|
|
|
return tb;
|
|
|
|
}
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('tables.before', text, options, globals);
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2017-02-21 22:13:12 +08:00
|
|
|
// find escaped pipe characters
|
|
|
|
text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
|
|
|
|
|
|
|
|
// parse tables
|
2016-01-25 13:24:54 +08:00
|
|
|
text = text.replace(tableRgx, function (rawTable) {
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
var i, tableLines = rawTable.split('\n');
|
|
|
|
|
|
|
|
// strip wrong first and last column if wrapped tables are used
|
|
|
|
for (i = 0; i < tableLines.length; ++i) {
|
2016-08-30 13:24:19 +08:00
|
|
|
if (/^ {0,3}\|/.test(tableLines[i])) {
|
|
|
|
tableLines[i] = tableLines[i].replace(/^ {0,3}\|/, '');
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
|
|
|
if (/\|[ \t]*$/.test(tableLines[i])) {
|
|
|
|
tableLines[i] = tableLines[i].replace(/\|[ \t]*$/, '');
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
}
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
var rawHeaders = tableLines[0].split('|').map(function (s) { return s.trim();}),
|
|
|
|
rawStyles = tableLines[1].split('|').map(function (s) { return s.trim();}),
|
|
|
|
rawCells = [],
|
|
|
|
headers = [],
|
|
|
|
styles = [],
|
|
|
|
cells = [];
|
2015-07-11 23:44:24 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
tableLines.shift();
|
|
|
|
tableLines.shift();
|
2015-07-12 01:21:43 +08:00
|
|
|
|
2016-01-25 13:24:54 +08:00
|
|
|
for (i = 0; i < tableLines.length; ++i) {
|
|
|
|
if (tableLines[i].trim() === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rawCells.push(
|
|
|
|
tableLines[i]
|
|
|
|
.split('|')
|
|
|
|
.map(function (s) {
|
|
|
|
return s.trim();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rawHeaders.length < rawStyles.length) {
|
|
|
|
return rawTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < rawStyles.length; ++i) {
|
|
|
|
styles.push(parseStyles(rawStyles[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < rawHeaders.length; ++i) {
|
|
|
|
if (showdown.helper.isUndefined(styles[i])) {
|
|
|
|
styles[i] = '';
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
headers.push(parseHeaders(rawHeaders[i], styles[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < rawCells.length; ++i) {
|
|
|
|
var row = [];
|
|
|
|
for (var ii = 0; ii < headers.length; ++ii) {
|
|
|
|
if (showdown.helper.isUndefined(rawCells[i][ii])) {
|
|
|
|
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
row.push(parseCells(rawCells[i][ii], styles[ii]));
|
2015-07-11 23:44:24 +08:00
|
|
|
}
|
2016-01-25 13:24:54 +08:00
|
|
|
cells.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildTable(headers, cells);
|
|
|
|
});
|
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('tables.after', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
|
|
|
return text;
|
2015-07-11 23:44:24 +08:00
|
|
|
});
|