From 1e18bb89bc5fa648342ea1a03110d03218555c34 Mon Sep 17 00:00:00 2001 From: TeeVahReeeeeen Date: Thu, 4 Jul 2024 11:05:46 +0100 Subject: [PATCH 1/7] test1 --- in2lambda/json_convert/minimal_template.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/in2lambda/json_convert/minimal_template.json b/in2lambda/json_convert/minimal_template.json index dfef133..356d08e 100644 --- a/in2lambda/json_convert/minimal_template.json +++ b/in2lambda/json_convert/minimal_template.json @@ -22,5 +22,5 @@ "displayStructuredTutorial": true, "displayWorkedSolution": true }, - "title": "Question title here" + "title": "Question title here test" } From 32b973431e511be66e493524ec0df41cff37b77f Mon Sep 17 00:00:00 2001 From: TeeVahReeeeeen Date: Thu, 4 Jul 2024 11:06:43 +0100 Subject: [PATCH 2/7] test2 --- in2lambda/json_convert/json_convert.py | 2 +- in2lambda/json_convert/minimal_template.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/in2lambda/json_convert/json_convert.py b/in2lambda/json_convert/json_convert.py index 40862c2..54df5a3 100644 --- a/in2lambda/json_convert/json_convert.py +++ b/in2lambda/json_convert/json_convert.py @@ -50,7 +50,7 @@ def converter( ) # Output file - filename = "question_" + str(i + 1) + filename = "question_" + str(i + 1) + "test" # create directory to put the questions os.makedirs(output_dir, exist_ok=True) diff --git a/in2lambda/json_convert/minimal_template.json b/in2lambda/json_convert/minimal_template.json index 356d08e..dfef133 100644 --- a/in2lambda/json_convert/minimal_template.json +++ b/in2lambda/json_convert/minimal_template.json @@ -22,5 +22,5 @@ "displayStructuredTutorial": true, "displayWorkedSolution": true }, - "title": "Question title here test" + "title": "Question title here" } From 3073b2cef994c98667077dd057c9860799a9a975 Mon Sep 17 00:00:00 2001 From: TeeVahReeeeeen Date: Thu, 4 Jul 2024 11:38:21 +0100 Subject: [PATCH 3/7] more testing --- in2lambda/json_convert/json_convert.py | 8 ++++---- in2lambda/json_convert/minimal_template.json | 10 ++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/in2lambda/json_convert/json_convert.py b/in2lambda/json_convert/json_convert.py index 54df5a3..eb0a268 100644 --- a/in2lambda/json_convert/json_convert.py +++ b/in2lambda/json_convert/json_convert.py @@ -29,9 +29,9 @@ def converter( # add title to the question file if ListQuestions[i].title != "": - output["title"] = ListQuestions[i].title + output["title"] = ListQuestions[i].title + "test" else: - output["title"] = "Question " + str(i + 1) + output["title"] = "Question " + str(i + 1) + "test" # add main text to the question file output["masterContent"] = ListQuestions[i].main_text @@ -58,7 +58,7 @@ def converter( os.makedirs(output_question, exist_ok=True) # create directory to put image - output_image = os.path.join(output_question, "media") + output_image = os.path.join(output_question, "media" + "test") os.makedirs(output_image, exist_ok=True) # write questions into directory @@ -73,7 +73,7 @@ def converter( shutil.copy(image_path, output_image) # copies image into the directory # output zip file in destination folder - shutil.make_archive(output_question, "zip", output_question) + shutil.make_archive(output_question + "test", "zip", output_question) def main(questions: list[Question], output_dir: str) -> None: diff --git a/in2lambda/json_convert/minimal_template.json b/in2lambda/json_convert/minimal_template.json index dfef133..3b1d920 100644 --- a/in2lambda/json_convert/minimal_template.json +++ b/in2lambda/json_convert/minimal_template.json @@ -4,7 +4,7 @@ { "answer": "", "content": "Part text here", - "responseArea": [], + "responseAreas": [], "tutorial": [], "universalPartId": "N/A", "workedSolution": [ @@ -17,10 +17,8 @@ } ], "publish": false, - "questionSettings": { - "displayFinalAnswer": true, - "displayStructuredTutorial": true, - "displayWorkedSolution": true - }, + "displayFinalAnswer": true, + "displayStructuredTutorial": true, + "displayWorkedSolution": true, "title": "Question title here" } From 9fdbd8039da0713b313eb7d709e0f87f55ca07a4 Mon Sep 17 00:00:00 2001 From: TeeVahReeeeeen Date: Thu, 4 Jul 2024 13:47:00 +0100 Subject: [PATCH 4/7] finally working? --- in2lambda/json_convert/json_convert.py | 30 +++++++++++--------- in2lambda/json_convert/minimal_template.json | 18 ++++++------ in2lambda/main.py | 5 ++++ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/in2lambda/json_convert/json_convert.py b/in2lambda/json_convert/json_convert.py index eb0a268..01b6487 100644 --- a/in2lambda/json_convert/json_convert.py +++ b/in2lambda/json_convert/json_convert.py @@ -24,14 +24,24 @@ def converter( """ # Create output by copying template + # create directory to put the questions + os.makedirs(output_dir, exist_ok=True) + output_question = os.path.join(output_dir, "set") + os.makedirs(output_question, exist_ok=True) + + + # create directory to put images - should be in set + output_image = os.path.join(output_question, "media") + os.makedirs(output_image, exist_ok=True) + for i in range(len(ListQuestions)): output = deepcopy(template) # add title to the question file if ListQuestions[i].title != "": - output["title"] = ListQuestions[i].title + "test" + output["title"] = ListQuestions[i].title else: - output["title"] = "Question " + str(i + 1) + "test" + output["title"] = "Question " + str(i + 1) # add main text to the question file output["masterContent"] = ListQuestions[i].main_text @@ -39,27 +49,19 @@ def converter( # add parts to the question file if ListQuestions[i].parts: output["parts"][0]["content"] = ListQuestions[i].parts[0].text - output["parts"][0]["workedSolution"][0]["content"] = ( + output["parts"][0]["workedSolution"]["content"] = ( ListQuestions[i].parts[0].worked_solution ) for j in range(1, len(ListQuestions[i].parts)): output["parts"].append(deepcopy(template["parts"][0])) output["parts"][j]["content"] = ListQuestions[i].parts[j].text - output["parts"][j]["workedSolution"][0]["content"] = ( + output["parts"][j]["workedSolution"]["content"] = ( ListQuestions[i].parts[j].worked_solution ) # Output file - filename = "question_" + str(i + 1) + "test" - - # create directory to put the questions - os.makedirs(output_dir, exist_ok=True) - output_question = os.path.join(output_dir, filename) - os.makedirs(output_question, exist_ok=True) + filename = "question_" + str(i + 1) - # create directory to put image - output_image = os.path.join(output_question, "media" + "test") - os.makedirs(output_image, exist_ok=True) # write questions into directory with open(f"{output_question}/{filename}.json", "w") as file: @@ -73,7 +75,7 @@ def converter( shutil.copy(image_path, output_image) # copies image into the directory # output zip file in destination folder - shutil.make_archive(output_question + "test", "zip", output_question) + shutil.make_archive(output_question, "zip", output_question) def main(questions: list[Question], output_dir: str) -> None: diff --git a/in2lambda/json_convert/minimal_template.json b/in2lambda/json_convert/minimal_template.json index 3b1d920..8b4896b 100644 --- a/in2lambda/json_convert/minimal_template.json +++ b/in2lambda/json_convert/minimal_template.json @@ -1,5 +1,10 @@ { + "title": "Question title here", "masterContent": "Top level question here", + "publish": false, + "displayFinalAnswer": true, + "displayStructuredTutorial": true, + "displayWorkedSolution": true, "parts": [ { "answer": "", @@ -7,18 +12,11 @@ "responseAreas": [], "tutorial": [], "universalPartId": "N/A", - "workedSolution": [ - { + "workedSolution": { "content": "Part worked solution here", "id": "N/A", "title": "" - } - ] + } } - ], - "publish": false, - "displayFinalAnswer": true, - "displayStructuredTutorial": true, - "displayWorkedSolution": true, - "title": "Question title here" + ] } diff --git a/in2lambda/main.py b/in2lambda/main.py index 32ccc8c..82678a7 100644 --- a/in2lambda/main.py +++ b/in2lambda/main.py @@ -1,5 +1,10 @@ """The main input for in2lambda, defining both the CLT and main library function.""" +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + + import importlib import pkgutil from typing import Optional From e3531106dc034258762dc6994c4616f945bda355 Mon Sep 17 00:00:00 2001 From: Tom W Date: Sun, 7 Jul 2024 15:41:14 +0100 Subject: [PATCH 5/7] made .docx files work --- in2lambda/main.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/in2lambda/main.py b/in2lambda/main.py index 82678a7..15ce5ee 100644 --- a/in2lambda/main.py +++ b/in2lambda/main.py @@ -15,6 +15,12 @@ import in2lambda.filters from in2lambda.api.module import Module +import subprocess + + +def docx_to_md(docx_file: str) -> str: + md_output = subprocess.check_output(['pandoc', docx_file, '-t', 'markdown']) + return md_output.decode('utf-8') def file_type(file: str) -> str: """Determines which pandoc file format to use for a given file. @@ -95,14 +101,21 @@ def runner( # Dynamically import the correct pandoc filter depending on the subject. filter_module = importlib.import_module(f"in2lambda.filters.{chosen_filter}.filter") - with open(question_file, "r", encoding="utf-8") as file: - text = file.read() + + if file_type(question_file) == 'docx': + # Convert .docx to md using Pandoc + text = docx_to_md(question_file) + input_format = "markdown" + else: + with open(question_file, "r", encoding="utf-8") as file: + text = file.read() + input_format=file_type(question_file) # Parse the Pandoc AST using the relevant panflute filter. pf.run_filter( filter_module.pandoc_filter, doc=pf.convert_text( - text, input_format=file_type(question_file), standalone=True + text, input_format=input_format, standalone=True ), module=module, tex_file=question_file, @@ -111,13 +124,18 @@ def runner( # If separate answer TeX file provided, parse that as well. if answer_file: - with open(answer_file, "r", encoding="utf-8") as file: - answer_text = file.read() + if file_type(answer_file) == 'docx': + answer_text = docx_to_md(answer_file) + answer_format = "markdown" + else: + with open(answer_file, "r", encoding="utf-8") as file: + answer_text = file.read() + answer_format = file_type(answer_file) pf.run_filter( filter_module.pandoc_filter, doc=pf.convert_text( - answer_text, input_format=file_type(answer_file), standalone=True + answer_text, input_format=answer_format, standalone=True ), module=module, tex_file=answer_file, @@ -177,3 +195,8 @@ def cli( if __name__ == "__main__": cli() +#%% + +# import panflute as pf + +# pf.convert_text("PS/TestQ.docx", input_format="docx", standalone=True) From f1ec6eca06ea0523bfa88c05567fb244f1114be0 Mon Sep 17 00:00:00 2001 From: TeeVahReeeeeen Date: Mon, 8 Jul 2024 13:38:57 +0100 Subject: [PATCH 6/7] removed local running thingy --- in2lambda/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/in2lambda/main.py b/in2lambda/main.py index 15ce5ee..4de0861 100644 --- a/in2lambda/main.py +++ b/in2lambda/main.py @@ -1,8 +1,8 @@ """The main input for in2lambda, defining both the CLT and main library function.""" -import sys -import os -sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +# import sys +# import os +# sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import importlib From fb87caecc0fb499b5215a93a0fafa983cb6eb61f Mon Sep 17 00:00:00 2001 From: TeeVahReeeeeen Date: Mon, 8 Jul 2024 13:47:43 +0100 Subject: [PATCH 7/7] Added comments --- in2lambda/main.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/in2lambda/main.py b/in2lambda/main.py index 4de0861..0a39793 100644 --- a/in2lambda/main.py +++ b/in2lambda/main.py @@ -1,5 +1,7 @@ """The main input for in2lambda, defining both the CLT and main library function.""" +#This commented block makes it run the local files rather than the pip library (I think, I don't understand it. Kevin wrote it.) +# # import sys # import os # sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) @@ -17,7 +19,7 @@ import subprocess - +#Converts .docx files to markdown def docx_to_md(docx_file: str) -> str: md_output = subprocess.check_output(['pandoc', docx_file, '-t', 'markdown']) return md_output.decode('utf-8') @@ -63,7 +65,7 @@ def file_type(file: str) -> str: ): return "markdown" case "docx": - return "docx" # Pandoc doesn't seem to support doc + return "docx" # Pandoc doesn't seem to support .doc, and panflute doesn't like .docx. raise RuntimeError(f"Unsupported file extension: .{extension}") @@ -103,7 +105,7 @@ def runner( if file_type(question_file) == 'docx': - # Convert .docx to md using Pandoc + # Convert .docx to md using Pandoc and proceed text = docx_to_md(question_file) input_format = "markdown" else: @@ -195,8 +197,3 @@ def cli( if __name__ == "__main__": cli() -#%% - -# import panflute as pf - -# pf.convert_text("PS/TestQ.docx", input_format="docx", standalone=True)