Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7e00c2

Browse files
committed
ENH: Add ExampleTitleSortKey
1 parent 3967e60 commit d7e00c2

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

sphinx_gallery/sorting.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,19 @@ class FileNameSortKey(_SortKey):
100100

101101
def __call__(self, filename):
102102
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]

sphinx_gallery/tests/test_gen_gallery.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_example_sorting_default(config_app):
200200
'within_subsection_order': FileSizeSortKey,
201201
}""")
202202
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."""
204204
_check_order(config_app, 'filesize')
205205

206206

@@ -214,5 +214,19 @@ def test_example_sorting_filesize(config_app):
214214
'within_subsection_order': FileNameSortKey,
215215
}""")
216216
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."""
218218
_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')

0 commit comments

Comments
 (0)