File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -100,3 +100,19 @@ class FileNameSortKey(_SortKey):
100
100
101
101
def __call__ (self , filename ):
102
102
return filename
103
+
104
+
105
+ class ExampleTitleSortKey (_SortKey ):
106
+ """Sort examples in src_dir by example title.
107
+
108
+ Parameters
109
+ ----------
110
+ src_dir : str
111
+ The source directory.
112
+ """
113
+
114
+ def __call__ (self , filename ):
115
+ src_file = os .path .normpath (os .path .join (self .src_dir , filename ))
116
+ _ , script_blocks = split_code_and_text_blocks (src_file )
117
+ # title should be the second line of the first script_blocks entry
118
+ return script_blocks [0 ][1 ].strip ().split ('\n ' )[1 ]
Original file line number Diff line number Diff line change @@ -200,7 +200,7 @@ def test_example_sorting_default(config_app):
200
200
'within_subsection_order': FileSizeSortKey,
201
201
}""" )
202
202
def test_example_sorting_filesize (config_app ):
203
- """Test sorting of examples by default key (number of code lines) ."""
203
+ """Test sorting of examples by filesize ."""
204
204
_check_order (config_app , 'filesize' )
205
205
206
206
@@ -214,5 +214,19 @@ def test_example_sorting_filesize(config_app):
214
214
'within_subsection_order': FileNameSortKey,
215
215
}""" )
216
216
def test_example_sorting_filename (config_app ):
217
- """Test sorting of examples by default key (number of code lines) ."""
217
+ """Test sorting of examples by filename ."""
218
218
_check_order (config_app , 'filename' )
219
+
220
+
221
+ @pytest .mark .conf_file (content = """
222
+ import sphinx_gallery
223
+ from sphinx_gallery.sorting import ExampleTitleSortKey
224
+ extensions = ['sphinx_gallery.gen_gallery']
225
+ sphinx_gallery_conf = {
226
+ 'examples_dirs': 'src',
227
+ 'gallery_dirs': 'ex',
228
+ 'within_subsection_order': ExampleTitleSortKey,
229
+ }""" )
230
+ def test_example_sorting_title (config_app ):
231
+ """Test sorting of examples by title."""
232
+ _check_order (config_app , 'title' )
You can’t perform that action at this time.
0 commit comments