diff --git a/examples/verticalLines/vertical.pdf b/examples/verticalLines/vertical.pdf new file mode 100644 index 00000000..1047c314 Binary files /dev/null and b/examples/verticalLines/vertical.pdf differ 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()