mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 14:23:07 +02:00
feat: impmenet tabular_columns_parameters
This commit is contained in:
parent
443cc3b0fe
commit
25f3b5d995
@ -1,7 +1,7 @@
|
||||
\documentclass{article}
|
||||
\begin{document}
|
||||
|
||||
\begin{tabular}{|p{20cm}}
|
||||
\begin{tabular}{|}
|
||||
test \\
|
||||
test
|
||||
\end{tabular}
|
||||
|
||||
@ -5,13 +5,15 @@
|
||||
from code.latex_classes.latex_classes import return_latex_classes
|
||||
from code.error_messages.error_arrays import return_error_arrays
|
||||
|
||||
|
||||
def command_name_check(latex_string, command_name):
|
||||
error_arrays = return_error_arrays()
|
||||
if latex_string[1 : (len(command_name) + 1)] != command_name:
|
||||
if latex_string[1: (len(command_name) + 1)] != command_name:
|
||||
print(latex_string + error_arrays[4])
|
||||
return "Error!"
|
||||
return
|
||||
|
||||
|
||||
def generic_checks(latex_string):
|
||||
error_arrays = return_error_arrays()
|
||||
if latex_string == "":
|
||||
@ -22,6 +24,7 @@ def generic_checks(latex_string):
|
||||
return "Error!"
|
||||
return
|
||||
|
||||
|
||||
def generic_checks_command(latex_string):
|
||||
error_arrays = return_error_arrays()
|
||||
if generic_checks(latex_string) == "Error!":
|
||||
@ -45,7 +48,8 @@ def document_class(latex_string):
|
||||
return "Error!"
|
||||
if command_name_check(latex_string, "documentclass") == "Error!":
|
||||
return "Error!"
|
||||
document_type = latex_string[len("\\documentclass{") : (len(latex_string) - 1)]
|
||||
document_type = latex_string[len(
|
||||
"\\documentclass{"): (len(latex_string) - 1)]
|
||||
latex_classes = return_latex_classes()
|
||||
if document_type not in latex_classes:
|
||||
return f"Error! class {document_type} is not known!"
|
||||
@ -63,6 +67,7 @@ def begin_document(latex_string):
|
||||
return "Error!"
|
||||
return "<html>"
|
||||
|
||||
|
||||
def begin_tabular(latex_string):
|
||||
r"""
|
||||
Checks if LaTeX begin tabular method is correct
|
||||
@ -73,6 +78,7 @@ def begin_tabular(latex_string):
|
||||
return "Error!"
|
||||
return "<table>"
|
||||
|
||||
|
||||
def tabular_parameters(latex_string):
|
||||
r"""
|
||||
Checks if LaTeX tabular environment has any parameters
|
||||
@ -83,11 +89,22 @@ def tabular_parameters(latex_string):
|
||||
return ""
|
||||
return "Error!"
|
||||
|
||||
def tabular_columns_parameters(latex_string):
|
||||
|
||||
def tabular_required_parameters(latex_string):
|
||||
if generic_checks(latex_string) == "Error!":
|
||||
return "Error!"
|
||||
if latex_string == "{}":
|
||||
print("tabular_required_parameters, required table parameters are empty!:", latex_string)
|
||||
return "Error!"
|
||||
simple_parameters_dictionary = {
|
||||
"l": "align='left'",
|
||||
"c": "align='center'",
|
||||
"r": "align='right'",
|
||||
"|": "style=\"border-left: 1px solid black"
|
||||
}
|
||||
return latex_string
|
||||
|
||||
|
||||
def length_conversions(latex_length):
|
||||
length_dictionary = {
|
||||
"pt": [1.3, "px"],
|
||||
@ -104,8 +121,48 @@ def length_conversions(latex_length):
|
||||
}
|
||||
return length_dictionary.get(latex_length, "Error!")
|
||||
|
||||
|
||||
def tabular_columns_parameters(latex_string):
|
||||
if generic_checks(latex_string) == "Error!":
|
||||
return "Error!"
|
||||
parameter_dictionary = {
|
||||
"p": "vertical-align: top;",
|
||||
"m": "vertical-align: middle;",
|
||||
"b": "vertical-align: bottom;"
|
||||
}
|
||||
vertical_align_type = parameter_dictionary.get(latex_string[0], "Error!")
|
||||
if vertical_align_type == "Error!":
|
||||
print("tabular_columns_parameters, unknown parameter: ",
|
||||
latex_string[0])
|
||||
return "Error!"
|
||||
if latex_string[1] != '{':
|
||||
print(
|
||||
"tabular_columns_parameters, parameter length does not start with {", latex_string)
|
||||
return "Error!"
|
||||
length_parameter_with_bracket = latex_string.partition("{")[2]
|
||||
length_parameter = length_parameter_with_bracket.partition("}")[0]
|
||||
length_value = ""
|
||||
i = 0
|
||||
for character in length_parameter:
|
||||
if character.isalpha():
|
||||
break
|
||||
length_value += character
|
||||
i += 1
|
||||
length_unit = length_parameter[i:]
|
||||
conversed_unit = length_conversions(length_unit)
|
||||
if conversed_unit == "Error!":
|
||||
print("tabular_columns_parameters, Unit could not be conversed!", 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 return_string
|
||||
|
||||
|
||||
def parameter_arguments(latex_string):
|
||||
return latex_string
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
document_class("")
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
from code.main import length_conversions
|
||||
|
||||
# Write python tests for a function translating LaTeX documentclass to html
|
||||
|
||||
|
||||
def given_empty_then_error():
|
||||
"""
|
||||
Given: ""
|
||||
@ -12,6 +14,7 @@ def given_empty_then_error():
|
||||
"""
|
||||
assert length_conversions("") == "Error!"
|
||||
|
||||
|
||||
def given_pt_then_px():
|
||||
"""
|
||||
Given: ""
|
||||
@ -20,6 +23,7 @@ def given_pt_then_px():
|
||||
"""
|
||||
assert length_conversions("pt") == [1.3, "px"]
|
||||
|
||||
|
||||
def given_mm_then_mm():
|
||||
"""
|
||||
Given: ""
|
||||
@ -28,6 +32,7 @@ def given_mm_then_mm():
|
||||
"""
|
||||
assert length_conversions("mm") == [1, "mm"]
|
||||
|
||||
|
||||
def given_cm_then_cm():
|
||||
"""
|
||||
Given: ""
|
||||
@ -36,6 +41,7 @@ def given_cm_then_cm():
|
||||
"""
|
||||
assert length_conversions("cm") == [1, "cm"]
|
||||
|
||||
|
||||
def given_in_then_in():
|
||||
"""
|
||||
Given: ""
|
||||
@ -44,6 +50,7 @@ def given_in_then_in():
|
||||
"""
|
||||
assert length_conversions("in") == [1, "in"]
|
||||
|
||||
|
||||
def given_ex_then_ex():
|
||||
"""
|
||||
Given: ""
|
||||
@ -52,6 +59,7 @@ def given_ex_then_ex():
|
||||
"""
|
||||
assert length_conversions("ex") == [1, "ex"]
|
||||
|
||||
|
||||
def given_em_then_em():
|
||||
"""
|
||||
Given: ""
|
||||
@ -60,6 +68,7 @@ def given_em_then_em():
|
||||
"""
|
||||
assert length_conversions("em") == [1, "em"]
|
||||
|
||||
|
||||
def given_mu_then_error():
|
||||
"""
|
||||
Given: ""
|
||||
@ -68,6 +77,7 @@ def given_mu_then_error():
|
||||
"""
|
||||
assert length_conversions("mu") == "Error!"
|
||||
|
||||
|
||||
def given_sp_then_error():
|
||||
"""
|
||||
Given: ""
|
||||
@ -76,13 +86,15 @@ def given_sp_then_error():
|
||||
"""
|
||||
assert length_conversions("sp") == "Error!"
|
||||
|
||||
|
||||
def given_unknown_then_error():
|
||||
"""
|
||||
Given: ""
|
||||
When: N/A
|
||||
Then: Error message
|
||||
"""
|
||||
assert length_conversions("unknown") == "Error!"
|
||||
assert length_conversions("unknown") == "Error!"
|
||||
|
||||
|
||||
def test_begin_tabular():
|
||||
given_empty_then_error()
|
||||
@ -95,4 +107,3 @@ def test_begin_tabular():
|
||||
given_mu_then_error()
|
||||
given_sp_then_error()
|
||||
given_unknown_then_error()
|
||||
|
||||
|
||||
@ -1,49 +0,0 @@
|
||||
from code.main import parameter_arguments
|
||||
|
||||
def given_empty_then_error():
|
||||
"""
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("") == "Error!"
|
||||
|
||||
def given_unknown_then_error():
|
||||
"""
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("l{1.3cm}") == "Error!"
|
||||
|
||||
def given_p_then_array():
|
||||
"""
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("p{1.3pt}") == "style=\"vertical-align: top; width: 1.69px;\""
|
||||
|
||||
def given_m_then_array():
|
||||
"""
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("m{1.3pt}") == "style=\"vertical-align: middle; width: 1.69px;\""
|
||||
|
||||
def given_b_then_array():
|
||||
"""
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("b{1.3pt}") == "style=\"vertical-align: bottom; width: 1.69px;\""
|
||||
|
||||
|
||||
def test_parameter_arguments():
|
||||
given_empty_then_empty()
|
||||
given_unknown_then_error()
|
||||
given_p_then_array()
|
||||
given_m_then_array()
|
||||
given_b_then_array()
|
||||
@ -1,10 +1,7 @@
|
||||
"""
|
||||
Tests tabular_parameters function
|
||||
"""
|
||||
|
||||
from code.main import tabular_columns_parameters
|
||||
|
||||
def given_empty_then_empty():
|
||||
|
||||
def given_empty_then_error():
|
||||
"""
|
||||
Given:
|
||||
When: N/A
|
||||
@ -12,97 +9,49 @@ def given_empty_then_empty():
|
||||
"""
|
||||
assert tabular_columns_parameters("") == "Error!"
|
||||
|
||||
def given_empty_brackets_then_empty():
|
||||
|
||||
def given_unknown_then_error():
|
||||
"""
|
||||
Given: {}
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{}") == "Error!"
|
||||
assert tabular_columns_parameters("l{1.3cm}") == "Error!"
|
||||
|
||||
def given_c_then_array():
|
||||
|
||||
def given_p_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{c}") == ["align='center'"]
|
||||
assert tabular_columns_parameters(
|
||||
"p{1.3pt}") == "style=\"vertical-align: top; width: 1.69px;\""
|
||||
|
||||
def given_l_then_array():
|
||||
|
||||
def given_m_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{l}") == ["align='left'"]
|
||||
assert tabular_columns_parameters(
|
||||
"m{1.3pt}") == "style=\"vertical-align: middle; width: 1.69px;\""
|
||||
|
||||
def given_r_then_array():
|
||||
|
||||
def given_b_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
Given:
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{r}") == ["align='right'"]
|
||||
|
||||
def given_line_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("|") == ["style=\"border-left: 1px solid black;\""]
|
||||
|
||||
def given_double_line_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("||") == ["style=\"border-left: 1px solid black;\"", "style=\"border-left: 1px solid black;\""]
|
||||
|
||||
def given_c_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{c}") == ["c"]
|
||||
|
||||
def given_c_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{c}") == ["c"]
|
||||
|
||||
def given_c_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{c}") == ["c"]
|
||||
|
||||
def given_c_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{c}") == ["c"]
|
||||
|
||||
def given_c_then_array():
|
||||
"""
|
||||
Given: [c]
|
||||
When: N/A
|
||||
Then: <!DOCTYPE html><html>
|
||||
"""
|
||||
assert tabular_columns_parameters("{c}") == ["c"]
|
||||
|
||||
assert tabular_columns_parameters(
|
||||
"b{1.3pt}") == "style=\"vertical-align: bottom; width: 1.69px;\""
|
||||
|
||||
|
||||
def test_tabular_columns_parameters():
|
||||
given_empty_then_empty()
|
||||
given_empty_brackets_then_empty()
|
||||
given_c_then_array()
|
||||
given_empty_then_error()
|
||||
given_unknown_then_error()
|
||||
given_p_then_array()
|
||||
given_m_then_array()
|
||||
given_b_then_array()
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
from code.main import tabular_required_parameters
|
||||
|
||||
|
||||
def given_empty_then_error():
|
||||
"""
|
||||
Given:
|
||||
@ -12,6 +13,7 @@ def given_empty_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("") == "Error!"
|
||||
|
||||
|
||||
def given_empty_brackets_then_error():
|
||||
"""
|
||||
Given: {}
|
||||
@ -20,6 +22,7 @@ def given_empty_brackets_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("{}") == "Error!"
|
||||
|
||||
|
||||
def given_just_line_then_error():
|
||||
"""
|
||||
Given: {}
|
||||
@ -37,6 +40,7 @@ def given_just_lines_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("{|||||||}") == "Error!"
|
||||
|
||||
|
||||
def given_single_left_then_correct():
|
||||
"""
|
||||
Given: {l}
|
||||
@ -45,6 +49,7 @@ def given_single_left_then_correct():
|
||||
"""
|
||||
assert tabular_required_parameters("{l}") == ['l']
|
||||
|
||||
|
||||
def given_single_center_then_correct():
|
||||
"""
|
||||
Given: {c}
|
||||
@ -53,6 +58,7 @@ def given_single_center_then_correct():
|
||||
"""
|
||||
assert tabular_required_parameters("{c}") == ['c']
|
||||
|
||||
|
||||
def given_single_right_then_correct():
|
||||
"""
|
||||
Given: {r}
|
||||
@ -61,6 +67,7 @@ def given_single_right_then_correct():
|
||||
"""
|
||||
assert tabular_required_parameters("{r}") == ['r']
|
||||
|
||||
|
||||
def given_empty_wrap_p_then_error():
|
||||
"""
|
||||
Given: {r}
|
||||
@ -69,6 +76,7 @@ def given_empty_wrap_p_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("{p}") == "Error!"
|
||||
|
||||
|
||||
def given_empty_wrap_m_then_error():
|
||||
"""
|
||||
Given: {r}
|
||||
@ -77,6 +85,7 @@ def given_empty_wrap_m_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("{m}") == "Error!"
|
||||
|
||||
|
||||
def given_empty_wrap_b_then_error():
|
||||
"""
|
||||
Given: {r}
|
||||
@ -85,6 +94,7 @@ def given_empty_wrap_b_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("{b}") == "Error!"
|
||||
|
||||
|
||||
def given_empty_wrap_p_brackets_then_error():
|
||||
"""
|
||||
Given: {r}
|
||||
@ -93,6 +103,7 @@ def given_empty_wrap_p_brackets_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("{p{}}") == "Error!"
|
||||
|
||||
|
||||
def given_empty_wrap_m_brackets_then_error():
|
||||
"""
|
||||
Given: {r}
|
||||
@ -101,6 +112,7 @@ def given_empty_wrap_m_brackets_then_error():
|
||||
"""
|
||||
assert tabular_required_parameters("{m{}}") == "Error!"
|
||||
|
||||
|
||||
def given_empty_wrap_b_brackets_then_error():
|
||||
"""
|
||||
Given: {r}
|
||||
@ -110,7 +122,6 @@ def given_empty_wrap_b_brackets_then_error():
|
||||
assert tabular_required_parameters("{b{}}") == "Error!"
|
||||
|
||||
|
||||
|
||||
def test_tabular_required_parameters():
|
||||
given_empty_then_error()
|
||||
given_empty_brackets_then_error()
|
||||
@ -124,4 +135,4 @@ def test_tabular_required_parameters():
|
||||
given_empty_wrap_b_then_error()
|
||||
given_empty_wrap_p_brackets_then_error()
|
||||
given_empty_wrap_m_brackets_then_error()
|
||||
given_empty_wrap_b_brackets_then_error()
|
||||
given_empty_wrap_b_brackets_then_error()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user