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
7 changes: 5 additions & 2 deletions in2lambda/api/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ def to_json(self, output_dir: str) -> None:
... module.to_json(temp_dir)
... # Check the contents of the directory
... sorted(os.listdir(temp_dir))
... # Check the contents of the set directory
... sorted(os.listdir(f"{temp_dir}/set"))
... # Check the title of the first question
... with open(f"{temp_dir}/question_1/question_1.json") as file:
... with open(f"{temp_dir}/set/question_1.json") as file:
... print(f"Question 1's title: {json.load(file)['title']}")
['question_1', 'question_1.zip', 'question_2', 'question_2.zip']
['set', 'set.zip']
['media', 'question_1.json', 'question_2.json']
Question 1's title: Question 1

"""
Expand Down
6 changes: 3 additions & 3 deletions in2lambda/filters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def image_directories(tex_file: str) -> list[str]:
>>> temp_dir = tempfile.mkdtemp()
>>> tex_file = os.path.join(temp_dir, 'test.tex')
>>> with open(tex_file, 'w') as f:
... f.write("\graphicspath{{subdir1/}{subdir2/}{subdir3/}}")
... f.write("\\graphicspath{{subdir1/}{subdir2/}{subdir3/}}")
45
>>> image_directories(tex_file)
['subdir1/', 'subdir2/', 'subdir3/']
>>> with open(tex_file, 'w') as f:
... f.write("\graphicspath{ { subdir1/ }, { subdir2/ }, { subdir3/ } }")
... f.write("\\graphicspath{ { subdir1/ }, { subdir2/ }, { subdir3/ } }")
57
>>> image_directories.cache_clear()
>>> image_directories(tex_file)
Expand Down Expand Up @@ -78,7 +78,7 @@ def image_path(image_name: str, tex_file: str) -> Optional[str]:
>>> temp_dir = tempfile.mkdtemp()
>>> tex_file = os.path.join(temp_dir, 'test.tex')
>>> with open(tex_file, 'w') as f:
... f.write("\graphicspath{{./subdir1/}{./subdir2/}{./subdir3/}}")
... f.write("\\graphicspath{{./subdir1/}{./subdir2/}{./subdir3/}}")
51
>>> # Example image in a relative subdirectory
>>> sub_dir = os.path.join(temp_dir, 'subdir3')
Expand Down
2 changes: 0 additions & 2 deletions in2lambda/json_convert/json_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def converter(
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)
Expand Down Expand Up @@ -62,7 +61,6 @@ def converter(
# Output file
filename = "question_" + str(i + 1)


# write questions into directory
with open(f"{output_question}/{filename}.json", "w") as file:
json.dump(output, file)
Expand Down
33 changes: 20 additions & 13 deletions in2lambda/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""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.)
# 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
import subprocess
from typing import Optional

import panflute as pf
Expand All @@ -17,12 +17,19 @@
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')
"""Converts .docx files to markdown.

Args:
docx_file: A file path with the file extension included.

Returns:
the contents of the .docx file in markdown formatting
"""
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 @@ -103,30 +110,30 @@ def runner(
# Dynamically import the correct pandoc filter depending on the subject.
filter_module = importlib.import_module(f"in2lambda.filters.{chosen_filter}.filter")


if file_type(question_file) == 'docx':
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)

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=input_format, standalone=True
),
doc=pf.convert_text(text, input_format=input_format, standalone=True),
module=module,
tex_file=question_file,
parsing_answers=False,
)

# If separate answer TeX file provided, parse that as well.
if answer_file:
if file_type(answer_file) == 'docx':

if file_type(answer_file) == "docx":

answer_text = docx_to_md(answer_file)
answer_format = "markdown"
else:
Expand Down