Browse Source

Markdown.pl: support table row merging

Given a table like this:

 | H1 | H2 | H3 |
 |----|----|----|
 | x1 | x2 | x3 |
 | y1 |too long|y3|
 | z1 | z2 | z3 |

The problematic second row can now be split across mulitple lines
like this:

 | H1 | H2 | H3 |
 |----|----|----|
 | x1 | x2 | x3 |
 | y1 |too | y3 |\
 |    |long|    |
 | z1 | z2 | z3 |

While the example is contrived, even with "sloppy" tables, having
the ability to merge row data like this usually avoids the need for
unsightly long lines when an exceptional cell overflows excessively.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
master
Kyle J. McKay 5 years ago
parent
commit
a17ab1a3d8
  1. 47
      Markdown.pl
  2. 55
      syntax.md

47
Markdown.pl

@ -2005,8 +2005,8 @@ sub _DoTables {
unless $nohdr;
my $cnt = 0;
my @classes = ("class=\"$opt{style_prefix}row-even\"", "class=\"$opt{style_prefix}row-odd\"");
$tab .= " <tr " . $classes[++$cnt % 2] . ">" . _MakeTableRow("td", \@align, _SplitTableRow($_)) . "</tr>\n"
foreach split(/\n/, $rows);
$tab .= " <tr " . $classes[++$cnt % 2] . ">" . _MakeTableRow("td", \@align, @$_) . "</tr>\n"
foreach (_SplitMergeRows($rows));
$tab .= "</table>\n\n";
} else {
$w;
@ -2017,6 +2017,49 @@ sub _DoTables {
}
sub _SplitMergeRows {
my @rows = ();
my ($mergeprev, $mergenext) = (0,0);
foreach (split(/\n/, $_[0])) {
$mergeprev = $mergenext;
$mergenext = 0;
my @cols = _SplitTableRow($_);
if (_endswithbareslash($cols[$#cols])) {
my $last = $cols[$#cols];
substr($last, -1, 1) = "";
$last =~ s/[ ]+$//;
$cols[$#cols] = $last;
$mergenext = 1;
}
if ($mergeprev) {
for (my $i = 0; $i <= $#cols; ++$i) {
my $cell = $rows[$#rows]->[$i];
defined($cell) or $cell = "";
$rows[$#rows]->[$i] = _MergeCells($cell, $cols[$i]);
}
} else {
push(@rows, [@cols]);
}
}
return @rows;
}
sub _endswithbareslash {
return 0 unless substr($_[0], -1, 1) eq "\\";
my @parts = split(/\\\\/, $_[0], -1);
return substr($parts[$#parts], -1, 1) eq "\\";
}
sub _MergeCells {
my ($c1, $c2) = @_;
return $c1 if $c2 eq "";
return $c2 if $c1 eq "";
return $c1 . " " . $c2;
}
sub _SplitTableRow {
my $row = shift;
$row =~ s/^$LEAD//;

55
syntax.md

@ -619,6 +619,61 @@ Inline markup is recognized just fine within each column:
|:-
|~~Strikeout~~ `code` _etc._
Row text can be split over multiple rows by ending a row with a
backslash (`\`) as the last character on the line.
For example, this:
Item|Price|Description
-|-:|-
Nut|$1.29|Delicious
Bean|$0.37|Fiber
Squash|$1.83|Healthy
Generates output something like this:
<table>
<tr><th>Item</th><th>Price</th><th>Description</th></tr>
<tr><td>Nut</td><td>$1.29</td><td>Delicious</td></tr>
<tr><td>Bean</td><td>$0.37</td><td>Fiber</td></tr>
<tr><td>Squash</td><td>$1.83</td><td>Healthy</td></tr>
</table>
But adding a trailing `\` to the end of first table body row like
so:
Item|Price|Description
-|-:|-
Nut|$1.29|Delicious \
Bean|$0.37|Fiber
Squash|$1.83|Healthy
Generates this output instead:
<table>
<tr><th>Item</th><th>Price</th><th>Description</th></tr>
<tr><td>Nut Bean</td><td>$1.29 $0.37</td><td>Delicious Fiber</td></tr>
<tr><td>Squash</td><td>$1.83</td><td>Healthy</td></tr>
</table>
The corresponding columns of the first two rows are merged. It's
possible to merge multiple rows. Adding a trailing `\` to the
second row too would result in a single row output table.
The `\` must be the very last character on the line to be recognized
as a "row-joiner". If the optional trailing `|` has been included
the "row-joiner" must appear after that like so:
Item|Price|Description|
-|-:|-|
Nut|$1.29|Delicious| \
Bean|$0.37|Fiber|
Squash|$1.83|Healthy|
The advantage of including the optional trailing `|` when using a
"row-joiner" is that renderers that do not support the "row-joiner"
will see that as a superfluous extra column instead and discard it.
~~~~~~~~~~~
Style Sheet

Loading…
Cancel
Save