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

Skip to content

False positive WARNING: Could not match a code example to HTML with jupytext #191

Open
@tovrstra

Description

@tovrstra

Issue

I get WARNING: Could not match a code example to HTML when building documentation with jupytext sources (using the mystnb extension in combination with sphinx_codeautolink.) This happens when code cells have leading empty lines.

Expected behavior

No warnings

Steps to reproduce

source/conf.py

extensions = [
    "myst_nb",
    "sphinx_codeautolink",
]
nb_custom_formats = {
    ".py": ["jupytext.reads", {"fmt": "py:percent"}],
}
exclude_patterns = ["conf.py"]

source/index.py

#!/usr/bin/env python3

# %% [markdown]
#
# This is a bit of text

# %%

print("This is code. Mind the leading empty line.")

Run:

sphinx-build source build

Output (starting without a build directory):

loading translations [en]... done
making output directory... done
myst v4.0.1: MdParserConfig(commonmark_only=False, gfm_only=False, enable_extensions=set(), disable_syntax=[], all_links_external=False, links_external_new_tab=False, url_schemes=('http', 'https', 'mailto', 'ftp'), ref_domains=None, fence_as_directive=set(), number_code_blocks=[], title_to_header=False, heading_anchors=0, heading_slug_func=None, html_meta={}, footnote_sort=True, footnote_transition=True, words_per_minute=200, substitutions={}, linkify_fuzzy_links=True, dmath_allow_labels=True, dmath_allow_space=True, dmath_allow_digits=True, dmath_double_inline=False, update_mathjax=True, mathjax_classes='tex2jax_process|mathjax_process|math|output_area', enable_checkboxes=False, suppress_warnings=[], highlight_code_blocks=True)
myst-nb v1.2.0: NbParserConfig(custom_formats={'.py': ('jupytext.reads', {'fmt': 'py:percent'}, False)}, metadata_key='mystnb', cell_metadata_key='mystnb', kernel_rgx_aliases={}, eval_name_regex='^[a-zA-Z_][a-zA-Z0-9_]*$', execution_mode='auto', execution_cache_path='', execution_excludepatterns=(), execution_timeout=30, execution_in_temp=False, execution_allow_errors=False, execution_raise_on_error=False, execution_show_tb=False, merge_streams=False, render_plugin='default', remove_code_source=False, remove_code_outputs=False, code_prompt_show='Show code cell {type}', code_prompt_hide='Hide code cell {type}', number_source_lines=False, output_stderr='show', render_text_lexer='myst-ansi', render_error_lexer='ipythontb', render_image_options={}, render_figure_options={}, render_markdown_format='commonmark', output_folder='build', append_css=True, metadata_to_fm=False)
Using jupyter-cache at: /home/toon/tmp/.jupyter_cache
building [mo]: targets for 0 po files that are out of date
writing output... 
building [html]: targets for 1 source files that are out of date
updating environment: [new config] 1 added, 0 changed, 0 removed
/home/toon/tmp/source/index.py: Executing notebook using local CWD [mystnb]
/home/toon/tmp/source/index.py: Executed notebook in 0.90 seconds [mystnb]

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
copying assets... 
copying static files... 
Writing evaluated template result to /home/toon/tmp/build/_static/basic.css
Writing evaluated template result to /home/toon/tmp/build/_static/documentation_options.js
Writing evaluated template result to /home/toon/tmp/build/_static/language_data.js
Writing evaluated template result to /home/toon/tmp/build/_static/alabaster.css
copying static files: done
copying extra files... 
copying extra files: done
copying assets: done
writing output... [100%] index
generating indices... genindex done
writing additional pages... search done
dumping search index in English (code: en)... done
dumping object inventory... done
/home/toon/tmp/source/index.py: WARNING: Could not match a code example to HTML, source:

print("This is code. Mind the leading empty line.") [codeautolink.match_block]
build succeeded, 1 warning.

The HTML pages are in build.

Comments

This might also be an issue with mystnb, but this is not clear to me at the moment. I don't see where the empty line gets removed, which causes the warning.

The matching fails because the code with and without leading empty line are compared here:

for trans in transforms:
for ix in range(len(inners)):
candidate = copy(inners[ix])
# remove line numbers for matching
for lineno in candidate.find_all("span", attrs={"class": "linenos"}):
lineno.extract()
if trans.source.rstrip() == "".join(candidate.strings).rstrip():
inner = inners.pop(ix)
break
else:
msg = f"Could not match a code example to HTML, source:\n{trans.source}"
logger.warning(
msg, type=warn_type, subtype="match_block", location=document
)
continue

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions