Git based wiki inspired by Gollum
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.2KB

  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'), tbl = [], 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. tbl.push('<table>');
  64. hs = line.substring(1, line.length -1).split('|');
  65. tbl.push(tables.thead.apply(this, hs));
  66. line = lines[++i];
  67. if (!line.trim().match(/^[|]{1}[-=| ]+[|]{1}$/)) {
  68. // not a table rolling back
  69. line = lines[--i];
  70. }
  71. else {
  72. line = lines[++i];
  73. tbl.push('<tbody>');
  74. while (line.trim().match(/^[|]{1}.*[|]{1}$/)) {
  75. line = line.trim();
  76. tbl.push(tables.tr.apply(this, line.substring(1, line.length -1).split('|')));
  77. line = lines[++i];
  78. }
  79. tbl.push('</tbody>');
  80. tbl.push('</table>');
  81. // we are done with this table and we move along
  82. out.push(tbl.join('\n'));
  83. continue;
  84. }
  85. }
  86. out.push(line);
  87. }
  88. return out.join('\n');
  89. };
  90. return [
  91. {
  92. type: 'lang',
  93. filter: filter
  94. }
  95. ];
  96. };
  97. // Client-side export
  98. if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.table = table; }
  99. // Server-side export
  100. if (typeof module !== 'undefined') {
  101. module.exports = table;
  102. }
  103. }());