@@ -104,6 +104,7 @@ def __call__(self, gallery_conf, script_vars):
104
104
'prefer_full_module' : [],
105
105
'api_usage_ignore' : '.*__.*__' ,
106
106
'show_api_usage' : False ,
107
+ 'copyfile_regex' : None ,
107
108
}
108
109
109
110
logger = sphinx .util .logging .getLogger ('sphinx-gallery' )
@@ -481,7 +482,6 @@ def generate_gallery_rst(app):
481
482
check_spaces_in_filenames (files )
482
483
483
484
for examples_dir , gallery_dir in workdirs :
484
-
485
485
examples_dir_abs_path = os .path .join (app .builder .srcdir , examples_dir )
486
486
gallery_dir_abs_path = os .path .join (app .builder .srcdir , gallery_dir )
487
487
@@ -505,81 +505,84 @@ def generate_gallery_rst(app):
505
505
costs += this_costs
506
506
write_computation_times (gallery_conf , gallery_dir_abs_path , this_costs )
507
507
508
- # We create an index.rst with all examples
509
- # (this will overwrite the rst file generated by the previous call
510
- # to generate_dir_rst)
511
- index_rst_new = os .path .join (gallery_dir_abs_path , 'index.rst.new' )
512
- with codecs .open (index_rst_new , 'w' , encoding = 'utf-8' ) as fhindex :
513
- # :orphan: to suppress "not included in TOCTREE" sphinx warnings
514
- fhindex .write (":orphan:\n \n " + this_content )
515
-
516
- # Write toctree with gallery items from gallery root folder
517
- if len (this_toctree_items ) > 0 :
518
- this_toctree = """
508
+ if this_content :
509
+
510
+ # We create an index.rst with all examples
511
+ # (this will overwrite the rst file generated by the previous call
512
+ # to generate_dir_rst)
513
+ index_rst_new = os .path .join (gallery_dir_abs_path , 'index.rst.new' )
514
+ with codecs .open (index_rst_new , 'w' , encoding = 'utf-8' ) as fhindex :
515
+ # :orphan: to suppress "not included in TOCTREE" sphinx
516
+ # warnings
517
+ fhindex .write (":orphan:\n \n " + this_content )
518
+
519
+ # Write toctree with gallery items from gallery root folder
520
+ if len (this_toctree_items ) > 0 :
521
+ this_toctree = """
519
522
.. toctree::
520
523
:hidden:
521
524
522
525
%s\n
523
526
""" % "\n " .join (this_toctree_items )
524
- fhindex .write (this_toctree )
525
-
526
- # list all paths to subsection index files in this array
527
- subsection_index_files = []
528
-
529
- for subsection in get_subsections (
530
- app . builder . srcdir , examples_dir_abs_path , gallery_conf ) :
531
- src_dir = os .path .join (examples_dir_abs_path , subsection )
532
- target_dir = os .path .join (gallery_dir_abs_path , subsection )
533
- subsection_index_files .append (
534
- '/' .join ([
535
- '' , gallery_dir , subsection , 'index.rst'
536
- ]).replace (os .sep , '/' ) # fwd slashes needed inside rst
537
- )
538
-
539
- (
540
- subsection_index_path ,
541
- subsection_index_content ,
542
- subsection_costs ,
543
- subsection_toctree_filenames ,
544
- ) = generate_dir_rst (
545
- src_dir , target_dir , gallery_conf , seen_backrefs
546
- )
547
-
548
- # Filter out tags from subsection content
549
- # to prevent tag duplication across the documentation
550
- tag_regex = r"^\.\.(\s+)\_(.+)\:(\s*)$"
551
- subsection_index_content = "\n " .join ([
552
- line for line in subsection_index_content .splitlines ()
553
- if re .match (tag_regex , line ) is None
554
- ] + ['' ])
555
-
556
- fhindex .write (subsection_index_content )
557
-
558
- # Write subsection toctree in main file only if
559
- # nested_sections is False or None, and
560
- # toctree filenames were generated for the subsection.
561
- if not gallery_conf ["nested_sections" ]:
562
- if len (subsection_toctree_filenames ) > 0 :
563
- subsection_index_toctree = """
527
+ fhindex .write (this_toctree )
528
+
529
+ # list all paths to subsection index files in this array
530
+ subsection_index_files = []
531
+ subsecs = get_subsections ( app . builder . srcdir ,
532
+ examples_dir_abs_path , gallery_conf )
533
+ for subsection in subsecs :
534
+ src_dir = os .path .join (examples_dir_abs_path , subsection )
535
+ target_dir = os .path .join (gallery_dir_abs_path , subsection )
536
+ subsection_index_files .append (
537
+ '/' .join ([
538
+ '' , gallery_dir , subsection , 'index.rst'
539
+ ]).replace (os .sep , '/' ) # fwd slashes needed in rst
540
+ )
541
+
542
+ (
543
+ subsection_index_path ,
544
+ subsection_index_content ,
545
+ subsection_costs ,
546
+ subsection_toctree_filenames ,
547
+ ) = generate_dir_rst (
548
+ src_dir , target_dir , gallery_conf , seen_backrefs
549
+ )
550
+
551
+ # Filter out tags from subsection content
552
+ # to prevent tag duplication across the documentation
553
+ tag_regex = r"^\.\.(\s+)\_(.+)\:(\s*)$"
554
+ subsection_index_content = "\n " .join ([
555
+ line for line in subsection_index_content .splitlines ()
556
+ if re .match (tag_regex , line ) is None
557
+ ] + ['' ])
558
+
559
+ fhindex .write (subsection_index_content )
560
+
561
+ # Write subsection toctree in main file only if
562
+ # nested_sections is False or None, and
563
+ # toctree filenames were generated for the subsection.
564
+ if not gallery_conf ["nested_sections" ]:
565
+ if len (subsection_toctree_filenames ) > 0 :
566
+ subsection_index_toctree = """
564
567
.. toctree::
565
568
:hidden:
566
569
567
570
%s\n
568
571
""" % "\n " .join (subsection_toctree_filenames )
569
- fhindex .write (subsection_index_toctree )
570
- # Otherwise, a new index.rst.new file should
571
- # have been created and it needs to be parsed
572
- else :
573
- _replace_md5 (subsection_index_path , mode = 't' )
572
+ fhindex .write (subsection_index_toctree )
573
+ # Otherwise, a new index.rst.new file should
574
+ # have been created and it needs to be parsed
575
+ else :
576
+ _replace_md5 (subsection_index_path , mode = 't' )
574
577
575
- costs += subsection_costs
576
- write_computation_times (
577
- gallery_conf , target_dir , subsection_costs
578
- )
578
+ costs += subsection_costs
579
+ write_computation_times (
580
+ gallery_conf , target_dir , subsection_costs
581
+ )
579
582
580
- # generate toctree with subsections
581
- if gallery_conf ["nested_sections" ] is True :
582
- subsections_toctree = """
583
+ # generate toctree with subsections
584
+ if gallery_conf ["nested_sections" ] is True :
585
+ subsections_toctree = """
583
586
.. toctree::
584
587
:hidden:
585
588
:includehidden:
@@ -588,20 +591,42 @@ def generate_gallery_rst(app):
588
591
""" % "\n " .join (subsection_index_files )
589
592
# """ % "\n ".join(this_toctree_items + subsection_index_files)
590
593
591
- # add toctree to file only if there are subsections
592
- if len (subsection_index_files ) > 0 :
593
- fhindex .write (subsections_toctree )
594
+ # add toctree to file only if there are subsections
595
+ if len (subsection_index_files ) > 0 :
596
+ fhindex .write (subsections_toctree )
594
597
595
- if gallery_conf ['download_all_examples' ]:
596
- download_fhindex = generate_zipfiles (
597
- gallery_dir_abs_path , app .builder .srcdir
598
- )
599
- fhindex .write (download_fhindex )
598
+ if gallery_conf ['download_all_examples' ]:
599
+ download_fhindex = generate_zipfiles (
600
+ gallery_dir_abs_path , app .builder .srcdir
601
+ )
602
+ fhindex .write (download_fhindex )
600
603
601
- if (app .config .sphinx_gallery_conf ['show_signature' ]):
602
- fhindex .write (SPHX_GLR_SIG )
604
+ if (app .config .sphinx_gallery_conf ['show_signature' ]):
605
+ fhindex .write (SPHX_GLR_SIG )
606
+
607
+ _replace_md5 (index_rst_new , mode = 't' )
608
+ else :
609
+ # we still need to check each subfolder and generate any
610
+ # galleries, but we don't need to make the rst for the top level.
611
+ subsecs = [subfolder for subfolder in
612
+ os .listdir (examples_dir_abs_path )]
613
+ subsecs = [os .path .basename (s ) for s in subsecs if
614
+ os .path .isdir (os .path .join (examples_dir_abs_path , s ))]
615
+ for subsection in subsecs :
616
+ src_dir = os .path .join (examples_dir_abs_path , subsection )
617
+ target_dir = os .path .join (gallery_dir_abs_path , subsection )
618
+ (
619
+ subsection_index_path ,
620
+ subsection_index_content ,
621
+ subsection_costs ,
622
+ subsection_toctree_filenames ,
623
+ ) = generate_dir_rst (
624
+ src_dir , target_dir , gallery_conf , seen_backrefs
625
+ )
626
+ if (gallery_conf ["nested_sections" ] and
627
+ subsection_index_content ):
628
+ _replace_md5 (subsection_index_path , mode = 't' )
603
629
604
- _replace_md5 (index_rst_new , mode = 't' )
605
630
if gallery_conf ['show_api_usage' ] is not False :
606
631
init_api_usage (app .builder .srcdir )
607
632
_finalize_backreferences (seen_backrefs , gallery_conf )
0 commit comments