From 63b9e63ced2aaa5e01ddc2edb9bc92dc7f9859f0 Mon Sep 17 00:00:00 2001 From: Maximilian Linhoff Date: Mon, 17 Jul 2023 13:05:45 +0200 Subject: [PATCH] Fix Tool repr_html and add html escaping --- ctapipe/core/component.py | 15 +++++++++------ ctapipe/core/tool.py | 10 +++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ctapipe/core/component.py b/ctapipe/core/component.py index cbfc05b2584..b3b5b91a733 100644 --- a/ctapipe/core/component.py +++ b/ctapipe/core/component.py @@ -1,4 +1,5 @@ """ Class to handle configuration for algorithms """ +import html import warnings import weakref from abc import ABCMeta @@ -235,7 +236,7 @@ def _repr_html_(self): lines = [ '
', f"{name}", - f"

{docstring}

", + docstring, "", " ", " ", @@ -246,21 +247,23 @@ def _repr_html_(self): ] for key, val in self.get_current_config()[name].items(): htmlval = ( - str(val).replace("/", "/").replace("_", "_") + html.escape(str(val)).replace("/", "/").replace("_", "_") ) # allow breaking at boundary # traits of the current component if key in traits: - thehelp = f"{traits[key].help} (default: {traits[key].default_value})" + thehelp = html.escape( + f"{traits[key].help} (default: {traits[key].default_value})" + ) lines.append(f"") if val != traits[key].default_value: lines.append( - f"" + f'' ) else: - lines.append(f"") + lines.append(f'') lines.append( - f"" + f'' ) lines.append(" ") lines.append("
{key}{htmlval}{htmlval}{htmlval}{htmlval}{thehelp}
{thehelp}
") diff --git a/ctapipe/core/tool.py b/ctapipe/core/tool.py index 5f6744d2c95..5d28c1585a3 100644 --- a/ctapipe/core/tool.py +++ b/ctapipe/core/tool.py @@ -1,4 +1,5 @@ """Classes to handle configurable command-line user interfaces.""" +import html import logging import logging.config import os @@ -495,8 +496,9 @@ def _repr_html_(self): or "Undocumented" ) lines = [ + '
', f"{name}", - f"

{docstring}

", + docstring, "", " ", " ", @@ -507,12 +509,14 @@ def _repr_html_(self): ] for key, val in self.get_current_config()[name].items(): htmlval = ( - str(val).replace("/", "/").replace("_", "_") + html.escape(str(val)).replace("/", "/").replace("_", "_") ) # allow breaking at boundary # traits of the current component if key in traits: - thehelp = f"{traits[key].help} (default: {traits[key].default_value})" + thehelp = html.escape( + f"{traits[key].help} (default: {traits[key].default_value})" + ) lines.append(f"") if val != traits[key].default_value: lines.append(
{key}