diff --git a/ts/common/cli.ts b/ts/common/cli.ts index dd12cc009..a425decdd 100644 --- a/ts/common/cli.ts +++ b/ts/common/cli.ts @@ -314,6 +314,9 @@ export class Cli { .option('-T, --speechStructure', 'Return speech structure only.', () => processor('speechStructure') ) + .option('-W, --workerStructure', 'Return worker speech structure only.', () => + processor('workerSpeechStructure') + ) .option( '-t, --latex', 'Accepts LaTeX input for certain locale/modality combinations.', diff --git a/ts/common/engine.ts b/ts/common/engine.ts index 19ff09376..43cbd415a 100644 --- a/ts/common/engine.ts +++ b/ts/common/engine.ts @@ -57,6 +57,7 @@ export class SREError extends Error { * */ export class Engine { + public options: Options = new Options(); /** diff --git a/ts/common/processor_factory.ts b/ts/common/processor_factory.ts index 66505b414..dd5c0a573 100644 --- a/ts/common/processor_factory.ts +++ b/ts/common/processor_factory.ts @@ -426,5 +426,67 @@ set( }) ); + +export type OptionsList = { [key: string]: string }; +type SpeechList = { [id: string]: { [mod: string]: string } }; + +export type WorkerStructure = { + speech?: SpeechList; + braille?: SpeechList; + mactions?: SpeechList; + options?: OptionsList; + translations?: OptionsList; + label?: string; + postfix?: string; + braillelabel?: string; + ssml?: string; +}; + +// The new speech structure for the webworker integration. +// TODO: Cleanup and remove duplication with system.ts +set( + new Processor('workerSpeechStructure', { + processor: function (expr) { + const mml = DomUtil.parseInput(expr); + let sxml; + try { + const rebuilt = new RebuildStree(mml); + sxml = rebuilt.stree.xml(); + } catch(_e) { + sxml = Semantic.xmlTree(mml, Engine.getInstance().options); + } + Engine.getInstance().options.automark = true; + const json: WorkerStructure = {}; + assembleSpeechStructure(json, mml, sxml); + return json; + }, + print: function (descrs) { + return JSON.stringify(descrs); + }, + pprint: function (descrs) { + return JSON.stringify(descrs, null, 2); + } + }) +); + + +export function assembleSpeechStructure( + json: WorkerStructure, + mml: Element, + sxml: Element, + options: OptionsList = {} +) { + json.options = options; + json.mactions = SpeechGeneratorUtil.connectMactionSelections(mml, sxml); + json.speech = SpeechGeneratorUtil.computeSpeechStructure(sxml); + const root = (sxml.childNodes[0] as Element)?.getAttribute('id'); + json.label = json.speech[root]['speech-none'] + ''; + json.ssml = json.speech[root]['speech-ssml']; + json.translations = Object.assign({}, LOCALE.MESSAGES.navigate); + const links = DomUtil.querySelectorAllByAttr(sxml, 'href').length; + if (links) { + json.label += `, ${links} ${(links === 1) ? 'link' : 'links'}`; + } +} // ./bin/sre -T -P -k ssml -d clearspeak < ../sre-resources/samples/quadratic-line.xml // echo "ab" | ./bin/sre -T -P -d clearspeak diff --git a/ts/common/system.ts b/ts/common/system.ts index 1faea7f0c..d772a4f79 100644 --- a/ts/common/system.ts +++ b/ts/common/system.ts @@ -26,6 +26,7 @@ import * as EngineConst from './engine_const.js'; import { KeyCode } from './event_util.js'; import * as FileUtil from './file_util.js'; import * as ProcessorFactory from './processor_factory.js'; +import { OptionsList, WorkerStructure } from './processor_factory.js'; import { SystemExternal } from './system_external.js'; import { Variables } from './variables.js'; import { standardLoader } from '../speech_rules/math_map.js'; @@ -412,21 +413,6 @@ export function toSpeechStructure(expr: string): string { /** * Web worker related API methods. */ -import { LOCALE } from '../l10n/locale.js'; - -type OptionsList = { [key: string]: string }; -type SpeechList = { [id: string]: { [mod: string]: string } }; - -type WorkerStructure = { - speech?: SpeechList; - braille?: SpeechList; - mactions?: SpeechList; - options?: OptionsList; - translations?: OptionsList; - label?: string; - braillelabel?: string; - ssml?: string; -}; /** * Compute speech structure for the expression. @@ -537,13 +523,7 @@ async function assembleWorkerStructure( await setupEngine(options); Engine.getInstance().options.automark = true; const json: WorkerStructure = {}; - json.options = options; - json.mactions = SpeechGeneratorUtil.connectMactionSelections(mml, sxml); - json.speech = SpeechGeneratorUtil.computeSpeechStructure(sxml); - const root = (sxml.childNodes[0] as Element)?.getAttribute('id'); - json.label = json.speech[root]['speech-none']; - json.ssml = json.speech[root]['speech-ssml']; - json.translations = Object.assign({}, LOCALE.MESSAGES.navigate); + ProcessorFactory.assembleSpeechStructure(json, mml, sxml, options); if (options.braille === 'none') { return json; } @@ -553,6 +533,7 @@ async function assembleWorkerStructure( domain: 'default', style: 'default' }); + const root = (sxml.childNodes[0] as Element)?.getAttribute('id'); json.braille = SpeechGeneratorUtil.computeBrailleStructure(sxml); json.braillelabel = json.braille[root]['braille-none']; return json;