Debian Codegraph @

man/make_man_page.py

raw
1 #!/usr/bin/env python
2
3 # Creates a man page from a C file.
4
5 # Comments beginning with `/**` are treated as Groff man, except that
6 # 'this' is converted to \fIthis\f[], and ''this'' to \fBthis\f[].
7
8 # Non-blank lines immediately following a man page comment are treated
9 # as function signatures or examples and parsed into .Ft, .Fo, .Fa, .Fc. The
10 # immediately preceding man documentation chunk is printed after the example
11 # as a comment on it.
12
13 # That's about it!
14
15 import sys, re, os, platform
16 from datetime import date
17 from ctypes import CDLL, c_char_p, c_long, c_void_p
18
19 sysname = platform.system()
20
21 curdir = os.getcwd()
22
23 if sysname == 'Darwin':
24 cmark = CDLL(curdir + "/build/src/libcmark.dylib")
25 else:
26 cmark = CDLL(curdir + "/build/src/libcmark.so")
27
28 parse_document = cmark.cmark_parse_document
29 parse_document.restype = c_void_p
30 parse_document.argtypes = [c_char_p, c_long]
31
32 render_man = cmark.cmark_render_man
33 render_man.restype = c_char_p
34 render_man.argtypes = [c_void_p, c_long, c_long]
35
36 cmark_version_string = cmark.cmark_version_string
37 cmark_version_string.restype = c_char_p
38 cmark_version_string.argtypes = []
39
40 def md2man(text):
41 if sys.version_info >= (3,0):
42 textbytes = text.encode('utf-8')
43 textlen = len(textbytes)
44 return render_man(parse_document(textbytes, textlen), 0, 65).decode('utf-8')
45 else:
46 textbytes = text
47 textlen = len(text)
48 return render_man(parse_document(textbytes, textlen), 0, 72)
49
50 comment_start_re = re.compile('^\/\*\* ?')
51 comment_delim_re = re.compile('^[/ ]\** ?')
52 comment_end_re = re.compile('^ \**\/')
53 function_re = re.compile('^ *(?:CMARK_EXPORT\s+)?(?P<type>(?:const\s+)?\w+(?:\s*[*])?)\s*(?P<name>\w+)\s*\((?P<args>[^)]*)\)')
54 blank_re = re.compile('^\s*$')
55 macro_re = re.compile('CMARK_EXPORT *')
56 typedef_start_re = re.compile('typedef.*{$')
57 typedef_end_re = re.compile('}')
58 single_quote_re = re.compile("(?<!\w)'([^']+)'(?!\w)")
59 double_quote_re = re.compile("(?<!\w)''([^']+)''(?!\w)")
60
61 def handle_quotes(s):
62 return re.sub(double_quote_re, '**\g<1>**', re.sub(single_quote_re, '*\g<1>*', s))
63
64 typedef = False
65 mdlines = []
66 chunk = []
67 sig = []
68
69 if len(sys.argv) > 1:
70 sourcefile = sys.argv[1]
71 else:
72 print("Usage: make_man_page.py sourcefile")
73 exit(1)
74
75 with open(sourcefile, 'r') as cmarkh:
76 state = 'default'
77 for line in cmarkh:
78 # state transition
79 oldstate = state
80 if comment_start_re.match(line):
81 state = 'man'
82 elif comment_end_re.match(line) and state == 'man':
83 continue
84 elif comment_delim_re.match(line) and state == 'man':
85 state = 'man'
86 elif not typedef and blank_re.match(line):
87 state = 'default'
88 elif typedef and typedef_end_re.match(line):
89 typedef = False
90 elif typedef_start_re.match(line):
91 typedef = True
92 state = 'signature'
93 elif state == 'man':
94 state = 'signature'
95
96 # handle line
97 if state == 'man':
98 chunk.append(handle_quotes(re.sub(comment_delim_re, '', line)))
99 elif state == 'signature':
100 ln = re.sub(macro_re, '', line)
101 if typedef or not re.match(blank_re, ln):
102 sig.append(ln)
103 elif oldstate == 'signature' and state != 'signature':
104 if len(mdlines) > 0 and mdlines[-1] != '\n':
105 mdlines.append('\n')
106 rawsig = ''.join(sig)
107 m = function_re.match(rawsig)
108 mdlines.append('.PP\n')
109 if m:
110 mdlines.append('\\fI' + m.group('type') + '\\f[]' + ' ')
111 mdlines.append('\\fB' + m.group('name') + '\\f[]' + '(')
112 first = True
113 for argument in re.split(',', m.group('args')):
114 if not first:
115 mdlines.append(', ')
116 first = False
117 mdlines.append('\\fI' + argument.strip() + '\\f[]')
118 mdlines.append(')\n')
119 else:
120 mdlines.append('.nf\n\\fC\n.RS 0n\n')
121 mdlines += sig
122 mdlines.append('.RE\n\\f[]\n.fi\n')
123 if len(mdlines) > 0 and mdlines[-1] != '\n':
124 mdlines.append('\n')
125 mdlines += md2man(''.join(chunk))
126 mdlines.append('\n')
127 chunk = []
128 sig = []
129 elif oldstate == 'man' and state != 'signature':
130 if len(mdlines) > 0 and mdlines[-1] != '\n':
131 mdlines.append('\n')
132 mdlines += md2man(''.join(chunk)) # add man chunk
133 chunk = []
134 mdlines.append('\n')
135
136 sys.stdout.write('.TH ' + os.path.basename(sourcefile).replace('.h','') + ' 3 "' + date.today().strftime('%B %d, %Y') + '" "cmark ' + cmark_version_string().decode('utf-8') + '" "Library Functions Manual"\n')
137 sys.stdout.write(''.join(mdlines))