Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions js/src/tracklist/emphasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number[]> = { "*": [], "**": [], _: [], __: [] };
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];
Expand All @@ -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;
Expand Down
15 changes: 11 additions & 4 deletions js/src/tracklist/times.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -196,7 +203,7 @@ export function scanTime(raw: string): TimeScan {
fracDigits: frac.length,
},
invalid: false,
text: withoutMarkers(raw, j),
text: withoutMarkers(raw, j, open),
};
}

Expand Down
8 changes: 4 additions & 4 deletions js/test/tracklist.properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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));
};

Expand All @@ -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 },
);
Expand Down
33 changes: 27 additions & 6 deletions python/src/trackparse/_tracklist_emphasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
16 changes: 13 additions & 3 deletions python/src/trackparse/_tracklist_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
)


Expand Down
8 changes: 4 additions & 4 deletions python/tests/test_tracklist_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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) :]


Expand All @@ -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.
Expand Down
23 changes: 16 additions & 7 deletions spec/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down
Loading
Loading