tables.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. showdown.subParser('tables', function (text, options, globals) {
  2. 'use strict';
  3. if (!options.tables) {
  4. return text;
  5. }
  6. var tableRgx = /^ {0,3}\|?.+\|.+\n[ \t]{0,3}\|?[ \t]*:?[ \t]*(?:-|=){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:-|=){2,}[\s\S]+?(?:\n\n|¨0)/gm;
  7. function parseStyles (sLine) {
  8. if (/^:[ \t]*--*$/.test(sLine)) {
  9. return ' style="text-align:left;"';
  10. } else if (/^--*[ \t]*:[ \t]*$/.test(sLine)) {
  11. return ' style="text-align:right;"';
  12. } else if (/^:[ \t]*--*[ \t]*:$/.test(sLine)) {
  13. return ' style="text-align:center;"';
  14. } else {
  15. return '';
  16. }
  17. }
  18. function parseHeaders (header, style) {
  19. var id = '';
  20. header = header.trim();
  21. if (options.tableHeaderId) {
  22. id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"';
  23. }
  24. header = showdown.subParser('spanGamut')(header, options, globals);
  25. return '<th' + id + style + '>' + header + '</th>\n';
  26. }
  27. function parseCells (cell, style) {
  28. var subText = showdown.subParser('spanGamut')(cell, options, globals);
  29. return '<td' + style + '>' + subText + '</td>\n';
  30. }
  31. function buildTable (headers, cells) {
  32. var tb = '<table>\n<thead>\n<tr>\n',
  33. tblLgn = headers.length;
  34. for (var i = 0; i < tblLgn; ++i) {
  35. tb += headers[i];
  36. }
  37. tb += '</tr>\n</thead>\n<tbody>\n';
  38. for (i = 0; i < cells.length; ++i) {
  39. tb += '<tr>\n';
  40. for (var ii = 0; ii < tblLgn; ++ii) {
  41. tb += cells[i][ii];
  42. }
  43. tb += '</tr>\n';
  44. }
  45. tb += '</tbody>\n</table>\n';
  46. return tb;
  47. }
  48. text = globals.converter._dispatch('tables.before', text, options, globals);
  49. text = text.replace(tableRgx, function (rawTable) {
  50. var i, tableLines = rawTable.split('\n');
  51. // strip wrong first and last column if wrapped tables are used
  52. for (i = 0; i < tableLines.length; ++i) {
  53. if (/^ {0,3}\|/.test(tableLines[i])) {
  54. tableLines[i] = tableLines[i].replace(/^ {0,3}\|/, '');
  55. }
  56. if (/\|[ \t]*$/.test(tableLines[i])) {
  57. tableLines[i] = tableLines[i].replace(/\|[ \t]*$/, '');
  58. }
  59. }
  60. var rawHeaders = tableLines[0].split('|').map(function (s) { return s.trim();}),
  61. rawStyles = tableLines[1].split('|').map(function (s) { return s.trim();}),
  62. rawCells = [],
  63. headers = [],
  64. styles = [],
  65. cells = [];
  66. tableLines.shift();
  67. tableLines.shift();
  68. for (i = 0; i < tableLines.length; ++i) {
  69. if (tableLines[i].trim() === '') {
  70. continue;
  71. }
  72. rawCells.push(
  73. tableLines[i]
  74. .split('|')
  75. .map(function (s) {
  76. return s.trim();
  77. })
  78. );
  79. }
  80. if (rawHeaders.length < rawStyles.length) {
  81. return rawTable;
  82. }
  83. for (i = 0; i < rawStyles.length; ++i) {
  84. styles.push(parseStyles(rawStyles[i]));
  85. }
  86. for (i = 0; i < rawHeaders.length; ++i) {
  87. if (showdown.helper.isUndefined(styles[i])) {
  88. styles[i] = '';
  89. }
  90. headers.push(parseHeaders(rawHeaders[i], styles[i]));
  91. }
  92. for (i = 0; i < rawCells.length; ++i) {
  93. var row = [];
  94. for (var ii = 0; ii < headers.length; ++ii) {
  95. if (showdown.helper.isUndefined(rawCells[i][ii])) {
  96. }
  97. row.push(parseCells(rawCells[i][ii], styles[ii]));
  98. }
  99. cells.push(row);
  100. }
  101. return buildTable(headers, cells);
  102. });
  103. text = globals.converter._dispatch('tables.after', text, options, globals);
  104. return text;
  105. });