Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jpz support #24

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions lib/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare module "saxen" {
export class Parser {
on(event: "openTag", cb: (elementName: string, attrGetter: () => any) => void): void
on(event: "closeTag", cb: (elementName: string) => void): void
on(event: "text", cb: (text: string) => void): void
parse(xmlString: string): void
}
}
96 changes: 96 additions & 0 deletions lib/jpzToXD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Parser } from "saxen"
import { CrosswordJSON } from "./types"
import { JSONToXD } from "./JSONtoXD"

//

/** Takes a jpz string and converts it to an xd file */
export function jpzToXD(xmlString: string) {
const parser = new Parser()
let xd = ""

const blankJSON: Omit<CrosswordJSON, "report"> = {
meta: {
title: "Not set",
author: "Not set",
editor: "Not set",
date: "Not set",
},
tiles: [],
clues: { across: [], down: [] },
notes: "",
rebuses: {},
}

let lastTag = "xml"
let inClueParsing = false
let clueDirection = null as null | "across" | "down"

parser.on("openTag", function (elementName, attrGetter) {
console.log("openTag", elementName)
lastTag = elementName

const attrs = attrGetter()
console.log("attrs", attrs)

if (elementName === "grid") {
blankJSON.meta.width = attrs.width
blankJSON.meta.height = attrs.height
}

if (elementName === "cell") {
const isBlock = attrs.type === "block"
const isCell = !attrs.type

const { x, y, solution } = attrs
if (!blankJSON.tiles[y]) blankJSON.tiles[y] = []
if (isBlock) {
blankJSON.tiles[y][x] = {
type: "blank",
}
} else if (isCell) {
blankJSON.tiles[y][x] = {
type: "letter",
letter: solution,
}
}
}

if (elementName === "clues") {
inClueParsing = true
}

if (elementName === "clue" && clueDirection) {
const { number } = attrs
const clue = {
number,
clue: "",
answer: "",
}
// blankJSON.clues[clueDirection].push({
// number,
// tiles: [],
// body:
// })
}
})

parser.on("text", function (text) {
if (lastTag === "creator") blankJSON.meta.author = text
if (lastTag === "title" && !inClueParsing) blankJSON.meta.title = text
if (lastTag === "description") blankJSON.notes = text
if (lastTag === "created_at") blankJSON.meta.date = text

// This might need strengthening
if (lastTag === "title" && inClueParsing) clueDirection = text.toLowerCase() as "across" | "down"
})

parser.on("closeTag", function (elementName) {
console.log("closeTag", elementName)
xd += elementName
})

parser.parse(xmlString)

return JSONToXD({ ...blankJSON, report: { success: true, errors: [], warnings: [] } })
}
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,23 @@
"printWidth": 140,
"semi": false
},
"resolutions": {
"typescript": "^5.6.3"
},
"dependencies": {
"saxen": "10.0.0"
},
"devDependencies": {
"@swc/cli": "^0.1.55",
"@swc/core": "^1.2.135",
"@swc/jest": "^0.2.17",
"@swc/core": "^1.7.40",
"@swc/jest": "^0.2.36",
"@types/jest": "^27.4.0",
"@types/node": "^16.11.21",
"jest": "^27.4.7",
"jest-file-snapshot": "^0.5.0",
"markdown-magic": "^2.6.0",
"tsup": "^5.11.13",
"typescript": "^4.5.5",
"typescript": "^5.6.3",
"@confuzzle/writepuz": "^1.2.3",
"@confuzzle/readpuz": "^1.2.3"
},
Expand Down
Loading