1919
2020# list from https://pygments.org/docs/lexers/#pygments.lexers.python.PythonLexer
2121BUILTIN_BLOCKS = {
22+ "default" : None ,
2223 "python" : None ,
2324 "python3" : None ,
2425 "py" : None ,
@@ -104,7 +105,7 @@ def clean_ipython(source: str) -> tuple[str, str]:
104105
105106
106107class CodeBlockAnalyser (nodes .SparseNodeVisitor ):
107- """Transform literal blocks of Python with links to reference documentation."""
108+ """Transform blocks of Python with links to reference documentation."""
108109
109110 def __init__ (
110111 self ,
@@ -113,6 +114,7 @@ def __init__(
113114 global_preface : list [str ],
114115 custom_blocks : dict [str , Callable [[str ], str ]],
115116 concat_default : bool ,
117+ default_highlight_lang : str | None ,
116118 ** kwargs ,
117119 ) -> None :
118120 super ().__init__ (* args , ** kwargs )
@@ -130,6 +132,7 @@ def __init__(
130132 self .concat_section = False
131133 self .concat_sources = []
132134 self .skip = None
135+ self .highlight_lang = default_highlight_lang
133136
134137 def unknown_visit (self , node ) -> None :
135138 """Handle and delete custom directives, ignore others."""
@@ -162,6 +165,10 @@ def unknown_visit(self, node) -> None:
162165 def unknown_departure (self , node ) -> None :
163166 """Ignore unknown nodes."""
164167
168+ def visit_highlightlang (self , node ) -> None :
169+ """Set expected highlight language."""
170+ self .highlight_lang = node ["lang" ]
171+
165172 def visit_title (self , node ) -> None :
166173 """Track section names and break concatenation and skipping."""
167174 self .title_stack .append (node .astext ())
@@ -185,7 +192,7 @@ def visit_doctest_block(self, node):
185192
186193 def visit_literal_block (self , node : nodes .literal_block ):
187194 """Visit a generic literal block."""
188- return self .parse_source (node , node .get ("language" , None ))
195+ return self .parse_source (node , node .get ("language" , self . highlight_lang ))
189196
190197 def parse_source (
191198 self , node : nodes .literal_block | nodes .doctest_block , language : str | None
0 commit comments