Skip to content

Commit 293a416

Browse files
committed
fix: hide the statement file's footer comment in the page
1 parent 31394d9 commit 293a416

5 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎cli/src/coding_study/entrypoints/web/markdown.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"""The class of the button a link to a library file becomes, which the page opens in a library
6666
tab at the line its `data-uri` names."""
6767
BUTTON = "button"
68+
_COMMENT = re.compile(r"<!--.*?-->(\s*<!--.*?-->)*", re.DOTALL)
6869
_GUIDE_LINK = re.compile(r"\.\./(?P<slug>[a-z][a-z0-9-]*)/guide\.md")
6970
"""A link from one guide.md to another, as guides link each other."""
7071
GUIDE_ROUTE = "#/guides/{slug}"
@@ -254,6 +255,26 @@ def _without_title(state: StateCore) -> None:
254255
state.tokens = tokens[TITLE_TOKENS:]
255256

256257

258+
def _without_comments(state: StateCore) -> None:
259+
"""A paragraph that is only an HTML comment, such as the footer of a rendered statement
260+
file, which is for editors: raw HTML is never inserted, and a comment is not shown as text."""
261+
tokens = state.tokens
262+
kept: list[Token] = []
263+
index = 0
264+
while index < len(tokens):
265+
if (
266+
tokens[index].type == "paragraph_open"
267+
and index + 2 < len(tokens)
268+
and tokens[index + 1].type == "inline"
269+
and _COMMENT.fullmatch(tokens[index + 1].content.strip())
270+
):
271+
index += 3
272+
continue
273+
kept.append(tokens[index])
274+
index += 1
275+
state.tokens = kept
276+
277+
257278
def _diagram_figures(state: StateCore) -> None:
258279
"""Each `mermaid` block, with the marker and the text drawing that follow it, becomes one
259280
figure: its light and dark SVG drawings, of which the page's stylesheet shows the one of its
@@ -412,16 +433,19 @@ def _accept_every_link(url: str) -> bool:
412433
_RENDERER = MarkdownIt("commonmark", {"html": False, "highlight": _highlighted}).enable(
413434
["table", "strikethrough"]
414435
)
436+
_RENDERER.core.ruler.push("without_comments", _without_comments)
415437
_RENDERER.core.ruler.push("alignment_classes", _alignment_classes)
416438
_DOCUMENTATION = MarkdownIt("commonmark", {"html": False, "highlight": _highlighted}).enable(
417439
["table", "strikethrough"]
418440
)
419441
_DOCUMENTATION.validateLink = _accept_every_link
442+
_DOCUMENTATION.core.ruler.push("without_comments", _without_comments)
420443
_DOCUMENTATION.core.ruler.push("alignment_classes", _alignment_classes)
421444
_DOCUMENTATION.core.ruler.push("plain_links_and_images", _plain_links_and_images)
422445
_GUIDE = MarkdownIt("commonmark", {"html": False, "highlight": _highlighted}).enable(
423446
["table", "strikethrough"]
424447
)
448+
_GUIDE.core.ruler.push("without_comments", _without_comments)
425449
_GUIDE.core.ruler.push("alignment_classes", _alignment_classes)
426450
_GUIDE.core.ruler.push("without_title", _without_title)
427451
_GUIDE.core.ruler.push("diagram_figures", _diagram_figures)
@@ -430,6 +454,7 @@ def _accept_every_link(url: str) -> bool:
430454
_DOC = MarkdownIt("commonmark", {"html": False, "highlight": _highlighted}).enable(
431455
["table", "strikethrough"]
432456
)
457+
_DOC.core.ruler.push("without_comments", _without_comments)
433458
_DOC.core.ruler.push("alignment_classes", _alignment_classes)
434459
_DOC.core.ruler.push("doc_headings", _doc_headings)
435460
_DOC.core.ruler.push("doc_links", _doc_links)

‎cli/tests/entrypoints/web/test_browser_smoke.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ def test_a_project_problem_shows_its_statement(visit):
249249
assert "Bank" in visit.statement
250250

251251

252+
def test_the_statement_does_not_show_the_footer_comment_of_its_file(visit):
253+
assert "Rendered by cstudy" not in visit.statement
254+
255+
252256
def test_resuming_the_attempt_shows_the_projects_tree(visit):
253257
assert FILE in visit.tree
254258

‎cli/tests/entrypoints/web/test_browsing_api.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ def test_the_statement_comes_rendered_as_html(client):
274274
assert html.startswith("<h1>Two Sum</h1>")
275275

276276

277+
def test_the_statement_html_leaves_out_the_footer_comment_of_the_statement_file(client):
278+
document = client.get("/api/v1/problems/two-sum/statement").json()
279+
assert "Rendered by cstudy" in document["markdown"]
280+
assert "Rendered by cstudy" not in document["html"]
281+
assert "<!--" not in document["html"]
282+
283+
277284
def test_the_statement_names_the_jdk_range_the_problem_declares(tmp_path):
278285
root = repository_root(tmp_path, PRACTICE, FIXTURE_RUBRIC_PROBLEMS)
279286
declare_jdk(root, "two-sum", "{min: 24}")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""An HTML comment in markdown, such as the footer of a rendered statement file, is not shown
2+
(docs/specs/web-ui.md#documentation): raw HTML is never inserted and a comment is not text."""
3+
4+
import pytest
5+
6+
from coding_study.entrypoints.web.markdown import (
7+
render_doc_markdown,
8+
render_documentation_markdown,
9+
render_guide_markdown,
10+
render_markdown,
11+
)
12+
13+
FOOTER = "<!-- Rendered by cstudy from the problem's problem.md; edits here are overwritten. -->"
14+
15+
RENDERERS = [
16+
render_markdown,
17+
render_documentation_markdown,
18+
render_guide_markdown,
19+
lambda text: render_doc_markdown(text, "intro"),
20+
]
21+
22+
23+
@pytest.mark.parametrize("render", RENDERERS)
24+
def test_a_comment_paragraph_is_dropped(render):
25+
assert render(f"Body.\n\n{FOOTER}\n") == "<p>Body.</p>\n"
26+
27+
28+
@pytest.mark.parametrize("render", RENDERERS)
29+
def test_a_comment_over_several_lines_is_dropped(render):
30+
assert render("Body.\n\n<!--\nnote\n-->\n") == "<p>Body.</p>\n"
31+
32+
33+
def test_a_comment_in_a_code_block_is_code():
34+
assert "&lt;!-- keep --&gt;" in render_markdown("```\n<!-- keep -->\n```\n")
35+
36+
37+
def test_other_raw_html_is_still_text():
38+
assert "&lt;script&gt;" in render_markdown("<script>x</script>\n")

‎docs/specs/web-ui.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Errors share one document:
206206
|-----------------|---------|-----------------------|
207207
| `GET /api/v1/problems` | `set`, `track`, `kind`, `checked` (`tests`, `rubric` or `ai-review`, as `cstudy list --checked-by`), `lang`, `difficulty`, `status`, `pattern`, `content`, `domain`, `role`, `scope`, each optional, with the values of `cstudy list`, and `widen` (repeatable: `lang`, `role`, `domain`, as `cstudy list --widen`) | [`cstudy list --json`](cli.md#cstudy-list), the [learner profile](cli.md#the-learner-profile) applied as the command applies it, with its `profile`, with each entry's `jdk` and the document's `selected_jdk`; each entry has `id` and `checked_by` as the command gives them |
208208
| `GET /api/v1/problems/{slug}` | | The problem's entry of `cstudy list --json`, with its `id` and `checked_by`, `languages` the languages `cstudy start` accepts instead of the reference languages, plus `approaches` (`id`, `name`, `role`), `broken` (the broken reference ids, or empty), `levels` (`last`, `passed` and `mastered`, as `cstudy list --json` gives them, or null), `hints_total`, `bench` (whether `cstudy bench` applies), `oracle` (whether `--stress N` adds oracle inputs) and `guides`, the [concept guides](guides.md) its `meta.yaml` names that exist, in its order, each `{"id", "slug", "title", "read"}`. For a rubric problem: `languages`, `approaches`, `broken` and `guides` empty, `levels` null, `hints_total` 0, `bench` and `oracle` false |
209-
| `GET /api/v1/problems/{slug}/statement` | `level` (optional, as `cstudy show --level`) | `{"markdown": …, "html": …}`: the text `cstudy show` prints, and its HTML; `show_statement` |
209+
| `GET /api/v1/problems/{slug}/statement` | `level` (optional, as `cstudy show --level`) | `{"markdown": …, "html": …}`: the text `cstudy show` prints, and its HTML, which leaves out an HTML comment paragraph such as the footer of the statement file; `show_statement` |
210210
| `GET /api/v1/problems/{slug}/history` | | [`cstudy history --json`](cli.md#cstudy-history) |
211211
| `GET /api/v1/problems/{slug}/needs` | | `{"needs": [{"label", "text", "problem"}]}`: what a [service project](service-projects.md#where-it-shows) depends on, the rows `cstudy show` prints, `problem` the reason one is not met here or null; `{"needs": null}` for any other problem, which asks Docker nothing; `service_needs_of`. It asks the engine its state and starts nothing |
212212
| `GET /api/v1/next` | `set`, `track` (repeatable), `pattern`, `difficulty`, `lang`, `domain`, `role`, `scope`, `widen` (repeatable: `role`, `domain`), `new` or `review` (booleans) | [`cstudy next --json`](cli.md#cstudy-next) |

0 commit comments

Comments
 (0)