-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdoc2md.py
More file actions
executable file
·325 lines (266 loc) · 8.92 KB
/
Copy pathdoc2md.py
File metadata and controls
executable file
·325 lines (266 loc) · 8.92 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#! /usr/bin/env python
# encoding: utf-8
r"""
Simplistic utility to extract docstrings from a module or class and throw
them into a simple [GitHub Flavoured Markdown](md) document. Its purpose is
to quickly generate `README.md` files for small projects.
[md]: https://help.github.com/articles/github-flavored-markdown
### Project status
I stopped using this package and therefore will not push any updates (I now
usually write README.rst manually). Nonetheless, you may still find it useful.
Should you encounter bugs or have improvements, feel free to submit a PR. If
you want to take over maintenance, feel free to contact me.
For a more feature-rich and well maintained alternative, see:
- https://github.com/NiklasRosenstein/pydoc-markdown/ (I didn't try it)
### Installation
No installation necessary. However, if you want:
$ pip install doc2md
### Usage
You can run this script from the command line like:
$ doc2md.py [-a] [--no-toc] [-t title] [-d depth] module-name [class-name] \
> README.md
At the moment this is suited only for a very specific use case. It is
hardly forseeable, if I will decide to improve on it in the near future.
For a simple example output document, see the generated README (i.e. the
github frontpage). It is extracted from the `doc2md.py` file using this
very utility:
$ ./doc2md.py -a -d1 doc2md > README.md
### License
Copyright © 2013-2017 Thomas Gläßle <t_glaessle@gmx.de>
This work is free. You can redistribute it and/or modify it under the
terms of the MIT license. See the COPYING file for more details.
This program is free software. It comes without any warranty, to the
extent permitted by applicable law.
"""
import re
import sys
import inspect
__all__ = ['doctrim', 'doc2md']
doctrim = inspect.cleandoc
def unindent(lines):
"""
Remove common indentation from string.
Unlike doctrim there is no special treatment of the first line.
"""
try:
# Determine minimum indentation:
indent = min(len(line) - len(line.lstrip())
for line in lines if line)
except ValueError:
return lines
else:
return [line[indent:] for line in lines]
def code_block(lines, language=''):
"""
Mark the code segment for syntax highlighting.
"""
return ['```' + language] + unindent(lines) + ['```']
def doctest2md(lines):
"""
Convert the given doctest to a syntax highlighted markdown segment.
"""
is_only_code = True
lines = unindent(lines)
for line in lines:
if not line.startswith('>>> ') and not line.startswith('... ') and line not in ['>>>', '...']:
is_only_code = False
break
if is_only_code:
orig = lines
lines = []
for line in orig:
lines.append(line[4:])
return lines
def doc_code_block(lines, language):
if language == 'python':
lines = doctest2md(lines)
return code_block(lines, language)
_reg_section = re.compile('^#+ ')
def is_heading(line):
return _reg_section.match(line)
def get_heading(line):
assert is_heading(line)
part = line.partition(' ')
return len(part[0]), part[2]
def make_heading(level, title):
return '#'*max(level, 1) + ' ' + title
def find_sections(lines):
"""
Find all section names and return a list with their names.
"""
sections = []
for line in lines:
if is_heading(line):
sections.append(get_heading(line))
return sections
def make_toc(sections, maxdepth=0):
"""
Generate table of contents for array of section names.
"""
if not sections:
return []
outer = min(n for n,t in sections)
refs = []
for ind,sec in sections:
if maxdepth and ind-outer+1 > maxdepth:
continue
ref = sec.lower()
ref = ref.replace('`', '')
ref = ref.replace(' ', '-')
ref = ref.replace('?', '')
refs.append(" "*(ind-outer) + "- [%s](#%s)" % (sec, ref))
return refs
def _doc2md(lines, shiftlevel=0):
md = []
is_code = False
for line in lines:
trimmed = line.lstrip()
if is_code:
if line:
code.append(line)
else:
is_code = False
md += doc_code_block(code, language)
md += [line]
elif trimmed.startswith('>>> '):
is_code = True
language = 'python'
code = [line]
elif trimmed.startswith('$ '):
is_code = True
language = 'bash'
code = [line]
elif shiftlevel != 0 and is_heading(line):
level, title = get_heading(line)
md += [make_heading(level + shiftlevel, title)]
else:
md += [line]
if is_code:
md += doc_code_block(code, language)
return md
def doc2md(docstr, title, min_level=1, more_info=False, toc=True, maxdepth=0):
"""
Convert a docstring to a markdown text.
"""
text = doctrim(docstr)
lines = text.split('\n')
sections = find_sections(lines)
if sections:
level = min(n for n,t in sections) - 1
else:
level = 1
shiftlevel = 0
if level < min_level:
shiftlevel = min_level - level
level = min_level
sections = [(lev+shiftlevel, tit) for lev,tit in sections]
head = next((i for i, l in enumerate(lines) if is_heading(l)), 0)
md = [
make_heading(level, title),
"",
] + lines[:head]
if toc:
md += make_toc(sections, maxdepth)
md += ['']
md += _doc2md(lines[head:], shiftlevel)
if more_info:
return (md, sections)
else:
return "\n".join(md)
def mod2md(module, title, title_api_section, toc=True, maxdepth=0):
"""
Generate markdown document from module, including API section.
"""
docstr = module.__doc__
text = doctrim(docstr)
lines = text.split('\n')
sections = find_sections(lines)
if sections:
level = min(n for n,t in sections) - 1
else:
level = 1
api_md = []
api_sec = []
if title_api_section and module.__all__:
sections.append((level+1, title_api_section))
for name in module.__all__:
api_sec.append((level+2, "`" + name + "`"))
api_md += ['', '']
entry = module.__dict__[name]
if entry.__doc__:
md, sec = doc2md(entry.__doc__, "`" + name + "`",
min_level=level+2, more_info=True, toc=False)
api_sec += sec
api_md += md
sections += api_sec
# headline
head = next((i for i, l in enumerate(lines) if is_heading(l)), 0)
md = [
make_heading(level, title),
"",
] + lines[:head]
# main sections
if toc:
md += make_toc(sections, maxdepth)
md += ['']
md += _doc2md(lines[head:])
# API section
md += [
'',
'',
make_heading(level+1, title_api_section),
]
if toc:
md += ['']
md += make_toc(api_sec, 1)
md += api_md
return "\n".join(md)
def main(args=None):
# parse the program arguments
import argparse
parser = argparse.ArgumentParser(
description='Convert docstrings to markdown.')
parser.add_argument(
'module', help='The module containing the docstring.')
group = parser.add_mutually_exclusive_group()
group.add_argument(
'entry', nargs='?',
help='Convert only docstring of this entry in module.')
group.add_argument(
'-a', '--all', dest='all', action='store_true',
help='Create an API section with the contents of module.__all__.')
parser.add_argument(
'-t', '--title', dest='title',
help='Document title (default is module name)')
parser.add_argument(
'--no-toc', dest='toc', action='store_false', default=True,
help='Do not automatically generate the TOC')
parser.add_argument(
'-d', '--depth', dest='depth', type=int, default=0,
help='Max subsection level in TOC')
args = parser.parse_args(args)
import importlib
import inspect
import os
def add_path(*pathes):
for path in reversed(pathes):
if path not in sys.path:
sys.path.insert(0, path)
file = inspect.getfile(inspect.currentframe())
add_path(os.path.realpath(os.path.abspath(os.path.dirname(file))))
add_path(os.getcwd())
mod_name = args.module
if mod_name.endswith('.py'):
mod_name = mod_name.rsplit('.py', 1)[0]
title = args.title or mod_name.replace('_', '-')
module = importlib.import_module(mod_name)
if args.all:
print(mod2md(module, title, 'API', toc=args.toc, maxdepth=args.depth))
else:
if args.entry:
docstr = module.__dict__[args.entry].__doc__
else:
docstr = module.__doc__
print(doc2md(docstr, title, toc=args.toc, maxdepth=args.depth))
if __name__ == "__main__":
main()