Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions in2lambda/json_convert/json_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ 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)

Expand All @@ -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)

# 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)

# create directory to put image
output_image = os.path.join(output_question, "media")
os.makedirs(output_image, exist_ok=True)

# write questions into directory
with open(f"{output_question}/{filename}.json", "w") as file:
Expand Down
22 changes: 9 additions & 13 deletions in2lambda/json_convert/minimal_template.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
{
"title": "Question title here",
"masterContent": "Top level question here",
"publish": false,
"displayFinalAnswer": true,
"displayStructuredTutorial": true,
"displayWorkedSolution": true,
"parts": [
{
"answer": "",
"content": "Part text here",
"responseArea": [],
"responseAreas": [],
"tutorial": [],
"universalPartId": "N/A",
"workedSolution": [
{
"workedSolution": {
"content": "Part worked solution here",
"id": "N/A",
"title": ""
}
]
}
}
],
"publish": false,
"questionSettings": {
"displayFinalAnswer": true,
"displayStructuredTutorial": true,
"displayWorkedSolution": true
},
"title": "Question title here"
]
}
39 changes: 32 additions & 7 deletions in2lambda/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
"""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__), '..')))


import importlib
import pkgutil
from typing import Optional
Expand All @@ -10,6 +17,12 @@
import in2lambda.filters
from in2lambda.api.module import Module

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')

def file_type(file: str) -> str:
"""Determines which pandoc file format to use for a given file.
Expand Down Expand Up @@ -52,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}")


Expand Down Expand Up @@ -90,14 +103,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 and proceed
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,
Expand All @@ -106,13 +126,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,
Expand Down