From 39466ca0aad56d710094f37d38f6b95bfbd7ef2e Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Wed, 3 May 2023 20:29:08 +0200 Subject: [PATCH] feat: document_class function --- program/code/error_messages/__init__.py | 0 program/code/error_messages/error_arrays.py | 12 + program/code/latex_classes/latex_classes.py | 443 +----------------- program/code/main.py | 30 +- .../tests/test_code/test_document_class.py | 94 +++- 5 files changed, 125 insertions(+), 454 deletions(-) create mode 100644 program/code/error_messages/__init__.py create mode 100644 program/code/error_messages/error_arrays.py diff --git a/program/code/error_messages/__init__.py b/program/code/error_messages/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/program/code/error_messages/error_arrays.py b/program/code/error_messages/error_arrays.py new file mode 100644 index 00000000..88255b66 --- /dev/null +++ b/program/code/error_messages/error_arrays.py @@ -0,0 +1,12 @@ +error_arrays = [ + "Error! No input given to document_class function", + "Error! documentclass curly bracket not closed!", + "Error! documentclass has optional parameters!", + "Error! documentclass curly bracket not opened!", + "Error! documentclass command misspeled!", + "Error! class is not known!", + "Error! documentclass has no slash at begining" + ] + +def return_error_arrays(): + return error_arrays \ No newline at end of file diff --git a/program/code/latex_classes/latex_classes.py b/program/code/latex_classes/latex_classes.py index be216752..21f16015 100644 --- a/program/code/latex_classes/latex_classes.py +++ b/program/code/latex_classes/latex_classes.py @@ -1,446 +1,9 @@ """ Holds an array of all classes from https://ctan.org/topic/class """ -classes = [ - "abntex2", - "acmart", - "acmconf", - "acmsiggraph", - "actapress", - "adelaide-thesis", - "afthesis", - "aguplus", - "amsart", - "amsbook", - "amsproc", - "amsrefs", - "apa6", - "apa7", - "apa", - "arabbook", - "articleingud", - "asaetr", - "ascelike", - "assignment", - "beamer", - "bgteubner", - "bookman", - "booktabs", - "cascadilla", - "cascdoc", - "casconky", - "cd", - "charter", - "chem-journal", - "chicago", - "cje", - "cls-bst-ex", - "cmactex", - "cmbright", - "cmpj", - "coclass", - "codeanatomy", - "concepts", - "conference-omega", - "constitution", - "context", - "cooking-units", - "coseoul", - "crimson", - "cslatex", - "csthesis", - "dcu", - "dinbrief", - "docbook", - "dtk", - "dtucolor", - "dvgloss", - "easychair", - "ebsthesis", - "ecothesis", - "edge", - "egs", - "eiadoc", - "elsarticle", - "els-cas-templates", - "els-cas-templates-3.0", - "elsdoc", - "emisa", - "engtlc", - "erdc", - "eskd", - "eth-biblatex", - "ethz-thesis", - "europasscv", - "exercise", - "expdlist", - "fancyslides", - "fei", - "feiinput", - "fhnw", - "fithesis", - "fkssd", - "flashcards", - "flippdf", - "fontinst", - "fonts-tlwg", - "frankenstein", - "frege", - "gaceta", - "galois", - "gatech-thesis", - "gzt", - "hacm", - "hagenberg-thesis", - "har2nat", - "harvard", - "hecthese", - "hep", - "hepthesis", - "herm-pic", - "hithesis", - "holtpolt", - "html-book", - "hustthesis", - "hwk-mactex", - "ieeetran", - "ifacconf", - "ifmbe", - "igm", - "ijsra", - "illcdiss", - "imsproc", - "imsproc-accepted", - "imsproc-nrc", - "informs3", - "informs4", - "isomath", - "issuulinks", - "iwhdp", - "jacow", - "jmlr", - "jnlpba", - "jpsj", - "kluwer", - "koma-moderncvclassic", - "ksp-thesis", - "kurier", - "l2picfaq", - "laa", - "laboratorybook", - "latex2e-help-texinfo", - "latex2man", - "latex2rtf", - "latex4wp", - "latexbug", - "latexdiff", - "latexfileversion", - "latexindent", - "latexmp", - "latex-notes-zh-cn", - "latex-refsheet", - "latex-subfiles", - "latex-tabellen", - "latex-tools", - "latex-uni8", - "layout", - "lcyw", - "letter1", - "letter2", - "lettergen", - "lettre", - "lexikon", - "linenofix", - "lingmacros", - "linguex", - "listings-ext", - "logsys", - "longnamefilelist", - "lplfitch", - "lps", - "lt3graph", - "ltx2any", - "ltxdockit", - "luatexja", - "lxfonts", - "makecell", - "makecirc", - "makeplot", - "mandi", - "manfnt", - "manuscript", - "mathalfa", - "mathastext", - "mathdesign", - "mathdots", - "mathpartir", - "mathspec", - "mathspic", - "maths-symbols", - "maths-symbols-a4", - "matlab-prettifier", - "mdframed", - "memoir", - "mentis", - "mfnfss", - "mh", - "microtype", - "minifp", - "mla-paper", - "mlacls", - "mlbib", - "moderncv", - "moderntimeline", - "modular", - "mparhack", - "msc", - "mugsthesis", - "musixguit", - "nag", - "natbib", - "newlfm", - "newpx", - "newspaper", - "newtx", - "nicefilelist", - "nidanfloat", - "nih", - "ntgclass", - "nwafu", - "ocg-p", - "ociamthesis", - "ocr-b-outline", - "ocropus", - "octavo", - "odsfile", - "ojps", - "opteng", - "osa", - "overlays", - "oztex", - "pacioli", - "paper", - "parallel", - "phd", - "phfnote", - "philosophy-doc-ru", - "phonenumbers", - "physconst", - "physor2016", - "piff", - "pitex", - "pkuthss", - "placeins", - "plaintex", - "play", - "plipsum", - "poemscol", - "polynom", - "powerdot", - "pracjourn", - "proceedings", - "progress", - "prosper", - "pst-exa", - "pst-plot", - "publist", - "pxfonts", - "qcm", - "qobitree", - "quran", - "raman-thesis", - "rcs-multi", - "rechnung", - "refcheck", - "refman", - "resphilosophica", - "revquantum", - "rfc", - "rochester-thesis", - "romanbar", - "rotating", - "rsfso", - "rspublications", - "rticle", - "sageep", - "samplepdf", - "samplics", - "sansmath", - "sasnrdisplay", - "sasnrtext", - "schule", - "sciposter", - "scratch", - "sduthesis", - "seminar", - "seuthesis", - "sf298", - "sffms", - "sgame", - "shdoc", - "sig-alternate", - "sigcas", - "sigchi", - "siggraph", - "siggraph2015", - "siggraph-2016-schedule", - "siggraph-2017-schedule", - "siggraph-2018-schedule", - "siggraph-2019-schedule", - "siggraph-2020-schedule", - "siggraph-2021-schedule", - "siggraph-2022-schedule", - "siggraph-asia", - "siggraph-extended-abstracts", - "siggraph-talks", - "siggraph-videos", - "siggraph-virtual-reality-contest", - "sigirconf", - "simplecv", - "sistthesis", - "skaknew", - "slides", - "smalltalk", - "smfbook", - "spie", - "spin", - "splncs", - "statrep", - "stdclsdv", - "stdpage", - "stex", - "studenthandouts", - "subcaption", - "subeqn", - "subfiles", - "subfigure", - "subfigmat", - "suftesi", - "svclasses", - "svg-inkscape", - "swimgraf", - "swimte", - "sympytex", - "syntaxdi", - "tablists", - "tablor", - "tabriz-thesis", - "tabstackengine", - "tango", - "tcolorbox", - "tetex", - "tex", - "tex2tok", - "tex4ht", - "texapi", - "texdoc", - "texdraw", - "texfot", - "texi2dvi", - "texi2html", - "texi2roff", - "texinfo", - "texlive", - "texmacs", - "texmate", - "tex-overview", - "tex-ps", - "texsis", - "texsort", - "textcase", - "textfit", - "textglos", - "textmerg", - "textools", - "textpath", - "textpos", - "thucoursework", - "thucslide", - "thuthesis", - "tioga", - "tkz", - "toascii", - "tocloft", - "todonotes", - "tools", - "tpic2pdftex", - "tpx", - "trajan", - "tree-dvips", - "tufte-latex", - "turabian", - "turnstile", - "twoinone", - "uafthesis", - "ucthesis", - "ucsmonograph", - "ucthesis", - "udesoftec", - "uiucthesis", - "ukthesis", - "unamthesis", - "unamth-template", - "undthesis", - "unswcover", - "uothesis", - "upmethodology", - "uptex", - "uspatent", - "uowthesis", - "uowthesistitlepage", - "urlbst", - "utf8mex", - "uwthesis", - "uythesis", - "uzbek", - "vaucanson-g", - "vancouver", - "vandocument", - "varsfromjobname", - "vassiliev", - "verbasef", - "verse", - "vhistory", - "vita", - "vmargin", - "voss-mathmode", - "vpe", - "vrbexin", - "web-o-mints", - "widetable", - "williams", - "wordlike", - "wrapfig", - "wsemclassic", - "wsuipa", - "wtref", - "xassoccnt", - "xcite", - "xcomment", - "xdot", - "xepersian", - "xetex", - "xetexfontinfo", - "xfor", - "xgreek", - "xhfill", - "xifthen", - "xint", - "xkeyval", - "xmltex", - "xyling", - "xymtex", - "yathesis", - "yfonts", - "yhmath", - "youngtab", - "yplan", - "ytableau", - "ytext", - "ziffer", - "zhlipsum", -] +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 latex_classes(): + +def return_latex_classes(): """ Returns latex classes array""" return classes diff --git a/program/code/main.py b/program/code/main.py index 897b20b4..35628915 100644 --- a/program/code/main.py +++ b/program/code/main.py @@ -2,11 +2,33 @@ Program for converting Latex files into html files """ -from latex_classes.latex_classes import latex_classes +from code.latex_classes.latex_classes import return_latex_classes +from code.error_messages.error_arrays import return_error_arrays + def document_class(latex_string): - """ + r""" Converts LaTeX documentclass method to html + \documentclass{article} """ - print(latex_classes) - return latex_string + 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]) + return "Error!" + if latex_string[len("\\documentclass{") - 1] != "{": + print(error_arrays[3]) + return "Error!" + if latex_string[0] != "\\": + print(error_arrays[6]) + return "Error!" + document_type = latex_string[len("\\documentclass{"):(len(latex_string) - 1)] + if document_type not in latex_classes: + return f"Error! class {document_type} is not known!" + return "" + +if __name__ == "__main__": + document_class("") diff --git a/program/tests/test_code/test_document_class.py b/program/tests/test_code/test_document_class.py index 7b63df74..d145c277 100644 --- a/program/tests/test_code/test_document_class.py +++ b/program/tests/test_code/test_document_class.py @@ -2,19 +2,93 @@ 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 test_document_class(): +def given_empty_then_error(): """ - Given - When - Then + Given: "" + When: N/A + Then: Error message + """ + assert document_class("") == "Error!" + + +def given_not_closed_then_error(): + """ + Given: "\\documentclass{article" + When: N/A + Then: Error message + """ + assert document_class("\\documentclass{article") == "Error!" + + +def given_parameters_then_error(): + """ + Given: class parameters + When: N/A + Then: Error message + """ + assert document_class("\\documentclass[12pt]{article}") == "Error!" + + +def given_no_opening_then_error(): + """ + Given: No opening curly bracket + When: N/A + Then: Error message + """ + assert document_class("\\documentclassarticle}") == "Error!" + + +def given_misspeled_then_error(): + """ + Given: misspelled document class + When: N/A + Then: Error message + """ + assert document_class("\\documentclas{article}") == "Error!" + + +def given_class_not_recognized_then_error(): + """ + Given: class not recognized + When: N/A + Then: Error message + """ + assert ( + document_class("\\documentclass{idonotexist}") + == "Error! class idonotexist is not known!" + ) + + +def given_no_slash_then_error(): + """ + Given: no backslash at start + When: N/A + Then: Error message + """ + assert document_class("documentclass{article}") == "Error!" + + +def given_correct_then_html(): + """ + Given: \\documentclass{article} + When: N/A + Then: """ - assert document_class("") == "Error! No input given to document_class function" assert document_class("\\documentclass{article}") == "" - assert document_class("\\documentclass{article") == "Error! documentclass curly bracket not closed!" - assert document_class("\\documentclass[12pt]{article}") == "Error! documentclass has optional parameters!" - assert document_class("\\documentclassarticle}") == "Error! documentclass curly bracket not opened!" - assert document_class("\\documentclas{article}") == "Error! documentclass command misspeled!" - assert document_class("\\documentclass{idonotexist}") == "Error! class idonotexist is not known!" + + +def test_document_class(): + given_correct_then_html() + given_class_not_recognized_then_error() + given_empty_then_error() + given_misspeled_then_error() + given_no_opening_then_error() + given_not_closed_then_error() + given_no_slash_then_error() + given_parameters_then_error()