Skip to content

Commit 4722296

Browse files
committed
Automatic support for ipython3 code blocks
1 parent 7110adf commit 4722296

File tree

1 file changed

+11
-2
lines changed
  • src/sphinx_codeautolink/extension

1 file changed

+11
-2
lines changed

src/sphinx_codeautolink/extension/block.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def clean_pycon(source: str) -> Tuple[str, str]:
5353
return source, '\n'.join(clean_lines)
5454

5555

56+
def clean_ipython(source: str) -> Tuple[str, str]:
57+
"""Clean up IPython syntax to pure Python."""
58+
from IPython.core.inputtransformer2 import TransformerManager
59+
return source, TransformerManager().transform_cell(source)
60+
61+
5662
class CodeBlockAnalyser(nodes.SparseNodeVisitor):
5763
"""Transform literal blocks of Python with links to reference documentation."""
5864

@@ -70,7 +76,10 @@ def __init__(
7076
relative_path = Path(self.document['source']).relative_to(source_dir)
7177
self.current_document = str(relative_path.with_suffix(''))
7278
self.global_preface = global_preface
73-
self.custom_blocks = {'pycon': clean_pycon}
79+
self.custom_blocks = {
80+
'pycon': clean_pycon,
81+
'ipython3': clean_ipython,
82+
}
7483
self.custom_blocks.update(custom_blocks)
7584
self.valid_blocks = ('py', 'python') + tuple(self.custom_blocks.keys())
7685
self.title_stack = []
@@ -230,7 +239,7 @@ def link_html(
230239
text = document.read_text('utf-8')
231240
soup = BeautifulSoup(text, 'html.parser')
232241

233-
block_types = {'python', 'pycon'} | set(custom_blocks.keys())
242+
block_types = {'python', 'pycon', 'ipython3'} | set(custom_blocks.keys())
234243
classes = [f'highlight-{t}' for t in block_types] + ['doctest']
235244
classes += search_css_classes
236245

0 commit comments

Comments
 (0)