Add alignment feature to tables
diff --git a/pkgs/markdown/lib/src/block_parser.dart b/pkgs/markdown/lib/src/block_parser.dart index 162734b..0b03b8a 100644 --- a/pkgs/markdown/lib/src/block_parser.dart +++ b/pkgs/markdown/lib/src/block_parser.dart
@@ -49,7 +49,7 @@ new RegExp(r'^([ ]{0,3})(\d{1,9})([\.)])(([ \t])([ \t]*)(.*))?$'); /// A line of hyphens separated by at least one pipe. -final _tablePattern = new RegExp(r'^[ ]{0,3}\|?(\-+\|)+\-*$'); +final _tablePattern = new RegExp(r'^[ ]{0,3}\|?(:?\-+:?\|)+(:?\-+:?)?$'); /// Maintains the internal state needed to parse a series of lines into blocks /// of Markdown suitable for further inline parsing. @@ -727,21 +727,34 @@ /// * a divider of hyphens and pipes (not rendered) /// * many body rows of body cells (`<td>` cells) Node parse(BlockParser parser) { - var head = new Element('thead', [parseRow(parser, 'th')]); + var alignments = parseAlignments(parser.next); + var head = new Element('thead', [parseRow(parser, alignments, 'th')]); // Advance past the divider of hyphens. parser.advance(); var rows = <Element>[]; while (!parser.isDone && !parser.matches(_emptyPattern)) { - rows.add(parseRow(parser, 'td')); + rows.add(parseRow(parser, alignments, 'td')); } var body = new Element('tbody', rows); return new Element('table', [head, body]); } - Node parseRow(BlockParser parser, String cellType) { + List<String> parseAlignments(String line) { + line = line + .replaceFirst(new RegExp(r'^\|'), '') + .replaceFirst(new RegExp(r'\|$'), ''); + return line.split('|').map((column) { + if (column.startsWith(':') && column.endsWith(':')) return 'center'; + if (column.startsWith(':')) return 'left'; + if (column.endsWith(':')) return 'right'; + return null; + }).toList(); + } + + Node parseRow(BlockParser parser, List<String> alignments, String cellType) { var line = parser.current .replaceFirst(new RegExp(r'^\|\s*'), '') .replaceFirst(new RegExp(r'\s*\|$'), ''); @@ -767,6 +780,11 @@ }); row.add(new Element(cellType, cellContents)); + for (int i = 0; i < row.length && i < alignments.length; i++) { + if (alignments[i] == null) continue; + row[i].attributes['style'] = 'text-align: ${alignments[i]};'; + } + return new Element('tr', row); } }
diff --git a/pkgs/markdown/test/extensions/tables.unit b/pkgs/markdown/test/extensions/tables.unit index e28def5..e16cde5 100644 --- a/pkgs/markdown/test/extensions/tables.unit +++ b/pkgs/markdown/test/extensions/tables.unit
@@ -45,3 +45,11 @@ row with | two cells <<< <table><thead><tr><th>head</th><th>foo</th><th>bar</th></tr></thead><tbody><tr><td>body</td></tr><tr><td>row with</td><td>two cells</td></tr></tbody></table> +>>> left, center, and right alignment +head | cells | here +:----|:-----:|----: +body | cells | here +too | many | cells | here + +<<< +<table><thead><tr><th style="text-align: left;">head</th><th style="text-align: center;">cells</th><th style="text-align: right;">here</th></tr></thead><tbody><tr><td style="text-align: left;">body</td><td style="text-align: center;">cells</td><td style="text-align: right;">here</td></tr><tr><td style="text-align: left;">too</td><td style="text-align: center;">many</td><td style="text-align: right;">cells</td><td>here</td></tr></tbody></table>