diff --git a/nbconvert/exporters/html.py b/nbconvert/exporters/html.py index 055e69915..367039d7a 100644 --- a/nbconvert/exporters/html.py +++ b/nbconvert/exporters/html.py @@ -211,7 +211,7 @@ def default_config(self): @validate("language_code") def _valid_language_code(self, proposal): if self.language_code not in iso639_1: - self.log.warn( + self.log.warning( f'"{self.language_code}" is not an ISO 639-1 language code. ' 'It has been replaced by the default value "en".' ) @@ -259,8 +259,12 @@ def from_notebook_node( # type:ignore html, resources = super().from_notebook_node(nb, resources, **kw) soup = BeautifulSoup(html, features="html.parser") # Add image's alternative text + missing_alt = 0 for elem in soup.select("img:not([alt])"): - elem.attrs["alt"] = "Image" + elem.attrs["alt"] = "No description has been provided for this image" + missing_alt += 1 + if missing_alt: + self.log.warning(f"Alternative text is missing on {missing_alt} image(s).") # Set input and output focusable for elem in soup.select(".jp-Notebook div.jp-Cell-inputWrapper"): elem.attrs["tabindex"] = "0" diff --git a/nbconvert/exporters/tests/test_html.py b/nbconvert/exporters/tests/test_html.py index 04c2a74f8..3b8be156d 100644 --- a/nbconvert/exporters/tests/test_html.py +++ b/nbconvert/exporters/tests/test_html.py @@ -73,10 +73,17 @@ def test_png_metadata(self): """ Does HTMLExporter with the 'classic' template treat pngs with width/height metadata correctly? """ - (output, resources) = HTMLExporter(template_name="classic").from_filename( - self._get_notebook(nb_name="pngmetadata.ipynb") + exporter = HTMLExporter(template_name="classic") + with self.assertLogs(exporter.log, level="WARN") as log: + (output, resources) = exporter.from_filename( + self._get_notebook(nb_name="pngmetadata.ipynb") + ) + assert len(log.output) == 1 + assert "Alternative text is missing on 1 image(s)." in log.output[0] + + check_for_png = re.compile( + r'No description has been provided for this image]*?)>' ) - check_for_png = re.compile(r'Image]*?)>') result = check_for_png.search(output) assert result attr_string = result.group(1) @@ -198,3 +205,23 @@ def test_javascript_injection(self): assert "" not in output assert "" not in output assert "alert('application/javascript output')" not in output + + def test_language_code_not_set(self): + (output, resources) = HTMLExporter(template_name="classic").from_filename( + self._get_notebook() + ) + assert '' in output + + def test_set_language_code(self): + exporter = HTMLExporter(template_name="classic", language_code="fr") + (output, resources) = exporter.from_filename(self._get_notebook()) + assert '' in output + + def test_language_code_error(self): + with self.assertLogs(level="WARN") as log: + exporter = HTMLExporter(template_name="classic", language_code="zz") + assert len(log.output) == 1 + assert '"zz" is not an ISO 639-1 language code.' in log.output[0] + (output, resources) = exporter.from_filename(self._get_notebook()) + + assert '' in output