-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfCreate.py
69 lines (65 loc) · 2.47 KB
/
pdfCreate.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
from pathlib import Path
import datetime
import argparse
import re
import tempfile
import pdfkit
from bases.utils import render_html
def main():
parser = argparse.ArgumentParser(
prog="Source2PDF",
usage="%(prog)s [options] path",
allow_abbrev=False,
description="Convert source codes listed in given directory to PDF file",
epilog="Happy Coding <3"
)
parser.add_argument("path", metavar="PATH", type=str,
help="path to source codes")
parser.add_argument("-i", "--gitignore", action="store_true",
help="Don't include file listed in gitignore in PDF file")
parser.add_argument("-o", "--output", type=str, action="store",
help="Name for an output file", default="output.pdf")
parser.add_argument("-t", "--title", metavar="PDF TITLE", action="store", type=str,
help="PDF Title eg: C Programming Assignments", default="Assignments")
parser.add_argument("-d", "--date", metavar="DATE", action="store",
help="Date To Mentioned", type=str, default=str(datetime.date.today()))
args = parser.parse_args()
path = Path(args.path).resolve()
if args.gitignore:
gitignore = list(
path.glob("**/.gitignore")
) or list(
(path.parent.glob("**/.gitignore"))
)
if gitignore:
gitignore = gitignore[-1]
with open(gitignore, "r") as f:
ignoredFiles = [
ignoredfile.split("/")[-1] for ignoredfile in f.read().split()
]
else:
ignoredFiles = []
source_codes = []
for file in path.iterdir():
if not (file.name in ignoredFiles) and not file.is_dir():
with open(file, "r") as source_code_fo:
source_code = source_code_fo.read()
comment = re.search(r"/\*([\w\d\s=,;\^/\(\)\+\-\.:!\*'\"\~]+)\*/",source_code).group(1)
source_codes.append(
{
"header": comment.strip().split("\n")[0],
"code": source_code,
}
)
html = render_html(
title=args.title,
source_codes=source_codes,
date=args.date
)
with tempfile.NamedTemporaryFile(delete=True) as tmp:
with open(tmp.name, "w+") as f:
f.write(html)
f.seek(0)
pdfkit.from_file(f, args.output)
if __name__ == "__main__":
main()