Markdown Extra table formatter

HTML

<textarea rows="20" cols="80">

Familiar	|	Stat	|	Reference	|	Requirement	|	Granted bonus	|	Note
Bat	|
MMI p268
|
PHB p52
||
Masters gains a +3 on Listen checks
||
Cat	|
MMI p270
|
PHB p52
||
Masters gains a +3 on Move silently checks
||
Hawk	|
MMI p273
|
PHB p52
||
Masters gains a +3 on Spot checks in bright light
||
Lizard	|
MMI p275
|
PHB p52
||
Masters gains a +3 on Climb checks
||
Owl	|
MMI p277
|
PHB p52
||
Masters gains a +3 on Spot checks in shadow
||
Rat	|
MMI p278
|
PHB p52
||
Masters gains a +2 on Fortitude saves
||
Raven	|
MMI p278
|
PHB p52
||
Masters gains a +3 on Appraise checks
||
Viper Snake (Tiny)	|
MMI p279
|
PHB p52
||
Masters gains a +3 on Bluff checks
||
Toad	|
MMI p282
|
PHB p52
||
Masters gains a +3 hit points
||
Weasel	|
MMI p282
|
PHB p52
||
Masters gains a +2 on Reflex saves
||
Ferret	|
DMG p203
|
DMG p203
|
Tiny or smaller size
|
Masters gains a +2 on Reflex saves
||
Hedgehog	|
DMG p203
|
DMG p203
|
Tiny or smaller size
|
Masters gains a +1 natural armor bonus
||
Mouse	|
DMG p203
|
DMG p203
|
Tiny or smaller size
|
Masters gains a +2 on Move silently checks
||
Screech Owl	|
DMG p203
|
DMG p203
|
Tiny or smaller size
|
Masters gains a +2 on Move silently checks
||
Thrush	|
DMG p203
|
DMG p203
|
Tiny or smaller size
|||
Dire Rat	|
MMI p64
|
DMG p203
|
Huge or bigger size
|
Masters gains a +2 on Fortitude saves
||
Leopard	|
MMI p274
|
DMG p203
|
Huge or bigger size
|
Masters gains a +2 on Move silently checks
||
Monitor Lizard	|
MMI p275
|
DMG p203
|
Huge or bigger size
|
Masters gains a +3 hit points
||
Owl (Medium)	|
DMG p204
|
DMG p203
|
Huge or bigger size
|
Masters gains a +2 on Move silently checks
||
Raven (Small)	|
DMG p204
|
DMG p203
|
Huge or bigger size
|||
Viper snake (Medium)	|
MMI p279
|
DMG p203
|
Huge or bigger size
|||
Wolverine	|
MMI p283
|
DMG p203
|
Huge or bigger size
|
Masters gains a +2 on Reflex saves
||
Horned Lizard	|
Sa p194
|
Sa p49
||
Masters gains a +2 on Will saves
||
Albatross	|
Sto p165
|
Sto...

JavaScript

$(function()
{
    $('button').click(function()
    {
        var textarea = $('textarea');
        textarea.val(new TableFormatter().format(textarea.val()));
    });
})

/*
var button = $('<button class="btn btn-success"><i class="icon-table"></i></button>');

userCustom.onCreateEditorButton = function()
{
    return button[0];
};

userCustom.onAceCreated = function(editor)
{
    button.click(function()
    {
        editor.setValue(new TableFormatter().format(editor.getValue()));
        editor.clearSelection();
    });
};
*/

// ---------------------------------------------------------------------

function TableFormatter()
{
    this.tableLines = [];
    this.colLengths = [];
    this.colCount = 0;

    this.outputLines = [];
}

TableFormatter.prototype.format = function(text)
{
    var inputLines = text.split('\n');
    
    while (inputLines.length)
    {
        var line = inputLines.shift();
        
        if (
            (this.tableLines.length == 1 && this._isTableSeparator(line)) ||
            (this.tableLines.length != 1 && this._isTableRow(line))
        )
        {
            this.tableLines.push(line);
        }
        else
        {
            this._concatTable();
            this.outputLines.push(line);
        }
    }
    
    this._concatTable();
    
    return this.outputLines.join('\n');
};

TableFormatter.prototype._isTableRow = function(line)
{
    return line.indexOf('|') !== -1;
};

TableFormatter.prototype._isTableSeparator = function(line)
{
    return /^\s*(\|(\s*:?-+:?\s*)\||\|?(\s*:?-+:?\s*)(\|\s*:?-+:?\s*)+\|?)\s*$/.test(line)
    //           111111111111111111 2223333333333333344444444444444444222
    // 1 - Single column with required start and end bars (e.g., |-----|)
    // 2 - Optional start or end bars
    // 3 - Required first column (e.g., |----|, | :-: |, |:---|)
    //     Optional padding allowed and left/right/center
    // 4 - Second to nth column with required bar between...