-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (62 loc) · 3.01 KB
/
main.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
import os
import sys
import pyperclip
inputminuted = False
if __name__ == "__main__":
root_path = None
# Read path from given command-line argument
if len(sys.argv) > 0:
arg_path = ' '.join(sys.argv[1:])
if os.path.exists(arg_path):
root_path = arg_path
# If no argument was given, use base directory
if not root_path:
root_path = os.path.dirname(os.path.realpath(__file__))
# pathname = os.path.join(root_path, "src")
pathname = root_path
latex_output = " \\subsection{Code} \label{code}\n\n"
for root, dirs, files in os.walk(pathname, topdown=True):
for filename in files:
filepath = os.path.join(root, filename)
if "." not in filename or filename.split(".")[1] in ["png", "svg", "puml", "gitignore", "properties"] or any(x in filepath for x in ["cmake-build-debug", ".idea"]):
continue
# If it's a png or smthn, we can't read it, so read it first.
file_code = ""
try:
with open(filepath, "r", encoding="utf-8") as file:
file_code = file.read()
except Exception as e:
continue
minted_language = str(filename.split(".")[1])
if minted_language == "py":
minted_language = "python"
elif minted_language == "h":
minted_language = "c"
elif minted_language == "yml":
minted_language = "yaml"
elif minted_language == "txt":
minted_language = "text"
elif minted_language in ["conf", "service", "db"]:
minted_language = "text"
filepath_document = (filepath.replace(pathname, "")).replace("\\", "/")
if filepath_document.startswith("/"):
filepath_document = filepath_document[1:]
latex_output = str(
f"{latex_output}"
" \\subsubsection{"+filepath_document+"} \label{"+filename.lower().replace(".", "_")+"}\n"
"\n"
" \\begin{longlisting}\n"
" \\centering\n" +
(" \\begin{minted}[\n" if not inputminuted else " \\inputminted[\n") +
" framesep=4mm,\n"
" baselinestretch=1,\n"
" breaklines,\n"
" breakanywhere\n"
" ]{"+minted_language+"}" + (("{project_files/"+(filepath.replace(root_path+"\\src\\", "")).replace("\\", "/")+"}\n") if inputminuted else f"\n{file_code}\n \\end{'{minted}'}\n") +
" \\caption{"+filename+"}\n"
" \\label{lst:"+filename.lower().replace(".", "_")+"}\n"
" \\end{longlisting}\n"
"\n"
)
pyperclip.copy(latex_output)
print("Output copied to clipboard!")