feat: table insides and outsides

This commit is contained in:
Krzysztof Rudnicki 2023-05-31 02:14:25 +02:00
parent bdce763077
commit 82e3bf8ec7
10 changed files with 185 additions and 13 deletions

Binary file not shown.

View File

@ -0,0 +1,8 @@
\documentclass{article}
\begin{document}
\begin{tabular}{ l | c | r }
test & 2 & test \\
4 & 5 & 6 \\
\end{tabular}
\end{document}

View File

@ -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, "<hr>")
replaced_newline = replaced_hline.replace('\newline', "<br>")
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 = "<html> <table>"
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 += "<tr>"
columns = split_columns(row, column_amount)
column_number = 0
for column in columns:
return_string += "<td "
current_style = column_style[column_number]
while current_style == line_string:
return_string += line_string
column_number += 1
current_style = column_style[column_number]
return_string += column_style[column_number] + ">"
return_string += translate_column(column)
column_number += 1
return_string += "</td>"
return_string += "</tr>"
return_string += " </table> </html>"
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("")

0
program/code/texfile.tex Normal file
View File

View File

@ -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()

View File

@ -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()

View File

@ -26,7 +26,7 @@ def given_p_then_array():
Then: <!DOCTYPE html><html>
"""
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: <!DOCTYPE html><html>
"""
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: <!DOCTYPE html><html>
"""
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():

View File

@ -129,18 +129,18 @@ def given_filled_p_brackets_then_correct():
Then: <!DOCTYPE html><html>
"""
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'"
]

View File

@ -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") == "<hr>"
def given_just_newline_then_correct():
assert translate_column('\newline') == "<br>"
def given_all_then_correct():
assert translate_column(
'\\hline \newline hline newline test') == "<hr> <br> 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()

View File

@ -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) == "<html> <table><tr><td align='left'>test </td><td style='border-left: 1px solid black'align='center'> 2 </td><td style='border-left: 1px solid black'align='right'> test </td></tr><tr><td align='left'> 4 </td><td style='border-left: 1px solid black'align='center'> 5 </td><td style='border-left: 1px solid black'align='right'> 6 </td></tr><tr><td align='left'></td></tr> </table> </html>"
def test_translate_inside_to_html():
given_correct_then_correct()