Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4565,7 +4565,7 @@ The main `xd-crossword-tools` package provides comprehensive functionality for f
| `xdDiff` | Creates semantic diff between two XD files | `beforeXD: string`, `afterXD: string` | `DiffResults` | Line-by-line differences with metadata awareness |
| `runLinterForClue` | Runs linting checks on individual clues | `clue: Clue`, `ordinal: "across" \| "down"` | `Report[]` | Checks for common crossword construction issues |
| `validateClueAnswersMatchGrid` | Validates clue answers match grid tiles | `json: CrosswordJSON` | `Report[]` | Checks consistency between answers and grid |
| `resolveFullClueAnswer` | Resolves clue answer with rebus and splits | `rebusMap: Rebuses`, `clue: Clue`, `splitChar: string` | `string` | Handles rebus substitution and split characters |
| `resolveFullClueAnswer` | Resolves clue answer with rebus and splits | `clue: Clue`, `splitChar: string` | `string` | Handles rebus substitution and split characters |

### xd-crossword-tools-parser Utility Functions

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "xd-crossword-monorepo",
"private": true,
"version": "11.1.4",
"version": "12.0.0",
"description": "Tools for taking different crossword file formats and converting them to xd, and for converting an xd file to useful JSON",
"scripts": {
"build": "yarn workspaces foreach -A run build",
Expand Down
103 changes: 91 additions & 12 deletions packages/xd-crossword-tools-parser/src/parser/xdparser2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ export function xdToJSON(xd: string, strict = false, editorInfo = false): Crossw
continue
}

const answerWithRebusSymbols = replaceWordWithSymbol(clue.answer, tiles, json.meta.splitcharacter)
const splits = parseSplitsFromAnswer(answerWithRebusSymbols, json.meta.splitcharacter)
const splitResult = parseSplitsFromAnswer(clue.answer, json.meta.splitcharacter, tiles)

if (editorInfo && clue.metadata) clue.metadata["answer:unprocessed"] = clue.answer

Expand All @@ -412,7 +411,8 @@ export function xdToJSON(xd: string, strict = false, editorInfo = false): Crossw
metadata: hasFields ? processedMetadata : undefined,
display: clue.display,
direction: dirKey,
...(splits ? { splits } : {}),
...(splitResult.splits ? { splits: splitResult.splits } : {}),
...(splitResult.rebusInternalSplits ? { rebusInternalSplits: splitResult.rebusInternalSplits } : {}),
})
}

Expand Down Expand Up @@ -817,20 +817,99 @@ function parseStyleCSSLike(str: string, xd: string, errorReporter: (msg: string,
* @param splitCharacter character to split on
* @returns an array of split locations
*/
function parseSplitsFromAnswer(answerWithSplits: string, splitCharacter?: string): number[] | undefined {
if (!splitCharacter) return undefined
const splits = []
const characters = [...answerWithSplits] // account for unicode characters like emojis that could take up more than one utf-16 unit
for (var i = 0; i < characters.length; i++) {
function parseSplitsFromAnswer(
answerWithSplits: string,
splitCharacter?: string,
tiles?: Tile[]
): {
splits?: number[]
rebusInternalSplits?: Record<number, number[]>
} {
if (!splitCharacter) return {}

// Extract split positions from the answer, counting by code points
const characters = [...answerWithSplits]
const splitPositions: number[] = []

let charIndex = 0
for (let i = 0; i < characters.length; i++) {
if (characters[i] === splitCharacter) {
splits.push(characters.slice(0, i).filter((c) => c !== splitCharacter).length - 1)
splitPositions.push(charIndex - 1)
} else {
charIndex++
}
}

if (splitPositions.length === 0) return {}

// If no tiles provided, return simple splits (backward compatibility)
if (!tiles) {
// Deduplicate and sort in case of repeated split chars
const dedup = Array.from(new Set(splitPositions)).sort((a, b) => a - b)
return { splits: dedup }
}

// Map code-point character positions to tile positions and internal indices
let currentCharIndex = 0
let currentTileIndex = 0
const charToTileMap = new Map<number, { tileIndex: number; internalIndex: number }>()

for (const tile of tiles) {
if (tile.type === "rebus") {
let internal = 0
for (const _ch of [...tile.word]) {
charToTileMap.set(currentCharIndex, { tileIndex: currentTileIndex, internalIndex: internal })
currentCharIndex++
internal++
}
} else {
charToTileMap.set(currentCharIndex, { tileIndex: currentTileIndex, internalIndex: 0 })
currentCharIndex++
}
currentTileIndex++
}

// Categorize splits: tile boundary vs internal rebus splits
const tileBoundarySplits: number[] = []
const rebusInternalSplits: Record<number, number[]> = {}

for (const splitPos of splitPositions) {
const mapping = charToTileMap.get(splitPos)
if (!mapping) continue

const { tileIndex, internalIndex } = mapping
const tile = tiles[tileIndex]

if (tile.type === "rebus") {
const cpLen = [...tile.word].length
if (internalIndex < cpLen - 1) {
// Internal split within a rebus (by code-point index)
if (!rebusInternalSplits[tileIndex]) rebusInternalSplits[tileIndex] = []
rebusInternalSplits[tileIndex].push(internalIndex)
continue
}
}
// Otherwise, a split at a tile boundary
tileBoundarySplits.push(tileIndex)
}

// Only include the splits when it is used
if (splits.length === 0) return undefined
const result: { splits?: number[]; rebusInternalSplits?: Record<number, number[]> } = {}

if (tileBoundarySplits.length > 0) {
result.splits = Array.from(new Set(tileBoundarySplits)).sort((a, b) => a - b)
}

if (Object.keys(rebusInternalSplits).length > 0) {
// Dedupe and sort internal splits for each rebus
for (const tileIndex in rebusInternalSplits) {
const dedup = Array.from(new Set(rebusInternalSplits[tileIndex]))
dedup.sort((a, b) => a - b)
rebusInternalSplits[tileIndex] = dedup
}
result.rebusInternalSplits = rebusInternalSplits
}

return splits
return result
}

/** xd spec compliant parser for markup inside a clue */
Expand Down
2 changes: 2 additions & 0 deletions packages/xd-crossword-tools-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ export interface Clue {
direction: CursorDirection
/** If an answer contains a split character, then this would include the indexes where it was used */
splits?: number[]
/** For splits that occur within rebus squares, maps tile index to array of internal split positions */
rebusInternalSplits?: Record<number, number[]>
/**
* Duplicating a clue and using a meta suffix (e.g. "A23 ^Hint. A shot to the heart" )
* would add to { "hint": " A shot to the heart" } to the metadata. This works for any key
Expand Down
86 changes: 50 additions & 36 deletions packages/xd-crossword-tools/src/JSONtoXD.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
import type { Clue, CrosswordJSON, Tile } from "xd-crossword-tools-parser"

export function resolveFullClueAnswer(rebusMap: CrosswordJSON["rebuses"], clue: Clue, splitChar: string) {
// For simple cases (no rebus, no splits), just return the answer directly
export function resolveFullClueAnswer(clue: Clue, splitChar: string) {
// For simple cases (no rebus, no splits, no internal splits), just return the answer directly
const hasRebus = clue.tiles.some((t) => t.type === "rebus")
const hasSplits = clue.splits && clue.splits.length > 0
const hasInternalSplits = clue.rebusInternalSplits && Object.keys(clue.rebusInternalSplits).length > 0

if (!hasRebus && !hasSplits) {
if (!hasRebus && !hasSplits && !hasInternalSplits) {
return clue.answer
}

// In order to correctly pipe rebus clues, we must temporarily substitute in rebus symbols
const replacedWithSymbol = clue.tiles
.map((t: Tile) => {
if (t.type === "rebus") {
return t.symbol
} else if (t.type === "letter") {
return t.letter
} else if (t.type === "schrodinger") {
// For Schrödinger tiles that appear as regular letter tiles with "*",
// we need to get the actual letter from the answer
const tileIndex = clue.tiles.indexOf(t)
return clue.answer[tileIndex] || "*"
} else if (t.type === "blank") {
return "" // This shouldn't happen in a clue answer
}
throw new Error(`Invalid tile type: ${(t as any).type}`)
})
.join("")

// now, apply splits after the replace
const withSplits = addSplits(replacedWithSymbol, splitChar, clue.splits)

// now, replace symbols with words again
const answer = withSplits
.split("")
.map((char) => {
if (rebusMap[char]) {
return rebusMap[char]
// Build the answer tile by tile, applying internal splits to rebus tiles
// Work in Unicode code points to avoid surrogate pair issues.
let result = ""
const answerCodePoints = [...clue.answer]
let currentCharIndex = 0

for (let tileIndex = 0; tileIndex < clue.tiles.length; tileIndex++) {
const tile = clue.tiles[tileIndex]

// Add split before this tile if needed
if (clue.splits && clue.splits.includes(tileIndex - 1)) {
result += splitChar
}

if (tile.type === "rebus") {
const rebusWord = tile.word
const internalSplits = clue.rebusInternalSplits?.[tileIndex] || []

const rebusCP = [...rebusWord]
// Add each code point of the rebus word, with internal splits
for (let i = 0; i < rebusCP.length; i++) {
if (i > 0 && internalSplits.includes(i - 1)) {
result += splitChar
}
result += rebusCP[i]
}
return char
})
.join("")
} else if (tile.type === "letter") {
result += tile.letter
} else if (tile.type === "schrodinger") {
// Get the actual letter from the answer at this position (by code point)
result += answerCodePoints[currentCharIndex] || "*"
} else if (tile.type === "blank") {
// This shouldn't happen in a clue answer
result += ""
} else {
throw new Error(`Invalid tile type: ${(tile as any).type}`)
}

// Update character index (by code points)
if (tile.type === "rebus") {
currentCharIndex += [...tile.word].length
} else if (tile.type !== "blank") {
currentCharIndex++
}
}

return answer
return result
}

export function addSplits(answer: string, splitChar: string, splits?: number[]): string {
Expand Down Expand Up @@ -98,7 +112,7 @@ export const JSONToXD = (json: CrosswordJSON): string => {
const getCluesXD = (clues: Clue[], direction: "A" | "D") => {
return clues
.map((clue) => {
const final = resolveFullClueAnswer(json.rebuses, clue, splitChar)
const final = resolveFullClueAnswer(clue, splitChar)
let line = `${direction}${clue.number}. ${clue.body} ~ ${final}`
if (clue.metadata) {
let printed = false
Expand Down
Loading