@@ -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