refactoring previous Markdown generator into a legacy mode
This commit is contained in:
parent
e9407a6b6e
commit
df1cff80d8
3 changed files with 47 additions and 26 deletions
|
@ -11,7 +11,6 @@ actual values are kept in YAML files in order to version them with git.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
import string
|
|
||||||
import os,sys
|
import os,sys
|
||||||
import yaml
|
import yaml
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
@ -40,6 +39,8 @@ class CourseBuilder:
|
||||||
parser.add_argument('--table-gen',type=str,default=None,help='runs table generator')
|
parser.add_argument('--table-gen',type=str,default=None,help='runs table generator')
|
||||||
parser.add_argument('--template',type=str,default=None,help='defines a template to be used with fields')
|
parser.add_argument('--template',type=str,default=None,help='defines a template to be used with fields')
|
||||||
parser.add_argument('-o','--out',type=str,default=None,help='set the output type')
|
parser.add_argument('-o','--out',type=str,default=None,help='set the output type')
|
||||||
|
parser.add_argument('--legacy',action="store_true",help="use legacy generator mode for compatibility")
|
||||||
|
|
||||||
|
|
||||||
parser.add_argument('--maxcol',type=int,default=28,help='maximum size of left column')
|
parser.add_argument('--maxcol',type=int,default=28,help='maximum size of left column')
|
||||||
|
|
||||||
|
@ -122,25 +123,19 @@ class CourseBuilder:
|
||||||
|
|
||||||
# TODO - something more processable for Pandas?
|
# TODO - something more processable for Pandas?
|
||||||
draft = [
|
draft = [
|
||||||
{ 'name' : 'Computergrafik' },
|
{ 'name' : 'Modulname', 'lang' : 'de' },
|
||||||
{ 'credits' : '5 ECTS' },
|
{ 'credits' : '5 ECTS' },
|
||||||
]
|
]
|
||||||
|
|
||||||
# get the dataframe
|
draft_2 = {
|
||||||
df = pd.DataFrame(table_items)
|
}
|
||||||
|
|
||||||
# use first column for
|
if args.legacy:
|
||||||
df.columns = df.iloc[0]
|
MarkdownGenerator.generate_table_legacy(table_items=table_items,add_pagebreak=args.pagebreak,title_template=args.title,first_colwidth=args.maxcol)
|
||||||
df = df[1:]
|
else:
|
||||||
|
MarkdownGenerator.generate_table(table_items=table_items,add_pagebreak=args.pagebreak,title_template=args.title,first_colwidth=args.maxcol)
|
||||||
|
|
||||||
if args.title != None:
|
|
||||||
print(args.title.format(df.columns[1]),'\n')
|
|
||||||
|
|
||||||
print(df.to_markdown(tablefmt='grid', index=False, maxcolwidths=[args.maxcol,None]))
|
|
||||||
print('\n') # always add a newline after the table
|
|
||||||
|
|
||||||
if args.pagebreak:
|
|
||||||
print('\\pagebreak')
|
|
||||||
# MarkdownGenerator.generate(table_items,pagebreak=args.pagebreak,title=args.title,header_level=args.level)
|
# MarkdownGenerator.generate(table_items,pagebreak=args.pagebreak,title=args.title,header_level=args.level)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,13 +1,8 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import textwrap,itertools
|
import 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:
|
class MarkdownGenerator:
|
||||||
|
|
||||||
|
@ -15,11 +10,38 @@ class MarkdownGenerator:
|
||||||
def generate_tablerow() -> str:
|
def generate_tablerow() -> str:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def generate(ti,pagebreak = False,title = False,header_level = 1) -> str:
|
def generate_table(table_items,add_pagebreak = False,title_template = None,first_colwidth = 28):
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
import tabulate
|
||||||
|
|
||||||
|
# get the dataframe
|
||||||
|
df = pd.DataFrame(table_items)
|
||||||
|
|
||||||
|
# use first column for
|
||||||
|
df.columns = df.iloc[0]
|
||||||
|
df = df[1:]
|
||||||
|
|
||||||
|
if title_template != None:
|
||||||
|
print(title_template.format(df.columns[1]),'\n')
|
||||||
|
|
||||||
|
print(df.to_markdown(tablefmt='grid', index=False, maxcolwidths=[first_colwidth,None]))
|
||||||
|
print('\n') # always add a newline after the table
|
||||||
|
|
||||||
|
if add_pagebreak:
|
||||||
|
print('\\pagebreak')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def generate_table_legacy(table_items,add_pagebreak = False,title_template = None,first_colwidth = 28):
|
||||||
|
|
||||||
|
import textwrap
|
||||||
|
|
||||||
line_length = 128
|
line_length = 128
|
||||||
column_ratio= 0.28
|
column_ratio = float(first_colwidth) / 100
|
||||||
|
|
||||||
h_len = int(line_length * column_ratio)
|
h_len = int(line_length * column_ratio)
|
||||||
d_len = line_length-h_len
|
d_len = line_length-h_len
|
||||||
|
@ -27,8 +49,8 @@ class MarkdownGenerator:
|
||||||
#
|
#
|
||||||
# generate title (currently the first one)
|
# generate title (currently the first one)
|
||||||
#
|
#
|
||||||
if title:
|
if title_template != None:
|
||||||
print('#' * header_level,ti[0][1],'\n')
|
print(title_template.format(table_items[0][1]),'\n')
|
||||||
|
|
||||||
print(''.join(['+',"".ljust(h_len,'-'),'+',"".ljust(d_len,'-'),'+']))
|
print(''.join(['+',"".ljust(h_len,'-'),'+',"".ljust(d_len,'-'),'+']))
|
||||||
|
|
||||||
|
@ -40,7 +62,7 @@ class MarkdownGenerator:
|
||||||
|
|
||||||
# test if this affected by a third item!
|
# test if this affected by a third item!
|
||||||
|
|
||||||
for k,v in ti:
|
for k,v in table_items:
|
||||||
|
|
||||||
#
|
#
|
||||||
if v == None:
|
if v == None:
|
||||||
|
@ -79,5 +101,5 @@ class MarkdownGenerator:
|
||||||
|
|
||||||
|
|
||||||
# to control pagebreaks for pandoc
|
# to control pagebreaks for pandoc
|
||||||
if pagebreak:
|
if add_pagebreak:
|
||||||
print('\n\\newpage')
|
print('\n\\newpage')
|
|
@ -29,5 +29,9 @@ debug:
|
||||||
python ${coursebuilder} -s schema.yaml -m mod.cg.yaml mod.interactsys.yaml mod.test.yaml -p --title "## {}" -l de -f name credits goal content
|
python ${coursebuilder} -s schema.yaml -m mod.cg.yaml mod.interactsys.yaml mod.test.yaml -p --title "## {}" -l de -f name credits goal content
|
||||||
# | pandoc ${target_flags} -V lang:de -o ${target_de}
|
# | pandoc ${target_flags} -V lang:de -o ${target_de}
|
||||||
|
|
||||||
|
debug-legacy:
|
||||||
|
python ${coursebuilder} -s schema.yaml -m mod.cg.yaml mod.interactsys.yaml mod.test.yaml -p --legacy --title "## {}" -l de -f name credits goal content
|
||||||
|
# | pandoc ${target_flags} -V lang:de -o ${target_de}
|
||||||
|
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
Loading…
Add table
Add a link
Reference in a new issue