-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
58 lines (46 loc) · 1.92 KB
/
convert.py
File metadata and controls
58 lines (46 loc) · 1.92 KB
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
import argparse
import os
import subprocess
import zipfile
parser = argparse.ArgumentParser(
prog="convert.py",
description="converts lecture notes in adoc format into either HTML or PDF",
)
parser.add_argument("-dirpath", "-d")
parser.add_argument("-outname", "-o")
parser.add_argument("-filetype", "-t")
args = parser.parse_args()
target, outname, filetype = (args.dirpath, args.outname, args.filetype)
# Filter for lecture directories
lectures = filter(lambda d: d.startswith("lec"), os.listdir(target))
for lecture in lectures:
# Create full path to lecture directory
lecture_path = os.path.join(target, lecture)
# Filter for .adoc files in the lecture directory
adocs = filter(lambda f: f.endswith(".adoc"), os.listdir(lecture_path))
for adoc in adocs:
# Create full path to .adoc file
adoc_path = os.path.join(lecture_path, adoc)
match filetype:
case "html":
subprocess.run(["asciidoctor", adoc_path])
case "pdf":
subprocess.run(["asciidoctor-pdf", adoc_path])
case _:
raise ValueError(
f"filetype must be one of html, pdf but found {filetype}"
)
zip_filename = f"{outname}.zip"
# Now, create a ZIP file with all converted files
with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as zipf:
# Walk through all directories and subdirectories
for root, dirs, files in os.walk(target):
for file in files:
if file.endswith(f".{filetype}"):
# Get the full path to the converted file
file_path = os.path.join(root, file)
# Create a relative path for the file in the ZIP
# This preserves the directory structure within the ZIP
rel_path = os.path.relpath(file_path, start=target)
# Add the file to the ZIP
zipf.write(file_path, rel_path)