forked from man-pages-zh/man2ronn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathman2ronn.py
executable file
·120 lines (105 loc) · 4.31 KB
/
man2ronn.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
'''
Generate ronn-format man pages from standard man pages
AUTHOR: sadhen <[email protected]>
LICENSE: MIT
'''
import sys
import os
import re
from argparse import ArgumentParser
def main(argv=None):
if argv is None:
argv = sys.argv
else:
sys.argv.extend(argv)
parser = ArgumentParser()
parser.add_argument('-i', '--input',
metavar='<man_file>', dest='input',
help="Specifies man input file to convert")
args = parser.parse_args()
try:
with open(args.input, 'r') as f:
text = []
for line in f:
line = line.replace('\-', '-').replace('\n\r', '\n')
line = line.replace('\.', '.').replace('\\e','\\')
line = line.replace('<', '<')
line = re.sub('\\\\fB(.*?)\\\\fR',
lambda obj: "`"+obj.group(1)+"`",
line)
line = re.sub('\\\\fB(.*?)\\\\fP',
lambda obj: "`"+obj.group(1)+"`",
line)
line = re.sub('\\\\fI(.*?)\\\\fR',
lambda obj: "<"+obj.group(1)+">",
line)
line = re.sub('\\\\fI(.*?)\\\\fP',
lambda obj: "<"+obj.group(1)+">",
line)
if line.startswith(r'.\"'):
line = line.replace(r'.\"', '..')
elif line == '.\n':
line = ''
elif line == '.br\n':
text.append(text.pop().rstrip("\n"))
line = "<br>\n"
elif line == '.P\n':
line = "\n"
elif line.startswith('.TH '):
line = line.replace('.TH ', '')
line = line + '='*(len(line)-1) + '\n'
elif line.startswith('.SH '):
line = "\n## " + line.replace('.SH "', '').replace('"\n', '\n')
elif line.startswith('.B '):
line = line.replace('.B ', '**'
).replace('\n', '').rstrip() + '**\n'
elif line.startswith('.I '):
line = line.replace('.I ', '*'
).replace('\n', '').rstrip() + '*\n'
elif line.startswith('.IP '):
line = line.replace('.IP ', '*'
).replace('\n', '').rstrip() + '*\n'
elif line.startswith('.PP'):
line = line.replace('.PP', ''
).replace('\n', '').rstrip() + '\n'
else:
pass
if len(text) > 0:
if text[-1] == ".TP\n":
text.pop()
line = "* " + line
elif text[-1].startswith("* "):
text[-1] = text[-1].rstrip("\n") + ":\n"
line = " " + line
else:
pass
if len(line) > 0:
text.append(line)
for i, line in enumerate(text):
if line[-2:] == '*\n':
text[i] = line.replace('*\n', '* ')
padding = [0] * (len(text) + 1)
pads = 0
for i, line in enumerate(text):
padding[i] = pads
if line.startswith('.RS'):
pads += 2
text[i] = '\n\n'
elif '.RS\n' in line:
text[i] = line.replace('.RS\n', '\n\n')
pads += 2
elif line.startswith('.RE'):
pads -= 2
text[i] = '\n'
elif '.RE\n' in line:
pads -= 2
text[i] = line.replace('.RE\n', '\n')
for i in range(len(text)):
text[i] = text[i].ljust(padding[i])
except IOError:
sys.stderr.write("Cannot load input file: '%s'\n" % args.input)
sys.exit(1)
print(''.join(text))
if __name__ == "__main__":
sys.exit(main())