Skip to content

Instantly share code, notes, and snippets.

@NaanProphet
Forked from bsweger/markdown-table-to-html.py
Last active August 25, 2016 00:44
Show Gist options
  • Save NaanProphet/81deddf6cecb4e7cf66820f87c5299ae to your computer and use it in GitHub Desktop.
Save NaanProphet/81deddf6cecb4e7cf66820f87c5299ae to your computer and use it in GitHub Desktop.
Convert markdown table rows to html (we converted some markdown tables to html so we could use the "scope" attribute in the header rows for accessibility).
import sys
import re
# input: first argument is a file that contains the piped markdown table rows
with open (sys.argv[1]) as f:
content = f.readlines()
rows = []
pattern = re.compile("^\|[ -][ -]+")
for c in content:
# poor man's way of excluding non-table rows in the file
# and excluding empty header and alignment rows
if (c.startswith('|') and not (pattern.match(c))):
cells = c.split('|')
# "if not..." is the poor man's way of excluding empty rows
cells = ['<td>{}</td>'.format(cell.strip()) for cell in cells if not cell.strip() == ""]
rows.append(cells)
# output: second argument contains the html table rows
file = open(sys.argv[2], 'w+')
file.write('<table>\n')
for r in rows:
file.write('<tr>\n')
for c in r:
file.write(' {}\n'.format(c))
file.write('</tr>\n')
file.write('</table>\n')
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment