##// END OF EJS Templates
do not show pending deprecation warnign
do not show pending deprecation warnign

File last commit:

r18317:64ec5bd9
r21401:e1b98ee4
Show More
autogen_magics.py
63 lines | 1.9 KiB | text/x-python | PythonLexer
/ docs / autogen_magics.py
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 ""])
with open("source/interactive/magics-generated.txt", "w") as f:
f.write("\n".join(output))