Skip to content

Commit

Permalink
Stop encoding list styles into annot
Browse files Browse the repository at this point in the history
This makes the Event type easier to work with.
  • Loading branch information
not-my-profile committed Oct 20, 2024
1 parent ac6ba5b commit 3853a52
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
18 changes: 9 additions & 9 deletions src/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
24 changes: 12 additions & 12 deletions src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,6 @@ class Container {
export type BlockAnnot =
| `+${BlockType}`
| `-${BlockType}`
| `+list|${string}`
| `+list_item|${string}`
| "-list"
| "-list_item"
| "blankline"
| "checkbox_checked"
| "checkbox_unchecked"
Expand All @@ -139,6 +135,8 @@ type BlockType =
| "div"
| "footnote"
| "heading"
| "list"
| "list_item"
| "para"
| "reference_definition"
| "row"
Expand Down Expand Up @@ -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: () => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Annot, '+list' | "+list_item"> });
}
}

getInlineMatches(): void {
Expand Down
11 changes: 7 additions & 4 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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">;
} | {
annot : "+list" | "+list_item";
listStyles: string[];
});

export type { Event }
15 changes: 5 additions & 10 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from "./event";
import { Annot, Event } from "./event";
import { parseEvents } from "./block";
import { Options, Warning } from "./options";
import {
Expand Down Expand Up @@ -246,10 +246,10 @@ const parse = function(input: string, options: ParseOptions = {}): Doc {
}


const handlers : Record<string, (suffixes : string[],
const handlers : Partial<Record<Annot, (suffixes : string[],
startpos : number,
endpos : number,
pos : Pos | undefined) => void> =
pos : Pos | undefined) => void>> =
{ str: (suffixes, startpos, endpos, pos) => {
const txt = input.substring(startpos, endpos + 1);
if (context === Context.Normal) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 3853a52

Please sign in to comment.