Show More
@@ -1,68 +1,66 b'' | |||
|
1 | ||
|
2 | 1 |
|
|
3 | 2 | from IPython.core.alias import Alias |
|
4 | 3 | from IPython.core.interactiveshell import InteractiveShell |
|
5 | 4 | from IPython.core.magic import MagicAlias |
|
6 | 5 | from IPython.utils.text import dedent, indent |
|
7 | 6 | |
|
8 | 7 | shell = InteractiveShell.instance() |
|
9 | 8 | magics = shell.magics_manager.magics |
|
10 | 9 | |
|
11 | 10 | def _strip_underline(line): |
|
12 | 11 | chars = set(line.strip()) |
|
13 | 12 | if len(chars) == 1 and ("-" in chars or "=" in chars): |
|
14 | 13 | return "" |
|
15 | 14 | else: |
|
16 | 15 | return line |
|
17 | 16 | |
|
18 | 17 | def format_docstring(func): |
|
19 | 18 | docstring = (func.__doc__ or "Undocumented").rstrip() |
|
20 | 19 | docstring = indent(dedent(docstring)) |
|
21 | 20 | # Sphinx complains if indented bits have rst headings in, so strip out |
|
22 | 21 | # any underlines in the docstring. |
|
23 | 22 | lines = [_strip_underline(l) for l in docstring.splitlines()] |
|
24 | 23 | return "\n".join(lines) |
|
25 | 24 | |
|
26 | 25 | output = [ |
|
27 | 26 | "Line magics", |
|
28 | 27 | "===========", |
|
29 | 28 | "", |
|
30 | 29 | ] |
|
31 | 30 | |
|
32 | 31 | # Case insensitive sort by name |
|
33 | 32 | def sortkey(s): return s[0].lower() |
|
34 | 33 | |
|
35 | 34 | for name, func in sorted(magics["line"].items(), key=sortkey): |
|
36 | 35 | if isinstance(func, Alias) or isinstance(func, MagicAlias): |
|
37 | 36 | # Aliases are magics, but shouldn't be documented here |
|
38 | 37 | # Also skip aliases to other magics |
|
39 | 38 | continue |
|
40 | 39 | output.extend([".. magic:: {}".format(name), |
|
41 | 40 | "", |
|
42 | 41 | format_docstring(func), |
|
43 | 42 | ""]) |
|
44 | 43 | |
|
45 | 44 | output.extend([ |
|
46 | 45 | "Cell magics", |
|
47 | 46 | "===========", |
|
48 | 47 | "", |
|
49 | 48 | ]) |
|
50 | 49 | |
|
51 | 50 | for name, func in sorted(magics["cell"].items(), key=sortkey): |
|
52 | 51 | if name == "!": |
|
53 | 52 | # Special case - don't encourage people to use %%! |
|
54 | 53 | continue |
|
55 | 54 | if func == magics["line"].get(name, "QQQP"): |
|
56 | 55 | # Don't redocument line magics that double as cell magics |
|
57 | 56 | continue |
|
58 | 57 | if isinstance(func, MagicAlias): |
|
59 | 58 | continue |
|
60 | 59 | output.extend([".. cellmagic:: {}".format(name), |
|
61 | 60 | "", |
|
62 | 61 | format_docstring(func), |
|
63 | 62 | ""]) |
|
64 | 63 | |
|
65 | ||
|
66 | 64 | src_path = Path(__file__).parent |
|
67 | 65 | dest = src_path.joinpath("source", "interactive", "magics-generated.txt") |
|
68 | 66 | dest.write_text("\n".join(output)) |
General Comments 0
You need to be logged in to leave comments.
Login now