Last active
August 1, 2019 02:41
-
-
Save yohannd1/930f95b0807b65f4d1baae9c92bd38ab to your computer and use it in GitHub Desktop.
A python program that generates markdown tables based on lists, with support for alignment.
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
# Markdown Table Generator | |
# You can use the main function by importing it, and it won't use the table by default; | |
# However, if you run this file as the main program it will process the table declared below. | |
# | |
# Quick Documentation | |
# The first list (Align) can only receive the values 1, 0 and -1. They mean: | |
# 1: Align to the Right | |
# 0: Align to the Center | |
# -1: Align to the Left | |
table = [ | |
# Align: | |
[1, 0, -1], | |
# Header: | |
['Lorem', 'Ipsum', 'Dolor Sit'], | |
# Contents: | |
[0, 2, 5], | |
[['Amet', 1], (7, {0: 'n'})] | |
] | |
def lsmaxstr(ls): | |
ret = -1 | |
for i in ls: | |
ret = max(len(str(i)), ret) | |
return ret | |
def main(arg): | |
if len(arg) >= 3: # Check the length of the table and verify if it can be converted to markdown. | |
# SECTION.1: Proccess table length and alignment: | |
table_columnLength = len(arg[0]) # Gets the table length (columns) via the alignment list. | |
for x in arg: | |
while len(x) < table_columnLength: | |
x.append('') | |
mask_tableColumns = [] # A new list, this time for storing the data in column order, to proceed on the mask_maxLength calculation.. | |
for x in range(0, table_columnLength): # Populate mask_tableColumns with empty lists based on the amount of columns. | |
mask_tableColumns.append([]) | |
# Make a list out of the 'arg' list, but without the alignment list. | |
argNeo = arg.copy() | |
del argNeo[0] | |
# Get the values for mask_tableColumns | |
for (id_x, x) in enumerate(argNeo): | |
for (id_y, y) in enumerate(argNeo[id_x]): # These are the strings. | |
mask_tableColumns[id_y].append(str(y)) | |
# Finish the calculation for the length | |
mask_maxLength = [] | |
for x in mask_tableColumns: | |
mask_maxLength.append(lsmaxstr(x)) | |
# Finally make the arranged strings. | |
for (id_x, x) in enumerate(argNeo): | |
for (id_y, y) in enumerate(argNeo[id_x]): | |
y = str(y) | |
z = '' | |
if arg[0][id_y] == -1: | |
z = y.ljust(mask_maxLength[id_y], ' ') | |
elif arg[0][id_y] == 0: | |
z = y.center(mask_maxLength[id_y], ' ') | |
elif arg[0][id_y] == 1: | |
z = y.rjust(mask_maxLength[id_y], ' ') | |
else: | |
return 'Error: Invalid Alignment ID; it should be only -1, 0 or 1.' | |
argNeo[id_x][id_y] = z | |
# Make the final string. | |
final = '''''' | |
isVertSeparatorCalculated = False | |
for (id_x, x) in enumerate(argNeo): | |
final += '|' # Starting Separator | |
if (id_x != 0) and (not isVertSeparatorCalculated): # Calculate the vertical dividers between elements 0 and 1. | |
isVertSeparatorCalculated = True | |
for (id_y, y) in enumerate(range(0, table_columnLength)): | |
final += ' ' | |
if arg[0][id_y] == -1: | |
final += ':'.ljust(mask_maxLength[id_y], '-') | |
elif arg[0][id_y] == 0: | |
final += ''.center(mask_maxLength[id_y], '-') | |
elif arg[0][id_y] == 1: | |
final += ':'.rjust(mask_maxLength[id_y], '-') | |
else: | |
return 'Error: Invalid Alignment ID; it should be only -1, 0 or 1.' | |
final += ' |' | |
final += '\n|' | |
for y in x: | |
final += ' ' + y + ' |' # Spacing + End Separator | |
final += '\n' | |
# And finally, return the table. | |
return final | |
else: | |
return 'Error: Table is too small. You need to add at least 3 columuns: one for the alignment (-1, 0 or 1), one for the headers and one for the text.' | |
if __name__ == '__main__': | |
print('Result:\n{}'.format(main(table))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment