diff --git a/exam_template/Makefile b/exam_template/Makefile index c39fe29..afa885c 100644 --- a/exam_template/Makefile +++ b/exam_template/Makefile @@ -4,7 +4,12 @@ default: python pdflatex main.tex +SRC_DIR := $(shell find graphics/src -type f -printf "%f\n" -name *.txt | cut -d '.' -f1) + python: - python python/Hasse.py graphics/src/example1.txt graphics/example1.tex - python python/FSA.py graphics/src/example2.txt graphics/example2.tex - python python/Truthtable.py graphics/src/example3.txt graphics/example3.tex + @for f in $(SRC_DIR); \ + do \ + echo -e "\033[33mCOMPILING $$f.tex\033[0m"; \ + python ../exam_template/python/run.py graphics/src/$$f.txt graphics/$$f.tex; \ + echo ""; \ + done \ No newline at end of file diff --git a/exam_template/python/FSA.py b/exam_template/python/FSA.py index 462d13a..be9d299 100644 --- a/exam_template/python/FSA.py +++ b/exam_template/python/FSA.py @@ -62,6 +62,9 @@ def generate_latex(inputLines): return '\n'.join(output) +def processFileContent(raw, template): + content = generate_latex(raw) + return template.replace('%CONTENT', content) if __name__ == '__main__': filename = argv[1] diff --git a/exam_template/python/Hasse.py b/exam_template/python/Hasse.py index 97662fe..e1ca3f7 100644 --- a/exam_template/python/Hasse.py +++ b/exam_template/python/Hasse.py @@ -65,6 +65,11 @@ def latex_hasse(hasse): return '\n'.join(output) +def processFileContent(raw, template): + rels = getRels(raw) + content = latex_hasse(hasse_diagram(rels)) + return template.replace("%CONTENT", content) + if __name__ == '__main__': filename = argv[1] diff --git a/exam_template/python/Truthtable.py b/exam_template/python/Truthtable.py index 2a99bdf..fe7f7d5 100644 --- a/exam_template/python/Truthtable.py +++ b/exam_template/python/Truthtable.py @@ -1,5 +1,10 @@ from sys import argv from pathlib import Path +try: + from tabulate import tabulate +except: + print('Couldn\'t find tabulate. Do you have it installed?') + def parseExpressionList(inputData): return [e.strip() for e in inputData.split(',')] @@ -77,12 +82,27 @@ def latexify(exps, truthtable): """.format( '|'.join('e' if e else 'c' for e,_ in latex_expressions), ' & '.join(f'${exp}$' for _,exp in latex_expressions), - '\n'.join(' ' + ' & '.join('\\T' if b else '\\F' for b in line) for line in truthtable) + '\n'.join(' ' + ' & '.join('\\T' if b else '\\F' for b in line) + ' \\\\' for line in truthtable) ) def printTruthtable(exps, truthtable): stringTable = [ ['\033[32mT\033[0m ' if b else '\033[31mF\033[0m' for b in line] for line in truthtable] - print(tabulate(stringTable, headers=exps)) + try: + print(tabulate(stringTable, headers=exps)) + except: + pass + + +def processFileContent(raw, template): + exps = parseExpressionList(raw) + truthtable = generateTruthTable(exps) + + printTruthtable(exps, generateTruthTable(exps)) + + content = latexify(exps, truthtable) + return template.replace('%CONTENT', content) + + if __name__ == '__main__': filename = argv[1] @@ -91,11 +111,11 @@ if __name__ == '__main__': exps = parseExpressionList(file.read()) truthtable = generateTruthTable(exps) - try: - from tabulate import tabulate - printTruthtable(exps, generateTruthTable(exps)) - except: - print('Couldn\'t find tabulate. Do you have it installed?') + # try: + from tabulate import tabulate + printTruthtable(exps, generateTruthTable(exps)) + # except e: + # print(e) content = latexify(exps, truthtable) diff --git a/exam_template/python/run.py b/exam_template/python/run.py new file mode 100644 index 0000000..cb8aeb3 --- /dev/null +++ b/exam_template/python/run.py @@ -0,0 +1,35 @@ +from sys import argv +from pathlib import Path + +import FSA +import Graph +import Hasse +import Truthtable + +def fetchContentType(content): + new_content = content.split('\n') + contentType = new_content.pop(0)[2:] + return (contentType, '\n'.join(new_content)) + +def processContent(content): + contentType, content = fetchContentType(content) + with open(str(Path(__file__).parent.absolute()) + f'/tex_templates/{contentType}.tex') as template: + if contentType == 'Hasse': + result = Hasse.processFileContent(content, template.read()) + elif contentType == 'FSA': + result = FSA.processFileContent(content, template.read()) + elif contentType == 'Truthtable': + result = Truthtable.processFileContent(content, template.read()) + else: + print('DIDN\'T RECOGNIZE FILE TYPE') + exit(1) + return result + +if __name__ == '__main__': + filename = argv[1] + + with open(filename) as file: + content = processContent(file.read()) + + with open(argv[2], 'w') as destination_file: + destination_file.write(content) \ No newline at end of file