1212from in2lambda .json_convert import json_convert
1313
1414
15+ def file_type (file : str ) -> str :
16+ """Determines which pandoc file format to use for a given file.
17+
18+ See https://github.com/jgm/pandoc/blob/bad922a69236e22b20d51c4ec0b90c5a6c038433/src/Text/Pandoc/Format.hs#L171
19+ (or any newer commit) for pandoc's supported file extensions.
20+
21+ Args:
22+ file: A file path with the file extension included.
23+
24+ Returns:
25+ An option in `pandoc --list-input-formats` that matches the given file type
26+
27+ Examples:
28+ >>> from in2lambda.main import file_type
29+ >>> file_type("example.tex")
30+ 'latex'
31+ >>> file_type("/some/random/path/demo.md")
32+ 'markdown'
33+ >>> file_type("no_extension")
34+ Traceback (most recent call last):
35+ RuntimeError: Unsupported file extension: .no_extension
36+ >>> file_type("demo.unknown_extension")
37+ Traceback (most recent call last):
38+ RuntimeError: Unsupported file extension: .unknown_extension
39+ """
40+ match (extension := file .split ("." )[- 1 ].lower ()):
41+ case "tex" | "latex" | "ltx" :
42+ return "latex"
43+ case "md" | "rmd" | "markdown" | "mdown" | "mdwn" | "mkd" | "mkdn" | "text" | "txt" :
44+ return "markdown"
45+ case "docx" :
46+ return "docx" # Pandoc doesn't seem to support doc
47+ raise RuntimeError (f"Unsupported file extension: .{ extension } " )
48+
49+
1550def runner (
1651 question_file : str ,
1752 chosen_filter : str ,
@@ -52,7 +87,9 @@ def runner(
5287 # Parse the Pandoc AST using the relevant panflute filter.
5388 pf .run_filter (
5489 filter_module .pandoc_filter ,
55- doc = pf .convert_text (text , input_format = "latex" , standalone = True ),
90+ doc = pf .convert_text (
91+ text , input_format = file_type (question_file ), standalone = True
92+ ),
5693 module = module ,
5794 tex_file = question_file ,
5895 parsing_answers = False ,
@@ -65,7 +102,9 @@ def runner(
65102
66103 pf .run_filter (
67104 filter_module .pandoc_filter ,
68- doc = pf .convert_text (answer_text , input_format = "latex" , standalone = True ),
105+ doc = pf .convert_text (
106+ answer_text , input_format = file_type (answer_file ), standalone = True
107+ ),
69108 module = module ,
70109 tex_file = answer_file ,
71110 parsing_answers = True ,
0 commit comments