From 2febd90cca3ba884e77ba858082cbf24cdc3b89b Mon Sep 17 00:00:00 2001 From: ivanharvard <144486839+ivanharvard@users.noreply.github.com> Date: Mon, 14 Jul 2025 16:15:13 -0400 Subject: [PATCH 1/4] Added feature where (trailing) invisible chars are highlighted --- check50/renderer/_renderers.py | 52 ++++++++++++++++++++++++- check50/renderer/templates/results.html | 5 ++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/check50/renderer/_renderers.py b/check50/renderer/_renderers.py index 0ddc252a..f026ba42 100644 --- a/check50/renderer/_renderers.py +++ b/check50/renderer/_renderers.py @@ -1,5 +1,6 @@ import json import pathlib +import html import jinja2 import termcolor @@ -14,11 +15,56 @@ def to_html(slug, results, version): content = f.read() template = jinja2.Template( - content, autoescape=jinja2.select_autoescape(enabled_extensions=("html",))) - html = template.render(slug=slug, results=results, version=version) + content, autoescape=jinja2.select_autoescape(enabled_extensions=("html",)) + ) + + html = template.render( + slug=slug, + results=results, + version=version, + fmt_special_chars=_fmt_special_chars, + color="rgba(161, 161, 161, 0.5)" + ) return html +def _fmt_special_chars(txt, color): + """Converts a plaintext string into a string of HTML elements that highlights special chars.""" + def highlight_char(char, color): + """Highlights and escapes a char.""" + return f"{repr(char)[1:-1]}" + + # We'd like to interpret whitespace (ws) as HTML in only these specific cases: + ws_to_html = { + "\n": "
", + " ": " ", + } + fmtted_txt = [] + + for i, char in enumerate(txt): + is_last = i == len(txt) - 1 + + if not char.isprintable() and char not in ws_to_html: + # Most special characters, excluding those in ws_to_html, are highlighted + fmtted_txt.append(highlight_char(char, color)) + elif char in ws_to_html: + # If there's a trailing whitespace character, we highlight it + if is_last: + # Spaces aren't normally highlightable, so we convert to nbsp. + if char == ' ': + char = ws_to_html[char] + + fmtted_txt.append(highlight_char(char, color)) + else: + # Certain special chars are interpreted in HTML, without escaping or highlighting + fmtted_txt.append(ws_to_html[char]) + else: + # Non-special characters are unchanged + fmtted_txt.append(char) + + # Return the text as a string of plaintext + html elements + return ''.join(fmtted_txt) + def to_json(slug, results, version): return json.dumps({"slug": slug, "results": results, "version": version}, indent=4) @@ -46,3 +92,5 @@ def to_ansi(slug, results, version, _log=False): lines += (f" {line}" for line in result["log"]) return "\n".join(lines) + + diff --git a/check50/renderer/templates/results.html b/check50/renderer/templates/results.html index 6a51efa8..40b58d7f 100644 --- a/check50/renderer/templates/results.html +++ b/check50/renderer/templates/results.html @@ -86,7 +86,7 @@

:| {{ check.description }}

{% else %} {% set expected = check.cause.expected | e %} {% endif %} - {{ expected | replace(" ", " ") | replace("\n", "
") }} + {{ fmt_special_chars(expected, color) }} {% endautoescape %} @@ -102,12 +102,13 @@

:| {{ check.description }}

{% else %} {% set actual = check.cause.actual | e %} {% endif %} - {{ actual | replace(" ", " ") | replace("\n", "
") }} + {{ fmt_special_chars(actual, color) }} {% endautoescape %} +
{% endif %} {# Missing if there was one #} From 2d86fb989bcc2b62b26b33387cc06bc1f20596c9 Mon Sep 17 00:00:00 2001 From: ivanharvard <144486839+ivanharvard@users.noreply.github.com> Date: Mon, 14 Jul 2025 16:16:11 -0400 Subject: [PATCH 2/4] Comment fix --- check50/renderer/_renderers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check50/renderer/_renderers.py b/check50/renderer/_renderers.py index f026ba42..dbf5709e 100644 --- a/check50/renderer/_renderers.py +++ b/check50/renderer/_renderers.py @@ -45,7 +45,7 @@ def highlight_char(char, color): is_last = i == len(txt) - 1 if not char.isprintable() and char not in ws_to_html: - # Most special characters, excluding those in ws_to_html, are highlighted + # Most special invisible characters, excluding those in ws_to_html, are highlighted fmtted_txt.append(highlight_char(char, color)) elif char in ws_to_html: # If there's a trailing whitespace character, we highlight it From 3b1bfd793d89f0834b1a80d440add4b61bb5ed3b Mon Sep 17 00:00:00 2001 From: ivanharvard <144486839+ivanharvard@users.noreply.github.com> Date: Mon, 14 Jul 2025 16:16:53 -0400 Subject: [PATCH 3/4] Removed unused import --- check50/renderer/_renderers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/check50/renderer/_renderers.py b/check50/renderer/_renderers.py index dbf5709e..46fa524d 100644 --- a/check50/renderer/_renderers.py +++ b/check50/renderer/_renderers.py @@ -1,6 +1,5 @@ import json import pathlib -import html import jinja2 import termcolor From af90cfb784ebff39682bf56a9fcb5d9aabcdd6c6 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Tue, 15 Jul 2025 12:01:09 -0400 Subject: [PATCH 4/4] Use hex value for color in the renderer --- check50/renderer/_renderers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/check50/renderer/_renderers.py b/check50/renderer/_renderers.py index 46fa524d..0d42244b 100644 --- a/check50/renderer/_renderers.py +++ b/check50/renderer/_renderers.py @@ -22,7 +22,7 @@ def to_html(slug, results, version): results=results, version=version, fmt_special_chars=_fmt_special_chars, - color="rgba(161, 161, 161, 0.5)" + color="808080" # RGB (128, 128, 128) ) return html @@ -91,5 +91,3 @@ def to_ansi(slug, results, version, _log=False): lines += (f" {line}" for line in result["log"]) return "\n".join(lines) - -