Skip to content

Commit 9ad7c1f

Browse files
committed
Add option debug to runpython
1 parent eb0f880 commit 9ad7c1f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

CHANGELOGS.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Change Logs
22
===========
33

4+
0.4.0
5+
+++++
6+
7+
* :pr:``: add option ``:debug:`` to runpython to append rst code to a page
8+
49
0.3.0
510
+++++
611

sphinx_runpython/runpython/sphinx_runpython_extension.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ class RunPythonDirective(Directive):
541541
this trick is needed when the script to executes relies on
542542
function such :epkg:`*py:inspect:getsource` which requires
543543
the script to be stored somewhere in order to retrieve it.
544+
* ``:debug:`` if ``:rst:`` is used, it shows some information to help
545+
debugging.
544546
545547
Option *rst* can be used the following way::
546548
@@ -611,6 +613,7 @@ class RunPythonDirective(Directive):
611613
"numpy_precision": directives.unchanged,
612614
"store_in_file": directives.unchanged,
613615
"linenos": directives.unchanged,
616+
"debug": directives.unchanged,
614617
}
615618
has_content = True
616619
runpython_class = runpython_node
@@ -650,6 +653,7 @@ def run(self):
650653
"linenos": "linenos" in self.options,
651654
"showout": "showout" in self.options,
652655
"rst": "rst" in self.options,
656+
"debug": "debug" in self.options,
653657
"sin": self.options.get("sin", TITLES[language_code]["In"]),
654658
"sout": self.options.get("sout", TITLES[language_code]["Out"]),
655659
"sout2": self.options.get("sout2", TITLES[language_code]["Out2"]),
@@ -897,9 +901,24 @@ def add_indent(content, nbind):
897901
elif len(p["sout"]) > 0:
898902
secout += nodes.paragraph(text=p["sout"])
899903

904+
content_rows = content.replace("\r", "").split("\n")
905+
906+
if p["debug"]:
907+
full_content = "\n".join(
908+
f" {i+1:05d}: {row}" for i, row in enumerate(content_rows)
909+
)
910+
content_rows.extend(["", f"*n_rows: {len(content_rows)}*"])
911+
for i, row in enumerate(content_rows):
912+
if set(row) in ({"+"}, {"="}, {"-"}):
913+
title = content_rows[i - 1]
914+
content_rows.append(f"* *{i}:{title}*")
915+
content_rows.extend(
916+
["", "", ".. code-block:: text", "", full_content, ""]
917+
)
918+
900919
try:
901920
if p["sphinx"]:
902-
st = StringList(content.replace("\r", "").split("\n"))
921+
st = StringList(content_rows)
903922
nested_parse_with_titles(self.state, st, secout)
904923
dt = None
905924
else:

0 commit comments

Comments
 (0)