forked from noraj/OSCP-Exam-Report-Template-Markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.py
89 lines (68 loc) · 2.37 KB
/
format.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
#!/usr/bin/env python
import glob
import re
from itertools import islice, zip_longest
OUTPUT_FILE = 'exam/report.md'
INPUT_FILE = 'exam/report-base.md'
EXAM_DIR = 'exam/writeup/'
FULL_DIR = 'exam/writeup/'
IMG_PATTERN = re.compile(r'!\[(.+?)\]\((.+?)\s=.+?\)')
WRAPAT = 92
def in_ignore_list(s, hs):
for i in hs:
if i in s:
return True
return False
def batched(iterable, n):
"Batch data into lists of length n. The last batch may be shorter."
# batched('ABCDEFG', 3) --> ABC DEF G
it = iter(iterable)
while True:
batch = list(islice(it, n))
if not batch:
return
yield batch
def format_it(input_filename, output_filename, exam_dir, full_dir):
# Get original data
data = open(input_filename, 'rb').read()
# Get the sections.
md_parts = []
md_data = b'\n'
for i in md_parts:
md_data += open('{}/{}.md'.format(exam_dir, i), 'rb').read()
md_data += b'\n\n'
lab_md = IMG_PATTERN.sub('![\g<1>]({}\g<2>)'.format(full_dir),
md_data.decode('utf-8')).encode('utf-8')
# Transform the data.
data = data.replace(b'{{REPORT_REPLACE}}', lab_md)
# Replace unicode.
data = data.replace('│'.encode(), b'|')
data = re.sub(r"[^\x00-\x7F]+", "-", data.decode()).encode()
# Automatically add newlines to overly long lines in code blocks.
lines = data.split(b'\n')
processed_lines = []
in_code_block = False
for line in lines:
if line.startswith(b'```'):
# Never allow naked code blocks. Always default to console.
if not in_code_block and line == b'```':
line = b'```console'
in_code_block = not in_code_block
if in_code_block and len(line) > WRAPAT:
# First line
processed_lines.append(line[:WRAPAT])
# Rest of the lines are indented.
indented_lines = batched(line[WRAPAT:], WRAPAT-2)
for i in indented_lines:
processed_lines.append(b' ' + bytes(i))
else:
processed_lines.append(line)
data = b'\n'.join(processed_lines)
# Output the modified data
with open(output_filename, 'wb') as outfile:
outfile.write(data)
def main():
'''Main function'''
format_it(INPUT_FILE, OUTPUT_FILE, EXAM_DIR, FULL_DIR)
if __name__ == '__main__':
main()