From ce8d77a6e9814e079e955414ef0a588f91e4efa0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 8 Jul 2012 19:42:56 -0500 Subject: [PATCH] naive table implementation. see #50. --- lib/marked.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/lib/marked.js b/lib/marked.js index 9156bf38b0..10478e4be6 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -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*)+/, @@ -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); @@ -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 '\n' + + body + + '
\n'; + } + case 'row_start': { + var body = ''; + while (next().type !== 'row_end') { + body += '' + + tok() + + '\n'; + } + return '\n' + + body + + '\n'; + } + //case 'cell': { + // return '' + // + inline.lexer(token.text) + // + ''; + //} case 'paragraph': { return '

' + inline.lexer(token.text)