Skip to content

Commit

Permalink
Merge branch 'feature/tables-in-lists'
Browse files Browse the repository at this point in the history
* feature/tables-in-lists:
  naive table implementation. see markedjs#50.
  • Loading branch information
rhiokim committed Mar 23, 2014
2 parents 00cc3f2 + c0e9430 commit b03659a
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var block = {
code: /^( {4}[^\n]+\n*)+/,
fences: noop,
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
table: /^(([^|]+\|)+[^\n]*){2,}/,
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
nptable: noop,
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
Expand Down Expand Up @@ -197,6 +198,41 @@ Lexer.prototype.token = function(src, top, bq) {
continue;
}

// table
if (cap = block.table.exec(src)) {
src = src.substring(cap[0].length);

tokens.push({
type: 'table_start'
});

var rows = cap[0].split(/\n+ *-+ *\n+/);
rows.forEach(function(row) {
tokens.push({
type: 'row_start'
});

var cols = row.split(' | ');
cols.forEach(function(col) {
//tokens.push({
// type: 'cell', // or paragraph
// text: col
//});
block.token(col, tokens, top);
});

tokens.push({
type: 'row_end'
});
});

tokens.push({
type: 'table_end'
});

continue;
}

// heading
if (cap = this.rules.heading.exec(src)) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -1256,6 +1292,31 @@ Parser.prototype.tok = function() {
: this.token.text;
return this.renderer.html(html);
}
case 'table_start': {
var body = '';
while (next().type !== 'table_end') {
body += tok();
}
return '<table>\n'
+ body
+ '</table>\n';
}
case 'row_start': {
var body = '';
while (next().type !== 'row_end') {
body += '<td>'
+ tok()
+ '</td>\n';
}
return '<tr>\n'
+ body
+ '</tr>\n';
}
//case 'cell': {
// return '<td>'
// + inline.lexer(token.text)
// + '</td>';
//}
case 'paragraph': {
return this.renderer.paragraph(this.inline.output(this.token.text));
}
Expand Down

0 comments on commit b03659a

Please sign in to comment.