Skip to content

Commit

Permalink
Switch clueReferencer and grid referencing over to new parseClueRefer…
Browse files Browse the repository at this point in the history
…ences helper
  • Loading branch information
mdirolf committed Mar 31, 2024
1 parent b30c819 commit 1d38924
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 84 deletions.
6 changes: 4 additions & 2 deletions app/components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import rehypeReact, { Components } from 'rehype-react';
import { unified } from 'unified';

import type { Root } from 'hast';
import { ReferenceData } from '../lib/markdown/clueReferencer';

import * as prod from 'react/jsx-runtime';
import { ClueReferenceData } from '../lib/parse';
const production = { Fragment: prod.Fragment, jsx: prod.jsx, jsxs: prod.jsxs };

export const Markdown = (props: {
Expand All @@ -27,7 +27,9 @@ export const Markdown = (props: {
);
},
span: ({ node, children, className, ...props }) => {
const ref = node?.data as (ReferenceData & { text: string }) | undefined;
const ref = node?.data as
| (ClueReferenceData & { text: string })
| undefined;
if (className === 'clueref' && ref !== undefined) {
return (
<ClueReference
Expand Down
43 changes: 2 additions & 41 deletions app/lib/markdown/clueReferencer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { Plugin } from 'unified';

import { Direction } from '../../lib/types';
import { Node } from 'unist';
import { Text, Element } from 'hast';
import { is } from 'unist-util-is';
import { flatMap } from './utils';

export interface ReferenceData {
direction: Direction;
labelNumber: number;
start: number;
end: number;
}
import { parseClueReferences } from '../parse';

export const clueReferencer: Plugin = () => {
return (tree) => {
Expand All @@ -24,39 +17,7 @@ export const clueReferencer: Plugin = () => {
return [node];
}
const value = node.value;
const refs: ReferenceData[] = [];
let match;
const re =
/(^|\s|\/|\()(?=(?<numSection>(,? ?(and)? ?\b\d+-? ?)+))\k<numSection>(?<dir>a(cross(es)?)?|d(owns?)?)\b/gi;
while ((match = re.exec(value)) !== null) {
const preLength = match[1]?.length ?? 0;
const dirString = match.groups?.dir?.toLowerCase();
if (!dirString) {
throw new Error('missing dir string');
}
const direction = dirString.startsWith('a')
? Direction.Across
: Direction.Down;
const numSection = match.groups?.numSection;
if (!numSection) {
throw new Error('missing numSection');
}
let numMatch: RegExpExecArray | null;
const numRe = /\d+/g;
while ((numMatch = numRe.exec(numSection)) !== null && numMatch[0]) {
const labelNumber = parseInt(numMatch[0]);
refs.push({
direction,
labelNumber,
start: match.index + numMatch.index + preLength,
end: match.index + numMatch.index + numMatch[0].length + preLength,
});
}
const last = refs[refs.length - 1];
if (last && match[0]) {
last.end = match.index + match[0].length;
}
}
const refs = parseClueReferences(value);
if (!refs.length) {
return [node];
}
Expand Down
10 changes: 2 additions & 8 deletions app/lib/markdown/entryReferencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import { Node } from 'unist';
import { Text, Element } from 'hast';
import { is } from 'unist-util-is';
import { flatMap } from './utils';

interface ReferenceData {
direction: Direction;
labelNumber: number;
start: number;
end: number;
}
import { ClueReferenceData } from '../parse';

interface EntryReferencerOptions {
clueMap: Map<string, [number, Direction, string]>;
Expand All @@ -35,7 +29,7 @@ export const entryReferencer: Plugin<[EntryReferencerOptions]> = (options) => {
) {
return [node];
}
const refs: ReferenceData[] = [];
const refs: ClueReferenceData[] = [];
const value = node.value;
let match;
while ((match = regex.exec(value)) !== null) {
Expand Down
11 changes: 5 additions & 6 deletions app/lib/parse.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Direction } from './types';

export interface ClueReference {
export interface ClueReferenceData {
direction: Direction;
labelNumber: number;
start: number;
end: number;
}

const re =
/(^|\s|\/|\()(?=(?<numSection>(,? ?(and)? ?\b\d+-? ?)+))\k<numSection>(?<dir>a(cross(es)?)?|d(owns?)?)\b/gi;
/(^|\s|\/|\()(?<numSection>(,? ?(and)? ?\b\d{1,3}-? ?){1,7})(?<dir>a(cross(es)?)?|d(owns?)?)\b/gi;

export const parseClueReferences = (text: string): ClueReference[] => {
const refs: ClueReference[] = [];
export const parseClueReferences = (text: string): ClueReferenceData[] => {
const refs: ClueReferenceData[] = [];
let match;
re.lastIndex = 0;

Expand All @@ -36,8 +36,7 @@ export const parseClueReferences = (text: string): ClueReference[] => {
direction,
labelNumber,
start: match.index + numMatch.index + preLength,
end:
match.index + numMatch.index + numMatch[0].length + preLength,
end: match.index + numMatch.index + numMatch[0].length + preLength,
});
}

Expand Down
34 changes: 7 additions & 27 deletions app/lib/viewableGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { AccountPrefsFlagsT } from './prefs';
import type { Root } from 'hast';
import { toString } from 'hast-util-to-string';
import { parseClueReferences } from './parse';

export interface ViewableEntry extends EntryBase {
labelNumber: number;
Expand Down Expand Up @@ -670,40 +671,19 @@ export function getRefs(grid: CluedGrid): [Set<number>[], RefPosition[][]] {
for (const e of grid.entries) {
const refs = new Set<number>();
const refPos: RefPosition[] = [];
let match;
const re =
/(?=(?<numSection>(,? ?(and)? ?\b\d+-? ?)+))\k<numSection>(?<dir>a(cross(es)?)?|d(owns?)?)\b/gi;
while (!e.clue.startsWith('!@') && (match = re.exec(e.clue)) !== null) {
const dirString = match.groups?.dir?.toLowerCase();
if (!dirString) {
throw new Error('missing dir string');
}
const dir = dirString.startsWith('a') ? Direction.Across : Direction.Down;
const numSection = match.groups?.numSection;
if (!numSection) {
throw new Error('missing numSection');
}
let numMatch: RegExpExecArray | null;
const numRe = /\d+/g;
while ((numMatch = numRe.exec(numSection)) !== null && numMatch[0]) {
const labelNumber = parseInt(numMatch[0]);
if (!e.clue.startsWith('!@')) {
for (const res of parseClueReferences(e.clue)) {
const entryIndex = grid.entries.findIndex(
(e) => e.labelNumber === labelNumber && e.direction === dir
(e) =>
e.labelNumber === res.labelNumber && e.direction === res.direction
);
if (entryIndex !== -1) {
refs.add(entryIndex);
refPos.push([
entryIndex,
match.index + numMatch.index,
match.index + numMatch.index + numMatch[0].length,
]);
refPos.push([entryIndex, res.start, res.end]);
}
}
const last = refPos[refPos.length - 1];
if (last && match[0]) {
last[2] = match.index + match[0].length;
}
}

const lowerClue = e.clue.toLowerCase();
for (const starTerm of ['starred', '*ed', '*']) {
for (const entryTerm of [
Expand Down

0 comments on commit 1d38924

Please sign in to comment.