showdown.subParser('tables', function (text, options, globals) { 'use strict'; if (!options.tables) { return text; } var tableRgx = /^[ \t]{0,3}\|?.+\|.+\n[ \t]{0,3}\|?[ \t]*:?[ \t]*(?:-|=){3,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:-|=){3,}[^]+?(?:\n\n|~0)/gm; function parseStyles(sLine) { if (/^:[ \t]*---*$/.test(sLine)) { return ' style="text-align:left;"'; } else if (/^---*[ \t]*:[ \t]*$/.test(sLine)) { return ' style="text-align:right;"'; } else if (/^:[ \t]*---*[ \t]*:$/.test(sLine)) { return ' style="text-align:center;"'; } else { return ''; } } function parseHeaders(header, style) { var id = ''; header = header.trim(); if (options.tableHeaderId) { id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"'; } header = showdown.subParser('spanGamut')(header, options, globals); return '' + header + '\n'; } function parseCells(cell, style) { var subText = showdown.subParser('spanGamut')(cell, options, globals); return '' + subText + '\n'; } function buildTable(headers, cells) { var tb = '\n\n\n', tblLgn = headers.length; for (var i = 0; i < tblLgn; ++i) { tb += headers[i]; } tb += '\n\n\n'; for (i = 0; i < cells.length; ++i) { tb += '\n'; for (var ii = 0; ii < tblLgn; ++ii) { tb += cells[i][ii]; } tb += '\n'; } tb += '\n
\n'; return tb; } text = globals.converter._dispatch('tables.before', text, options, globals); text = text.replace(tableRgx, function (rawTable) { var i, tableLines = rawTable.split('\n'); // strip wrong first and last column if wrapped tables are used for (i = 0; i < tableLines.length; ++i) { if (/^[ \t]{0,3}\|/.test(tableLines[i])) { tableLines[i] = tableLines[i].replace(/^[ \t]{0,3}\|/, ''); } if (/\|[ \t]*$/.test(tableLines[i])) { tableLines[i] = tableLines[i].replace(/\|[ \t]*$/, ''); } } 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 = []; tableLines.shift(); tableLines.shift(); 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] = ''; } 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])) { } row.push(parseCells(rawCells[i][ii], styles[ii])); } cells.push(row); } return buildTable(headers, cells); }); text = globals.converter._dispatch('tables.after', text, options, globals); return text; });