Skip to content

Commit

Permalink
Fix Tool repr_html and add html escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Jul 17, 2023
1 parent 4f5f051 commit 63b9e63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 9 additions & 6 deletions ctapipe/core/component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Class to handle configuration for algorithms """
import html
import warnings
import weakref
from abc import ABCMeta
Expand Down Expand Up @@ -235,7 +236,7 @@ def _repr_html_(self):
lines = [
'<div style="border:1px solid black; max-width: 700px; padding:2em; word-wrap:break-word;">',
f"<b>{name}</b>",
f"<p> {docstring} </p>",
docstring,
"<table>",
" <colgroup>",
" <col span='1' style=' '>",
Expand All @@ -246,21 +247,23 @@ def _repr_html_(self):
]
for key, val in self.get_current_config()[name].items():
htmlval = (
str(val).replace("/", "/<wbr>").replace("_", "_<wbr>")
html.escape(str(val)).replace("/", "/<wbr>").replace("_", "_<wbr>")
) # 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"<tr><th>{key}</th>")
if val != traits[key].default_value:
lines.append(
f"<td style='text-align: left;'><span style='color:blue; max-width:30em;'>{htmlval}</span></td>"
f'<td style="text-align: left;"><span style="color:blue; max-width:30em;">{htmlval}</span></td>'
)
else:
lines.append(f"<td style='text-align: left;'>{htmlval}</td>")
lines.append(f'<td style="text-align: left;">{htmlval}</td>')
lines.append(
f"<td style='text-align: left;'><i>{thehelp}</i></td></tr>"
f'<td style="text-align: left;"><i>{thehelp}</i></td></tr>'
)
lines.append(" </tbody>")
lines.append("</table>")
Expand Down
10 changes: 7 additions & 3 deletions ctapipe/core/tool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Classes to handle configurable command-line user interfaces."""
import html
import logging
import logging.config
import os
Expand Down Expand Up @@ -495,8 +496,9 @@ def _repr_html_(self):
or "Undocumented"
)
lines = [
'<div style="border:1px solid black; max-width: 700px; padding:2em; word-wrap:break-word;">',
f"<b>{name}</b>",
f"<p> {docstring} </p>",
docstring,
"<table>",
" <colgroup>",
" <col span='1' style=' '>",
Expand All @@ -507,12 +509,14 @@ def _repr_html_(self):
]
for key, val in self.get_current_config()[name].items():
htmlval = (
str(val).replace("/", "/<wbr>").replace("_", "_<wbr>")
html.escape(str(val)).replace("/", "/<wbr>").replace("_", "_<wbr>")
) # 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"<tr><th>{key}</th>")
if val != traits[key].default_value:
lines.append(
Expand Down

0 comments on commit 63b9e63

Please sign in to comment.