Skip to content

Commit 31ff979

Browse files
Formatting (#16)
*Making tests pass
1 parent 68af95d commit 31ff979

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

in2lambda/api/module.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ def to_json(self, output_dir: str) -> None:
112112
... module.to_json(temp_dir)
113113
... # Check the contents of the directory
114114
... sorted(os.listdir(temp_dir))
115+
... # Check the contents of the set directory
116+
... sorted(os.listdir(f"{temp_dir}/set"))
115117
... # Check the title of the first question
116-
... with open(f"{temp_dir}/question_1/question_1.json") as file:
118+
... with open(f"{temp_dir}/set/question_1.json") as file:
117119
... print(f"Question 1's title: {json.load(file)['title']}")
118-
['question_1', 'question_1.zip', 'question_2', 'question_2.zip']
120+
['set', 'set.zip']
121+
['media', 'question_1.json', 'question_2.json']
119122
Question 1's title: Question 1
120123
121124
"""

in2lambda/filters/markdown.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ def image_directories(tex_file: str) -> list[str]:
3232
>>> temp_dir = tempfile.mkdtemp()
3333
>>> tex_file = os.path.join(temp_dir, 'test.tex')
3434
>>> with open(tex_file, 'w') as f:
35-
... f.write("\graphicspath{{subdir1/}{subdir2/}{subdir3/}}")
35+
... f.write("\\graphicspath{{subdir1/}{subdir2/}{subdir3/}}")
3636
45
3737
>>> image_directories(tex_file)
3838
['subdir1/', 'subdir2/', 'subdir3/']
3939
>>> with open(tex_file, 'w') as f:
40-
... f.write("\graphicspath{ { subdir1/ }, { subdir2/ }, { subdir3/ } }")
40+
... f.write("\\graphicspath{ { subdir1/ }, { subdir2/ }, { subdir3/ } }")
4141
57
4242
>>> image_directories.cache_clear()
4343
>>> image_directories(tex_file)
@@ -78,7 +78,7 @@ def image_path(image_name: str, tex_file: str) -> Optional[str]:
7878
>>> temp_dir = tempfile.mkdtemp()
7979
>>> tex_file = os.path.join(temp_dir, 'test.tex')
8080
>>> with open(tex_file, 'w') as f:
81-
... f.write("\graphicspath{{./subdir1/}{./subdir2/}{./subdir3/}}")
81+
... f.write("\\graphicspath{{./subdir1/}{./subdir2/}{./subdir3/}}")
8282
51
8383
>>> # Example image in a relative subdirectory
8484
>>> sub_dir = os.path.join(temp_dir, 'subdir3')

in2lambda/json_convert/json_convert.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def converter(
2929
output_question = os.path.join(output_dir, "set")
3030
os.makedirs(output_question, exist_ok=True)
3131

32-
3332
# create directory to put images - should be in set
3433
output_image = os.path.join(output_question, "media")
3534
os.makedirs(output_image, exist_ok=True)
@@ -62,7 +61,6 @@ def converter(
6261
# Output file
6362
filename = "question_" + str(i + 1)
6463

65-
6664
# write questions into directory
6765
with open(f"{output_question}/{filename}.json", "w") as file:
6866
json.dump(output, file)

in2lambda/main.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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.)
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.)
44
#
55
# import sys
66
# import os
77
# sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
8-
98

109
import importlib
1110
import pkgutil
11+
import subprocess
1212
from typing import Optional
1313

1414
import panflute as pf
@@ -17,12 +17,19 @@
1717
import in2lambda.filters
1818
from in2lambda.api.module import Module
1919

20-
import subprocess
2120

22-
#Converts .docx files to markdown
2321
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')
22+
"""Converts .docx files to markdown.
23+
24+
Args:
25+
docx_file: A file path with the file extension included.
26+
27+
Returns:
28+
the contents of the .docx file in markdown formatting
29+
"""
30+
md_output = subprocess.check_output(["pandoc", docx_file, "-t", "markdown"])
31+
return md_output.decode("utf-8")
32+
2633

2734
def file_type(file: str) -> str:
2835
"""Determines which pandoc file format to use for a given file.
@@ -103,30 +110,30 @@ def runner(
103110
# Dynamically import the correct pandoc filter depending on the subject.
104111
filter_module = importlib.import_module(f"in2lambda.filters.{chosen_filter}.filter")
105112

106-
107-
if file_type(question_file) == 'docx':
113+
if file_type(question_file) == "docx":
108114
# Convert .docx to md using Pandoc and proceed
109115
text = docx_to_md(question_file)
110116
input_format = "markdown"
111117
else:
112118
with open(question_file, "r", encoding="utf-8") as file:
113119
text = file.read()
114-
input_format=file_type(question_file)
120+
121+
input_format = file_type(question_file)
115122

116123
# Parse the Pandoc AST using the relevant panflute filter.
117124
pf.run_filter(
118125
filter_module.pandoc_filter,
119-
doc=pf.convert_text(
120-
text, input_format=input_format, standalone=True
121-
),
126+
doc=pf.convert_text(text, input_format=input_format, standalone=True),
122127
module=module,
123128
tex_file=question_file,
124129
parsing_answers=False,
125130
)
126131

127132
# If separate answer TeX file provided, parse that as well.
128133
if answer_file:
129-
if file_type(answer_file) == 'docx':
134+
135+
if file_type(answer_file) == "docx":
136+
130137
answer_text = docx_to_md(answer_file)
131138
answer_format = "markdown"
132139
else:

0 commit comments

Comments
 (0)