##// END OF EJS Templates
the __future__ is now.
the __future__ is now.

File last commit:

r21590:1b9c89e1
r22963:2961b531
Show More
autogen_magics.py
68 lines | 1.9 KiB | text/x-python | PythonLexer
/ docs / autogen_magics.py
Min RK
abspath fixes for autogen scripts
r21590 import os
Thomas Kluyver
Generate documentation of line & cell magics
r18294 from IPython.core.alias import Alias
from IPython.core.interactiveshell import InteractiveShell
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 from IPython.core.magic import MagicAlias
Thomas Kluyver
Generate documentation of line & cell magics
r18294 from IPython.utils.text import dedent, indent
shell = InteractiveShell.instance()
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 magics = shell.magics_manager.magics
Thomas Kluyver
Generate documentation of line & cell magics
r18294
Thomas Kluyver
Various improvements to docs infrastructure for magics
r18296 def _strip_underline(line):
chars = set(line.strip())
if len(chars) == 1 and ('-' in chars or '=' in chars):
return ""
else:
return line
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 def format_docstring(func):
docstring = (func.__doc__ or "Undocumented").rstrip()
Thomas Kluyver
Various improvements to docs infrastructure for magics
r18296 docstring = indent(dedent(docstring))
# Sphinx complains if indented bits have rst headings in, so strip out
# any underlines in the docstring.
lines = [_strip_underline(l) for l in docstring.splitlines()]
return "\n".join(lines)
Thomas Kluyver
Generate documentation of line & cell magics
r18294 output = [
"Line magics",
"===========",
"",
]
Thomas Kluyver
Various improvements to docs infrastructure for magics
r18296 # Case insensitive sort by name
def sortkey(s): return s[0].lower()
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 for name, func in sorted(magics['line'].items(), key=sortkey):
if isinstance(func, Alias) or isinstance(func, MagicAlias):
Thomas Kluyver
Generate documentation of line & cell magics
r18294 # Aliases are magics, but shouldn't be documented here
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 # Also skip aliases to other magics
Thomas Kluyver
Generate documentation of line & cell magics
r18294 continue
Thomas Kluyver
Various improvements to docs infrastructure for magics
r18296 output.extend([".. magic:: {}".format(name),
Thomas Kluyver
Generate documentation of line & cell magics
r18294 "",
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 format_docstring(func),
Thomas Kluyver
Generate documentation of line & cell magics
r18294 ""])
output.extend([
Thomas Kluyver
Various improvements to docs infrastructure for magics
r18296 "Cell magics",
"===========",
Thomas Kluyver
Generate documentation of line & cell magics
r18294 "",
])
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 for name, func in sorted(magics['cell'].items(), key=sortkey):
Thomas Kluyver
Various improvements to docs infrastructure for magics
r18296 if name == "!":
# Special case - don't encourage people to use %%!
continue
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 if func == magics['line'].get(name, 'QQQP'):
Thomas Kluyver
Generate documentation of line & cell magics
r18294 # Don't redocument line magics that double as cell magics
continue
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 if isinstance(func, MagicAlias):
continue
Thomas Kluyver
Various improvements to docs infrastructure for magics
r18296 output.extend([".. cellmagic:: {}".format(name),
Thomas Kluyver
Generate documentation of line & cell magics
r18294 "",
Thomas Kluyver
Skip documenting aliases to other magic commands
r18317 format_docstring(func),
Thomas Kluyver
Generate documentation of line & cell magics
r18294 ""])
Min RK
abspath fixes for autogen scripts
r21590 here = os.path.dirname(__file__)
dest = os.path.join(here, 'source', 'interactive', 'magics-generated.txt')
with open(dest, "w") as f:
f.write("\n".join(output))