feat: add begin document

This commit is contained in:
Krzysztof Rudnicki 2023-05-03 20:55:46 +02:00
parent 02463d0829
commit 7562f3e06c
5 changed files with 134 additions and 26 deletions

View File

@ -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
return error_arrays

View File

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

View File

@ -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 "<!DOCTYPE html><html>"
return "<!DOCTYPE html>"
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 "<html>"
if __name__ == "__main__":
document_class("")

View File

@ -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: <!DOCTYPE html><html>
"""
assert begin_document(r"\begin{document}") == "<html>"
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()

View File

@ -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: <!DOCTYPE html><html>
"""
assert document_class("\\documentclass{article}") == "<!DOCTYPE html><html>"
assert document_class("\\documentclass{article}") == "<!DOCTYPE html>"
def test_document_class():