From 58cdd2bce9aae98286762eb439d2900007245b5c Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Wed, 3 May 2023 20:55:46 +0200 Subject: [PATCH] feat: add begin document --- program/code/error_messages/error_arrays.py | 15 +++-- program/code/latex_classes/latex_classes.py | 25 ++++++- program/code/main.py | 45 ++++++++++--- .../tests/test_code/test_begin_document.py | 67 +++++++++++++++++++ .../tests/test_code/test_document_class.py | 8 +-- 5 files changed, 134 insertions(+), 26 deletions(-) create mode 100644 program/tests/test_code/test_begin_document.py diff --git a/program/code/error_messages/error_arrays.py b/program/code/error_messages/error_arrays.py index 88255b66..459e3caf 100644 --- a/program/code/error_messages/error_arrays.py +++ b/program/code/error_messages/error_arrays.py @@ -1,12 +1,13 @@ error_arrays = [ - "Error! No input given to document_class function", - "Error! documentclass curly bracket not closed!", + "Error! No input given to function", + "Error! curly bracket not closed!", "Error! documentclass has optional parameters!", - "Error! documentclass curly bracket not opened!", - "Error! documentclass command misspeled!", + "Error! curly bracket not opened!", + "Error! command misspeled!", "Error! class is not known!", - "Error! documentclass has no slash at begining" - ] + "Error! has no slash at begining", +] + def return_error_arrays(): - return error_arrays \ No newline at end of file + return error_arrays diff --git a/program/code/latex_classes/latex_classes.py b/program/code/latex_classes/latex_classes.py index 21f16015..616f1d46 100644 --- a/program/code/latex_classes/latex_classes.py +++ b/program/code/latex_classes/latex_classes.py @@ -1,9 +1,30 @@ """ Holds an array of all classes from https://ctan.org/topic/class """ -classes = ['article', 'report', 'book', 'beamer', 'letter', 'memoir', 'slides', 'IEEEtran', 'exam', 'amsart', 'amsbook', 'tufte-book', 'scrartcl', 'scrreprt', 'moderncv', 'cv', 'res', 'elsarticle', 'acm_proc_article-sp', 'scrbook'] +classes = [ + "article", + "report", + "book", + "beamer", + "letter", + "memoir", + "slides", + "IEEEtran", + "exam", + "amsart", + "amsbook", + "tufte-book", + "scrartcl", + "scrreprt", + "moderncv", + "cv", + "res", + "elsarticle", + "acm_proc_article-sp", + "scrbook", +] def return_latex_classes(): - """ Returns latex classes array""" + """Returns latex classes array""" return classes diff --git a/program/code/main.py b/program/code/main.py index 35628915..2106a09b 100644 --- a/program/code/main.py +++ b/program/code/main.py @@ -5,6 +5,18 @@ from code.latex_classes.latex_classes import return_latex_classes from code.error_messages.error_arrays import return_error_arrays +def generic_checks(latex_string): + error_arrays = return_error_arrays() + if latex_string == "": + print(latex_string + error_arrays[0]) + return "Error!" + if "}" not in latex_string: + print(latex_string + error_arrays[1]) + return "Error!" + if latex_string[0] != "\\": + print(latex_string + error_arrays[6]) + return "Error!" + def document_class(latex_string): r""" @@ -12,23 +24,34 @@ def document_class(latex_string): \documentclass{article} """ error_arrays = return_error_arrays() - latex_classes = return_latex_classes() - if latex_string == "": - print(error_arrays[0]) - return "Error!" - if "}" not in latex_string: - print(error_arrays[1]) + if generic_checks(latex_string) == "Error!": return "Error!" if latex_string[len("\\documentclass{") - 1] != "{": - print(error_arrays[3]) + print(latex_string + error_arrays[3]) return "Error!" - if latex_string[0] != "\\": - print(error_arrays[6]) + if latex_string[1 : (len("documentclass") + 1)] != "documentclass": + print(latex_string + error_arrays[4]) 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!" - return "" + return "" + + +def begin_document(latex_string): + r""" + Converts LaTeX begin document method to html + \begin{document} + """ + if generic_checks(latex_string) == "Error!": + return "Error!" + error_arrays = return_error_arrays() + if latex_string[1 : (len("begin") + 1)] != "begin": + print(latex_string + error_arrays[4]) + return "Error!" + return "" + if __name__ == "__main__": document_class("") diff --git a/program/tests/test_code/test_begin_document.py b/program/tests/test_code/test_begin_document.py new file mode 100644 index 00000000..0a1f8528 --- /dev/null +++ b/program/tests/test_code/test_begin_document.py @@ -0,0 +1,67 @@ +""" + Tests begin document function +""" +from code.main import begin_document + +# Write python tests for a function translating LaTeX documentclass to html +def given_empty_then_error(): + """ + Given: "" + When: N/A + Then: Error message + """ + assert begin_document("") == "Error!" + + +def given_not_closed_then_error(): + r""" + Given: "\\begin\{document" + When: N/A + Then: Error message + """ + assert begin_document(r"\begin{document") == "Error!" + + +def given_no_opening_then_error(): + """ + Given: No opening curly bracket + When: N/A + Then: Error message + """ + assert begin_document(r"\\begindocument}") == "Error!" + + +def given_misspeled_then_error(): + """ + Given: misspelled begin document + When: N/A + Then: Error message + """ + assert begin_document(r"\\begim{document}") == "Error!" + + +def given_no_slash_then_error(): + """ + Given: no backslash at start + When: N/A + Then: Error message + """ + assert begin_document(r"begin{document}") == "Error!" + + +def given_correct_then_html(): + """ + Given: \\documentclass{article} + When: N/A + Then: + """ + assert begin_document(r"\begin{document}") == "" + + +def test_begin_document(): + given_correct_then_html() + given_empty_then_error() + given_misspeled_then_error() + given_no_opening_then_error() + given_not_closed_then_error() + given_no_slash_then_error() diff --git a/program/tests/test_code/test_document_class.py b/program/tests/test_code/test_document_class.py index d145c277..ad1d9ae5 100644 --- a/program/tests/test_code/test_document_class.py +++ b/program/tests/test_code/test_document_class.py @@ -2,10 +2,6 @@ Tests document class function """ from code.main import document_class -from code.error_messages.error_arrays import return_error_arrays - -error_arrays = return_error_arrays() - # Write python tests for a function translating LaTeX documentclass to html def given_empty_then_error(): @@ -50,7 +46,7 @@ def given_misspeled_then_error(): When: N/A Then: Error message """ - assert document_class("\\documentclas{article}") == "Error!" + assert document_class("\\documentclasZ{article}") == "Error!" def given_class_not_recognized_then_error(): @@ -80,7 +76,7 @@ def given_correct_then_html(): When: N/A Then: """ - assert document_class("\\documentclass{article}") == "" + assert document_class("\\documentclass{article}") == "" def test_document_class():