Markdown - shorthand tables

From: Peter (BOUGHTONP)12 Jun 2011 20:10
To: af (CAER) 1 of 7
You're probably the most likely to know, so I'll ask directly. :)

Do you know if Markdown (or any of the various clones) has any way to reduce how much crap I have to write in order to create a table?

I'm thinking maybe some form of macros or a templating system or something.

For example, if it would let me write something like this:

code:
!table template="args"
	[ Pattern | RegexString | yes | n/a
		| The regex pattern to compile into a Regex Object.
	]
	[ Flags   | StringList  | no  | none
		| List of flags to control regex modes.
	]


And I'd define the table head in an "args" file, so I don't have to keep copy-pasting it around the place, and still get an actual HTML table output.

I could probably write a wrapper to convert that myself, but if there's already a Markdown way of doing it, it's be nice to avoid the time/effort (and to use existing conventions).

So yeah, any ideas?
From: af (CAER)12 Jun 2011 22:10
To: Peter (BOUGHTONP) 2 of 7
There are versions of Markdown that provide a table syntax, I believe, although I've not used one so I don't know much about them, sorry :$
From: Peter (BOUGHTONP)12 Jun 2011 22:28
To: af (CAER) 3 of 7
Yeah, I poked around and found there's a PHP one that does it, but I couldn't be arsed figuring out how to get a command line PHP thing setup.

I'm working with an Eclipse plugin - which is ok though relatively basic (This one - it seems to be the only Markdown plugin for Eclipse in existence! :/ )

Anyway, that came with the source attached, so I just hacked in a quick bit of replacement code, to work with a variant of the syntax I did above, and I've got it working now with this:

code:
!table [ head | is | first | row ]
	[ any | number | of | rows ]
	[ with | bracket | for | tr ]
	[ and | pipe | for | td ]
/table


And that works well enough for now - relatively crude and fragile (e.g. doesn't attempt to handle nested/escaped brackets/pipes), but it means I can get on with doing more useful stuff.

At some point I might see about improving what I've done and releasing it, if there's actually anyone out there that wants it.
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. :)