-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdList2typList.py
107 lines (84 loc) · 4.26 KB
/
mdList2typList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import sys
"""
Quickstart:
python mdList2typList.py papersAboutCoding.md citations.typ
Purpose: Read a list of citekeys as strings from the file "papersAboutCoding.md",
reformat for use in the master main.typ file, and write them to "citations.tex".
The approach supports customizing the order of the entries in the annotated bibliography.
This list of citekeys can be ordered by categories instead of alphabetical order.
The script is run from the command line and takes two arguments:
the filename of the list of citekeys and the filename of the typst output file.
This script contains two functions called from the `if __main__` section below.
The first function `format_citations()` takes a list of strings from an itemized list in LaTeX and
formats each string to enable the inclusion of the associated bibliographic entry in the subsection heading
and the bibNote in the annotation below.
This script includes error handling to check if the file papersAboutCoding.tex exists.
If the file is not found, it prints an error message and proceeds with an empty list of strings.
This script includes the `create_bib_notes_file()` function,
which creates a new file in the ./bibNotes (or ~/bibNotes) directory,
if it does not already exist.
This script takes the input and output filenames as command line arguments.
Run it from the command line like this:
python script.py papersAboutCoding.tex citations.tex
Blaine Mooers, PhD
Department of Biochemistry and Physiology
College of Medicine
University of Oklahoma Health Sciences Center
Oklahoma City, OK
2024 October 29
"""
def format_citations(strings):
"""Assume that the citekeys (the strings) are in an itemized list without the flanking begin and end statements.
The leading \\item and whitespace are stripped when the strings are read from the file.
The written subsection heading will be unnumbered to reduce clutter.
Because the subsection is unnumbered, it is ignored for inclusion in the table of contents by default.
We have to add it via the \addconents macro on the line below the headline.
The corresponding annotation file is imported from the bibNotes folder in the third line.
"""
formatted_strings = []
for s in strings:
ss = s.lstrip("- ")
formatted_string = f'=== #cite(label("{ss}"), form: "full")\n#include "./bibNotes/{ss}.typ"\n\n'
formatted_strings.append(formatted_string)
return formatted_strings
def create_bib_notes_file(string):
"""Writes out the bibNotes to the bibNotes folder.
This folder could a subfolder or a global folder.
If the latter, replace the '.' with a '~'.
This function will create a bibNotes folder if it does not exist.
It will not overwrite existing bibNotes witn the same citekey."""
directory = "./bibNotes"
if not os.path.exists(directory):
os.makedirs(directory)
file_path = os.path.join(directory, f"{string}.typ")
if not os.path.exists(file_path):
with open(file_path, 'w') as file:
file.write("#include \"../template.typ\": *\n\n")
# *********************** __main__ **************************
if __name__ == "__main__":
# Check if the correct number of command line arguments are provided
if len(sys.argv) != 3:
print("The input file needs to consist of a list in the Markdown format.")
print("Usage: python script.py <input_filename> <output_filename>")
sys.exit(1)
input_filename = sys.argv[1]
output_filename = sys.argv[2]
# Read the strings from the input file
try:
with open(input_filename, 'r') as file:
strings = file.read().splitlines()
except FileNotFoundError:
print(f"The file '{input_filename}' was not found.")
sys.exit(1)
# Format the strings
formatted = format_citations(strings)
# Write the formatted strings to the output file
with open(output_filename, 'w') as file:
for f in formatted:
file.write(f + '\n\n')
# Create bib notes files if they do not exist
for s in strings:
ss = s.lstrip("- ")
create_bib_notes_file(ss)
print(f"Formatted citations have been written to {output_filename} and bib notes files have been created in ./bibNotes directory if they did not already exist.")