Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scripts to simplify the syntax for MultiMarkdown tables.
import re
table = '''First Header,Second Header,Third Header
1st Item,2nd\,Item,3rd Item
,One cell,Two cells
Two cells,One cell,
Awesome'''
ttable = [[re.sub('\\\,',',',cell) for cell in re.split('(?<!\\\),', row)] for row in table.split('\n')]
columnCount = len(ttable[0])
barCount = columnCount + 1
headerSeparator = '|:-' * columnCount + '|'
ttable2 = ['|' + '|'.join([cell.rstrip() if cell != ' ' else cell for cell in row]) + '|' for row in ttable]
for row in enumerate(ttable2):
rowCount = row[1].count('|')
if rowCount < barCount:
ttable2[row[0]]+='|' * (barCount - rowCount)
ttable2.insert(1, headerSeparator)
print '\n'.join(ttable2)
import re
import sys
import webbrowser
import urllib
table = sys.argv[1]
ttable = [[re.sub('\\\,',',',cell) for cell in re.split('(?<!\\\),', row)] for row in table.split('\n')]
columnCount = len(ttable[0])
barCount = columnCount + 1
headerSeparator = '|:-' * columnCount + '|'
ttable2 = ['|' + '|'.join([cell.rstrip() if cell != ' ' else cell for cell in row]) + '|' for row in ttable]
for row in enumerate(ttable2):
rowCount = row[1].count('|')
if rowCount < barCount:
ttable2[row[0]]+='|' * (barCount - rowCount)
ttable2.insert(1, headerSeparator)
callback = 'drafts://x-callback-url/create?text=%s' % urllib.quote('\n'.join(ttable2))
webbrowser.open(callback)
import re
table = '''First Header Second Header Third Header
1st Item 2nd Item 3rd Item
One cell Two cells
Two cells One cell
Awesome'''
ttable = [re.split('(?<=\s)\s', row) for row in table.split('\n')]
columnCount = len(ttable[0])
barCount = columnCount + 1
headerSeparator = '|:-' * columnCount + '|'
ttable2 = ['|' + '|'.join([cell.rstrip() for cell in row]) + '|' for row in ttable]
for row in enumerate(ttable2):
rowCount = row[1].count('|')
if rowCount < barCount:
ttable2[row[0]]+='|' * (barCount - rowCount)
ttable2.insert(1, headerSeparator)
print '\n'.join(ttable2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment