From 5aed8cffb4d404631c09b4b9faef89fceed304ee Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 13:57:21 -0500 Subject: [PATCH 1/9] Add table caption support to kramed.js. In books, tables are generally accompanied by captions and may have anchors embedded in the caption. This adds existing table syntax to allow for multiline caption to be embedded in tables. --- lib/kramed.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/kramed.js b/lib/kramed.js index 96b06e8..5e53d7b 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,11 @@ Renderer.prototype.paragraph = function(text) { return '

' + text + '

\n'; }; -Renderer.prototype.table = function(header, body) { +Renderer.prototype.table = function(caption, header, body) { return '\n' + + '\n' + '\n' + header + '\n' @@ -1092,12 +1097,16 @@ Parser.prototype.tok = function() { case 'table': { var header = '' , body = '' + , caption , i , row , cell , flags , j; + // caption + caption = this.token.caption + // header cell = ''; for (i = 0; i < this.token.header.length; i++) { @@ -1122,7 +1131,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 = ''; From a9c2009c67a70d47afe44706da48b795a9651129 Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 14:10:48 -0500 Subject: [PATCH 2/9] Update gfm_tables.html for captioned table syntax --- test/tests/gfm_tables.html | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 @@
\n' + + caption + + '
Cell 5Cell 6Cell 7Cell 8
+ + + + + + + + + +
Captioned table Multiple lines
Header 1Header 2
Cell 1Cell 2
Cell 3Cell 4
From 06ca1fbe3599a7873a1e98c1441ae4cb76275779 Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 14:11:52 -0500 Subject: [PATCH 3/9] Update gfm_tables.text for captioned table syntax --- test/tests/gfm_tables.text | 9 +++++++++ 1 file changed, 9 insertions(+) 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 From 7a24f6dcbe79fcf65271cdd2dd32c9e5945f45db Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 14:23:46 -0500 Subject: [PATCH 4/9] Fix syntax error in kramed.js --- lib/kramed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kramed.js b/lib/kramed.js index 5e53d7b..8ef09e6 100644 --- a/lib/kramed.js +++ b/lib/kramed.js @@ -417,7 +417,7 @@ Lexer.prototype.token = function(src, top, bq) { item = { type: 'table', - caption: cap[1] + caption: cap[1], header: cap[2].replace(/^ *| *\| *$/g, '').split(/ *\| */), align: cap[3].replace(/^ *|\| *$/g, '').split(/ *\| */), cells: cap[4].replace(/(?: *\| *)?\n$/, '').split('\n').slice(0), From a73d0d50866d225c5f7beca4e3df1450623efa62 Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 14:46:32 -0500 Subject: [PATCH 5/9] Fix bad regex for table caption syntax --- lib/kramed.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kramed.js b/lib/kramed.js index 8ef09e6..9df68ab 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: /^(?: *\'\'\'\n((?:.+\n?)+)\n\'\'\'\n)? *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/, - table: /^(?: *\'\'\'\n((?:.+\n?)+)\n\'\'\'\n)? *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/ + nptable: /^(?: *\'\'\'\n(.*(?:\n.+)*)\n\'\'\'\n)? *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/, + table: /^(?: *\'\'\'\n(.*(?:\n.+)*)\n\'\'\'\n)? *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/ }); /** From 961ec4a2bec27ebdbe484ebcf03ba5a727886b5b Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 14:51:24 -0500 Subject: [PATCH 6/9] Skip rendering a caption tag if no caption supplied. --- lib/kramed.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/kramed.js b/lib/kramed.js index 9df68ab..efdf7bf 100644 --- a/lib/kramed.js +++ b/lib/kramed.js @@ -899,9 +899,7 @@ Renderer.prototype.paragraph = function(text) { Renderer.prototype.table = function(caption, header, body) { return '\n' - + '\n' + + if(caption) { '\n' } else { '' } + '\n' + header + '\n' From 27db06d749df9368340a344d12ea8daeba94f7d0 Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 14:59:32 -0500 Subject: [PATCH 7/9] Fix syntax in table caption patch --- lib/kramed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kramed.js b/lib/kramed.js index efdf7bf..2bc1b6a 100644 --- a/lib/kramed.js +++ b/lib/kramed.js @@ -899,7 +899,7 @@ Renderer.prototype.paragraph = function(text) { Renderer.prototype.table = function(caption, header, body) { return '
\n' - + caption - + '\n' + caption + '
\n' - + if(caption) { '\n' } else { '' } + + ( if(caption) { '\n' } else { '' } ) + '\n' + header + '\n' From 941683fc860cb0999e39052717b2af3a8519978f Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 15:07:41 -0500 Subject: [PATCH 8/9] Fix syntax for table caption render again --- lib/kramed.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/kramed.js b/lib/kramed.js index 2bc1b6a..5b71c5a 100644 --- a/lib/kramed.js +++ b/lib/kramed.js @@ -898,8 +898,14 @@ Renderer.prototype.paragraph = function(text) { }; Renderer.prototype.table = function(caption, header, body) { + if (typeof caption === 'undefined') { + caption = '' + } + else { + caption = '\n' + } return '
\n' + caption + '\n' + caption + '\n' + caption + '\n
\n' - + ( if(caption) { '\n' } else { '' } ) + + caption + '\n' + header + '\n' From 806c920d88fc49237f422b1f9ae15625ae126886 Mon Sep 17 00:00:00 2001 From: Ari Epstein Date: Thu, 5 Feb 2015 15:26:16 -0500 Subject: [PATCH 9/9] Apply inline rendering to caption contents --- lib/kramed.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/kramed.js b/lib/kramed.js index 5b71c5a..0654725 100644 --- a/lib/kramed.js +++ b/lib/kramed.js @@ -898,12 +898,6 @@ Renderer.prototype.paragraph = function(text) { }; Renderer.prototype.table = function(caption, header, body) { - if (typeof caption === 'undefined') { - caption = '' - } - else { - caption = '\n' - } return '
\n' + caption + '\n' + caption + '\n
\n' + caption + '\n' @@ -1109,8 +1103,15 @@ Parser.prototype.tok = function() { , j; // caption - caption = this.token.caption - + if (typeof this.token.caption === 'undefined') { + caption = ''; + } + else { + caption = '\n'; + } + // header cell = ''; for (i = 0; i < this.token.header.length; i++) {
' + + this.inline.output( this.token.caption ) + + '