From 6fef474ade0c76d6a0b69e84fac4ddcaa38fabae Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Wed, 15 Nov 2023 20:20:30 +0100 Subject: [PATCH] factor out markdown and table generators --- coursebuilder/__main__.py | 104 +---------------------------- coursebuilder/markdowngenerator.py | 69 +++++++++++++++++++ coursebuilder/tablegenerator.py | 39 +++++++++++ test/simple/mod.test.yaml | 13 ++-- 4 files changed, 117 insertions(+), 108 deletions(-) create mode 100644 coursebuilder/markdowngenerator.py create mode 100644 coursebuilder/tablegenerator.py diff --git a/coursebuilder/__main__.py b/coursebuilder/__main__.py index d606540..2234625 100644 --- a/coursebuilder/__main__.py +++ b/coursebuilder/__main__.py @@ -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 '' in r[0] or '' 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: diff --git a/coursebuilder/markdowngenerator.py b/coursebuilder/markdowngenerator.py new file mode 100644 index 0000000..f7ff98a --- /dev/null +++ b/coursebuilder/markdowngenerator.py @@ -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 '' in r[0] or '' 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') \ No newline at end of file diff --git a/coursebuilder/tablegenerator.py b/coursebuilder/tablegenerator.py new file mode 100644 index 0000000..f61af47 --- /dev/null +++ b/coursebuilder/tablegenerator.py @@ -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)) + \ No newline at end of file diff --git a/test/simple/mod.test.yaml b/test/simple/mod.test.yaml index 17d2663..901553e 100644 --- a/test/simple/mod.test.yaml +++ b/test/simple/mod.test.yaml @@ -4,12 +4,13 @@ name: test: - en: | - + + competency-table: + de: + - "Lineare Algebra": 'ABC' + - "Vector Spaces": 'A' + + # # nested lists seem to work in Markdown only in the US style way