Browse Source

Add remarked tests for tables and fix renderer accordingly

pull/14/head
Alex Kocharin 10 years ago
committed by Vitaly Puzrin
parent
commit
6e704c2dce
  1. 21
      benchmark/samples/block-tables.md
  2. 22
      lib/renderer.js
  3. 6
      lib/rules_block/table.js
  4. 69
      test/fixtures/remarkable/tables.txt

21
benchmark/samples/block-tables.md

@ -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

22
lib/renderer.js

@ -140,8 +140,20 @@ rules.table_open = function (/*tokens, idx, options*/) {
rules.table_close = function (/*tokens, idx, options*/) {
return '</table>\n';
};
rules.thead_open = function (/*tokens, idx, options*/) {
return '\t<thead>\n';
};
rules.thead_close = function (/*tokens, idx, options*/) {
return '\t</thead>\n';
};
rules.tbody_open = function (/*tokens, idx, options*/) {
return '\t<tbody>\n';
};
rules.tbody_close = function (/*tokens, idx, options*/) {
return '\t</tbody>\n';
};
rules.tr_open = function (/*tokens, idx, options*/) {
return '<tr>\n';
return '\t\t<tr>';
};
rules.tr_close = function (/*tokens, idx, options*/) {
return '</tr>\n';
@ -149,20 +161,20 @@ rules.tr_close = function (/*tokens, idx, options*/) {
rules.th_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return '<th'
+ (token.align ? ' align="' + token.align + '"' : '')
+ (token.align ? ' style="text-align:' + token.align + '"' : '')
+ '>';
};
rules.th_close = function (/*tokens, idx, options*/) {
return '</th>\n';
return '</th>';
};
rules.td_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return '<td'
+ (token.align ? ' align="' + token.align + '"' : '')
+ (token.align ? ' style="text-align:' + token.align + '"' : '')
+ '>';
};
rules.td_close = function (/*tokens, idx, options*/) {
return '</td>\n';
return '</td>';
};

6
lib/rules_block/table.js

@ -21,7 +21,7 @@ module.exports = function table(state, startLine, endLine, silent) {
// first character of the second line should be '|' or '-'
ch = state.src.charCodeAt(state.bMarks[startLine + 1]
+ state.tShift[startLine + 1]);
if (ch !== 0x7C/* | */ && ch !== 0x2D/* - */) { return false; }
if (ch !== 0x7C/* | */ && ch !== 0x2D/* - */ && ch !== 0x3A/* : */) { return false; }
secondLineMatch = lineMatch(state, startLine + 1,
/^ *\|?(( *[:-]-+[:-] *\|)+( *[:-]-+[:-] *))\|? *$/);
@ -48,6 +48,7 @@ module.exports = function table(state, startLine, endLine, silent) {
if (silent) { return true; }
state.tokens.push({ type: 'table_open', level: state.level++ });
state.tokens.push({ type: 'thead_open', level: state.level++ });
state.tokens.push({ type: 'tr_open', level: state.level++ });
for (i = 0; i < rows.length; i++) {
@ -60,6 +61,8 @@ module.exports = function table(state, startLine, endLine, silent) {
state.tokens.push({ type: 'th_close', level: --state.level });
}
state.tokens.push({ type: 'tr_close', level: --state.level });
state.tokens.push({ type: 'thead_close', level: --state.level });
state.tokens.push({ type: 'tbody_open', level: state.level++ });
for (nextLine = startLine + 2; nextLine < endLine; nextLine++) {
m = lineMatch(state, nextLine, /^ *\|?(.*?\|.*?)\|? *$/);
@ -77,6 +80,7 @@ module.exports = function table(state, startLine, endLine, silent) {
}
state.tokens.push({ type: 'tr_close', level: --state.level });
}
state.tokens.push({ type: 'tbody_close', level: --state.level });
state.tokens.push({ type: 'table_close', level: --state.level });
state.line = nextLine;

69
test/fixtures/remarkable/tables.txt

@ -0,0 +1,69 @@
Tests from marked:
.
| Heading 1 | Heading 2
| --------- | ---------
| Cell 1 | Cell 2
| Cell 3 | Cell 4
.
<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>
.
.
| Header 1 | Header 2 | Header 3 | Header 4 |
| :------: | -------: | :------- | -------- |
| Cell 1 | Cell 2 | Cell 3 | Cell 4 |
| Cell 5 | Cell 6 | Cell 7 | Cell 8 |
.
<table>
<thead>
<tr><th style="text-align:center">Header 1</th><th style="text-align:right">Header 2</th><th style="text-align:left">Header 3</th><th>Header 4</th></tr>
</thead>
<tbody>
<tr><td style="text-align:center">Cell 1</td><td style="text-align:right">Cell 2</td><td style="text-align:left">Cell 3</td><td>Cell 4</td></tr>
<tr><td style="text-align:center">Cell 5</td><td style="text-align:right">Cell 6</td><td style="text-align:left">Cell 7</td><td>Cell 8</td></tr>
</tbody>
</table>
.
.
Header 1 | Header 2
-------- | --------
Cell 1 | Cell 2
Cell 3 | Cell 4
.
<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>
.
.
Header 1|Header 2|Header 3|Header 4
:-------|:------:|-------:|--------
Cell 1 |Cell 2 |Cell 3 |Cell 4
*Cell 5*|Cell 6 |Cell 7 |Cell 8
.
<table>
<thead>
<tr><th style="text-align:left">Header 1</th><th style="text-align:center">Header 2</th><th style="text-align:right">Header 3</th><th>Header 4</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left">Cell 1</td><td style="text-align:center">Cell 2</td><td style="text-align:right">Cell 3</td><td>Cell 4</td></tr>
<tr><td style="text-align:left"><em>Cell 5</em></td><td style="text-align:center">Cell 6</td><td style="text-align:right">Cell 7</td><td>Cell 8</td></tr>
</tbody>
</table>
.
Loading…
Cancel
Save