barmark 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,19 @@ Releases follow the process in [RELEASING.md](RELEASING.md).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.13.0] - 2026-11-03
13
+
14
+ ### Added
15
+
16
+ - **`part:` names the chart's part** (grammar 7.3, issue #70): `part:
17
+ Electric Guitar 1`, or with a short label, `part: Gtr = Electric
18
+ Guitar 1`. It goes on a line of its own, once, before the first bar,
19
+ and the name may hold commas. The parse gains `part` (`{ name, label?
20
+ }`, or null) and a `part` line kind; the canonical form is `part:
21
+ Label = Name`. Errors for a missing name or label, a `part:` after the
22
+ first bar or a second one, and one sharing its line with a directive;
23
+ `instrument:` and `parts:` are near misses.
24
+
12
25
  ## [0.12.0] - 2026-11-03
13
26
 
14
27
  Rehearsal marks and words on a bar, issue #70 (grammar 2.5 and 2.6);
@@ -304,7 +317,8 @@ The first working release: the reference parser and printer for the
304
317
 
305
318
  - **A placeholder package that holds the `barmark` name** while the spec and parser are written.
306
319
 
307
- [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.12.0...HEAD
320
+ [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.13.0...HEAD
321
+ [0.13.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.12.0...barmark-v0.13.0
308
322
  [0.12.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.11.0...barmark-v0.12.0
309
323
  [0.11.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.10.0...barmark-v0.11.0
310
324
  [0.10.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.9.0...barmark-v0.10.0
package/README.md CHANGED
@@ -47,6 +47,7 @@ import {
47
47
 
48
48
  const chart = parseChart(text);
49
49
  chart.title; // "Example Song"
50
+ chart.part; // { name: "Electric Guitar 1", label: "Gtr" } from part: Gtr = Electric Guitar 1, or null
50
51
  chart.bars; // every bar in play order: its number as written, slots, section, key, meter, tempo, feel (and swingPercent)
51
52
  chart.sections; // each section as played, with its first and last bar
52
53
  chart.diagnostics; // errors and warnings, each with a line number
@@ -109,6 +109,10 @@ export function parseDirectiveLine(text, line) {
109
109
  const m = /^(\w+)\s*:\s*(.*)$/.exec(part);
110
110
  const name = m?.[1]?.toLowerCase();
111
111
  const value = m?.[2]?.trim() ?? "";
112
+ if (name === "part") {
113
+ error("part: goes on a line of its own; the name runs to the end of the line", part);
114
+ continue;
115
+ }
112
116
  if (!m || !name || !DIRECTIVE_NAMES.includes(name)) {
113
117
  error("unknown directive (key, time, tempo, feel)", part);
114
118
  continue;
@@ -1,7 +1,8 @@
1
- import type { ChartDiagnostic, Line } from "./types.js";
1
+ import type { ChartDiagnostic, Line, Part } from "./types.js";
2
2
  /** Classify every line of a chart: title, sections, directives, bar lines and text. */
3
3
  export declare function readDocument(text: string): {
4
4
  title: string | null;
5
+ part: Part | null;
5
6
  lines: Line[];
6
7
  diagnostics: ChartDiagnostic[];
7
8
  };
@@ -6,7 +6,26 @@ const NEAR_DIRECTIVES = {
6
6
  signature: "time",
7
7
  bpm: "tempo",
8
8
  groove: "feel",
9
+ instrument: "part",
10
+ parts: "part",
9
11
  };
12
+ const DIRECTIVE_AFTER = /,\s*(key|time|tempo|feel)\s*:/i;
13
+ /** A `part:` line's label and name, or why it has none (grammar 7.3). */
14
+ function readPart(text) {
15
+ const m = /^part\s*:(.*)$/i.exec(text);
16
+ if (!m)
17
+ return null;
18
+ const value = (m[1] ?? "").trim().replace(/\s+/g, " ");
19
+ if (DIRECTIVE_AFTER.test(value))
20
+ return "part: goes on a line of its own; the name runs to the end of the line";
21
+ const labelled = /^([^\s=]*)\s*=\s*(.*)$/.exec(value);
22
+ if (!labelled)
23
+ return value ? { name: value } : "part: needs a name";
24
+ const [, label, name] = labelled;
25
+ if (!label)
26
+ return "part: needs a label before =";
27
+ return name ? { label, name } : "part: needs a name after the label";
28
+ }
10
29
  function readHeading(text, line, diagnostics) {
11
30
  let rest = text.trim();
12
31
  const elided = /(->|→)$/.test(rest);
@@ -99,50 +118,65 @@ export function readDocument(text) {
99
118
  const lines = [];
100
119
  const diagnostics = [];
101
120
  let title = null;
102
- const physical = text.replace(/^/, "").split(/\r?\n/);
103
- physical.forEach((raw, i) => {
104
- const line = i + 1;
105
- const t = raw.trim();
106
- if (!t || t.startsWith("```"))
107
- return;
108
- if (t.startsWith("|")) {
109
- const r = parseBarLine(t, line);
110
- diagnostics.push(...r.diagnostics);
111
- lines.push(r.bars ?? { kind: "invalid", line, text: t, written: r.written });
112
- return;
113
- }
114
- if (t.startsWith("# ")) {
115
- if (title === null) {
116
- title = t.slice(2).trim();
117
- lines.push({ kind: "title", line, text: title });
118
- return;
119
- }
120
- diagnostics.push({
121
- line,
122
- severity: "warning",
123
- message: "a chart has one title; this is text",
124
- token: t,
125
- });
126
- lines.push({ kind: "text", line, text: t });
127
- return;
128
- }
129
- if (t === "##" || t.startsWith("## ")) {
130
- lines.push(readHeading(t.slice(2), line, diagnostics));
131
- return;
132
- }
121
+ let part = null;
122
+ /** The line kept as text, with why it isn't what it looks like. */
123
+ const asText = (t, line, severity, message) => {
124
+ diagnostics.push({ line, severity, message, token: t });
125
+ return { kind: "text", line, text: t };
126
+ };
127
+ const titleLine = (t, line) => {
128
+ if (title !== null)
129
+ return asText(t, line, "warning", "a chart has one title; this is text");
130
+ title = t.slice(2).trim();
131
+ return { kind: "title", line, text: title };
132
+ };
133
+ /** A `part:` line names the chart once, before its first bar (grammar 7.3). */
134
+ const partLine = (read, t, line) => {
135
+ if (typeof read === "string")
136
+ return asText(t, line, "error", read);
137
+ if (lines.some((l) => l.kind === "bars" || l.kind === "invalid"))
138
+ return asText(t, line, "error", "part: names the whole chart; put it before the first bar");
139
+ if (part)
140
+ return asText(t, line, "error", "a chart names one part; several parts are not supported yet");
141
+ part = read;
142
+ return { kind: "part", line, ...read };
143
+ };
144
+ const otherLine = (t, line) => {
145
+ const read = readPart(t);
146
+ if (read)
147
+ return partLine(read, t, line);
133
148
  const directive = parseDirectiveLine(t, line);
134
149
  if (directive) {
135
150
  diagnostics.push(...directive.diagnostics);
136
- lines.push({ kind: "directive", line, set: directive.set });
137
- return;
151
+ return { kind: "directive", line, set: directive.set };
138
152
  }
139
153
  const old = oldFormat(t, line);
140
154
  if (old)
141
155
  diagnostics.push(old);
142
- lines.push({ kind: "text", line, text: t });
156
+ return { kind: "text", line, text: t };
157
+ };
158
+ const classify = (t, line) => {
159
+ if (t.startsWith("|")) {
160
+ const r = parseBarLine(t, line);
161
+ diagnostics.push(...r.diagnostics);
162
+ return r.bars ?? { kind: "invalid", line, text: t, written: r.written };
163
+ }
164
+ if (t.startsWith("# "))
165
+ return titleLine(t, line);
166
+ if (t === "##" || t.startsWith("## "))
167
+ return readHeading(t.slice(2), line, diagnostics);
168
+ return otherLine(t, line);
169
+ };
170
+ text
171
+ .replace(/^\uFEFF/, "")
172
+ .split(/\r?\n/)
173
+ .forEach((raw, i) => {
174
+ const t = raw.trim();
175
+ if (t && !t.startsWith("```"))
176
+ lines.push(classify(t, i + 1));
143
177
  });
144
178
  const empty = noBarLines(lines);
145
179
  if (empty)
146
180
  diagnostics.push(empty);
147
- return { title, lines, diagnostics };
181
+ return { title, part, lines, diagnostics };
148
182
  }
@@ -8,6 +8,7 @@ export function parseChart(text) {
8
8
  const diagnostics = [...doc.diagnostics, ...played.diagnostics].sort((a, b) => a.line - b.line || rank[a.severity] - rank[b.severity]);
9
9
  return {
10
10
  title: doc.title,
11
+ part: doc.part,
11
12
  lines: doc.lines,
12
13
  events: played.events,
13
14
  bars: flattenBars(played.events),
@@ -136,6 +136,8 @@ export function printLine(l, beats = []) {
136
136
  return `## ${l.name.replace(/\s+/g, " ")}${printRepeat(l.repeat)}${l.elided ? " ->" : ""}`;
137
137
  case "directive":
138
138
  return printDirectives(l.set);
139
+ case "part":
140
+ return `part: ${l.label ? `${l.label} = ` : ""}${l.name}`;
139
141
  case "bars":
140
142
  return `${l.opens ? "|:" : l.ending ? `|${printPasses(l.ending)}` : "|"} ${l.cells.map((c, i) => printCell(c, beats[i])).join(" | ")} ${l.closes ? ":|" : "|"}${printRepeat(l.repeat)}`;
141
143
  default:
@@ -26,6 +26,14 @@ export type TempoChange = {
26
26
  kind: "a tempo";
27
27
  };
28
28
  /** `tempo:` gives either `tempo` (a steady tempo) or `tempoChange`, never both. */
29
+ /**
30
+ * The part a chart is written for (grammar 7.3): `part: Electric Guitar 1`,
31
+ * or with a short label, `part: Gtr = Electric Guitar 1`.
32
+ */
33
+ export type Part = {
34
+ name: string;
35
+ label?: string;
36
+ };
29
37
  export type Directives = {
30
38
  key?: Key;
31
39
  time?: Meter;
@@ -138,7 +146,10 @@ export type Line = {
138
146
  kind: "directive";
139
147
  line: number;
140
148
  set: Directives;
141
- } | {
149
+ } | ({
150
+ kind: "part";
151
+ line: number;
152
+ } & Part) | {
142
153
  kind: "bars";
143
154
  line: number;
144
155
  opens: boolean;
@@ -222,6 +233,8 @@ export type SectionOccurrence = {
222
233
  };
223
234
  export type ChartParse = {
224
235
  title: string | null;
236
+ /** What `part:` names (grammar 7.3); null when the chart names none. */
237
+ part: Part | null;
225
238
  lines: Line[];
226
239
  events: ChartEvent[];
227
240
  /** `events` flattened, with an open repeat's pass included once. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barmark",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Markdown-inspired plain-text notation for chord charts: a parser, a canonical printer and Harte-label conversion.",
5
5
  "type": "module",
6
6
  "exports": {