Tables

Patrick Kelly patrick at oakroad.com
Tue Nov 8 04:57:51 EST 2005


In my particular application with Markdown, I wanted "wikilinks" and
tables. Here's my basic "algorithm":

# Do our own CamelCase thing here. We don't want to turn every
# java name into a link, we don't wanna conflict confuse with
# markdown syntax, and we want it simple. I like twiddle because
# it's like a home-dir link, and no programming identifiers will
# start with it. I'm gonna do ~CamelCase => markdown link. I'm
# purposely NOT including underscore so that we can maintain a
# consistent naming style.

# Minimal example: ~Aa
$file_contents =~ s=~([A-Z][a-z0-9]+[A-Za-z0-9]*)=[$1]($1)=g;

$page_content = Markdown::Markdown( $file_contents );
$page_content = ConvertToTable( $page_content );

I wanted the text bits in my tables to be processed by Markdown, so I
couldn't do my table conversion first. (Everything inside the
<table>....</table> would be untouched by Markdown because it's a
block of HTML.) So I ended up with some extra gunk to strip off the
<p> and </p> that Markdown stuck on what it thought were paragraphs
and I wanted to be tables.

I used "!" or "~" at the beginning of the line to flag a "table
line". ex:

! Widget ~ Count
~ Blue ~ 25
~ Green ~ 10

becomes:

<table align="center">
<tr><th>Widget</th><th>Count</th></tr>
<tr><td>Blue</td><td>25</td></tr>
<tr><td>Green</td><td>10</td></tr>
</table>

So, it's pretty basic, but it's working for me so far. Here it is:

sub ConvertToTable {

my $text = shift;
my $new_text = "";
my $in_out = "out";

# Standardize line endings:
$text =~ s{\r\n}{\n}g; # DOS to Unix
$text =~ s{\r}{\n}g; # Mac to Unix

foreach $_ ( split(/\n/, $text )) {

# Markdown may have stuck a <p> on the beginning line of
# the table, so accept this fact.
if ( m/^(<p>)?([\!\~])(.*)/ ) {

$first_char = $2;
$_ = $3;

# Markdown sticks a close paragraph on the last line.
# Junk it.
s=</p>\$==;

@field_list = split /[\!\~]/, $_;

if ( $in_out eq "out" ) {
$in_out = "in";
$_ = "<table align=\"center\">\n";
} else {
$_ = "";
}

if ( $first_char eq "!" ) {
$whichit = "th";
} else {
$whichit = "td";
}

$_ .= "<tr><$whichit>";

$_ .= join( "</$whichit><$whichit>", @field_list );

$_ .= "</tr>";

} elsif ( $in_out eq "in" ) {
$in_out = "out";
$_ .= "</table>\n";
}

$new_text .= $_;
$new_text .= "\n";
}

return $new_text;
}


I suspect that this may be a case similar to the whole image
discussion where it's a bit beyond the scope of Markdown itself. I
suspect what's needed is a standard protocol/plugin architecture so
that one could pick a 3rd party tiddlywink that does the job at the
right level for you (or write an extension yourself). Somehow the
pipeline model of unix command shells comes to mind, where each small
program does it's little job, but they all work together. :) I
haven't used SmartyPants (and I may never). I think it's right that
it's separate from Markdown, even if it could all be wrapped up
together.



More information about the Markdown-Discuss mailing list