-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_seealso.py
58 lines (44 loc) · 1.25 KB
/
extract_seealso.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
#!/usr/bin/env python3
# This script walks through a man output file (e.g., man ./foobar > output),
# and extracts just the text following a line beginning with "See also"
#
import re
import sys
import os
from pathlib import Path
APPNAME=os.path.basename(__file__)
DIRNAME=os.path.dirname(__file__)
def usage():
print(f"{APPNAME} <input_file> [<output_file>]\n")
# Get input and optional output files
in_fname = sys.argv[1]
CMDNAME = os.path.basename(in_fname).rsplit('.',100)[0]
PARENTPATH = os.path.dirname(in_fname)
out_fname=""
if (len(sys.argv) > 2):
out_fname=sys.argv[2]
output_lines = list()
# Read input as an array of lines
with open(in_fname) as fp:
in_lines = fp.readlines()
# seealso
seealso = re.compile("[ ]*see also", flags = re.IGNORECASE )
SEEALSO=False
seealsolist=""
# Walk through all the lines, looking for "see also".
# Output all lines after "See Also"
for i in range(len(in_lines)):
curline = in_lines[i].rstrip()
curline = curline.lstrip()
if seealso.match(curline):
SEEALSO=True
if SEEALSO:
output_lines.append(curline)
if (out_fname):
with open(out_fname,'w') as outfile:
sys.stdout = outfile
for line in output_lines:
print(line)
else:
for line in output_lines:
print(line)