@@ -43,6 +43,41 @@ def tabbed(*tabs: tuple[str, str]) -> str:
4343 return "\n " .join (parts )
4444
4545
46+ def add_source (* , source : str , location : str , output : str , language : str , tabs : tuple [str , str ], ** extra : str ) -> str :
47+ """Add source code block to the output.
48+
49+ Parameters:
50+ source: The source code block.
51+ location: Where to add the source (above, below, tabbed-left, tabbed-right, console).
52+ output: The current output.
53+ language: The code language.
54+ tabs: Tabs titles (if used).
55+ **extra: Extra options added back to source code block.
56+
57+ Raises:
58+ ValueError: When the given location is not supported.
59+
60+ Returns:
61+ The updated output.
62+ """
63+ if location == "console" :
64+ return code_block (language , source + "\n " + output , ** extra )
65+
66+ source_block = code_block (language , source , ** extra )
67+ if location == "above" :
68+ return source_block + "\n \n " + output
69+ if location == "below" :
70+ return output + "\n \n " + source_block
71+
72+ source_tab_title , result_tab_title = tabs
73+ if location == "tabbed-left" :
74+ return tabbed ((source_tab_title , source_block ), (result_tab_title , output ))
75+ if location == "tabbed-right" :
76+ return tabbed ((result_tab_title , output ), (source_tab_title , source_block ))
77+
78+ raise ValueError (f"unsupported location for sources: { location } " )
79+
80+
4681# code taken from mkdocstrings, credits to @oprypin
4782class _IdPrependingTreeprocessor (Treeprocessor ):
4883 """Prepend the configured prefix to IDs of all HTML elements."""
0 commit comments