CodingMarkdown - shorthand tables

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Peter (BOUGHTONP)  
 To:  af (CAER)     
38604.1 
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?
0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  Peter (BOUGHTONP)     
38604.2 In reply to 38604.1 
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 :$
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  af (CAER)     
38604.3 In reply to 38604.2 
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.
0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  Peter (BOUGHTONP)     
38604.4 In reply to 38604.3 
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.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  af (CAER)     
38604.5 In reply to 38604.4 
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.
0/0
 Reply   Quote More 

 From:  af (CAER)  
 To:  Peter (BOUGHTONP)     
38604.6 In reply to 38604.5 
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.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  af (CAER)     
38604.7 In reply to 38604.6 
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. :)
0/0
 Reply   Quote More 

Reply to All    
 

1–7

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats