From 82e3bf8ec75675c76b45615595f12f6ef7fd753b Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Wed, 31 May 2023 02:14:25 +0200 Subject: [PATCH] feat: table insides and outsides --- examples/verticalLines/vertical.pdf | Bin 0 -> 3185 bytes examples/verticalLines/vertical.tex | 8 ++ program/code/main.py | 77 ++++++++++++++++-- program/code/texfile.tex | 0 program/tests/test_code/test_split_columns.py | 29 +++++++ program/tests/test_code/test_split_rows.py | 25 ++++++ .../test_tabular_columns_parameters.py | 6 +- .../test_tabular_required_parameters.py | 10 +-- .../tests/test_code/test_translate_column.py | 30 +++++++ .../test_translate_inside_to_html.py | 13 +++ 10 files changed, 185 insertions(+), 13 deletions(-) create mode 100644 examples/verticalLines/vertical.pdf create mode 100644 examples/verticalLines/vertical.tex create mode 100644 program/code/texfile.tex create mode 100644 program/tests/test_code/test_split_columns.py create mode 100644 program/tests/test_code/test_split_rows.py create mode 100644 program/tests/test_code/test_translate_column.py create mode 100644 program/tests/test_code/test_translate_inside_to_html.py diff --git a/examples/verticalLines/vertical.pdf b/examples/verticalLines/vertical.pdf new file mode 100644 index 0000000000000000000000000000000000000000..1047c314cad5927de9b8be55f40240880468149e GIT binary patch literal 3185 zcma)8XHXO97KPA*^b!$-(3_+|AOWN-J@gV#5D0;U9w3CS^e#mKK}wL`Ty{~AE(!`t z7gvfDk+uQ`6h%<#b7$Vn?!Mi(@BO>qop0`)d*gfCm9;Mj&}oyub)JTuTcWOd%88{DI-KNjG{idb(!} z_M`ic;|8RZO=Q3jMuB+&Pg#;3Z2;2wX`i~OsARe2&Kjp7G**IRq0g3NH3+vc?Isxo zYWpM&n*Dv z-0_%90kQC>2Wn=C@(rUw&=|0tTzjbm=OEyRgzmxn6Aku5A_)?C#9wgeT44+E$OesL zl8G8{Q0qXqMY+@HsUQ8RIHppSq5T?zO?@6>kDxn%b}kZKe}5h7x*3)aB#=D*J>B={ zH#p$1{|gt?pKx)$x#*h!<9@bythjy8oj=BWE{?Uh8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ZRB;I#Yxj~0QvYNQDing+vPgL{ zLDQ6qToRkz4F>#;va9^A?En3qfTRAX;J!FI`XmOOkuqL7I!+?}m%mm;{a*Ape}G!z zeQYWIPzx~nyHSvXDPW}XKR5;w{U?{swDk`thH;%xCbRpF8Em?m)ESJTGuS@m# zGd@jD*0r~ZD=UFB?*>CUGBVFy2=D{s#m{-|{%n0k+=b&%vC9W~F=P#j=L4xr2FSrF z&T2WLCU!9Vs)l_AK$|~K*t*YRO-@hOJg|fkxvRxK%rw_#Su@=2;<%uGV|2yv%bS$` z(6)|~&et36SI8n;c2j~u{!53!?yrKJwccmlz7Sw`O?1TOxGI?zMQu=rHutJaOOS&_ zUz%gFb(I9Rw6Nls`l6~a#i9mh{<*bJq9S6iA1+jow6*LGg~y5Jc;#>FaNh@?QduB# zw&vQs>|S=0)S>1AQRS%7?a!~eV<`!c$$6WLWTw97Y~1a`-Z34@X){HJC9=&3MSif8 zK+sgIQN(!Crks38C240X_v=!zi~6CU*yN*HPoBHoR`H^ikD63041_mMsyD*YPjZVV z=AIjc(Si%t^Sp2|GY3J{@LOeD!Z(jk#CEe0vMqqD0)=S$a#k72U%3iL{s&nv*${|O z6MZL56+FVj!vpD#N1+jD6cK??L#rW?ZbT&9Llvc_g3|imKb)a90Ra>s{Cf)}*p!j7bpJwnxzs^3 z`r7>>TpT)~3SBHIOVyjll^U5O6 literal 0 HcmV?d00001 diff --git a/examples/verticalLines/vertical.tex b/examples/verticalLines/vertical.tex new file mode 100644 index 00000000..81ca65c5 --- /dev/null +++ b/examples/verticalLines/vertical.tex @@ -0,0 +1,8 @@ +\documentclass{article} + +\begin{document} +\begin{tabular}{ l | c | r } +test & 2 & test \\ +4 & 5 & 6 \\ +\end{tabular} +\end{document} \ No newline at end of file diff --git a/program/code/main.py b/program/code/main.py index ffa5507f..cdab74f1 100644 --- a/program/code/main.py +++ b/program/code/main.py @@ -132,7 +132,7 @@ def tabular_required_parameters(latex_string): "l": "align='left'", "c": "align='center'", "r": "align='right'", - "|": "style=\"border-left: 1px solid black" + "|": "style='border-left: 1px solid black'" } if only_pipes_and_space(latex_string): print("tabular_required_parameters, required table parameters are only pipes and spaces!:", latex_string) @@ -190,14 +190,81 @@ def tabular_columns_parameters(latex_string): return "Error!" print(length_value, conversed_unit, conversed_unit[0]) final_length = round(float(length_value) * conversed_unit[0], 2) - return_string = "style=\"" + vertical_align_type + \ - " width: " + str(final_length) + conversed_unit[1] + ";\"" + return_string = "style='" + vertical_align_type + \ + " width: " + str(final_length) + conversed_unit[1] + ";'" return return_string -def parameter_arguments(latex_string): - return latex_string +def split_rows(latex_string): + double_backslash = "\\" + rows = latex_string.split(double_backslash) + print(rows) + return rows + + +def split_columns(table_row, column_count): + columns = table_row.split("&") + if len(columns) != column_count and columns != ['']: + print( + f"split_columns, table_row: {table_row} has different amount of columns than expected: {column_count}") + return "Error!" + return columns + + +def translate_column(latex_column): + hline_string_literal = "\hline" + replaced_hline = latex_column.replace(hline_string_literal, "
") + replaced_newline = replaced_hline.replace('\newline', "
") + print(latex_column, replaced_newline, + latex_column.find(hline_string_literal), hline_string_literal) + return replaced_newline + + +def translate_inside_to_html(latex_table_inside, column_style): + return_string = " " + column_amount = 0 + line_string = "style='border-left: 1px solid black'" + for style in column_style: + print(style) + if style != line_string: + column_amount += 1 + rows = split_rows(latex_table_inside) + for row in rows: + return_string += "" + + columns = split_columns(row, column_amount) + column_number = 0 + for column in columns: + return_string += "" + + return_string += "" + return_string += "
" + return_string += translate_column(column) + column_number += 1 + return_string += "
" + print(return_string) + return return_string + + +def read_file(tex_filename: string): + tex_file = open(tex_filename, "r") + data = tex_file.read() + tex_file.close() + return data if __name__ == "__main__": + tex_filename = "texfile.tex" + data = read_file(tex_filename) + document_class_index = data.find("\documentclass") + if document_class_index == -1: + print("Main function error! documentclass not found") + return "Error!" document_class("") diff --git a/program/code/texfile.tex b/program/code/texfile.tex new file mode 100644 index 00000000..e69de29b diff --git a/program/tests/test_code/test_split_columns.py b/program/tests/test_code/test_split_columns.py new file mode 100644 index 00000000..30f6012d --- /dev/null +++ b/program/tests/test_code/test_split_columns.py @@ -0,0 +1,29 @@ +from code.main import split_columns + + +def given_empty_then_correct(): + assert split_columns("", 0) == [''] + + +def given_no_and_then_correct(): + assert split_columns("estsegsegtswegseg", 1) == ['estsegsegtswegseg'] + + +def given_single_and_then_correct(): + assert split_columns("&", 2) == ['', ''] + + +def given_too_much_columns_then_error(): + assert split_columns("test & 2 & test", 1) == "Error!" + + +def given_default_then_correct(): + assert split_columns("test & 2 & test", 3) == ["test ", " 2 ", " test"] + + +def test_split_columns(): + given_empty_then_correct() + given_no_and_then_correct() + given_single_and_then_correct() + given_too_much_columns_then_error() + given_single_and_then_correct() diff --git a/program/tests/test_code/test_split_rows.py b/program/tests/test_code/test_split_rows.py new file mode 100644 index 00000000..98c9e43e --- /dev/null +++ b/program/tests/test_code/test_split_rows.py @@ -0,0 +1,25 @@ +from code.main import split_rows + + +def given_empty_then_correct(): + assert split_rows("") == [''] + + +def given_no_slash_then_correct(): + assert split_rows("estsegsegtswegseg") == ["estsegsegtswegseg"] + + +def given_double_slash_then_correct(): + assert split_rows("\\") == ['', ''] + + +def given_actual_string_then_correct(): + actual_string = "test & 2 & test \\ 4 & 5 & 6" + assert split_rows(actual_string) == ["test & 2 & test ", " 4 & 5 & 6"] + + +def test_split_rows(): + given_empty_then_correct() + given_no_slash_then_correct() + given_double_slash_then_correct() + given_actual_string_then_correct() diff --git a/program/tests/test_code/test_tabular_columns_parameters.py b/program/tests/test_code/test_tabular_columns_parameters.py index fcf5c338..07a91130 100644 --- a/program/tests/test_code/test_tabular_columns_parameters.py +++ b/program/tests/test_code/test_tabular_columns_parameters.py @@ -26,7 +26,7 @@ def given_p_then_array(): Then: """ assert tabular_columns_parameters( - "p{1.3pt}") == "style=\"vertical-align: top; width: 1.69px;\"" + "p{1.3pt}") == "style='vertical-align: top; width: 1.69px;'" def given_m_then_array(): @@ -36,7 +36,7 @@ def given_m_then_array(): Then: """ assert tabular_columns_parameters( - "m{1.3pt}") == "style=\"vertical-align: middle; width: 1.69px;\"" + "m{1.3pt}") == "style='vertical-align: middle; width: 1.69px;'" def given_b_then_array(): @@ -46,7 +46,7 @@ def given_b_then_array(): Then: """ assert tabular_columns_parameters( - "b{1.3pt}") == "style=\"vertical-align: bottom; width: 1.69px;\"" + "b{1.3pt}") == "style='vertical-align: bottom; width: 1.69px;'" def test_tabular_columns_parameters(): diff --git a/program/tests/test_code/test_tabular_required_parameters.py b/program/tests/test_code/test_tabular_required_parameters.py index 2141e18f..fb6623ae 100644 --- a/program/tests/test_code/test_tabular_required_parameters.py +++ b/program/tests/test_code/test_tabular_required_parameters.py @@ -129,18 +129,18 @@ def given_filled_p_brackets_then_correct(): Then: """ assert tabular_required_parameters("{p{1.3pt}}") == [ - "style=\"vertical-align: top; width: 1.69px;\""] + "style='vertical-align: top; width: 1.69px;'"] def given_complicated_then_correct(): assert tabular_required_parameters("{|lp{1.3pt}c|r|}") == [ - "style=\"border-left: 1px solid black", + "style='border-left: 1px solid black'", "align='left'", - "style=\"vertical-align: top; width: 1.69px;\"", + "style='vertical-align: top; width: 1.69px;'", "align='center'", - "style=\"border-left: 1px solid black", + "style='border-left: 1px solid black'", "align='right'", - "style=\"border-left: 1px solid black" + "style='border-left: 1px solid black'" ] diff --git a/program/tests/test_code/test_translate_column.py b/program/tests/test_code/test_translate_column.py new file mode 100644 index 00000000..493741af --- /dev/null +++ b/program/tests/test_code/test_translate_column.py @@ -0,0 +1,30 @@ +from code.main import translate_column + + +def given_empty_then_correct(): + assert translate_column("") == "" + + +def given_plain_text_then_correct(): + assert translate_column("plain text") == "plain text" + + +def given_just_hline_then_correct(): + assert translate_column(r"\hline") == "
" + + +def given_just_newline_then_correct(): + assert translate_column('\newline') == "
" + + +def given_all_then_correct(): + assert translate_column( + '\\hline \newline hline newline test') == "

hline newline test" + + +def test_translate_column(): + given_empty_then_correct() + given_plain_text_then_correct() + given_just_hline_then_correct() + given_just_newline_then_correct() + given_all_then_correct() diff --git a/program/tests/test_code/test_translate_inside_to_html.py b/program/tests/test_code/test_translate_inside_to_html.py new file mode 100644 index 00000000..5979c737 --- /dev/null +++ b/program/tests/test_code/test_translate_inside_to_html.py @@ -0,0 +1,13 @@ +from code.main import translate_inside_to_html +from code.main import tabular_required_parameters + + +def given_correct_then_correct(): + latex_string = "test & 2 & test \\ 4 & 5 & 6 \\" + parameters_string = "{ l | c | r }" + column_styles = tabular_required_parameters(parameters_string) + assert translate_inside_to_html(latex_string, column_styles) == "
test 2 test
4 5 6
" + + +def test_translate_inside_to_html(): + given_correct_then_correct()