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+
310import importlib
411import pkgutil
512from typing import Optional
1017import in2lambda .filters
1118from 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
1427def 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