-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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