From 8e080eac0ac0b95d1ad011b89827ce8394ca6192 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Sun, 29 Dec 2013 23:51:22 +0100 Subject: [PATCH 1/8] fixed issue #314 --- core/modules/parsers/wikiparser/rules/table.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index 16f75736c..57e1f9cad 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -51,6 +51,16 @@ var processRow = function(prevColumns) { colSpanCount++; // Move to just before the `|` terminating the cell this.parser.pos = cellRegExp.lastIndex - 1; + } else if(cellMatch[1] === "<" && prevCell) { + try { + colSpanCount = 1+prevCell.attributes.colspan.value; + } catch (e) { + colSpanCount = 2; + } + $tw.utils.addAttributeToParseTreeNode(prevCell,"colspan",colSpanCount); + colSpanCount = 1; + // Move to just before the `|` terminating the cell + this.parser.pos = cellRegExp.lastIndex - 1; } else if(cellMatch[2]) { // End of row if(prevCell && colSpanCount > 1) { From 3cc813813336520157643273e63837ea3883a8cc Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Sun, 29 Dec 2013 23:57:42 +0100 Subject: [PATCH 2/8] fixed issue #314 - fix for > at end of line --- core/modules/parsers/wikiparser/rules/table.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index 57e1f9cad..9a894d81e 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -64,6 +64,11 @@ var processRow = function(prevColumns) { } else if(cellMatch[2]) { // End of row if(prevCell && colSpanCount > 1) { + try { + colSpanCount+= prevCell.attributes.colspan.value; + } catch (e) { + colSpanCount-= 1; + } $tw.utils.addAttributeToParseTreeNode(prevCell,"colspan",colSpanCount); } this.parser.pos = cellRegExp.lastIndex - 1; From 14868d82283fae13058b94441cd4f79135924b29 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Mon, 30 Dec 2013 00:28:43 +0100 Subject: [PATCH 3/8] also fixed issue #315 --- core/modules/parsers/wikiparser/rules/table.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index 9a894d81e..973cea457 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -104,7 +104,7 @@ var processRow = function(prevColumns) { // Parse the cell cell.children = this.parser.parseInlineRun(cellTermRegExp,{eatTerminator: true}); // Set the alignment for the cell - if(cellMatch[1].substr(cellMatch[1].length-1,1) === " ") { // spaceRight + if(this.parser.source.substr(this.parser.pos-2,1) === " ") { // spaceRight $tw.utils.addAttributeToParseTreeNode(cell,"align",spaceLeft ? "center" : "left"); } else if(spaceLeft) { $tw.utils.addAttributeToParseTreeNode(cell,"align","right"); From eb7b82696b04971f17f30a0f781d7c53f875b91e Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Mon, 30 Dec 2013 09:51:54 +0100 Subject: [PATCH 4/8] fixed documentation for issue #314 --- editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid index 5d2f641b6..ed4ea7692 100644 --- a/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid +++ b/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid @@ -38,19 +38,21 @@ The example renders as: ! Cell Merging -To merge a table cell with the one above, use the special cell text `~`. To merge a cell with the one to its left use the text `>`. For example: +To merge a table cell with the one above, use the special cell text `~`. To merge a cell with the one to its left use the text `<`. To merge one to its right use `>`. For example: ``` |Cell1 |Cell2 |Cell3 |Cell4 | -|Cell5 |Cell6 |Cell7 |>| +|Cell5 |Cell6 |Cell7 |<| |Cell5 |~|Cell7 |Cell8 | +|>|Cell9 |Cell10 |Cell11 | ``` Renders as: |Cell1 |Cell2 |Cell3 |Cell4 | -|Cell5 |Cell6 |Cell7 |>| +|Cell5 |Cell6 |Cell7 |<| |Cell5 |~|Cell7 |Cell8 | +|>|Cell9 |Cell10 |Cell11 | ! Table Classes, Captions, Headers and Footers From ce8cc7607fee3d6161c10044d83d8cdaa87eabba Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Mon, 30 Dec 2013 13:44:32 +0100 Subject: [PATCH 5/8] =?UTF-8?q?changed=20to=20conform=20to=20Jeremy's=20st?= =?UTF-8?q?andards=20-=20sorry=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/modules/parsers/wikiparser/rules/table.js | 6 +----- core/modules/utils/parsetree.js | 7 +++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index 973cea457..be37f4091 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -52,11 +52,7 @@ var processRow = function(prevColumns) { // Move to just before the `|` terminating the cell this.parser.pos = cellRegExp.lastIndex - 1; } else if(cellMatch[1] === "<" && prevCell) { - try { - colSpanCount = 1+prevCell.attributes.colspan.value; - } catch (e) { - colSpanCount = 2; - } + colSpanCount = 1+$tw.utils.getAttributeValueFromParseTreeNode(prevCell, "colspan", 1); $tw.utils.addAttributeToParseTreeNode(prevCell,"colspan",colSpanCount); colSpanCount = 1; // Move to just before the `|` terminating the cell diff --git a/core/modules/utils/parsetree.js b/core/modules/utils/parsetree.js index 6e8ad27c6..d21659c22 100644 --- a/core/modules/utils/parsetree.js +++ b/core/modules/utils/parsetree.js @@ -19,6 +19,13 @@ exports.addAttributeToParseTreeNode = function(node,name,value) { } }; +exports.getAttributeValueFromParseTreeNode = function(node,name,defaultValue) { + if(node.type === "element" && node.attributes && node.attributes[name] && node.attributes[name].value != undefined) { + return node.attributes[name].value; + } + return defaultValue; +}; + exports.addClassToParseTreeNode = function(node,classString) { var classes = []; if(node.type === "element") { From ec14a0a16d310e1a3b50a206d51594c1efbb9611 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Tue, 31 Dec 2013 11:05:08 +0100 Subject: [PATCH 6/8] vertical alignment of cells - version 2 --- .../modules/parsers/wikiparser/rules/table.js | 16 +++++++++++- .../tiddlers/wikitext/Tables in WikiText.tid | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index be37f4091..5feb470cf 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -38,7 +38,8 @@ var processRow = function(prevColumns) { if(last) { last.rowSpanCount++; $tw.utils.addAttributeToParseTreeNode(last.element,"rowspan",last.rowSpanCount); - $tw.utils.addAttributeToParseTreeNode(last.element,"valign","center"); + var vAlign = $tw.utils.getAttributeValueFromParseTreeNode(last.element,"valign","center"); + $tw.utils.addAttributeToParseTreeNode(last.element,"valign",vAlign); if(colSpanCount > 1) { $tw.utils.addAttributeToParseTreeNode(last.element,"colspan",colSpanCount); colSpanCount = 1; @@ -75,6 +76,16 @@ var processRow = function(prevColumns) { // Look for a space at the start of the cell var spaceLeft = false, chr = this.parser.source.substr(this.parser.pos,1); + var vAlign; + if (chr === "^") { + vAlign = "top"; + } else if(chr === ",") { + vAlign = "bottom"; + } + if(vAlign) { + this.parser.pos++; + chr = this.parser.source.substr(this.parser.pos,1); + } while(chr === " ") { spaceLeft = true; this.parser.pos++; @@ -100,6 +111,9 @@ var processRow = function(prevColumns) { // Parse the cell cell.children = this.parser.parseInlineRun(cellTermRegExp,{eatTerminator: true}); // Set the alignment for the cell + if(vAlign) { + $tw.utils.addAttributeToParseTreeNode(cell,"valign",vAlign); + } if(this.parser.source.substr(this.parser.pos-2,1) === " ") { // spaceRight $tw.utils.addAttributeToParseTreeNode(cell,"align",spaceLeft ? "center" : "left"); } else if(spaceLeft) { diff --git a/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid index ed4ea7692..4be222b11 100644 --- a/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid +++ b/editions/tw5.com/tiddlers/wikitext/Tables in WikiText.tid @@ -36,6 +36,32 @@ The example renders as: | Centred content | |+++ a very wide column so we can see the alignment +++| +! Cell vertical Alignment + +Vertical alignment of cells is done by inserting either a `^` for top alignment or a `,` for bottom alignment as the first character of a cell. The normal horizontal alignment is still possible. + +A shortned example: + +``` +|^top left |^ top center |^ top right| +|middle left | middle center | middle right| +|,bottom left |, bottom center |, bottom right| +``` + +The example renders as: + +| :: | ::::::::::::::::::::::::::: | ::::::::::::::::::::::::::: | ::::::::::::::::::::::::::: | :: | +| ::
:: |^top left |^ top center |^ top right| ::
:: | +| ::
:: |middle left | middle center | middle right| ::
:: | +| ::
:: |,bottom left |, bottom center |, bottom right| ::
:: | +| :: | ::::::::::::::::::::::::::: | ::::::::::::::::::::::::::: | ::::::::::::::::::::::::::: | :: | + +Should you ever want to have a `^`or a `,` as the first character of a left aligned cell, you need to use html-escaping. + +| `^` | &#94; | +| `,` | &#44; | + + ! Cell Merging To merge a table cell with the one above, use the special cell text `~`. To merge a cell with the one to its left use the text `<`. To merge one to its right use `>`. For example: From 917865c3933e37329ec4d7120c2eddae8602c0b1 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Tue, 31 Dec 2013 11:09:17 +0100 Subject: [PATCH 7/8] fixed an initialization error --- core/modules/parsers/wikiparser/rules/table.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index 5feb470cf..75f36cf67 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -76,7 +76,7 @@ var processRow = function(prevColumns) { // Look for a space at the start of the cell var spaceLeft = false, chr = this.parser.source.substr(this.parser.pos,1); - var vAlign; + var vAlign = null; if (chr === "^") { vAlign = "top"; } else if(chr === ",") { From 9fee9b10431fbf2c3c725ec8efd3f7c6bee3d55c Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Tue, 7 Jan 2014 21:12:59 +0100 Subject: [PATCH 8/8] Fix for Paul's concerns --- .../modules/parsers/wikiparser/rules/table.js | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/table.js b/core/modules/parsers/wikiparser/rules/table.js index 75f36cf67..a4d89a18d 100644 --- a/core/modules/parsers/wikiparser/rules/table.js +++ b/core/modules/parsers/wikiparser/rules/table.js @@ -18,11 +18,11 @@ exports.types = {block: true}; exports.init = function(parser) { this.parser = parser; // Regexp to match - this.matchRegExp = /^\|(?:[^\n]*)\|(?:[fhck]?)\r?\n/mg; + this.matchRegExp = /^\|(?:[^\n]*)\|(?:[fhck]?)\r?(?:\n|$)/mg; }; var processRow = function(prevColumns) { - var cellRegExp = /(?:\|([^\n\|]*)\|)|(\|[fhck]?\r?\n)/mg, + var cellRegExp = /(?:\|([^\n\|]*)\|)|(\|[fhck]?\r?(?:\n|$))/mg, cellTermRegExp = /((?:\x20*)\|)/mg, tree = [], col = 0, @@ -53,7 +53,7 @@ var processRow = function(prevColumns) { // Move to just before the `|` terminating the cell this.parser.pos = cellRegExp.lastIndex - 1; } else if(cellMatch[1] === "<" && prevCell) { - colSpanCount = 1+$tw.utils.getAttributeValueFromParseTreeNode(prevCell, "colspan", 1); + colSpanCount = 1 + $tw.utils.getAttributeValueFromParseTreeNode(prevCell,"colspan",1); $tw.utils.addAttributeToParseTreeNode(prevCell,"colspan",colSpanCount); colSpanCount = 1; // Move to just before the `|` terminating the cell @@ -61,10 +61,10 @@ var processRow = function(prevColumns) { } else if(cellMatch[2]) { // End of row if(prevCell && colSpanCount > 1) { - try { - colSpanCount+= prevCell.attributes.colspan.value; - } catch (e) { - colSpanCount-= 1; + if(prevCell.attributes && prevCell.attributes && prevCell.attributes.colspan) { + colSpanCount += prevCell.attributes.colspan.value; + } else { + colSpanCount -= 1; } $tw.utils.addAttributeToParseTreeNode(prevCell,"colspan",colSpanCount); } @@ -74,18 +74,17 @@ var processRow = function(prevColumns) { // For ordinary cells, step beyond the opening `|` this.parser.pos++; // Look for a space at the start of the cell - var spaceLeft = false, - chr = this.parser.source.substr(this.parser.pos,1); + var spaceLeft = false; var vAlign = null; - if (chr === "^") { + if(this.parser.source.substr(this.parser.pos).search(/^\^([^\^]|\^\^)/) === 0) { vAlign = "top"; - } else if(chr === ",") { + } else if(this.parser.source.substr(this.parser.pos).search(/^,([^,]|,,)/) === 0) { vAlign = "bottom"; } if(vAlign) { this.parser.pos++; - chr = this.parser.source.substr(this.parser.pos,1); } + var chr = this.parser.source.substr(this.parser.pos,1); while(chr === " ") { spaceLeft = true; this.parser.pos++; @@ -114,7 +113,7 @@ var processRow = function(prevColumns) { if(vAlign) { $tw.utils.addAttributeToParseTreeNode(cell,"valign",vAlign); } - if(this.parser.source.substr(this.parser.pos-2,1) === " ") { // spaceRight + if(this.parser.source.substr(this.parser.pos - 2,1) === " ") { // spaceRight $tw.utils.addAttributeToParseTreeNode(cell,"align",spaceLeft ? "center" : "left"); } else if(spaceLeft) { $tw.utils.addAttributeToParseTreeNode(cell,"align","right"); @@ -132,8 +131,8 @@ var processRow = function(prevColumns) { exports.parse = function() { var rowContainerTypes = {"c":"caption", "h":"thead", "":"tbody", "f":"tfoot"}, table = {type: "element", tag: "table", children: []}, - rowRegExp = /^\|([^\n]*)\|([fhck]?)\r?\n/mg, - rowTermRegExp = /(\|(?:[fhck]?)\r?\n)/mg, + rowRegExp = /^\|([^\n]*)\|([fhck]?)\r?(?:\n|$)/mg, + rowTermRegExp = /(\|(?:[fhck]?)\r?(?:\n|$))/mg, prevColumns = [], currRowType, rowContainer, @@ -149,7 +148,7 @@ exports.parse = function() { this.parser.pos = rowMatch.index + rowMatch[0].length; } else { // Otherwise, create a new row if this one is of a different type - if(rowType != currRowType) { + if(rowType !== currRowType) { rowContainer = {type: "element", tag: rowContainerTypes[rowType], children: []}; table.children.push(rowContainer); currRowType = rowType;