diff --git a/src/extensions/table.js b/src/extensions/table.js new file mode 100644 index 0000000..5fb2332 --- /dev/null +++ b/src/extensions/table.js @@ -0,0 +1,92 @@ +/*global module:true*/ +(function(){ + var table = function(converter) { + var tables = {}, style = 'text-align:left;', filter; + tables.th = function(header){ + if (header.trim() === "") { return "";} + var id = header.trim().replace(/ /g, '_').toLowerCase(); + return '' + header + ''; + }; + tables.td = function(cell) { + return '' + cell + ''; + }; + tables.ths = function(){ + var out = "", i = 0, hs = [].slice.apply(arguments); + for (i;i'); + hs = line.substring(1, line.length -1).split('|'); + tbl.push(tables.thead.apply(this, hs)); + line = lines[++i]; + if (!line.trim().match(/^[|]{1}[-=| ]+[|]{1}$/)) { + // not a table rolling back + line = lines[--i]; + } + else { + line = lines[++i]; + tbl.push(''); + while (line.trim().match(/^[|]{1}.*[|]{1}$/)) { + line = line.trim(); + tbl.push(tables.tr.apply(this, line.substring(1, line.length -1).split('|'))); + line = lines[++i]; + } + tbl.push(''); + tbl.push(''); + // we are done with this table and we move along + out.push(tbl.join('\n')); + ++i; + continue; + } + } + out.push(line); + } + return out.join('\n'); + }; + return [ + { + type: 'lang', + filter: filter + } + ]; + }; + + // Client-side export + if (typeof window !== 'undefined' && window.Showdown && window.Showdown.extensions) { window.Showdown.extensions.table = table; } + // Server-side export + if (typeof module !== 'undefined') { + module.exports = table; + } +}()); diff --git a/test/extensions/table/basic.html b/test/extensions/table/basic.html new file mode 100644 index 0000000..4fd41db --- /dev/null +++ b/test/extensions/table/basic.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + +
First Header Second Header
Row 1 Cell 1 Row 1 Cell 2
Row 2 Cell 1 Row 2 Cell 2
diff --git a/test/extensions/table/basic.md b/test/extensions/table/basic.md new file mode 100644 index 0000000..d67f8fd --- /dev/null +++ b/test/extensions/table/basic.md @@ -0,0 +1,4 @@ +| First Header | Second Header | +| ------------- | ------------- | +| Row 1 Cell 1 | Row 1 Cell 2 | +| Row 2 Cell 1 | Row 2 Cell 2 | diff --git a/test/extensions/table/with-surroundings.html b/test/extensions/table/with-surroundings.html new file mode 100644 index 0000000..8fd4595 --- /dev/null +++ b/test/extensions/table/with-surroundings.html @@ -0,0 +1,32 @@ +

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nisi est, +ullamcorper euismod iaculis sed, tristique at neque. Nullam metus risus, +malesuada vitae imperdiet ac, tincidunt eget lacus. Proin ullamcorper +vulputate dictum. Vestibulum consequat ultricies nibh, sed tempus nisl mattis a.

+ + + + + + + + + + + + + + + + + + + + + +
First Header Second Header
Row 1 Cell 1 Row 1 Cell 2
Row 2 Cell 1 Row 2 Cell 2
+ +

Etiam iaculis urna vitae risus facilisis faucibus eu quis risus. Sed aliquet +rutrum dictum. Vivamus pulvinar malesuada ultricies. Pellentesque in commodo +nibh. Maecenas justo erat, sodales vel bibendum a, dignissim in orci. Duis +blandit ornare mi non facilisis. Aliquam rutrum fringilla lacus in semper. +Sed vel pretium lorem.

diff --git a/test/extensions/table/with-surroundings.md b/test/extensions/table/with-surroundings.md new file mode 100644 index 0000000..b55baa3 --- /dev/null +++ b/test/extensions/table/with-surroundings.md @@ -0,0 +1,16 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nisi est, +ullamcorper euismod iaculis sed, tristique at neque. Nullam metus risus, +malesuada vitae imperdiet ac, tincidunt eget lacus. Proin ullamcorper +vulputate dictum. Vestibulum consequat ultricies nibh, sed tempus nisl mattis a. + +| First Header | Second Header | +| ------------- | ------------- | +| Row 1 Cell 1 | Row 1 Cell 2 | +| Row 2 Cell 1 | Row 2 Cell 2 | + +Phasellus ac porttitor quam. Integer cursus accumsan mauris nec interdum. +Etiam iaculis urna vitae risus facilisis faucibus eu quis risus. Sed aliquet +rutrum dictum. Vivamus pulvinar malesuada ultricies. Pellentesque in commodo +nibh. Maecenas justo erat, sodales vel bibendum a, dignissim in orci. Duis +blandit ornare mi non facilisis. Aliquam rutrum fringilla lacus in semper. +Sed vel pretium lorem. diff --git a/test/extensions/table/without-body.html b/test/extensions/table/without-body.html new file mode 100644 index 0000000..5b37e4a --- /dev/null +++ b/test/extensions/table/without-body.html @@ -0,0 +1,11 @@ + + + + + + + + + + +
First Header Second Header
diff --git a/test/extensions/table/without-body.md b/test/extensions/table/without-body.md new file mode 100644 index 0000000..78c06f0 --- /dev/null +++ b/test/extensions/table/without-body.md @@ -0,0 +1,2 @@ +| First Header | Second Header | +| ------------- | ------------- | diff --git a/test/extensions/table/without-header-delimiter.html b/test/extensions/table/without-header-delimiter.html new file mode 100644 index 0000000..5d1af0f --- /dev/null +++ b/test/extensions/table/without-header-delimiter.html @@ -0,0 +1 @@ +

| First Header | Second Header |

diff --git a/test/extensions/table/without-header-delimiter.md b/test/extensions/table/without-header-delimiter.md new file mode 100644 index 0000000..a86f2da --- /dev/null +++ b/test/extensions/table/without-header-delimiter.md @@ -0,0 +1 @@ +| First Header | Second Header |