Browse Source

Markdown.pl: omit empty table header rows

There's no point to including an empty header row in the output
table.  It looks ugly.

While the header row is syntactically required in order to recognize
the table markup, omit it from the output if all its column cells
are empty ignoring any whitespace.

This gives a more pleasing result.  Additionally add an extra class
tag "...-table-nohdr" in addition to the usual "...-table" tag to
allow easy custom formatting of these headerless tables if desired.

Empty body rows are always preserved because they can always be
omitted without breaking the table syntax.

Update the docs to describe the new behavior.

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

8
Markdown.pl

@ -1944,8 +1944,12 @@ sub _DoTables {
elsif (/:$/) {" align=\"right\""}
else {""}
} @seps;
my $tab ="\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" class=\"$opt{style_prefix}table\">\n" .
" <tr class=\"$opt{style_prefix}row-hdr\">" . _MakeTableRow("th", \@align, @heads) . "</tr>\n";
my $nohdr = "";
$nohdr = " $opt{style_prefix}table-nohdr" if join("", @heads) eq "";
my $tab ="\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" class=\"$opt{style_prefix}table$nohdr\">\n";
$tab .=
" <tr class=\"$opt{style_prefix}row-hdr\">" . _MakeTableRow("th", \@align, @heads) . "</tr>\n"
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"

14
syntax.md

@ -574,14 +574,16 @@ Output:
<tr><td>Bean</td><td align="right">$0.37</td><td>Fiber</td></tr>
</table>
The leading and trailing `|` on each line are optional unless there is only
a single column in which case at least one `|` is always required -- two if
the single column contains only whitespace.
The leading `|` on each line is optional unless the first column contains only
zero or more spaces and/or tabs. The trailing `|` on each line is optional
unless the last column contains only zero or more spaces and/or tabs.
At least one `|` must be present in every row of the table.
Leading and trailing whitespace are always trimmed from each column's value
before using it.
To include a literal `|` (veritical bar) character in a column's value, precede
To include a literal `|` (vertical bar) character in a column's value, precede
it with a `\` (backslash). To include a literal `\` use `\\` (double them).
The number of columns in the separator row must match exactly the number of
@ -595,6 +597,10 @@ be `right`. And finally, with a colon on both ends the alignment will be
`center`. The alignment will be applied to the column in both header and body
rows.
If all columns in the header row are empty (i.e. contain only zero or more
spaces and/or tabs), the header row will be omitted from the output. Empty
rows in the body of the table are always preserved in the output.
Body rows that contain fewer columns than the header row have empty columns
added. Body rows that contain more columns than the header row have the
extra columns dropped.

Loading…
Cancel
Save