Markdown - shorthand tables

From: af (CAER)12 Jun 2011 22:32
To: Peter (BOUGHTONP) 4 of 7
Is there a reason you're not just using HTML for tables? I know it's a faff, but still, Markdown does allow for HTML.
From: Peter (BOUGHTONP)12 Jun 2011 22:44
To: af (CAER) 5 of 7
code:
<table>
	<thead>
		<tr><th> because <th> it <th> is <th> way </tr>
	</thead>
	<tbody>
		<tr><td> more <td> bloated <td> and <td> harder </tr>
		<tr><td> to   <td> read <td> cells <td> quickly </tr>
		<tr><td> when <td> viewing <td> in <td> markup </tr>
	</tbody>
</table>


code:
!table [ whilst | like | this | it ]
	[ is  | easier | to | ignore ]
	[ the | delimiters | and | focus ]
	[ on  | the | actual | contents ]
/table



More importantly though, you can't use markdown inside of HTML, which I've only just remembered I'll need to do, and fortunately my [s don't get in the way of regular markdown [s. Phew.
From: af (CAER)12 Jun 2011 22:53
To: Peter (BOUGHTONP) 6 of 7
Well yeah, I know it's more verbose. Just sayin. If you manage to get this thing working well, I may steal it and make a JavaScript version.
From: Peter (BOUGHTONP)12 Jun 2011 23:20
To: af (CAER) 7 of 7
Heh, the original is in Java, and can't be directly translated because JS doesn't have equivalents.

Although... it does have replace callbacks which Java doesn't, so could actually be an interesting comparison...

Here we go:

java code:
private String convertTables( String html )
{
   Pattern p = Pattern.compile("(?ms)^<p>!table(.*?)(?<=\\n\\s{0,99})/table</p>", 0);
   Matcher m = p.matcher(html);
   StringBuffer sb = new StringBuffer("");
 
   while (m.find())
   {
      String[] Rows = m.group(1).trim().split("(?:^|\\]?\\n)\\s*\\[");
 
      String Head = "<thead><tr><th>"
         + Rows[1]
              .replaceAll("\\|","<th>")
              .replaceAll("\\]\\s*+$","</th>")
         + "</thead>"
         ;
 
      String Body = "<tbody>";
 
      for ( int i = 2 ; i < Rows.length; i++ )
      {
         Body += "<tr><td>"
            + Rows[i]
            .replaceAll("\\|","<td>")
            .replaceAll("\\]\\s*+$","</td>")
            ;
      }
 
      m.appendReplacement(sb,"<table>" + Head + Body + "</tbody></table>");
   }
 
   m.appendTail(sb);
 
   return sb.toString();
}



javascript code:
function convertTables( html )
{
   return html.replace
      ( /^<p>!table([\w\W]*?)\/table<\/p>/gm
      , function()
         {
            var Rows = arguments[1].split(/(?:^|\]?\n)\s*\[/);
 
            var Head = '<thead><tr><th>'
               + Rows[1]
                    .replace(/\|/g,'<th>')
                    .replace(/\]\s*$/g,'</th>')
               + '</thead>'
               ;
 
            var Body = '<tbody>'
 
            for ( var i = 2 ; i < Rows.length; i++ )
            {
               Body += '<tr><td>'
                  + Rows[i]
                  .replace(/\|/g,'<td>')
                  .replace(/\]\s*$/g,'</td>')
                  ;
            }
 
            return '<table>' + Head + Body + '</tbody></table>'
 
         }
      )
}



Of course, the JS one is just as flaky as the original, but if you do feel like doing anything with it, go ahead. :)