From 3853a52ad901e0e9f9112be746a7059f180b6dec Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sun, 20 Oct 2024 16:17:33 +0200 Subject: [PATCH] Stop encoding list styles into annot This makes the Event type easier to work with. --- src/block.spec.ts | 18 +++++++++--------- src/block.ts | 24 ++++++++++++------------ src/event.ts | 11 +++++++---- src/parse.ts | 15 +++++---------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/block.spec.ts b/src/block.spec.ts index 1be2f76..e285197 100644 --- a/src/block.spec.ts +++ b/src/block.spec.ts @@ -130,33 +130,33 @@ describe("EventParser", () => { events.push(event); } expect(events).toStrictEqual([ - { startpos: 0, endpos: 0, annot: "+list|-" }, - { startpos: 0, endpos: 0, annot: "+list_item|-" }, + { startpos: 0, endpos: 0, annot: "+list", listStyles: ["-"] }, + { startpos: 0, endpos: 0, annot: "+list_item", listStyles: ["-"] }, { startpos: 2, endpos: 2, annot: "+para" }, { startpos: 2, endpos: 4, annot: "str" }, { startpos: 5, endpos: 5, annot: "-para" }, { startpos: 5, endpos: 5, annot: "-list_item" }, - { startpos: 6, endpos: 6, annot: "+list_item|-" }, + { startpos: 6, endpos: 6, annot: "+list_item", listStyles: ["-"] }, { startpos: 8, endpos: 8, annot: "+para" }, { startpos: 8, endpos: 10, annot: "str" }, { startpos: 11, endpos: 11, annot: "-para" }, { startpos: 11, endpos: 11, annot: "-list_item" }, { startpos: 12, endpos: 12, annot: "-list" }, - { startpos: 12, endpos: 13, annot: "+list|1." }, - { startpos: 12, endpos: 13, annot: "+list_item|1." }, + { startpos: 12, endpos: 13, annot: "+list", listStyles: ["1."] }, + { startpos: 12, endpos: 13, annot: "+list_item", listStyles: ["1."] }, { startpos: 15, endpos: 15, annot: "+para" }, { startpos: 15, endpos: 19, annot: "str" }, { startpos: 20, endpos: 20, annot: "-para" }, { startpos: 20, endpos: 20, annot: "-list_item" }, { startpos: 21, endpos: 21, annot: "-list" }, - { startpos: 21, endpos: 24, annot: "+list|(i)" }, - { startpos: 21, endpos: 24, annot: "+list_item|(i)" }, + { startpos: 21, endpos: 24, annot: "+list", listStyles: ["(i)"] }, + { startpos: 21, endpos: 24, annot: "+list_item", listStyles: ["(i)"] }, { startpos: 26, endpos: 26, annot: "+para" }, { startpos: 26, endpos: 29, annot: "str" }, { startpos: 30, endpos: 30, annot: "-para" }, { startpos: 31, endpos: 31, annot: "blankline" }, - { startpos: 33, endpos: 33, annot: "+list|-" }, - { startpos: 33, endpos: 33, annot: "+list_item|-" }, + { startpos: 33, endpos: 33, annot: "+list", listStyles: ["-"] }, + { startpos: 33, endpos: 33, annot: "+list_item", listStyles: ["-"] }, { startpos: 35, endpos: 35, annot: "+para" }, { startpos: 35, endpos: 37, annot: "str" }, { startpos: 38, endpos: 38, annot: "-para" }, diff --git a/src/block.ts b/src/block.ts index f86a3ec..b4c6f51 100644 --- a/src/block.ts +++ b/src/block.ts @@ -109,10 +109,6 @@ class Container { export type BlockAnnot = | `+${BlockType}` | `-${BlockType}` - | `+list|${string}` - | `+list_item|${string}` - | "-list" - | "-list_item" | "blankline" | "checkbox_checked" | "checkbox_unchecked" @@ -139,6 +135,8 @@ type BlockType = | "div" | "footnote" | "heading" + | "list" + | "list_item" | "para" | "reference_definition" | "row" @@ -461,7 +459,7 @@ class EventParser { const data = { styles: styles, indent: this.indent }; // adding container will close others this.addContainer(new Container(spec, data)); - this.addMatch(sp, ep - 1, `+list|${styles.join("|")}`); + this.addMatch(sp, ep - 1, '+list', styles); return true; }, close: () => { @@ -502,7 +500,7 @@ class EventParser { const data = { styles: styles, indent: this.indent }; // adding container will close others this.addContainer(new Container(spec, data)); - this.addMatch(sp, ep - 1, `+list_item|${styles.join("|")}`); + this.addMatch(sp, ep - 1, '+list_item', styles); this.pos = ep; if (checkbox) { @@ -783,12 +781,14 @@ class EventParser { } } - addMatch(startpos: number, endpos: number, annot: Annot): void { - this.matches.push({ - startpos: Math.min(startpos, this.maxoffset), - endpos: Math.min(endpos, this.maxoffset), - annot: annot - }); + addMatch(startpos: number, endpos: number, annot: Annot, listStyles?: string[]): void { + startpos = Math.min(startpos, this.maxoffset); + endpos = Math.min(endpos, this.maxoffset); + if (listStyles) { + this.matches.push({ startpos, endpos, annot: annot as '+list' | "+list_item", listStyles }); + } else { + this.matches.push({ startpos, endpos, annot: annot as Exclude }); + } } getInlineMatches(): void { diff --git a/src/event.ts b/src/event.ts index f3c831a..2222100 100644 --- a/src/event.ts +++ b/src/event.ts @@ -4,11 +4,14 @@ import { InlineAnnot } from "./inline"; export type Annot = BlockAnnot | InlineAnnot | AttrAnnot; -interface Event { +type Event = { startpos : number; endpos : number; - annot : Annot; - -} +} & ({ + annot : Exclude; +} | { + annot : "+list" | "+list_item"; + listStyles: string[]; +}); export type { Event } diff --git a/src/parse.ts b/src/parse.ts index 2552b83..6800f44 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -1,4 +1,4 @@ -import { Event } from "./event"; +import { Annot, Event } from "./event"; import { parseEvents } from "./block"; import { Options, Warning } from "./options"; import { @@ -246,10 +246,10 @@ const parse = function(input: string, options: ParseOptions = {}): Doc { } - const handlers : Record void> = + pos : Pos | undefined) => void>> = { str: (suffixes, startpos, endpos, pos) => { const txt = input.substring(startpos, endpos + 1); if (context === Context.Normal) { @@ -1196,13 +1196,7 @@ const parse = function(input: string, options: ParseOptions = {}): Doc { ep = getSourceLoc(event.endpos); pos = { start: sp, end: ep }; } - let annot: string = event.annot; - let suffixes: string[] = []; - if (event.annot.includes("|")) { - const parts = event.annot.split("|"); - annot = parts[0]; - suffixes = parts.slice(1); - } + const annot = event.annot; // The following is for tight/loose determination. // If blanklines have already been seen, and we're @@ -1232,6 +1226,7 @@ const parse = function(input: string, options: ParseOptions = {}): Doc { const fn = handlers[annot]; if (fn) { + const suffixes = 'listStyles' in event ? event.listStyles : []; fn(suffixes, event.startpos, event.endpos, pos); }