table.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*global module:true*/
  2. /*
  3. * Basic table support with re-entrant parsing, where cell content
  4. * can also specify markdown.
  5. *
  6. * Tables
  7. * ======
  8. *
  9. * | Col 1 | Col 2 |
  10. * |======== |====================================================|
  11. * |**bold** | ![Valid XHTML] (http://w3.org/Icons/valid-xhtml10) |
  12. * | Plain | Value |
  13. *
  14. */
  15. (function(){
  16. var table = function(converter) {
  17. var tables = {}, style = 'text-align:left;', filter;
  18. tables.th = function(header){
  19. if (header.trim() === "") { return "";}
  20. var id = header.trim().replace(/ /g, '_').toLowerCase();
  21. return '<th id="' + id + '" style="'+style+'">' + header + '</th>';
  22. };
  23. tables.td = function(cell) {
  24. return '<td style="'+style+'">' + converter.makeHtml(cell) + '</td>';
  25. };
  26. tables.ths = function(){
  27. var out = "", i = 0, hs = [].slice.apply(arguments);
  28. for (i;i<hs.length;i+=1) {
  29. out += tables.th(hs[i]) + '\n';
  30. }
  31. return out;
  32. };
  33. tables.tds = function(){
  34. var out = "", i = 0, ds = [].slice.apply(arguments);
  35. for (i;i<ds.length;i+=1) {
  36. out += tables.td(ds[i]) + '\n';
  37. }
  38. return out;
  39. };
  40. tables.thead = function() {
  41. var out, i = 0, hs = [].slice.apply(arguments);
  42. out = "<thead>\n";
  43. out += "<tr>\n";
  44. out += tables.ths.apply(this, hs);
  45. out += "</tr>\n";
  46. out += "</thead>\n";
  47. return out;
  48. };
  49. tables.tr = function() {
  50. var out, i = 0, cs = [].slice.apply(arguments);
  51. out = "<tr>\n";
  52. out += tables.tds.apply(this, cs);
  53. out += "</tr>\n";
  54. return out;
  55. };
  56. filter = function(text) {
  57. var i=0, lines = text.split('\n'), line, hs, rows, out = [];
  58. for (i; i<lines.length;i+=1) {
  59. line = lines[i];
  60. // looks like a table heading
  61. if (line.trim().match(/^[|]{1}.*[|]{1}$/)) {
  62. line = line.trim();
  63. var tbl = [];
  64. tbl.push('<table>');
  65. hs = line.substring(1, line.length -1).split('|');
  66. tbl.push(tables.thead.apply(this, hs));
  67. line = lines[++i];
  68. if (!line.trim().match(/^[|]{1}[-=| ]+[|]{1}$/)) {
  69. // not a table rolling back
  70. line = lines[--i];
  71. }
  72. else {
  73. line = lines[++i];
  74. tbl.push('<tbody>');
  75. while (line.trim().match(/^[|]{1}.*[|]{1}$/)) {
  76. line = line.trim();
  77. tbl.push(tables.tr.apply(this, line.substring(1, line.length -1).split('|')));
  78. line = lines[++i];
  79. }
  80. tbl.push('</tbody>');
  81. tbl.push('</table>');
  82. // we are done with this table and we move along
  83. out.push(tbl.join('\n'));
  84. continue;
  85. }
  86. }
  87. out.push(line);
  88. }
  89. return out.join('\n');
  90. };
  91. return [
  92. {
  93. type: 'lang',
  94. filter: filter
  95. }
  96. ];
  97. };
  98. // Client-side export
  99. if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.table = table; }
  100. // Server-side export
  101. if (typeof module !== 'undefined') {
  102. module.exports = table;
  103. }
  104. }());