refactor table implementation.

This commit is contained in:
Christopher Jeffrey 2013-01-02 14:34:19 -06:00
parent 1f09da120f
commit a412613f06

View File

@ -63,8 +63,8 @@ block.normal = {
block.gfm = { block.gfm = {
fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/, fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
paragraph: /^/, paragraph: /^/,
table: /^ {0,3}[|](.+)\n {0,3}[|]( *[-:]+[-| :]*)\n((?: *[|].*\n)*)\n*/, table: /^ {0,3}\|(.+)\n {0,3}\|( *[-:]+[-| :]*)\n((?: *\|.*\n)*)\n*/,
nptable: /^ {0,3}(\S.*[|].*)\n {0,3}([-:]+ *[|][-| :]*)\n((?:.*[|].*\n)*)\n*/ nptable: /^ {0,3}(\S.*\|.*)\n {0,3}([-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*)\n*/
}; };
block.gfm.paragraph = replace(block.paragraph) block.gfm.paragraph = replace(block.paragraph)
@ -137,15 +137,18 @@ block.token = function(src, tokens, top) {
src = src.substring(cap[0].length); src = src.substring(cap[0].length);
tokens.push({ tokens.push({
type: 'table', type: 'table',
header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */), header: cap[1].replace(/(^ *| *\| *$)/g, '').split(/ *\| */),
align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){ align: cap[2].replace(/(^ *|\| *$)/g, '').split(/ *\| */).map(function(row) {
return row.match(/^ *-+: *$/) ? "right" return /^ *-+: *$/.test(row)
: row.match(/^ *:-+: *$/) ? "center" ? 'right'
: row.match(/^ *:-+ *$/) ? "left" : /^ *:-+: *$/.test(row)
? 'center'
: /^ *:-+ *$/.test(row)
? 'left'
: false; : false;
}), }),
cells: cap[3].replace(/( *[|] *)?\n$/, '').split("\n").map(function(row){ cells: cap[3].replace(/( *\| *)?\n$/, '').split('\n').map(function(row) {
return row.replace(/(^ *[|] *| *[|] *$)/g, '').split(/ *[|] */); return row.replace(/(^ *\| *| *\| *$)/g, '').split(/ *\| */);
}) })
}); });
continue; continue;
@ -156,15 +159,18 @@ block.token = function(src, tokens, top) {
src = src.substring(cap[0].length); src = src.substring(cap[0].length);
tokens.push({ tokens.push({
type: 'table', type: 'table',
header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */), header: cap[1].replace(/(^ *| *\| *$)/g, '').split(/ *\| */),
align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){ align: cap[2].replace(/(^ *|\| *$)/g, '').split(/ *\| */).map(function(row) {
return row.match(/^ *-+: *$/) ? "right" return /^ *-+: *$/.test(row)
: row.match(/^ *:-+: *$/) ? "center" ? 'right'
: row.match(/^ *:-+ *$/) ? "left" : /^ *:-+: *$/.test(row)
? 'center'
: /^ *:-+ *$/.test(row)
? 'left'
: false; : false;
}), }),
cells: cap[3].replace(/\n$/, '').split("\n").map(function(row){ cells: cap[3].replace(/\n$/, '').split('\n').map(function(row) {
return row.split(/ *[|] */); return row.split(/ *\| */);
}) })
}); });
continue; continue;
@ -605,13 +611,17 @@ function tok() {
} }
case 'table': { case 'table': {
var thead = '\t<thead>\n\t\t<tr>'; var thead = '\t<thead>\n\t\t<tr>';
token.header.forEach(function(heading, i){ token.header.forEach(function(heading, i) {
heading = inline.lexer(heading); heading = inline.lexer(heading);
var align = i < token.align.length ? token.align[i] : false;
switch(align){ var align = i < token.align.length
case "left": ? token.align[i]
case "right": : false;
case "center":
switch (align) {
case 'left':
case 'right':
case 'center':
thead += '<th align="' + align + '">' + heading + '</td>'; thead += '<th align="' + align + '">' + heading + '</td>';
break; break;
default: default:
@ -621,15 +631,19 @@ function tok() {
thead += '</tr>\n\t</thead>\n'; thead += '</tr>\n\t</thead>\n';
var tbody = '\t<tbody>\n'; var tbody = '\t<tbody>\n';
token.cells.forEach(function(row){ token.cells.forEach(function(row) {
tbody += '\t\t<tr>'; tbody += '\t\t<tr>';
row.forEach(function(cell, i){ row.forEach(function(cell, i) {
cell = inline.lexer(cell); cell = inline.lexer(cell);
var align = i < token.align.length ? token.align[i] : false;
switch(align){ var align = i < token.align.length
case "left": ? token.align[i]
case "right": : false;
case "center":
switch (align) {
case 'left':
case 'right':
case 'center':
tbody += '<td align="' + align + '">' + cell + '</td>'; tbody += '<td align="' + align + '">' + cell + '</td>';
break; break;
default: default: