Software Repo random bits of code

A Markdown tables Syntax (amdtbl)

This is a specification/extension to the standard markdown syntax to add tables, here is a sample:

{ *column one* | *column 2* | *column 3* }
{ left  | center |  right }
{  right | left  |  center  }

That produces the following html:

<table>
<tr>
<td align='center'> *column one* </td>
<td align='center'> *column 2* </td>
<td align='center'> *column 3* </td>
</tr>
<tr>
<td align='left'> left  </td>
<td align='center'> center </td>
<td align='right'>  right </td>
</tr>
<tr>
<td align='right'>  right </td>
<td align='left'> left  </td>
<td align='center'>  center  </td>
</tr>
</table>

And looks like this:

*column one* *column 2* *column 3*
left center right
right left center

Here is a small awk program to handle this syntax:

    #!/usr/bin/awk -f
    BEGIN {
        FS="|"
        blank="[    ]"
        blanks=blank"+"
    }

    # Table lines
    /^\{.*\|.*\}$/ {
        if(! intable) print "<table>"
        intable=1
        print "<tr>"

        $0 = substr($0, 2, length($0)-2)
        split($0, a)
        for(i in a) {
            align="center"
            if(match(a[i], "^" blank blanks))
                if(! match(a[i], blanks blank "$"))
                    align="right"
            else if(match(a[i], blanks blank "$"))
                align="left"
            print "<td align='"align"'>" a[i] "</td>"
        }
        print "</tr>"
        next
    }

    # everything else
    {
        if(intable) print "</table>"
        intable=0
        print
    }