factor out markdown and table generators

This commit is contained in:
Hartmut Seichter 2023-11-15 20:20:30 +01:00
parent 094b5cc7e3
commit 6fef474ade
4 changed files with 117 additions and 108 deletions

View file

@ -11,113 +11,13 @@ actual values are kept in YAML files in order to version them with git.
"""
from argparse import ArgumentParser
import itertools
import yaml
import textwrap
import string
import os
from tablegenerator import TableGenerator
from markdowngenerator import MarkdownGenerator
class TableGenerator:
def __init__(self) -> None:
self.__cols_map = {
"de": {'': 'Thema', 'A': " Erkennen", 'B': ' Verstehen', 'C': ' Anwenden', 'D' : ' Analysiseren', 'E' : ' Synthetisieren' }
}
def generate_table(self,data,lang='de'):
header = False
for token in data.split(','):
if not header:
head_str = "| " + " | ".join(self.__cols_map[lang].values()) + " |"
print(head_str)
ruler = ['-' * len(s) for s in self.__cols_map[lang].values()]
ruler_str = "| " + " | ".join(ruler) + " |"
print(ruler_str)
header = True
t = tuple(token.split(':')[:2])
row = [t[0]]
for k in self.__cols_map[lang].keys():
if k in t[1]:
row.append('X')
else:
row.append(' ')
print(' | '.join(row))
class MarkdownGenerator:
def __init__(self) -> None:
pass
def generate_markdown(self,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
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
#
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')
class CourseBuilder:

View file

@ -0,0 +1,69 @@
#!/usr/bin/env python
import textwrap,itertools
class MarkdownGenerator:
def __init__(self) -> None:
pass
def generate_markdown(self,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
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
#
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')

View file

@ -0,0 +1,39 @@
import token
class TableGenerator:
def __init__(self) -> None:
self.__cols_map = {
"de": {'': 'Thema ', 'A': " Erkennen", 'B': ' Verstehen', 'C': ' Anwenden', 'D' : ' Analysiseren', 'E' : ' Synthetisieren' }
}
self.__checkmark = "X"
def generate_table(self,data,lang='de'):
header = False
for token in data.split(','):
if not header:
head_str = "| " + " | ".join(self.__cols_map[lang].values()) + " |"
print(head_str)
ruler = ['-' * len(s) for s in self.__cols_map[lang].values()]
ruler_str = "| " + " | ".join(ruler) + " |"
print(ruler_str)
header = True
t = tuple(token.split(':')[:2])
row = [t[0]]
for k in self.__cols_map[lang].keys():
if k in t[1]:
row.append(self.__checkmark)
else:
row.append(' ')
print(' | '.join(row))

View file

@ -4,12 +4,13 @@ name:
test:
en: |
<!-- bar-table.
one:
two:
three:
-->
competency-table:
de:
- "Lineare Algebra": 'ABC'
- "Vector Spaces": 'A'
#
# nested lists seem to work in Markdown only in the US style way