UNPKG

jmdict-streaming-parser

Version:

Streaming parser for JMdict and related files.

124 lines (123 loc) 4.35 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __markAsModule = (target) => __defProp(target, "__esModule", {value: true}); var __export = (target, all) => { for (var name in all) __defProp(target, name, {get: all[name], enumerable: true}); }; var __exportStar = (target, module2, desc) => { if (module2 && typeof module2 === "object" || typeof module2 === "function") { for (let key of __getOwnPropNames(module2)) if (!__hasOwnProp.call(target, key) && key !== "default") __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable}); } return target; }; var __toModule = (module2) => { if (module2 && module2.__esModule) return module2; return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", {value: module2, enumerable: true})), module2); }; __markAsModule(exports); __export(exports, { JmdictTransform: () => JmdictTransform }); var import_stream = __toModule(require("stream")); var import_sax = __toModule(require("sax")); var import_parse_grouped_entities = __toModule(require("./parse-grouped-entities")); class JmdictTransform extends import_stream.Duplex { constructor(opts = {}) { super({...opts, readableObjectMode: true}); this.entities = new Map(); this.initialChunk = ""; this.hasPassedOpeningTag = false; this.inputStack = []; this.outputStack = [{}]; this.parser = import_sax.createStream(true); this.parser.on("error", (err) => this.emit("error", err)); this.parser.on("doctype", (doctype) => { const lines = doctype.split("\n"); for (let line of lines) { line = line.trim(); const matchResult = line.match(/^<!ENTITY\s+([^\s]+)\s+"([^"]+)">$/); if (matchResult) { const [, name, value] = matchResult; this.parser._parser.ENTITIES[name] = name; this.entities.set(name, value); this.push({type: "entity", data: {name, value}}); } } }); this.parser.on("opentag", ({name, attributes}) => { this.inputStack.push({type: "start", name, attrs: attributes}); const lastEl = last(this.outputStack); if (!lastEl[name]) { lastEl[name] = []; } this.outputStack.push({}); }); this.parser.on("closetag", (name) => { let xmlNode = this.inputStack.pop(); if (this.inputStack.length === 0) { this.push(null); return; } const node = this.outputStack.pop(); Object.assign(node, xmlNode.attrs); if (Object.keys(node).length === 1 && "$text" in node) { ; last(this.outputStack)[name].push(node.$text); } else { if (this.inputStack.length === 1) { this.push({type: "node", data: node}); } else { ; last(this.outputStack)[name].push(node); } } }); this.parser.on("text", (text) => { if (text === "\n") { return; } last(this.outputStack).$text = text; }); this.parser.on("comment", (comment) => { comment = comment.trim(); this.push({type: "comment", data: comment}); const mdateHint = "JMdict created: "; if (comment.startsWith(mdateHint)) { const mdate = comment.slice(mdateHint.length); if (mdate) { this.push({type: "mdate", data: mdate}); this.push({type: "entities", data: this.entities}); } } }); } _write(chunk, encoding, callback) { this.parser.write(chunk); if (!this.hasPassedOpeningTag) { this.initialChunk += chunk; if (this.initialChunk.includes("\n<JMdict>\n")) { this.hasPassedOpeningTag = true; this.groupedEntities = import_parse_grouped_entities.parseGroupedEntities(this.initialChunk); this.push({type: "groupedEntities", data: this.groupedEntities}); } } callback(); } _read() { } [Symbol.asyncIterator]() { return super[Symbol.asyncIterator](); } } function last(arr) { return arr[arr.length - 1]; } //# sourceMappingURL=index.js.map