From 7b5f3a086139b5a761a075aa36e3060bd0c12a7d Mon Sep 17 00:00:00 2001 From: 4matic Date: Wed, 11 Nov 2026 01:17:14 +0200 Subject: [PATCH] fix(spec): pair Markdown emphasis only inside rest R13.12: runs in lead and gap are read by position and take no part in the pairing. A lead run the gap leaves open pairs only with the last run of the line (R13.12.4), so `**[**14:21**] A - B**` still loses its closing `**`, but `**00:00 Artist - F** You` keeps the `**` in the title instead of dropping it. Invariant R13.11.6 now requires a track part without `*` or `_`, and R13.2.4 says rest is raw after the bullet lead. --- js/src/tracklist/emphasis.ts | 28 ++- js/src/tracklist/times.ts | 15 +- js/test/tracklist.properties.test.ts | 8 +- python/src/trackparse/_tracklist_emphasis.py | 33 +++- python/src/trackparse/_tracklist_times.py | 16 +- python/tests/test_tracklist_properties.py | 8 +- spec/SPEC.md | 23 ++- spec/fixtures/handwritten/tracklist-mode.json | 161 ++++++++++++++++++ 8 files changed, 258 insertions(+), 34 deletions(-) diff --git a/js/src/tracklist/emphasis.ts b/js/src/tracklist/emphasis.ts index f582018..da36e7b 100644 --- a/js/src/tracklist/emphasis.ts +++ b/js/src/tracklist/emphasis.ts @@ -25,15 +25,18 @@ const isOuter = (ch: string | undefined) => const isInner = (ch: string | undefined) => ch !== undefined && !isWhitespace(ch); /** - * R13.12: `raw.slice(from)` without its emphasis markers. A run of 1 or 2 `*` or `_` closes when - * the character before it is an inner side and the one after it an outer side, and opens the other - * way round. A closer pairs with the nearest opener of the same characters; that opener and every - * opener after it leave the stack. One stack per run (`*`, `**`, `_`, `__`) keeps this linear. + * R13.12: `raw.slice(from)` (the line's `rest`) without its emphasis markers. A run of 1 or 2 `*` + * or `_` closes when the character before it is an inner side and the one after it an outer side, + * and opens the other way round. A closer pairs with the nearest opener of the same characters; + * that opener and every opener after it leave the stack. One stack per run (`*`, `**`, `_`, `__`) + * keeps this linear. Runs before `from` (lead and gap) never pair, except that `open`, a lead run + * the gap left open (R13.12.4), pairs with a closing run of the same character at the end of the + * line. */ -export function withoutMarkers(raw: string, from: number): string { +export function withoutMarkers(raw: string, from: number, open?: string): string { const marked: boolean[] = []; const stacks: Record = { "*": [], "**": [], _: [], __: [] }; - for (let i = 0; i < raw.length; ) { + for (let i = from; i < raw.length; ) { const len = runLength(raw, i) || 1; const own = stacks[raw.slice(i, i + len)]; const before = raw[i - 1]; @@ -45,6 +48,19 @@ export function withoutMarkers(raw: string, from: number): string { } else if (own && isOuter(before) && isInner(after)) own.push(i); i += len; } + // R13.12.4: the last run of the line, after which there is only R0.2 whitespace. + let end = raw.length; + while (end > from && isWhitespace(raw[end - 1])) end--; + let start = end; + while (start > from && raw[start - 1] === open) start--; + if ( + open && + end - start <= 2 && + start > from && + raw[start - 1] !== open && + isInner(raw[start - 1]) + ) + for (let m = start; m < end && !marked[m]; m++) marked[m] = true; let out = ""; for (let i = from; i < raw.length; i++) if (!marked[i]) out += raw[i]; return out; diff --git a/js/src/tracklist/times.ts b/js/src/tracklist/times.ts index 324552f..e290052 100644 --- a/js/src/tracklist/times.ts +++ b/js/src/tracklist/times.ts @@ -153,14 +153,21 @@ export function scanTime(raw: string): TimeScan { // R13.2.3 gap: closing runs for the lead's runs (same character), around the closing bracket. const followsTime = (at: number) => at >= raw.length || isWhitespace(raw[at]) || isGapSeparator(raw[at]); + // R13.12.4: a lead run the gap leaves open may pair with a run at the end of the line. + let open: string | undefined; if (closer) { - j = closingRun(raw, j, innerOpen); + let k = closingRun(raw, j, innerOpen); + if (k === j) open = innerOpen; + j = k; if (raw[j] !== closer) return untimed(raw, false); j++; - const k = closingRun(raw, j, outerOpen); + k = closingRun(raw, j, outerOpen); if (k > j && followsTime(k)) j = k; + else open = outerOpen ?? open; } else { - j = closingRun(raw, j, outerOpen); + const k = closingRun(raw, j, outerOpen); + if (k === j) open = outerOpen; + j = k; if (!followsTime(j)) return untimed(raw, false); } j = skipWhitespace(raw, j); @@ -196,7 +203,7 @@ export function scanTime(raw: string): TimeScan { fracDigits: frac.length, }, invalid: false, - text: withoutMarkers(raw, j), + text: withoutMarkers(raw, j, open), }; } diff --git a/js/test/tracklist.properties.test.ts b/js/test/tracklist.properties.test.ts index 6e37797..eb81e7a 100644 --- a/js/test/tracklist.properties.test.ts +++ b/js/test/tracklist.properties.test.ts @@ -5,9 +5,9 @@ import Ajv2020 from "ajv/dist/2020.js"; import fc from "fast-check"; import { describe, expect, it } from "vitest"; import { parse } from "../src/index.js"; -import { withoutMarkers } from "../src/tracklist/emphasis.js"; import { applyEdits, type Edit, parseTracklist } from "../src/tracklist/index.js"; import { layeredPrefix } from "../src/tracklist/kinds.js"; +import { scanTime } from "../src/tracklist/times.js"; import { specDir } from "./runner.js"; const ajv = new Ajv2020({ allErrors: true, strict: false }); @@ -394,8 +394,8 @@ describe("R13.11 tracklist invariants", () => { // R13.4.1: the track part of a one-line text (no neighbours, so no bare position) is `rest` // without its emphasis markers (R13.12) and a layered prefix (R13.13). - const trackPart = (raw: string, rest: string) => { - const text = withoutMarkers(raw, raw.length - rest.length); + const trackPart = (raw: string) => { + const text = scanTime(raw).text; return text.slice(layeredPrefix(text)); }; @@ -410,7 +410,7 @@ describe("R13.11 tracklist invariants", () => { fc.property(oneLine, (text) => { const e = parseTracklist(text).entries[0]; if (!e?.track) return; - expect(e.track).toEqual(parse(trackPart(e.raw, e.parts.rest), { mode: "tracklist" })); + expect(e.track).toEqual(parse(trackPart(e.raw), { mode: "tracklist" })); }), { numRuns: 1500 }, ); diff --git a/python/src/trackparse/_tracklist_emphasis.py b/python/src/trackparse/_tracklist_emphasis.py index eb4ad31..fe1a9de 100644 --- a/python/src/trackparse/_tracklist_emphasis.py +++ b/python/src/trackparse/_tracklist_emphasis.py @@ -35,15 +35,18 @@ def _is_inner(ch: Optional[str]) -> bool: return ch is not None and not is_whitespace(ch) -def without_markers(raw: str, frm: int) -> str: - """R13.12: ``raw[frm:]`` without its emphasis markers. A run of 1 or 2 ``*`` or ``_`` closes - when the character before it is an inner side and the one after it an outer side, and opens - the other way round. A closer pairs with the nearest opener of the same characters; that - opener and every opener after it leave the stack. One stack per run keeps this linear.""" +def without_markers(raw: str, frm: int, open_run: Optional[str] = None) -> str: + """R13.12: ``raw[frm:]`` (the line's ``rest``) without its emphasis markers. A run of 1 or 2 + ``*`` or ``_`` closes when the character before it is an inner side and the one after it an + outer side, and opens the other way round. A closer pairs with the nearest opener of the same + characters; that opener and every opener after it leave the stack. One stack per run keeps + this linear. Runs before ``frm`` (lead and gap) never pair, except that ``open_run``, a lead + run the gap left open (R13.12.4), pairs with a closing run of the same character at the end + of the line.""" n = len(raw) marked = [False] * n stacks: dict[str, list[int]] = {"*": [], "**": [], "_": [], "__": []} - i = 0 + i = frm while i < n: length = _run_length(raw, i) or 1 own = stacks.get(raw[i : i + length]) @@ -62,4 +65,22 @@ def without_markers(raw: str, frm: int) -> str: elif own is not None and _is_outer(before) and _is_inner(after): own.append(i) i += length + # R13.12.4: the last run of the line, after which there is only R0.2 whitespace. + end = n + while end > frm and is_whitespace(raw[end - 1]): + end -= 1 + start = end + while start > frm and raw[start - 1] == open_run: + start -= 1 + if ( + open_run is not None + and end - start <= 2 + and start > frm + and raw[start - 1] != open_run + and _is_inner(raw[start - 1]) + ): + m = start + while m < end and not marked[m]: + marked[m] = True + m += 1 return "".join(raw[k] for k in range(frm, n) if not marked[k]) diff --git a/python/src/trackparse/_tracklist_times.py b/python/src/trackparse/_tracklist_times.py index 5f26346..bb8ffc2 100644 --- a/python/src/trackparse/_tracklist_times.py +++ b/python/src/trackparse/_tracklist_times.py @@ -177,16 +177,26 @@ def scan_time(raw: str) -> TimeScan: def follows_time(at: int) -> bool: return at >= len(raw) or is_whitespace(raw[at]) or _is_gap_separator(raw[at]) + # R13.12.4: a lead run the gap leaves open may pair with a run at the end of the line. + open_run: Optional[str] = None if closer is not None: - j = _closing_run(raw, j, inner_open) + k = _closing_run(raw, j, inner_open) + if k == j: + open_run = inner_open + j = k if char_at(raw, j) != closer: return _untimed(raw, False) j += 1 k = _closing_run(raw, j, outer_open) if k > j and follows_time(k): j = k + elif outer_open is not None: + open_run = outer_open else: - j = _closing_run(raw, j, outer_open) + k = _closing_run(raw, j, outer_open) + if k == j: + open_run = outer_open + j = k if not follows_time(j): return _untimed(raw, False) j = _skip_whitespace(raw, j) @@ -222,7 +232,7 @@ def follows_time(at: int) -> bool: frac_digits=len(frac), ), False, - without_markers(raw, j), + without_markers(raw, j, open_run), ) diff --git a/python/tests/test_tracklist_properties.py b/python/tests/test_tracklist_properties.py index b091be8..dba1ed8 100644 --- a/python/tests/test_tracklist_properties.py +++ b/python/tests/test_tracklist_properties.py @@ -14,8 +14,8 @@ from runner import SPEC_DIR from trackparse import parse -from trackparse._tracklist_emphasis import without_markers from trackparse._tracklist_kinds import layered_prefix +from trackparse._tracklist_times import scan_time from trackparse.tracklist import apply_edits, parse_tracklist @@ -363,8 +363,8 @@ def eols(t: str) -> list[str]: # layered prefix (R13.13). -def track_part(raw: str, rest: str) -> str: - text = without_markers(raw, len(raw) - len(rest)) +def track_part(raw: str) -> str: + text = scan_time(raw).text return text[layered_prefix(text) :] @@ -376,7 +376,7 @@ def test_one_line_equals_parse(text: str) -> None: if not entries or entries[0].track is None: return e = entries[0] - assert e.track == parse(track_part(e.raw, e.parts.rest), mode="tracklist") + assert e.track == parse(track_part(e.raw), mode="tracklist") # 6. Emphasis around the time or the track part changes nothing. diff --git a/spec/SPEC.md b/spec/SPEC.md index 582d49a..1f4410f 100644 --- a/spec/SPEC.md +++ b/spec/SPEC.md @@ -581,8 +581,8 @@ Read at the start of `raw` (this scanner is separate from R4.1, which is unchang replaces `rest` (R13.9) never changes whether a line has a time. - **R13.2.4 invalid times:** the time candidate (after lead) is the **maximal** run `digits(:digits)+`, plus an optional fraction. When that run fails R13.2.2 (`1:75`, `0:61:00`, - `1:02:75`, `1:02:03:04`), it is not a time: the entry has no time, `rest === raw`, and the - tracklist warns `invalid-time` for the line. + `1:02:75`, `1:02:03:04`), it is not a time: the entry has no time, `rest` is `raw` after the bullet `lead` + (R13.1.3), and the tracklist warns `invalid-time` for the line. - There is no fallback to a shorter shape (`1:02:75` is not read as `1:02`). - A well-formed time followed by a character R13.2.3 does not allow (`12:34x`), or an opened bracket that is not closed after the time, means there is no time and **no** warning. @@ -776,13 +776,15 @@ marker words). Bad values are ignored (R0.8). `marker` line (R13.4.1; one line has no bare position, other kinds have `track: null`). 6. Emphasis changes nothing: wrapping a line's time as `**T**`, `__T__`, `*T*`, `_T_`, `**[T]**`, `[**T**]`, `**[**T**]` or `_[T]_`, or its track part as `**…**`, `__…__`, `*…*` or `_…_`, keeps - its `time.seconds`, `kind` and `track`, as long as the track part does not itself start or end - with `*`, `_` or whitespace. + its `time.seconds`, `kind` and `track`, as long as the track part contains no `*` or `_` and + does not start or end with whitespace. ### R13.12 Emphasis markers Markdown emphasis (`**bold**`, `__bold__`, `*italic*`, `_italic_`, from Discord, Reddit, Telegram or -YouTube comments) is found once per line, on `raw`, by pairing runs: +YouTube comments) is found once per line, in `rest`, by pairing runs. Runs in `lead` and `gap` are +read by position instead (R13.2.1, R13.2.3) and take no part in the pairing, except as R13.12.4 +says: - **R13.12.1 Runs.** A run is a maximal run of `*` or of `_`. Only runs of 1 or 2 characters can be markers; `***` and longer are always text. @@ -795,12 +797,19 @@ YouTube comments) is found once per line, on `raw`, by pairing runs: pairs with the nearest opener on the stack with the same character and length; that opener and every opener above it leave the stack. A run that did not close and can open is pushed. Paired runs are *emphasis markers*; every other run is text. +- **R13.12.4 Open lead run.** When the lead took an emphasis run that the gap did not close (the + first run if both are open), the last run of the line, followed only by R0.2 whitespace, is also + a marker when it is unpaired, is 1 or 2 characters of that run's character, and the character + before it is an inner side. `**[**14:21**] 10. Nitepunk - ID**` and `**14:21 - Nitepunk - ID**` + end that way. No other run pairs with an open lead run: in `**00:00 Artist - F** You` the `**` + of the title is text. So markers never touch a letter, digit or other punctuation on their outer side: `P*nk`, `A**hole`, `DJ_Name`, `Noisia_-_Stigma` and `_-_Intro` have none, and neither do unpaired runs (`*NSYNC - Bye Bye Bye`, `Artist - Holy Sh**`). Backticks, `~~` and `||` are never markers. -`**[**14:21**] 10. Nitepunk - ID**` has four (two pairs), `00:00 Nitepunk - No Manners (**VIP**)` -two. Markers are left out of the line text (R13.4.1); `raw` and `parts` keep them. +In `**[**14:21**] 10. Nitepunk - ID**` three runs sit in `lead` and `gap` and the last one closes +the lead's open `**` (R13.12.4); `00:00 Nitepunk - No Manners (**VIP**)` has two markers. Markers +are left out of the line text (R13.4.1); `raw` and `parts` keep them. ### R13.13 Layered tracks diff --git a/spec/fixtures/handwritten/tracklist-mode.json b/spec/fixtures/handwritten/tracklist-mode.json index 1c3caf4..33cd90c 100644 --- a/spec/fixtures/handwritten/tracklist-mode.json +++ b/spec/fixtures/handwritten/tracklist-mode.json @@ -1897,6 +1897,167 @@ "R13.4.1" ], "note": "Near misses: stars and underscores inside words, unpaired ones, backticks and the _-_ separator are text, never emphasis." + }, + { + "id": "tracklist-mode-062", + "input": "00:00 Artist - Holy Sh** (VIP)", + "expected": { + "entries": [ + { + "track": { + "artists": [ + "Artist" + ], + "title": "Holy Sh**", + "versions": [ + { + "type": "vip" + } + ] + } + } + ] + }, + "rules": [ + "R13.12" + ], + "note": "Near miss: an unpaired closing run inside a word is text." + }, + { + "id": "tracklist-mode-063", + "input": "**[**00:00**] Artist - Holy Sh** (VIP)", + "expected": { + "entries": [ + { + "parts": { + "lead": "**[**", + "time": "00:00", + "gap": "**] ", + "rest": "Artist - Holy Sh** (VIP)" + }, + "time": { + "raw": "00:00", + "seconds": 0 + }, + "track": { + "artists": [ + "Artist" + ], + "title": "Holy Sh**", + "versions": [ + { + "type": "vip" + } + ] + } + } + ] + }, + "rules": [ + "R13.12", + "R13.12.4", + "R13.2.3" + ], + "note": "The lead's open ** may only pair with a run at the end of the line, so the ** in the title stays." + }, + { + "id": "tracklist-mode-064", + "input": "**00:00 Artist - F** You", + "expected": { + "entries": [ + { + "parts": { + "lead": "**", + "time": "00:00", + "gap": " ", + "rest": "Artist - F** You" + }, + "time": { + "raw": "00:00", + "seconds": 0 + }, + "track": { + "artists": [ + "Artist" + ], + "title": "F** You" + } + } + ] + }, + "rules": [ + "R13.12", + "R13.12.4" + ], + "note": "A bold time without its closer does not eat a censored word." + }, + { + "id": "tracklist-mode-065", + "input": "**00:00 Artist - F** You**", + "expected": { + "entries": [ + { + "track": { + "artists": [ + "Artist" + ], + "title": "F** You" + } + } + ] + }, + "rules": [ + "R13.12", + "R13.12.4" + ], + "note": "The run at the end of the line closes the lead's open **; the one inside the title stays." + }, + { + "id": "tracklist-mode-066", + "input": "[**00:00] Artist - ID**", + "expected": { + "entries": [ + { + "parts": { + "lead": "[**", + "time": "00:00", + "gap": "] ", + "rest": "Artist - ID**" + }, + "track": { + "artists": [ + "Artist" + ], + "title": "ID" + } + } + ] + }, + "rules": [ + "R13.12", + "R13.12.4" + ] + }, + { + "id": "tracklist-mode-067", + "input": "**00:00 Artist - ID ***", + "expected": { + "entries": [ + { + "track": { + "artists": [ + "Artist" + ], + "title": "ID ***" + } + } + ] + }, + "rules": [ + "R13.12", + "R13.12.4" + ], + "note": "Near miss: a run of three at the end is never a marker." } ] }