Skip to content

Editing tracklists

applyEdits(text, edits) (apply_edits in Python and Rust, ApplyEdits in Go, trackparse_apply_edits in C) takes a tracklist as text and returns new text. It's built for editors: you get back the lines it changed, and every other line keeps its bytes and its line ending. The rules are R13.9 in the spec.

import { applyEdits, parseTracklist } from "trackparse/tracklist";

Using the list from the previous page, apply the suggested fix, then push everything from line 5 on back by 45 seconds:

// text is the list from the previous page.
const fix = parseTracklist(text).warnings[1].suggestion; // { line: 8, raw: '01:11:02', seconds: 4262 }
const result = applyEdits(text, [
{ op: "set-time", line: fix.line, seconds: fix.seconds },
{ op: "shift", seconds: 45, from: 5 },
]);
result.changed; // [ 5, 6, 7, 8, 9 ]
result.warnings; // [ { code: 'style-upgraded', edit: 1, line: 5 } ]
result.text.split("\r\n").slice(4);
// [
// '01:00:16 Alix Perez - Fade Away',
// '01:03:55 Interview with Hybris',
// '01:08:29 Imanu - Sacrifice',
// '01:11:47 Current Value feat. Teddy Killerz - Payload',
// '01:16:21 Hybris - Spinning Top'
// ]

In JavaScript an edit is an object with an op. In Python it is a plain dict with the same keys, "from" included. In Rust it is a value of the Edit enum, made with a function per edit: Edit::set_time(line, seconds), Edit::shift_from(seconds, from). In Go it is an Edit struct, made with a function per edit as well: trackparse.SetTime(line, seconds), trackparse.ShiftFrom(seconds, from). In C the edits go into a trackparse_edits list from trackparse_edits_new(), one call per edit (trackparse_edits_set_time(edits, line, seconds), trackparse_edits_shift_from(edits, seconds, from)), declared in trackparse.h. The list copies the strings it is given, and the result owns its memory, so trackparse_edits_free can run as soon as trackparse_apply_edits returns; the result is freed with trackparse_edit_result_free.

Lines 1 to 4 are untouched, CRLF included (R13.9.1). Edits run in order, each on the text the previous one produced, with line numbers as that text has them. The playground does the same with buttons: the suggested fix, a shift and a sort, with undo.

There are six edits. JavaScript and Python write them the same way, as an object or a dict. Rust and Go have a constructor for each, and C a function that appends it to a trackparse_edits list; the tabs under the table give those names:

JavaScript and Python Effect
{ op: "set-time", line, seconds } Rewrite the time on that line
{ op: "shift", seconds } Add seconds (negative to go back) to every timed line
{ op: "shift", seconds, from } The same, for every timed line from line from on
{ op: "shift", seconds, lines } The same, for the listed lines only
{ op: "sort-by-time" } Reorder by time. Untimed lines move with the timed line above them, and lines before the first time stay on top
{ op: "set-rest", line, rest } Replace everything after the time, for example with format(track) (Python and Rust format_track, Go Format, C trackparse_format)
{ op: "insert", after, text } Insert a line after line after (0 puts it at the top). It gets the text's usual line ending
{ op: "delete", line } Remove a line

An edit is an object with an op and the fields in the table.

Line numbers start at 1. Sorting is R13.9.4, and inserting and deleting R13.9.5.

This example sorts a list, rewrites one line's track text with format, inserts a line and deletes one:

import { format } from "trackparse";
const messy = "04:10 Mefjus ft. Phace - Sleepless\n00:00 Noisia - Stigma\n09:30 Outro";
const rest = format(parseTracklist(messy).entries[0].track, { joiners: "canonical" });
// 'Mefjus feat. Phace - Sleepless'
const tidy = applyEdits(messy, [
{ op: "sort-by-time" },
{ op: "set-rest", line: 2, rest },
{ op: "insert", after: 2, text: "06:45 Phace - Psycho" },
{ op: "delete", line: 4 },
]);
tidy.text; // '00:00 Noisia - Stigma\n04:10 Mefjus feat. Phace - Sleepless\n06:45 Phace - Psycho'
tidy.changed; // [ 1, 2, 3 ]

Each edit sees the text the one before it produced. After the sort, the Mefjus line is line 2, and after the insert the outro is line 4.

A rewritten time looks like its neighbors. Hours or no hours, digit widths and fraction digits are all kept, so 52:47 plus 45 seconds is 53:32, not 00:53:32 (R13.9.2).

When a time without hours crosses the hour, it gains hours in the style the rest of the list uses (59:31 → 01:00:16, or 1:00:16 if the list writes one-digit hours). The edit reports style-upgraded so you know the line got longer.

A list bullet or Markdown emphasis around a time sits in lead and gap (parts), so a rewritten time keeps it. Only the digits change:

const text = "**[**52:47**] Mefjus - Bandwagon**\n**[**59:31**] Alix Perez - Fade Away**";
const result = applyEdits(text, [{ op: "shift", seconds: 45 }]);
result.text;
// '**[**53:32**] Mefjus - Bandwagon**\n**[**01:00:16**] Alix Perez - Fade Away**'
result.warnings; // [ { code: 'style-upgraded', edit: 0, line: 2 } ]

set-rest replaces everything after the gap, including a closing ** at the end of the line. The time is still read afterwards, because emphasis next to a time doesn't need a partner. A layered w/ line without a time moves with the timed line above it when you sort.

Edits never throw or panic. A bad one changes nothing and reports why (R13.9.6). In Rust the codes are the enum EditWarningCode (EditInvalid, TimeClamped, StyleUpgraded), in Go the string type EditWarningCode (trackparse.EditWarningCodeEditInvalid and so on), in C the enum trackparse_edit_warning_code (TRACKPARSE_EDIT_WARNING_EDIT_INVALID and so on):

Code When
edit-invalid missing line, no time on the line, NaN or infinity, a time too large to write, a line break inside text or rest, an unknown op
time-clamped the time would go below zero, so it was set to zero (R13.9.3)
style-upgraded a time gained hours (see above)

Each edit warning is { code, edit, line }, where edit is the index in your edits array and line is null (None in Python and Rust, a nil *int in Go, a NULL const long long * in C) when the edit doesn't name a line. Warnings shows how to check them in each language.

The property tests check these on random text and random edit lists:

  • applyEdits(t, []) returns t.
  • An edit never touches a line it doesn't name.
  • shift(+s) followed by shift(-s) gives back the original text, unless something was clamped or upgraded on the way (R13.9.7).