Skip to content

Commit

Permalink
naive table implementation. see #50.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jul 9, 2012
1 parent d49ab2c commit ce8d77a
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 @@ -14,6 +14,7 @@ var block = {
code: /^( {4}[^\n]+\n*)+/,
fences: noop,
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
table: /^(([^|]+\|)+[^\n]*){2,}/,
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
blockquote: /^( *>[^\n]+(\n[^\n]+)*\n*)+/,
Expand Down Expand Up @@ -137,6 +138,41 @@ block.token = function(src, tokens, top) {
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 = block.heading.exec(src)) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -617,6 +653,31 @@ function tok() {
? inline.lexer(token.text)
: token.text;
}
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 '<p>'
+ inline.lexer(token.text)
Expand Down

0 comments on commit ce8d77a

Please sign in to comment.