Merge remote-tracking branch 'kitsonk/gfm_tables'

Conflicts:
	lib/marked.js
This commit is contained in:
Christopher Jeffrey 2013-01-02 14:20:57 -06:00
commit 1f09da120f
5 changed files with 211 additions and 3 deletions

View File

@ -13,6 +13,8 @@ var block = {
newline: /^\n+/,
code: /^( {4}[^\n]+\n*)+/,
fences: noop,
table: noop,
nptable: noop,
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
@ -53,12 +55,16 @@ block.paragraph = replace(block.paragraph)
block.normal = {
fences: block.fences,
paragraph: block.paragraph
paragraph: block.paragraph,
table: block.table,
nptable: block.nptable
};
block.gfm = {
fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
paragraph: /^/
paragraph: /^/,
table: /^ {0,3}[|](.+)\n {0,3}[|]( *[-:]+[-| :]*)\n((?: *[|].*\n)*)\n*/,
nptable: /^ {0,3}(\S.*[|].*)\n {0,3}([-:]+ *[|][-| :]*)\n((?:.*[|].*\n)*)\n*/
};
block.gfm.paragraph = replace(block.paragraph)
@ -126,6 +132,44 @@ block.token = function(src, tokens, top) {
continue;
}
// table (gfm)
if (cap = block.table.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'table',
header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */),
align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){
return row.match(/^ *-+: *$/) ? "right"
: row.match(/^ *:-+: *$/) ? "center"
: row.match(/^ *:-+ *$/) ? "left"
: false;
}),
cells: cap[3].replace(/( *[|] *)?\n$/, '').split("\n").map(function(row){
return row.replace(/(^ *[|] *| *[|] *$)/g, '').split(/ *[|] */);
})
});
continue;
}
// table no leading pipe (gfm)
if (cap = block.nptable.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'table',
header: cap[1].replace(/(^ *| *[|] *$)/g, '').split(/ *[|] */),
align: cap[2].replace(/(^ *|[|] *$)/g, '').split(/ *[|] */).map(function(row){
return row.match(/^ *-+: *$/) ? "right"
: row.match(/^ *:-+: *$/) ? "center"
: row.match(/^ *:-+ *$/) ? "left"
: false;
}),
cells: cap[3].replace(/\n$/, '').split("\n").map(function(row){
return row.split(/ *[|] */);
})
});
continue;
}
// heading
if (cap = block.heading.exec(src)) {
src = src.substring(cap[0].length);
@ -300,7 +344,7 @@ block.token = function(src, tokens, top) {
*/
var inline = {
escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
escape: /^\\([\\`*{}\[\]()#+\-.!_>|])/,
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
url: noop,
tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
@ -559,6 +603,48 @@ function tok() {
+ token.text
+ '</code></pre>\n';
}
case 'table': {
var thead = '\t<thead>\n\t\t<tr>';
token.header.forEach(function(heading, i){
heading = inline.lexer(heading);
var align = i < token.align.length ? token.align[i] : false;
switch(align){
case "left":
case "right":
case "center":
thead += '<th align="' + align + '">' + heading + '</td>';
break;
default:
thead += '<th>' + heading + '</th>';
}
});
thead += '</tr>\n\t</thead>\n';
var tbody = '\t<tbody>\n';
token.cells.forEach(function(row){
tbody += '\t\t<tr>';
row.forEach(function(cell, i){
cell = inline.lexer(cell);
var align = i < token.align.length ? token.align[i] : false;
switch(align){
case "left":
case "right":
case "center":
tbody += '<td align="' + align + '">' + cell + '</td>';
break;
default:
tbody += '<td>' + cell + '</td>';
}
});
tbody += '</tr>\n';
});
tbody += '\t</tbody>\n';
return '<table>\n'
+ thead
+ tbody
+ '</table>\n'
}
case 'blockquote_start': {
var body = '';
@ -740,11 +826,15 @@ function setOptions(opt) {
if (options.gfm) {
block.fences = block.gfm.fences;
block.paragraph = block.gfm.paragraph;
block.table = block.gfm.table;
block.nptable = block.gfm.nptable;
inline.text = inline.gfm.text;
inline.url = inline.gfm.url;
} else {
block.fences = block.normal.fences;
block.paragraph = block.normal.paragraph;
block.table = block.normal.table;
block.nptable = block.normal.table;
inline.text = inline.normal.text;
inline.url = inline.normal.url;
}

38
test/new/gfm_tables.html Normal file
View File

@ -0,0 +1,38 @@
<table>
<thead>
<tr><th>Heading 1</th><th>Heading 2</th></tr>
</thead>
<tbody>
<tr><td>Cell 1</td><td>Cell 2</td></tr>
<tr><td>Cell 3</td><td>Cell 4</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th align="center">Header 1</td><th align="right">Header 2</td><th align="left">Header 3</td><th>Header 4</th></tr>
</thead>
<tbody>
<tr><td align="center">Cell 1</td><td align="right">Cell 2</td><td align="left">Cell 3</td><td>Cell 4</td></tr>
<tr><td align="center">Cell 5</td><td align="right">Cell 6</td><td align="left">Cell 7</td><td>Cell 8</td></tr>
</tbody>
</table>
<pre><code>Test code</code></pre>
<table>
<thead>
<tr><th>Header 1</th><th>Header 2</th></tr>
</thead>
<tbody>
<tr><td>Cell 1</td><td>Cell 2</td></tr>
<tr><td>Cell 3</td><td>Cell 4</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th align="left">Header 1</td><th align="center">Header 2</td><th align="right">Header 3</td><th>Header 4</th></tr>
</thead>
<tbody>
<tr><td align="left">Cell 1</td><td align="center">Cell 2</td><td align="right">Cell 3</td><td>Cell 4</td></tr>
<tr><td align="left"><em>Cell 5</em></td><td align="center">Cell 6</td><td align="right">Cell 7</td><td>Cell 8</td></tr>
</tbody>
</table>

21
test/new/gfm_tables.text Normal file
View File

@ -0,0 +1,21 @@
| Heading 1 | Heading 2
| --------- | ---------
| Cell 1 | Cell 2
| Cell 3 | Cell 4
| Header 1 | Header 2 | Header 3 | Header 4 |
| :------: | -------: | :------- | -------- |
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
| Cell 5 | Cell 6 | Cell 7 | Cell 8 |
Test code
Header 1 | Header 2
-------- | --------
Cell 1 | Cell 2
Cell 3 | Cell 4
Header 1|Header 2|Header 3|Header 4
:-------|:------:|-------:|--------
Cell 1 |Cell 2 |Cell 3 |Cell 4
*Cell 5*|Cell 6 |Cell 7 |Cell 8

View File

@ -0,0 +1,38 @@
<table>
<thead>
<tr><th>Heading 1</th><th>Heading 2</th></tr>
</thead>
<tbody>
<tr><td>Cell 1</td><td>Cell 2</td></tr>
<tr><td>Cell 3</td><td>Cell 4</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th align="center">Header 1</td><th align="right">Header 2</td><th align="left">Header 3</td><th>Header 4</th></tr>
</thead>
<tbody>
<tr><td align="center">Cell 1</td><td align="right">Cell 2</td><td align="left">Cell 3</td><td>Cell 4</td></tr>
<tr><td align="center">Cell 5</td><td align="right">Cell 6</td><td align="left">Cell 7</td><td>Cell 8</td></tr>
</tbody>
</table>
<pre><code>Test code</code></pre>
<table>
<thead>
<tr><th>Header 1</th><th>Header 2</th></tr>
</thead>
<tbody>
<tr><td>Cell 1</td><td>Cell 2</td></tr>
<tr><td>Cell 3</td><td>Cell 4</td></tr>
</tbody>
</table>
<table>
<thead>
<tr><th align="left">Header 1</td><th align="center">Header 2</td><th align="right">Header 3</td><th>Header 4</th></tr>
</thead>
<tbody>
<tr><td align="left">Cell 1</td><td align="center">Cell 2</td><td align="right">Cell 3</td><td>Cell 4</td></tr>
<tr><td align="left"><em>Cell 5</em></td><td align="center">Cell 6</td><td align="right">Cell 7</td><td>Cell 8</td></tr>
</tbody>
</table>

View File

@ -0,0 +1,21 @@
| Heading 1 | Heading 2
| --------- | ---------
| Cell 1 | Cell 2
| Cell 3 | Cell 4
| Header 1 | Header 2 | Header 3 | Header 4 |
| :------: | -------: | :------- | -------- |
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
| Cell 5 | Cell 6 | Cell 7 | Cell 8 |
Test code
Header 1 | Header 2
-------- | --------
Cell 1 | Cell 2
Cell 3 | Cell 4
Header 1|Header 2|Header 3|Header 4
:-------|:------:|-------:|--------
Cell 1 |Cell 2 |Cell 3 |Cell 4
*Cell 5*|Cell 6 |Cell 7 |Cell 8