|
1 | 1 | """The main input for in2lambda, defining both the CLT and main library function.""" |
2 | 2 |
|
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.) |
4 | 4 | # |
5 | 5 | # import sys |
6 | 6 | # import os |
7 | 7 | # sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
8 | | - |
9 | 8 |
|
10 | 9 | import importlib |
11 | 10 | import pkgutil |
| 11 | +import subprocess |
12 | 12 | from typing import Optional |
13 | 13 |
|
14 | 14 | import panflute as pf |
|
17 | 17 | import in2lambda.filters |
18 | 18 | from in2lambda.api.module import Module |
19 | 19 |
|
20 | | -import subprocess |
21 | 20 |
|
22 | | -#Converts .docx files to markdown |
23 | 21 | 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 | + |
26 | 33 |
|
27 | 34 | def file_type(file: str) -> str: |
28 | 35 | """Determines which pandoc file format to use for a given file. |
@@ -103,30 +110,30 @@ def runner( |
103 | 110 | # Dynamically import the correct pandoc filter depending on the subject. |
104 | 111 | filter_module = importlib.import_module(f"in2lambda.filters.{chosen_filter}.filter") |
105 | 112 |
|
106 | | - |
107 | | - if file_type(question_file) == 'docx': |
| 113 | + if file_type(question_file) == "docx": |
108 | 114 | # Convert .docx to md using Pandoc and proceed |
109 | 115 | text = docx_to_md(question_file) |
110 | 116 | input_format = "markdown" |
111 | 117 | else: |
112 | 118 | with open(question_file, "r", encoding="utf-8") as file: |
113 | 119 | text = file.read() |
114 | | - input_format=file_type(question_file) |
| 120 | + |
| 121 | + input_format = file_type(question_file) |
115 | 122 |
|
116 | 123 | # Parse the Pandoc AST using the relevant panflute filter. |
117 | 124 | pf.run_filter( |
118 | 125 | 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), |
122 | 127 | module=module, |
123 | 128 | tex_file=question_file, |
124 | 129 | parsing_answers=False, |
125 | 130 | ) |
126 | 131 |
|
127 | 132 | # If separate answer TeX file provided, parse that as well. |
128 | 133 | if answer_file: |
129 | | - if file_type(answer_file) == 'docx': |
| 134 | + |
| 135 | + if file_type(answer_file) == "docx": |
| 136 | + |
130 | 137 | answer_text = docx_to_md(answer_file) |
131 | 138 | answer_format = "markdown" |
132 | 139 | else: |
|
0 commit comments