Wrote a version of the same algorithm in vimscript but it's very awkward.
Tablify command works.