Skip to content

Commit 68af95d

Browse files
Fixes and additions (#14)
*fixed to work with current lambda feedback versions * outputs a single .zip * made .docx files work * Added comments
1 parent 2969b0a commit 68af95d

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

in2lambda/json_convert/json_convert.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ def converter(
2424
"""
2525
# Create output by copying template
2626

27+
# create directory to put the questions
28+
os.makedirs(output_dir, exist_ok=True)
29+
output_question = os.path.join(output_dir, "set")
30+
os.makedirs(output_question, exist_ok=True)
31+
32+
33+
# create directory to put images - should be in set
34+
output_image = os.path.join(output_question, "media")
35+
os.makedirs(output_image, exist_ok=True)
36+
2737
for i in range(len(ListQuestions)):
2838
output = deepcopy(template)
2939

@@ -39,27 +49,19 @@ def converter(
3949
# add parts to the question file
4050
if ListQuestions[i].parts:
4151
output["parts"][0]["content"] = ListQuestions[i].parts[0].text
42-
output["parts"][0]["workedSolution"][0]["content"] = (
52+
output["parts"][0]["workedSolution"]["content"] = (
4353
ListQuestions[i].parts[0].worked_solution
4454
)
4555
for j in range(1, len(ListQuestions[i].parts)):
4656
output["parts"].append(deepcopy(template["parts"][0]))
4757
output["parts"][j]["content"] = ListQuestions[i].parts[j].text
48-
output["parts"][j]["workedSolution"][0]["content"] = (
58+
output["parts"][j]["workedSolution"]["content"] = (
4959
ListQuestions[i].parts[j].worked_solution
5060
)
5161

5262
# Output file
5363
filename = "question_" + str(i + 1)
5464

55-
# create directory to put the questions
56-
os.makedirs(output_dir, exist_ok=True)
57-
output_question = os.path.join(output_dir, filename)
58-
os.makedirs(output_question, exist_ok=True)
59-
60-
# create directory to put image
61-
output_image = os.path.join(output_question, "media")
62-
os.makedirs(output_image, exist_ok=True)
6365

6466
# write questions into directory
6567
with open(f"{output_question}/{filename}.json", "w") as file:
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
{
2+
"title": "Question title here",
23
"masterContent": "Top level question here",
4+
"publish": false,
5+
"displayFinalAnswer": true,
6+
"displayStructuredTutorial": true,
7+
"displayWorkedSolution": true,
38
"parts": [
49
{
510
"answer": "",
611
"content": "Part text here",
7-
"responseArea": [],
12+
"responseAreas": [],
813
"tutorial": [],
914
"universalPartId": "N/A",
10-
"workedSolution": [
11-
{
15+
"workedSolution": {
1216
"content": "Part worked solution here",
1317
"id": "N/A",
1418
"title": ""
15-
}
16-
]
19+
}
1720
}
18-
],
19-
"publish": false,
20-
"questionSettings": {
21-
"displayFinalAnswer": true,
22-
"displayStructuredTutorial": true,
23-
"displayWorkedSolution": true
24-
},
25-
"title": "Question title here"
21+
]
2622
}

in2lambda/main.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
"""The main input for in2lambda, defining both the CLT and main library function."""
22

3+
#This commented block makes it run the local files rather than the pip library (I think, I don't understand it. Kevin wrote it.)
4+
#
5+
# import sys
6+
# import os
7+
# sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
8+
9+
310
import importlib
411
import pkgutil
512
from typing import Optional
@@ -10,6 +17,12 @@
1017
import in2lambda.filters
1118
from in2lambda.api.module import Module
1219

20+
import subprocess
21+
22+
#Converts .docx files to markdown
23+
def docx_to_md(docx_file: str) -> str:
24+
md_output = subprocess.check_output(['pandoc', docx_file, '-t', 'markdown'])
25+
return md_output.decode('utf-8')
1326

1427
def file_type(file: str) -> str:
1528
"""Determines which pandoc file format to use for a given file.
@@ -52,7 +65,7 @@ def file_type(file: str) -> str:
5265
):
5366
return "markdown"
5467
case "docx":
55-
return "docx" # Pandoc doesn't seem to support doc
68+
return "docx" # Pandoc doesn't seem to support .doc, and panflute doesn't like .docx.
5669
raise RuntimeError(f"Unsupported file extension: .{extension}")
5770

5871

@@ -90,14 +103,21 @@ def runner(
90103
# Dynamically import the correct pandoc filter depending on the subject.
91104
filter_module = importlib.import_module(f"in2lambda.filters.{chosen_filter}.filter")
92105

93-
with open(question_file, "r", encoding="utf-8") as file:
94-
text = file.read()
106+
107+
if file_type(question_file) == 'docx':
108+
# Convert .docx to md using Pandoc and proceed
109+
text = docx_to_md(question_file)
110+
input_format = "markdown"
111+
else:
112+
with open(question_file, "r", encoding="utf-8") as file:
113+
text = file.read()
114+
input_format=file_type(question_file)
95115

96116
# Parse the Pandoc AST using the relevant panflute filter.
97117
pf.run_filter(
98118
filter_module.pandoc_filter,
99119
doc=pf.convert_text(
100-
text, input_format=file_type(question_file), standalone=True
120+
text, input_format=input_format, standalone=True
101121
),
102122
module=module,
103123
tex_file=question_file,
@@ -106,13 +126,18 @@ def runner(
106126

107127
# If separate answer TeX file provided, parse that as well.
108128
if answer_file:
109-
with open(answer_file, "r", encoding="utf-8") as file:
110-
answer_text = file.read()
129+
if file_type(answer_file) == 'docx':
130+
answer_text = docx_to_md(answer_file)
131+
answer_format = "markdown"
132+
else:
133+
with open(answer_file, "r", encoding="utf-8") as file:
134+
answer_text = file.read()
135+
answer_format = file_type(answer_file)
111136

112137
pf.run_filter(
113138
filter_module.pandoc_filter,
114139
doc=pf.convert_text(
115-
answer_text, input_format=file_type(answer_file), standalone=True
140+
answer_text, input_format=answer_format, standalone=True
116141
),
117142
module=module,
118143
tex_file=answer_file,

0 commit comments

Comments
 (0)