|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import * as fs from "fs" |
| 4 | +import * as path from "path" |
| 5 | + |
| 6 | +import { puzToXD } from "./puzToXD" |
| 7 | +import { jpzToXD } from "./jpzToXD" |
| 8 | +import { uclickXMLToXD } from "./uclickToXD" |
| 9 | +import { amuseToXD } from "./amuseJSONToXD" |
| 10 | +import { acrossTextToXD } from "./acrossTextToXD" |
| 11 | +import { decodePuzzleMeHTML } from "./puzzleMeDecode" |
| 12 | + |
| 13 | +const SUPPORTED_EXTENSIONS = [".puz", ".jpz", ".xml", ".json", ".txt"] |
| 14 | + |
| 15 | +function usage(): never { |
| 16 | + console.log(`Usage: xd-crossword-tools <input-files...> -o <output-dir> |
| 17 | +
|
| 18 | +Converts crossword puzzle files to .xd format. |
| 19 | +
|
| 20 | +Supported input formats: |
| 21 | + .puz - Across Lite binary format |
| 22 | + .jpz - JPZ XML format |
| 23 | + .xml - UClick XML format |
| 24 | + .json - Amuse Labs JSON format |
| 25 | + .txt - Across text format |
| 26 | + URL - PuzzleMe URL (https://puzzleme.amuselabs.com/...) |
| 27 | +
|
| 28 | +Options: |
| 29 | + -o, --output <dir> Output directory (default: current directory) |
| 30 | + -h, --help Show this help message |
| 31 | +
|
| 32 | +Examples: |
| 33 | + xd-crossword-tools puzzle.puz -o ./converted |
| 34 | + xd-crossword-tools *.puz *.jpz -o ./xd-files |
| 35 | + xd-crossword-tools https://puzzleme.amuselabs.com/pmm/crossword?id=abc -o ./converted`) |
| 36 | + process.exit(0) |
| 37 | +} |
| 38 | + |
| 39 | +function convertFile(filePath: string): string { |
| 40 | + const ext = path.extname(filePath).toLowerCase() |
| 41 | + |
| 42 | + switch (ext) { |
| 43 | + case ".puz": { |
| 44 | + const buffer = fs.readFileSync(filePath) |
| 45 | + return puzToXD(buffer) |
| 46 | + } |
| 47 | + case ".jpz": { |
| 48 | + const content = fs.readFileSync(filePath, "utf-8") |
| 49 | + return jpzToXD(content) |
| 50 | + } |
| 51 | + case ".xml": { |
| 52 | + const content = fs.readFileSync(filePath, "utf-8") |
| 53 | + return uclickXMLToXD(content) |
| 54 | + } |
| 55 | + case ".json": { |
| 56 | + const content = fs.readFileSync(filePath, "utf-8") |
| 57 | + const json = JSON.parse(content) |
| 58 | + return amuseToXD(json) |
| 59 | + } |
| 60 | + case ".txt": { |
| 61 | + const content = fs.readFileSync(filePath, "utf-8") |
| 62 | + return acrossTextToXD(content) |
| 63 | + } |
| 64 | + default: |
| 65 | + throw new Error(`Unsupported file format: ${ext}`) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +async function convertURL(url: string): Promise<string> { |
| 70 | + const response = await fetch(url) |
| 71 | + if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`) |
| 72 | + const html = await response.text() |
| 73 | + const amuse = decodePuzzleMeHTML(html) |
| 74 | + return amuseToXD(amuse) |
| 75 | +} |
| 76 | + |
| 77 | +async function main() { |
| 78 | + const args = process.argv.slice(2) |
| 79 | + |
| 80 | + if (args.length === 0 || args.includes("-h") || args.includes("--help")) { |
| 81 | + usage() |
| 82 | + } |
| 83 | + |
| 84 | + let outputDir = "." |
| 85 | + const inputs: string[] = [] |
| 86 | + |
| 87 | + for (let i = 0; i < args.length; i++) { |
| 88 | + if (args[i] === "-o" || args[i] === "--output") { |
| 89 | + outputDir = args[++i] |
| 90 | + if (!outputDir) { |
| 91 | + console.error("Error: -o requires a directory argument") |
| 92 | + process.exit(1) |
| 93 | + } |
| 94 | + } else if (args[i].startsWith("-")) { |
| 95 | + console.error(`Unknown option: ${args[i]}`) |
| 96 | + process.exit(1) |
| 97 | + } else { |
| 98 | + inputs.push(args[i]) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (inputs.length === 0) { |
| 103 | + console.error("Error: no input files specified") |
| 104 | + process.exit(1) |
| 105 | + } |
| 106 | + |
| 107 | + // Create output directory if needed |
| 108 | + fs.mkdirSync(outputDir, { recursive: true }) |
| 109 | + |
| 110 | + let converted = 0 |
| 111 | + let failed = 0 |
| 112 | + |
| 113 | + for (const input of inputs) { |
| 114 | + const isURL = input.startsWith("http://") || input.startsWith("https://") |
| 115 | + |
| 116 | + try { |
| 117 | + let xd: string |
| 118 | + let outputName: string |
| 119 | + |
| 120 | + if (isURL) { |
| 121 | + const url = new URL(input) |
| 122 | + const id = url.searchParams.get("id") ?? url.pathname.split("/").pop() ?? "puzzle" |
| 123 | + outputName = id |
| 124 | + xd = await convertURL(input) |
| 125 | + } else { |
| 126 | + const ext = path.extname(input).toLowerCase() |
| 127 | + if (!SUPPORTED_EXTENSIONS.includes(ext)) { |
| 128 | + console.error(`Skipping ${input}: unsupported format (${ext})`) |
| 129 | + failed++ |
| 130 | + continue |
| 131 | + } |
| 132 | + outputName = path.basename(input, ext) |
| 133 | + xd = convertFile(input) |
| 134 | + } |
| 135 | + |
| 136 | + const outputPath = path.join(outputDir, `${outputName}.xd`) |
| 137 | + fs.writeFileSync(outputPath, xd) |
| 138 | + console.log(`${input} -> ${outputPath}`) |
| 139 | + converted++ |
| 140 | + } catch (err: any) { |
| 141 | + console.error(`Error converting ${input}: ${err.message}`) |
| 142 | + failed++ |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + console.log(`\nDone: ${converted} converted, ${failed} failed`) |
| 147 | + if (failed > 0) process.exit(1) |
| 148 | +} |
| 149 | + |
| 150 | +main() |
0 commit comments