Skip to content

Commit

Permalink
implemented tables
Browse files Browse the repository at this point in the history
  • Loading branch information
redsun82 committed Oct 31, 2012
1 parent 50f6d69 commit 404344f
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,12 @@ Markdown.dialects.Gruber.inline = {
"]": function () {},
"}": function () {},

__escape__ : /^\\[\\`\*_{}\[\]()#\+.!\-]/,

"\\": function escaped( text ) {
// [ length of input processed, node/children to add... ]
// Only esacape: \ ` * _ { } [ ] ( ) # * + - . !
if ( text.match( /^\\[\\`\*_{}\[\]()#\+.!\-]/ ) )
if ( text.match( this.__escape__ ) )
return [ 2, text[1] ];
else
// Not an esacpe
Expand Down Expand Up @@ -1259,7 +1261,7 @@ Markdown.dialects.Maruku.block.definition_list = function definition_list( block
// one or more terms followed by one or more definitions, in a single block
var tight = /^((?:[^\s:].*\n)+):\s+([\s\S]+)$/,
list = [ "dl" ],
i;
i, m;

// see if we're dealing with a tight or loose block
if ( ( m = block.match( tight ) ) ) {
Expand Down Expand Up @@ -1293,6 +1295,69 @@ Markdown.dialects.Maruku.block.definition_list = function definition_list( block
return [ list ];
};

Markdown.dialects.Maruku.block.table = function table (block, next) {
var no_header_lp = /^\|\s*([\-:]+[\-| :]*)\n((?:\s*\|.*(?:\n|$))*)(?=\n|$)/,
header_lp = /^\|(.+)\n([\s\S]*)/,
no_header_no_lp = /^([\-:]+\s*\|[\-| :]*)\n((?:.*\|.*(?:\n|$))*)(?=\n|$)/,
header_no_lp = /^(\S.*\|.*)\n([\s\S]*)/,
i, m, header, attrs, body;
if (m = block.match(no_header_lp)) {
// remove leading pipes in contents
body = m[2].replace(/^\s*\|/gm, '');
} else if ((header = block.match(header_lp)) && (m = header[2].match(no_header_lp))) {
header = header[1];
body = m[2].replace(/^\s*\|/gm, '');
} else if (m = block.match(no_header_no_lp)) {
body = m[2];
} else if ((header = block.match(header_no_lp)) && (m = header[2].match(no_header_no_lp))) {
header = header[1];
body = m[2];
} else return undefined;

var table = [ "table" ];

// remove trailing pipes, then split on pipes
attrs = m[1].replace(/\|\s*$/, '').split('|');

// there is a difference from what I think markdown extra works
// splitting with pipes is done here before inline parsing
// you should therefore escape all pipes that are not column seprators

// process alignment
var html_attrs = [ ];
forEach (attrs, function (s) {
if (s.match(/^\s*-+:\s*$/)) html_attrs.push({align: "right"});
else if (s.match(/^\s*:-+\s*$/)) html_attrs.push({align: "left"});
else if (s.match(/^\s*:-+:\s*$/)) html_attrs.push({align: "center"});
else html_attrs.push({});
});

if (header) {
header = header.replace(/\|\s*$/, '').split('|');
table.push(['thead', ['tr']]);
for (i = 0; i < header.length; i++) {
table[1][1].push(["th", html_attrs[i] || {}].concat(this.processInline(header[i].trim())));
}
}

// put contents
body = body.replace(/\|\s*$/mg, '').split('\n');
var html_body = [ "tbody" ];
forEach (body, function (row) {
var html_row = ['tr'];
row = row.split('|');
for (i = 0; i < row.length; i++) {
html_row.push(['td', html_attrs[i] || {}].concat(this.processInline(row[i].trim())));
}
html_body.push(html_row);
}, this);

table.push(html_body);


return [table];
}

Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, out ) {
if ( !out.length ) {
return [ 2, "{:" ];
Expand Down Expand Up @@ -1330,6 +1395,8 @@ Markdown.dialects.Maruku.inline[ "{:" ] = function inline_meta( text, matches, o
return [ m[ 0 ].length, "" ];
};

Markdown.dialects.Maruku.inline.__escape__ = /^\\[\\`\*_{}\[\]()#\+.!\-|:]/

Markdown.buildBlockOrder ( Markdown.dialects.Maruku.block );
Markdown.buildInlinePatterns( Markdown.dialects.Maruku.inline );

Expand Down Expand Up @@ -1565,7 +1632,8 @@ function convert_tree_to_html( tree, references, options ) {
if ( attrs ) {
// if there are keys, skip over it
for ( var key in jsonml[ 1 ] ) {
i = 2;
i = 2;
break;
}
// if there aren't, remove it
if ( i === 1 ) {
Expand Down

0 comments on commit 404344f

Please sign in to comment.