diff --git a/CREDITS.md b/CREDITS.md index 1405b7a..39d9611 100644 --- a/CREDITS.md +++ b/CREDITS.md @@ -33,7 +33,11 @@ Credits Bug fixing and late maintainer * [Hannah Wolfe](https://github.com/ErisDS)
Bug fixes - * [Alexandre Courtiol](https://github.com/acourtiol) + * [Alexandre Courtiol](https://github.com/acourtiol)
+ Bug fixes and build optimization + * [Karthik Balakrishnan](https://github.com/torcellite)
+ Support for table alignment + - Original Project * [John Gruber](http://daringfireball.net/projects/markdown/)
diff --git a/dist/showdown.js b/dist/showdown.js index c71cccf..6731a43 100644 Binary files a/dist/showdown.js and b/dist/showdown.js differ diff --git a/dist/showdown.js.map b/dist/showdown.js.map index f0bbd74..d180338 100644 Binary files a/dist/showdown.js.map and b/dist/showdown.js.map differ diff --git a/dist/showdown.min.js b/dist/showdown.min.js index c938226..4e22f0d 100644 Binary files a/dist/showdown.min.js and b/dist/showdown.min.js differ diff --git a/dist/showdown.min.js.map b/dist/showdown.min.js.map index 310f7ae..91cb364 100644 Binary files a/dist/showdown.min.js.map and b/dist/showdown.min.js.map differ diff --git a/src/showdown.js b/src/showdown.js index 753b192..db816d3 100644 --- a/src/showdown.js +++ b/src/showdown.js @@ -15,7 +15,8 @@ var showdown = {}, simplifiedAutoLink: false, literalMidWordUnderscores: false, strikethrough: false, - tables: false + tables: false, + tablesHeaderId: false }, globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P diff --git a/src/subParsers/tables.js b/src/subParsers/tables.js index 01d7191..fabe60d 100644 --- a/src/subParsers/tables.js +++ b/src/subParsers/tables.js @@ -4,46 +4,68 @@ showdown.subParser('tables', function (text, options, globals) { var table = function () { var tables = {}, - style = 'text-align:left;', filter; - tables.th = function (header) { - if (header.trim() === '') { + tables.th = function (header, style) { + var id = ''; + header = header.trim(); + if (header === '') { return ''; } - var id = header.trim().replace(/ /g, '_').toLowerCase(); - return '' + header + ''; + if (options.tableHeaderId) { + id = ' id="' + header.replace(/ /g, '_').toLowerCase() + '"'; + } + if (style.trim() === '') { + style = ''; + } else { + style = ' style="' + style + '"'; + } + return '' + header + ''; }; - tables.td = function (cell) { - var subText = showdown.subParser('blockGamut')(cell, options, globals); - return '' + subText + ''; + tables.td = function (cell, style) { + var subText = showdown.subParser('spanGamut')(cell.trim(), options, globals); + if (style.trim() === '') { + style = ''; + } else { + style = ' style="' + style + '"'; + } + return '' + subText + ''; }; tables.ths = function () { var out = '', - i = 0, - hs = [].slice.apply(arguments); + i = 0, + hs = [].slice.apply(arguments[0]), + style = [].slice.apply(arguments[1]); + for (i; i < hs.length; i += 1) { - out += tables.th(hs[i]) + '\n'; + out += tables.th(hs[i], style[i]) + '\n'; } + return out; }; tables.tds = function () { - var out = '', i = 0, ds = [].slice.apply(arguments); + var out = '', + i = 0, + ds = [].slice.apply(arguments[0]), + style = [].slice.apply(arguments[1]); + for (i; i < ds.length; i += 1) { - out += tables.td(ds[i]) + '\n'; + out += tables.td(ds[i], style[i]) + '\n'; } return out; }; tables.thead = function () { var out, - hs = [].slice.apply(arguments); + hs = [].slice.apply(arguments[0]), + style = [].slice.apply(arguments[1]); + out = '\n'; out += '\n'; - out += tables.ths.apply(this, hs); + out += tables.ths.apply(this, [hs, style]); out += '\n'; out += '\n'; return out; @@ -51,9 +73,11 @@ showdown.subParser('tables', function (text, options, globals) { tables.tr = function () { var out, - cs = [].slice.apply(arguments); + cs = [].slice.apply(arguments[0]), + style = [].slice.apply(arguments[1]); + out = '\n'; - out += tables.tds.apply(this, cs); + out += tables.tds.apply(this, [cs, style]); out += '\n'; return out; }; @@ -64,15 +88,44 @@ showdown.subParser('tables', function (text, options, globals) { line, hs, out = []; + for (i; i < lines.length; i += 1) { line = lines[i]; // looks like a table heading if (line.trim().match(/^[|].*[|]$/)) { line = line.trim(); - var tbl = []; + + var tbl = [], + align = lines[i + 1].trim(), + styles = [], + j = 0; + + if (align.match(/^[|][-=|: ]+[|]$/)) { + styles = align.substring(1, align.length - 1).split('|'); + for (j = 0; j < styles.length; ++j) { + styles[j] = styles[j].trim(); + if (styles[j].match(/^[:][-=| ]+[:]$/)) { + styles[j] = 'text-align:center;'; + + } else if (styles[j].match(/^[-=| ]+[:]$/)) { + styles[j] = 'text-align:right;'; + + } else if (styles[j].match(/^[:][-=| ]+$/)) { + styles[j] = 'text-align:left;'; + } else { + styles[j] = ''; + } + } + } tbl.push(''); hs = line.substring(1, line.length - 1).split('|'); - tbl.push(tables.thead.apply(this, hs)); + + if (styles.length === 0) { + for (j = 0; j < hs.length; ++j) { + styles.push('text-align:left'); + } + } + tbl.push(tables.thead.apply(this, [hs, styles])); line = lines[++i]; if (!line.trim().match(/^[|][-=|: ]+[|]$/)) { // not a table rolling back @@ -82,7 +135,7 @@ showdown.subParser('tables', function (text, options, globals) { tbl.push(''); while (line.trim().match(/^[|].*[|]$/)) { line = line.trim(); - tbl.push(tables.tr.apply(this, line.substring(1, line.length - 1).split('|'))); + tbl.push(tables.tr.apply(this, [line.substring(1, line.length - 1).split('|'), styles])); line = lines[++i]; } tbl.push(''); diff --git a/test/features/tables/basic-alignment.html b/test/features/tables/basic-alignment.html index 2277f0e..6fa12e9 100644 --- a/test/features/tables/basic-alignment.html +++ b/test/features/tables/basic-alignment.html @@ -1,20 +1,20 @@
- - + + - - + + - - + + diff --git a/test/features/tables/basic.html b/test/features/tables/basic.html index d909e65..fe198b8 100644 --- a/test/features/tables/basic.html +++ b/test/features/tables/basic.html @@ -1,20 +1,20 @@
First Header Second Header First HeaderSecond Header

Row 1 Cell 1

Row 1 Cell 2

Row 1 Cell 1Row 1 Cell 2

Row 2 Cell 1

Row 2 Cell 2

Row 2 Cell 1Row 2 Cell 2
- - + + - - + + - - + + diff --git a/test/features/tables/basic_with_header_ids.html b/test/features/tables/basic_with_header_ids.html new file mode 100644 index 0000000..a5c61e0 --- /dev/null +++ b/test/features/tables/basic_with_header_ids.html @@ -0,0 +1,21 @@ +
First Header Second Header First HeaderSecond Header

Row 1 Cell 1

Row 1 Cell 2

Row 1 Cell 1Row 1 Cell 2

Row 2 Cell 1

Row 2 Cell 2

Row 2 Cell 1Row 2 Cell 2
+ + + + + + + + + + + + + + + + + + + +
First HeaderSecond Header
Row 1 Cell 1Row 1 Cell 2
Row 2 Cell 1Row 2 Cell 2
diff --git a/test/features/tables/basic_with_header_ids.md b/test/features/tables/basic_with_header_ids.md new file mode 100644 index 0000000..f3af2ff --- /dev/null +++ b/test/features/tables/basic_with_header_ids.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 | \ No newline at end of file diff --git a/test/features/tables/large-table-with-allignments.html b/test/features/tables/large-table-with-allignments.html new file mode 100644 index 0000000..8b758b6 --- /dev/null +++ b/test/features/tables/large-table-with-allignments.html @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
First HeaderSecond HeaderThird HeaderFourth Header
Row 1 Cell 1Row 1 Cell 2Row 1 Cell 3Row 1 Cell 4
Row 2 Cell 1Row 2 Cell 2Row 2 Cell 3Row 2 Cell 4
Row 3 Cell 1Row 3 Cell 2Row 3 Cell 3Row 3 Cell 4
Row 4 Cell 1Row 4 Cell 2Row 4 Cell 3Row 4 Cell 4
Row 5 Cell 1Row 5 Cell 2Row 5 Cell 3Row 5 Cell 4
diff --git a/test/features/tables/large-table-with-allignments.md b/test/features/tables/large-table-with-allignments.md new file mode 100644 index 0000000..b3fe137 --- /dev/null +++ b/test/features/tables/large-table-with-allignments.md @@ -0,0 +1,7 @@ +| First Header | Second Header | Third Header | Fourth Header | +| :------------ |: ----------- :| ------------ :| ------------- | +| Row 1 Cell 1 | Row 1 Cell 2 | Row 1 Cell 3 | Row 1 Cell 4 | +| Row 2 Cell 1 | Row 2 Cell 2 | Row 2 Cell 3 | Row 2 Cell 4 | +| Row 3 Cell 1 | Row 3 Cell 2 | Row 3 Cell 3 | Row 3 Cell 4 | +| Row 4 Cell 1 | Row 4 Cell 2 | Row 4 Cell 3 | Row 4 Cell 4 | +| Row 5 Cell 1 | Row 5 Cell 2 | Row 5 Cell 3 | Row 5 Cell 4 | diff --git a/test/features/tables/large.html b/test/features/tables/large.html index 332bdab..bfa4557 100644 --- a/test/features/tables/large.html +++ b/test/features/tables/large.html @@ -1,47 +1,47 @@ - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + diff --git a/test/features/tables/mixed-alignment.html b/test/features/tables/mixed-alignment.html new file mode 100644 index 0000000..bb400c4 --- /dev/null +++ b/test/features/tables/mixed-alignment.html @@ -0,0 +1,29 @@ +
First Header Second Header Third Header Fourth Header First HeaderSecond HeaderThird HeaderFourth Header

Row 1 Cell 1

Row 1 Cell 2

Row 1 Cell 3

Row 1 Cell 4

Row 1 Cell 1Row 1 Cell 2Row 1 Cell 3Row 1 Cell 4

Row 2 Cell 1

Row 2 Cell 2

Row 2 Cell 3

Row 2 Cell 4

Row 2 Cell 1Row 2 Cell 2Row 2 Cell 3Row 2 Cell 4

Row 3 Cell 1

Row 3 Cell 2

Row 3 Cell 3

Row 3 Cell 4

Row 3 Cell 1Row 3 Cell 2Row 3 Cell 3Row 3 Cell 4

Row 4 Cell 1

Row 4 Cell 2

Row 4 Cell 3

Row 4 Cell 4

Row 4 Cell 1Row 4 Cell 2Row 4 Cell 3Row 4 Cell 4

Row 5 Cell 1

Row 5 Cell 2

Row 5 Cell 3

Row 5 Cell 4

Row 5 Cell 1Row 5 Cell 2Row 5 Cell 3Row 5 Cell 4
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
Left-AlignedCenter-AlignedRight-Aligned
col 3 issome wordy paragraph$1600
col 2 iscentered$12
zebra stripesare neat$1
diff --git a/test/features/tables/mixed-alignment.md b/test/features/tables/mixed-alignment.md new file mode 100644 index 0000000..6504a00 --- /dev/null +++ b/test/features/tables/mixed-alignment.md @@ -0,0 +1,5 @@ +| Left-Aligned | Center-Aligned | Right-Aligned | +| :------------ |:--------------------:| -------------:| +| col 3 is | some wordy paragraph | $1600 | +| col 2 is | centered | $12 | +| zebra stripes | are neat | $1 | \ No newline at end of file diff --git a/test/features/tables/multiple-tables.html b/test/features/tables/multiple-tables.html index 5611db1..7f955d1 100644 --- a/test/features/tables/multiple-tables.html +++ b/test/features/tables/multiple-tables.html @@ -5,17 +5,17 @@ - - - + + + - - - + + + @@ -26,17 +26,17 @@
header1 header2 header3header1header2header3

Value1

Value2

Value3

Value1Value2Value3
- - - + + + - - - + + + diff --git a/test/features/tables/with-equals.html b/test/features/tables/with-equals.html index d909e65..fe198b8 100644 --- a/test/features/tables/with-equals.html +++ b/test/features/tables/with-equals.html @@ -1,20 +1,20 @@
headerA headerB headerCheaderAheaderBheaderC

ValueA

ValueB

ValueC

ValueAValueBValueC
- - + + - - + + - - + + diff --git a/test/features/tables/with-surroundings.html b/test/features/tables/with-surroundings.html index a8957c3..d99c181 100644 --- a/test/features/tables/with-surroundings.html +++ b/test/features/tables/with-surroundings.html @@ -6,20 +6,20 @@ vulputate dictum. Vestibulum consequat ultricies nibh, sed tempus nisl mattis a.
First Header Second Header First HeaderSecond Header

Row 1 Cell 1

Row 1 Cell 2

Row 1 Cell 1Row 1 Cell 2

Row 2 Cell 1

Row 2 Cell 2

Row 2 Cell 1Row 2 Cell 2
- - + + - - + + - - + + diff --git a/test/features/tables/without-body.html b/test/features/tables/without-body.html index 5b37e4a..4379949 100644 --- a/test/features/tables/without-body.html +++ b/test/features/tables/without-body.html @@ -1,8 +1,8 @@
First Header Second Header First HeaderSecond Header

Row 1 Cell 1

Row 1 Cell 2

Row 1 Cell 1Row 1 Cell 2

Row 2 Cell 1

Row 2 Cell 2

Row 2 Cell 1Row 2 Cell 2
- - + + diff --git a/test/node/showdown.js b/test/node/showdown.js index 643e950..e8a72a6 100644 --- a/test/node/showdown.js +++ b/test/node/showdown.js @@ -26,7 +26,8 @@ describe('showdown.options', function () { simplifiedAutoLink: false, literalMidWordUnderscores: false, strikethrough: false, - tables: false + tables: false, + tablesHeaderId: false }; expect(showdown.getDefaultOptions()).to.be.eql(opts); }); diff --git a/test/node/testsuite.features.js b/test/node/testsuite.features.js index 053f2ef..b549611 100644 --- a/test/node/testsuite.features.js +++ b/test/node/testsuite.features.js @@ -28,8 +28,13 @@ describe('makeHtml() features testsuite', function () { } describe('table support', function () { - var converter = new showdown.Converter({tables: true}); + var converter; for (var i = 0; i < tableSuite.length; ++i) { + if (tableSuite[i].name === 'basic_with_header_ids') { + converter = new showdown.Converter({tables: true, tableHeaderId: true}); + } else { + converter = new showdown.Converter({tables: true}); + } it(tableSuite[i].name, assertion(tableSuite[i], converter)); } });
First Header Second Header First HeaderSecond Header