-
Notifications
You must be signed in to change notification settings - Fork 211
ENH: allow rst files to pass through #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ def __call__(self, gallery_conf, script_vars): | |
'prefer_full_module': [], | ||
'api_usage_ignore': '.*__.*__', | ||
'show_api_usage': False, | ||
'copyfile_regex': '', | ||
} | ||
|
||
logger = sphinx.util.logging.getLogger('sphinx-gallery') | ||
|
@@ -397,7 +398,7 @@ def call_memory(func): | |
return gallery_conf | ||
|
||
|
||
def get_subsections(srcdir, examples_dir, gallery_conf): | ||
def get_subsections(srcdir, examples_dir, gallery_conf, check_for_index=True): | ||
"""Return the list of subsections of a gallery. | ||
|
||
Parameters | ||
|
@@ -408,16 +409,25 @@ def get_subsections(srcdir, examples_dir, gallery_conf): | |
path to the examples directory relative to conf.py | ||
gallery_conf : dict | ||
The gallery configuration. | ||
check_for_index : bool | ||
only return subfolders with a ReadMe, default True | ||
|
||
Returns | ||
------- | ||
out : list | ||
sorted list of gallery subsection folder names | ||
""" | ||
sortkey = gallery_conf['subsection_order'] | ||
subfolders = [subfolder for subfolder in os.listdir(examples_dir) | ||
if _get_readme(os.path.join(examples_dir, subfolder), | ||
gallery_conf, raise_error=False) is not None] | ||
subfolders = [subfolder for subfolder in os.listdir(examples_dir)] | ||
if check_for_index: | ||
subfolders = [subfolder for subfolder in subfolders | ||
if _get_readme(os.path.join(examples_dir, subfolder), | ||
gallery_conf, raise_error=False) | ||
is not None] | ||
else: | ||
# just make sure its a directory | ||
subfolders = [subfolder for subfolder in subfolders | ||
if os.path.isdir(os.path.join(examples_dir, subfolder))] | ||
base_examples_dir_path = os.path.relpath(examples_dir, srcdir) | ||
subfolders_with_path = [os.path.join(base_examples_dir_path, item) | ||
for item in subfolders] | ||
|
@@ -447,6 +457,24 @@ def _prepare_sphx_glr_dirs(gallery_conf, srcdir): | |
return list(zip(examples_dirs, gallery_dirs)) | ||
|
||
|
||
def _format_toctree(items, includehidden=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refactored this out because the lack of indentation was making it hard to follow. |
||
"""Format a toc tree""" | ||
|
||
st = """ | ||
.. toctree:: | ||
:hidden: | ||
""" | ||
if includehidden: | ||
st += """ | ||
:includehidden: | ||
""" | ||
st += """ | ||
|
||
%s\n""" % "\n ".join(items) | ||
|
||
return st | ||
|
||
|
||
def generate_gallery_rst(app): | ||
"""Generate the Main examples gallery reStructuredText | ||
|
||
|
@@ -481,7 +509,6 @@ def generate_gallery_rst(app): | |
check_spaces_in_filenames(files) | ||
|
||
for examples_dir, gallery_dir in workdirs: | ||
|
||
examples_dir_abs_path = os.path.join(app.builder.srcdir, examples_dir) | ||
gallery_dir_abs_path = os.path.join(app.builder.srcdir, gallery_dir) | ||
|
||
|
@@ -502,49 +529,52 @@ def generate_gallery_rst(app): | |
include_toctree=False, | ||
) | ||
|
||
has_readme = this_content is not None | ||
costs += this_costs | ||
write_computation_times(gallery_conf, gallery_dir_abs_path, this_costs) | ||
|
||
# We create an index.rst with all examples | ||
# (this will overwrite the rst file generated by the previous call | ||
# to generate_dir_rst) | ||
index_rst_new = os.path.join(gallery_dir_abs_path, 'index.rst.new') | ||
with codecs.open(index_rst_new, 'w', encoding='utf-8') as fhindex: | ||
# :orphan: to suppress "not included in TOCTREE" sphinx warnings | ||
fhindex.write(":orphan:\n\n" + this_content) | ||
|
||
# Write toctree with gallery items from gallery root folder | ||
if len(this_toctree_items) > 0: | ||
this_toctree = """ | ||
.. toctree:: | ||
:hidden: | ||
|
||
%s\n | ||
""" % "\n ".join(this_toctree_items) | ||
fhindex.write(this_toctree) | ||
|
||
# list all paths to subsection index files in this array | ||
subsection_index_files = [] | ||
|
||
for subsection in get_subsections( | ||
app.builder.srcdir, examples_dir_abs_path, gallery_conf): | ||
src_dir = os.path.join(examples_dir_abs_path, subsection) | ||
target_dir = os.path.join(gallery_dir_abs_path, subsection) | ||
subsection_index_files.append( | ||
'/'.join([ | ||
'', gallery_dir, subsection, 'index.rst' | ||
]).replace(os.sep, '/') # fwd slashes needed inside rst | ||
) | ||
|
||
( | ||
subsection_index_path, | ||
subsection_index_content, | ||
subsection_costs, | ||
subsection_toctree_filenames, | ||
) = generate_dir_rst( | ||
src_dir, target_dir, gallery_conf, seen_backrefs | ||
) | ||
|
||
if this_content: | ||
# :orphan: to suppress "not included in TOCTREE" sphinx warnings | ||
indexst = ":orphan:\n\n" + this_content | ||
else: | ||
# we are not going to use the index.rst.new that gets made here, | ||
# but go through the motions to run through all the subsections... | ||
indexst = 'Never used!' | ||
|
||
# Write toctree with gallery items from gallery root folder | ||
if len(this_toctree_items) > 0: | ||
this_toctree = _format_toctree(this_toctree_items) | ||
|
||
indexst += this_toctree | ||
|
||
# list all paths to subsection index files in this array | ||
subsection_index_files = [] | ||
subsecs = get_subsections(app.builder.srcdir, | ||
examples_dir_abs_path, gallery_conf, | ||
check_for_index=has_readme) | ||
for subsection in subsecs: | ||
src_dir = os.path.join(examples_dir_abs_path, subsection) | ||
target_dir = os.path.join(gallery_dir_abs_path, subsection) | ||
subsection_index_files.append( | ||
'/'.join([ | ||
'', gallery_dir, subsection, 'index.rst' | ||
]).replace(os.sep, '/') # fwd slashes needed in rst | ||
) | ||
|
||
( | ||
subsection_index_path, | ||
subsection_index_content, | ||
subsection_costs, | ||
subsection_toctree_filenames, | ||
) = generate_dir_rst( | ||
src_dir, target_dir, gallery_conf, seen_backrefs | ||
) | ||
|
||
if subsection_index_content: | ||
# Filter out tags from subsection content | ||
# to prevent tag duplication across the documentation | ||
tag_regex = r"^\.\.(\s+)\_(.+)\:(\s*)$" | ||
|
@@ -553,55 +583,50 @@ def generate_gallery_rst(app): | |
if re.match(tag_regex, line) is None | ||
] + ['']) | ||
|
||
fhindex.write(subsection_index_content) | ||
|
||
# Write subsection toctree in main file only if | ||
# nested_sections is False or None, and | ||
# toctree filenames were generated for the subsection. | ||
if not gallery_conf["nested_sections"]: | ||
if len(subsection_toctree_filenames) > 0: | ||
subsection_index_toctree = """ | ||
.. toctree:: | ||
:hidden: | ||
|
||
%s\n | ||
""" % "\n ".join(subsection_toctree_filenames) | ||
fhindex.write(subsection_index_toctree) | ||
# Otherwise, a new index.rst.new file should | ||
# have been created and it needs to be parsed | ||
else: | ||
_replace_md5(subsection_index_path, mode='t') | ||
|
||
costs += subsection_costs | ||
write_computation_times( | ||
gallery_conf, target_dir, subsection_costs | ||
) | ||
|
||
# generate toctree with subsections | ||
if gallery_conf["nested_sections"] is True: | ||
subsections_toctree = """ | ||
.. toctree:: | ||
:hidden: | ||
:includehidden: | ||
|
||
%s\n | ||
""" % "\n ".join(subsection_index_files) | ||
# """ % "\n ".join(this_toctree_items + subsection_index_files) | ||
|
||
# add toctree to file only if there are subsections | ||
if len(subsection_index_files) > 0: | ||
fhindex.write(subsections_toctree) | ||
|
||
if gallery_conf['download_all_examples']: | ||
download_fhindex = generate_zipfiles( | ||
gallery_dir_abs_path, app.builder.srcdir | ||
) | ||
fhindex.write(download_fhindex) | ||
|
||
if (app.config.sphinx_gallery_conf['show_signature']): | ||
fhindex.write(SPHX_GLR_SIG) | ||
indexst += subsection_index_content | ||
|
||
# Write subsection toctree in main file only if | ||
# nested_sections is False or None, and | ||
# toctree filenames were generated for the subsection. | ||
if not gallery_conf["nested_sections"]: | ||
if len(subsection_toctree_filenames) > 0: | ||
subsection_index_toctree = _format_toctree( | ||
subsection_toctree_filenames) | ||
indexst += subsection_index_toctree | ||
# Otherwise, a new index.rst.new file should | ||
# have been created and it needs to be parsed | ||
elif has_readme: | ||
_replace_md5(subsection_index_path, mode='t') | ||
|
||
costs += subsection_costs | ||
write_computation_times( | ||
gallery_conf, target_dir, subsection_costs | ||
) | ||
|
||
# generate toctree with subsections | ||
if gallery_conf["nested_sections"] is True: | ||
subsections_toctree = _format_toctree( | ||
subsection_index_files, includehidden=True) | ||
|
||
# add toctree to file only if there are subsections | ||
if len(subsection_index_files) > 0: | ||
indexst += subsections_toctree | ||
|
||
if gallery_conf['download_all_examples']: | ||
download_fhindex = generate_zipfiles( | ||
gallery_dir_abs_path, app.builder.srcdir | ||
) | ||
indexst += download_fhindex | ||
|
||
if (app.config.sphinx_gallery_conf['show_signature']): | ||
indexst += SPHX_GLR_SIG | ||
|
||
if has_readme: | ||
index_rst_new = os.path.join(gallery_dir_abs_path, 'index.rst.new') | ||
with codecs.open(index_rst_new, 'w', encoding='utf-8') as fhindex: | ||
fhindex.write(indexst) | ||
_replace_md5(index_rst_new, mode='t') | ||
|
||
_replace_md5(index_rst_new, mode='t') | ||
if gallery_conf['show_api_usage'] is not False: | ||
init_api_usage(app.builder.srcdir) | ||
_finalize_backreferences(seen_backrefs, gallery_conf) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.