diff --git a/lib/kramed.js b/lib/kramed.js index 0c9ad61..48b9252 100644 --- a/lib/kramed.js +++ b/lib/kramed.js @@ -99,8 +99,8 @@ block.gfm.paragraph = replace(block.paragraph) */ block.tables = merge({}, block.gfm, { - nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/, - table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/ + nptable: /^(?: *\'\'\'\n(.*(?:\n.+)*)\n\'\'\'\n)? *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/, + table: /^(?: *\'\'\'\n(.*(?:\n.+)*)\n\'\'\'\n)? *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/ }); /** @@ -245,9 +245,10 @@ Lexer.prototype.token = function(src, top, bq) { item = { type: 'table', - header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */), - align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */), - cells: cap[3].replace(/\n$/, '').split('\n') + caption: cap[1], + header: cap[2].replace(/^ *| *\| *$/g, '').split(/ *\| */), + align: cap[3].replace(/^ *|\| *$/g, '').split(/ *\| */), + cells: cap[4].replace(/\n$/, '').split('\n') }; for (i = 0; i < item.align.length; i++) { @@ -416,9 +417,10 @@ Lexer.prototype.token = function(src, top, bq) { item = { type: 'table', - header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */), - align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */), - cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n').slice(0), + caption: cap[1], + header: cap[2].replace(/^ *| *\| *$/g, '').split(/ *\| */), + align: cap[3].replace(/^ *|\| *$/g, '').split(/ *\| */), + cells: cap[4].replace(/(?: *\| *)?\n$/, '').split('\n').slice(0), }; for (i = 0; i < item.align.length; i++) { @@ -895,8 +897,9 @@ Renderer.prototype.paragraph = function(text) { return '

' + text + '

\n'; }; -Renderer.prototype.table = function(header, body) { +Renderer.prototype.table = function(caption, header, body) { return '\n' + + caption + '\n' + header + '\n' @@ -1092,12 +1095,23 @@ Parser.prototype.tok = function() { case 'table': { var header = '' , body = '' + , caption , i , row , cell , flags , j; + // caption + if (typeof this.token.caption === 'undefined') { + caption = ''; + } + else { + caption = '\n'; + } + // header cell = ''; for (i = 0; i < this.token.header.length; i++) { @@ -1122,7 +1136,7 @@ Parser.prototype.tok = function() { body += this.renderer.tablerow(cell); } - return this.renderer.table(header, body); + return this.renderer.table(caption, header, body); } case 'blockquote_start': { var body = ''; diff --git a/test/tests/gfm_tables.html b/test/tests/gfm_tables.html index 70bec82..505574a 100644 --- a/test/tests/gfm_tables.html +++ b/test/tests/gfm_tables.html @@ -35,3 +35,13 @@
' + + this.inline.output( this.token.caption ) + + '
Cell 5Cell 6Cell 7Cell 8
+ + + + + + + + + +
Captioned table Multiple lines
Header 1Header 2
Cell 1Cell 2
Cell 3Cell 4
diff --git a/test/tests/gfm_tables.text b/test/tests/gfm_tables.text index 5fd6321..36f3075 100644 --- a/test/tests/gfm_tables.text +++ b/test/tests/gfm_tables.text @@ -19,3 +19,12 @@ Header 1|Header 2|Header 3|Header 4 :-------|:------:|-------:|-------- Cell 1 |Cell 2 |Cell 3 |Cell 4 *Cell 5*|Cell 6 |Cell 7 |Cell 8 + +''' +Captioned table +Multiple *lines* +''' +Header 1|Header 2 +------- |-------- +Cell 1 | Cell 2 +Cell 3 | Cell 4