Skip to content

Instantly share code, notes, and snippets.

@WFreelandEcon
Forked from philgruneich/commas_mmdtable.py
Created March 18, 2018 13:17
Show Gist options
  • Save WFreelandEcon/4d51efe53eab4d3dfcf85f3cabd6f3a5 to your computer and use it in GitHub Desktop.
Save WFreelandEcon/4d51efe53eab4d3dfcf85f3cabd6f3a5 to your computer and use it in GitHub Desktop.
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