83 lines
No EOL
2.3 KiB
Python
83 lines
No EOL
2.3 KiB
Python
#!/usr/bin/env python
|
|
|
|
import textwrap,itertools
|
|
|
|
# we need raw value maybe add a third item - tuple the input?
|
|
|
|
# alternative use a dictionary
|
|
# { name: XYZ }
|
|
|
|
# or make it the class
|
|
|
|
class MarkdownGenerator:
|
|
|
|
@staticmethod
|
|
def generate_tablerow() -> str:
|
|
pass
|
|
|
|
@staticmethod
|
|
def generate(ti,pagebreak = False,title = False,header_level = 1) -> str:
|
|
|
|
line_length = 128
|
|
column_ratio= 0.28
|
|
|
|
h_len = int(line_length * column_ratio)
|
|
d_len = line_length-h_len
|
|
|
|
#
|
|
# generate title (currently the first one)
|
|
#
|
|
if title:
|
|
print('#' * header_level,ti[0][1],'\n')
|
|
|
|
print(''.join(['+',"".ljust(h_len,'-'),'+',"".ljust(d_len,'-'),'+']))
|
|
|
|
headline = False
|
|
|
|
#
|
|
# this implements a Markdown Grid-Table
|
|
#
|
|
|
|
# test if this affected by a third item!
|
|
|
|
for k,v in ti:
|
|
|
|
#
|
|
if v == None:
|
|
v = ''
|
|
|
|
# row head
|
|
h = textwrap.wrap(k, h_len, break_long_words=False)
|
|
wrapper = textwrap.TextWrapper(d_len,break_long_words=True,replace_whitespace=False)
|
|
|
|
# split test to wrap lines into correct length
|
|
t = [wrapper.wrap(line) for line in v.split('\n')]
|
|
|
|
|
|
# replace empty arrays from split with a empty string
|
|
t = list(map(lambda e: [""] if e == [] else e, t))
|
|
|
|
# zip items of list
|
|
t = list(itertools.chain.from_iterable(t))
|
|
|
|
# get rows
|
|
rows = list(itertools.zip_longest(h,t,fillvalue=""))
|
|
|
|
# expand rows
|
|
for r in rows:
|
|
# insider recognize this as the computational dump(b)ness feature
|
|
if '<!-- tablebreak -->' in r[0] or '<!-- tablebreak -->' in r[1]:
|
|
print(''.join(['+',"".ljust(h_len,'-'),'+',"".ljust(d_len,'-'),'+']))
|
|
else:
|
|
print(''.join(['|',r[0].ljust(h_len,' '),'|',r[1].ljust(d_len,' '),'|']))
|
|
|
|
if headline:
|
|
print(''.join(['+',"".ljust(h_len,'-'),'+',"".ljust(d_len,'-'),'+']))
|
|
else:
|
|
print(''.join(['+',"".ljust(h_len,'='),'+',"".ljust(d_len,'='),'+']))
|
|
headline = True
|
|
|
|
|
|
# to control pagebreaks for pandoc
|
|
if pagebreak:
|
|
print('\n\\newpage') |