From fafa746a632b1907d9cca262f689d7bec4e0f940 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 17 Jul 2021 17:10:25 -0400 Subject: Sandbox script folders (#1837) * Move scripts * Update paths * Fix ordering * Simplify eslint rules --- ext/background.html | 4 +- ext/js/data/anki-note-data-creator.js | 612 ------------------ ext/js/data/sandbox/anki-note-data-creator.js | 612 ++++++++++++++++++ ext/js/display/pronunciation-generator.js | 199 ------ ext/js/display/sandbox/pronunciation-generator.js | 199 ++++++ .../sandbox/structured-content-generator.js | 230 +++++++ ext/js/display/structured-content-generator.js | 230 ------- ext/js/dom/css-style-applier.js | 118 ---- ext/js/dom/sandbox/css-style-applier.js | 118 ++++ ext/js/language/dictionary-data-util.js | 299 --------- ext/js/language/japanese-util.js | 716 --------------------- ext/js/language/sandbox/dictionary-data-util.js | 299 +++++++++ ext/js/language/sandbox/japanese-util.js | 716 +++++++++++++++++++++ .../sandbox/template-renderer-frame-api.js | 109 ++++ .../sandbox/template-renderer-frame-main.js | 38 ++ .../sandbox/template-renderer-media-provider.js | 129 ++++ ext/js/templates/sandbox/template-renderer.js | 620 ++++++++++++++++++ ext/js/templates/template-renderer-frame-api.js | 109 ---- ext/js/templates/template-renderer-frame-main.js | 38 -- .../templates/template-renderer-media-provider.js | 129 ---- ext/js/templates/template-renderer.js | 620 ------------------ ext/popup.html | 8 +- ext/search.html | 8 +- ext/settings.html | 4 +- ext/sw.js | 4 +- ext/template-renderer.html | 18 +- 26 files changed, 3093 insertions(+), 3093 deletions(-) delete mode 100644 ext/js/data/anki-note-data-creator.js create mode 100644 ext/js/data/sandbox/anki-note-data-creator.js delete mode 100644 ext/js/display/pronunciation-generator.js create mode 100644 ext/js/display/sandbox/pronunciation-generator.js create mode 100644 ext/js/display/sandbox/structured-content-generator.js delete mode 100644 ext/js/display/structured-content-generator.js delete mode 100644 ext/js/dom/css-style-applier.js create mode 100644 ext/js/dom/sandbox/css-style-applier.js delete mode 100644 ext/js/language/dictionary-data-util.js delete mode 100644 ext/js/language/japanese-util.js create mode 100644 ext/js/language/sandbox/dictionary-data-util.js create mode 100644 ext/js/language/sandbox/japanese-util.js create mode 100644 ext/js/templates/sandbox/template-renderer-frame-api.js create mode 100644 ext/js/templates/sandbox/template-renderer-frame-main.js create mode 100644 ext/js/templates/sandbox/template-renderer-media-provider.js create mode 100644 ext/js/templates/sandbox/template-renderer.js delete mode 100644 ext/js/templates/template-renderer-frame-api.js delete mode 100644 ext/js/templates/template-renderer-frame-main.js delete mode 100644 ext/js/templates/template-renderer-media-provider.js delete mode 100644 ext/js/templates/template-renderer.js (limited to 'ext') diff --git a/ext/background.html b/ext/background.html index 15cd0b8c..57caa671 100644 --- a/ext/background.html +++ b/ext/background.html @@ -42,9 +42,9 @@ - - + + diff --git a/ext/js/data/anki-note-data-creator.js b/ext/js/data/anki-note-data-creator.js deleted file mode 100644 index 3622e837..00000000 --- a/ext/js/data/anki-note-data-creator.js +++ /dev/null @@ -1,612 +0,0 @@ -/* - * Copyright (C) 2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* global - * DictionaryDataUtil - */ - -/** - * This class is used to convert the internal dictionary entry format to the - * format used by Anki, for backwards compatibility. - */ -class AnkiNoteDataCreator { - /** - * Creates a new instance. - * @param japaneseUtil An instance of `JapaneseUtil`. - */ - constructor(japaneseUtil) { - this._japaneseUtil = japaneseUtil; - } - - /** - * Creates a compatibility representation of the specified data. - * @param marker The marker that is being used for template rendering. - * @returns An object used for rendering Anki templates. - */ - create(marker, { - dictionaryEntry, - resultOutputMode, - mode, - glossaryLayoutMode, - compactTags, - context, - media - }) { - const self = this; - const definition = this.createCachedValue(this._getDefinition.bind(this, dictionaryEntry, context, resultOutputMode)); - const uniqueExpressions = this.createCachedValue(this._getUniqueExpressions.bind(this, dictionaryEntry)); - const uniqueReadings = this.createCachedValue(this._getUniqueReadings.bind(this, dictionaryEntry)); - const context2 = this.createCachedValue(this._getPublicContext.bind(this, context)); - const pitches = this.createCachedValue(this._getPitches.bind(this, dictionaryEntry)); - const pitchCount = this.createCachedValue(this._getPitchCount.bind(this, pitches)); - if (typeof media !== 'object' || media === null || Array.isArray(media)) { media = {}; } - const result = { - marker, - get definition() { return self.getCachedValue(definition); }, - glossaryLayoutMode, - compactTags, - group: (resultOutputMode === 'group'), - merge: (resultOutputMode === 'merge'), - modeTermKanji: (mode === 'term-kanji'), - modeTermKana: (mode === 'term-kana'), - modeKanji: (mode === 'kanji'), - compactGlossaries: (glossaryLayoutMode === 'compact'), - get uniqueExpressions() { return self.getCachedValue(uniqueExpressions); }, - get uniqueReadings() { return self.getCachedValue(uniqueReadings); }, - get pitches() { return self.getCachedValue(pitches); }, - get pitchCount() { return self.getCachedValue(pitchCount); }, - get context() { return self.getCachedValue(context2); }, - media - }; - Object.defineProperty(result, 'dictionaryEntry', { - configurable: false, - enumerable: false, - writable: false, - value: dictionaryEntry - }); - return result; - } - - /** - * Creates a deferred-evaluation value. - * @param getter The function to invoke to get the return value. - * @returns An object which can be passed into `getCachedValue`. - */ - createCachedValue(getter) { - return {getter, hasValue: false, value: void 0}; - } - - /** - * Gets the value of a cached object. - * @param item An object that was returned from `createCachedValue`. - * @returns The result of evaluating the getter, which is cached after the first invocation. - */ - getCachedValue(item) { - if (item.hasValue) { return item.value; } - const value = item.getter(); - item.value = value; - item.hasValue = true; - return value; - } - - // Private - - _asObject(value) { - return (typeof value === 'object' && value !== null ? value : {}); - } - - _getPrimarySource(dictionaryEntry) { - for (const headword of dictionaryEntry.headwords) { - for (const source of headword.sources) { - if (source.isPrimary) { return source; } - } - } - return null; - } - - _getUniqueExpressions(dictionaryEntry) { - if (dictionaryEntry.type === 'term') { - const results = new Set(); - for (const {term} of dictionaryEntry.headwords) { - results.add(term); - } - return [...results]; - } else { - return []; - } - } - - _getUniqueReadings(dictionaryEntry) { - if (dictionaryEntry.type === 'term') { - const results = new Set(); - for (const {reading} of dictionaryEntry.headwords) { - results.add(reading); - } - return [...results]; - } else { - return []; - } - } - - _getPublicContext(context) { - let {documentTitle, query, fullQuery} = this._asObject(context); - if (typeof documentTitle !== 'string') { documentTitle = ''; } - return { - query, - fullQuery, - document: { - title: documentTitle - } - }; - } - - _getPitches(dictionaryEntry) { - const results = []; - if (dictionaryEntry.type === 'term') { - for (const {dictionary, pitches} of DictionaryDataUtil.getPitchAccentInfos(dictionaryEntry)) { - const pitches2 = []; - for (const {terms, reading, position, tags, exclusiveTerms, exclusiveReadings} of pitches) { - pitches2.push({ - expressions: terms, - reading, - position, - tags, - exclusiveExpressions: exclusiveTerms, - exclusiveReadings - }); - } - results.push({dictionary, pitches: pitches2}); - } - } - return results; - } - - _getPitchCount(cachedPitches) { - const pitches = this.getCachedValue(cachedPitches); - return pitches.reduce((i, v) => i + v.pitches.length, 0); - } - - _getDefinition(dictionaryEntry, context, resultOutputMode) { - switch (dictionaryEntry.type) { - case 'term': - return this._getTermDefinition(dictionaryEntry, context, resultOutputMode); - case 'kanji': - return this._getKanjiDefinition(dictionaryEntry, context); - default: - return {}; - } - } - - _getKanjiDefinition(dictionaryEntry, context) { - const self = this; - - const {character, dictionary, onyomi, kunyomi, definitions} = dictionaryEntry; - - let {url} = this._asObject(context); - if (typeof url !== 'string') { url = ''; } - - const stats = this.createCachedValue(this._getKanjiStats.bind(this, dictionaryEntry)); - const tags = this.createCachedValue(this._convertTags.bind(this, dictionaryEntry.tags)); - const frequencies = this.createCachedValue(this._getKanjiFrequencies.bind(this, dictionaryEntry)); - const cloze = this.createCachedValue(this._getCloze.bind(this, dictionaryEntry, context)); - - return { - type: 'kanji', - character, - dictionary, - onyomi, - kunyomi, - glossary: definitions, - get tags() { return self.getCachedValue(tags); }, - get stats() { return self.getCachedValue(stats); }, - get frequencies() { return self.getCachedValue(frequencies); }, - url, - get cloze() { return self.getCachedValue(cloze); } - }; - } - - _getKanjiStats(dictionaryEntry) { - const results = {}; - for (const [key, value] of Object.entries(dictionaryEntry.stats)) { - results[key] = value.map(this._convertKanjiStat.bind(this)); - } - return results; - } - - _convertKanjiStat({name, category, content, order, score, dictionary, value}) { - return { - name, - category, - notes: content, - order, - score, - dictionary, - value - }; - } - - _getKanjiFrequencies(dictionaryEntry) { - const results = []; - for (const {index, dictionary, dictionaryIndex, dictionaryPriority, character, frequency} of dictionaryEntry.frequencies) { - results.push({ - index, - dictionary, - dictionaryOrder: { - index: dictionaryIndex, - priority: dictionaryPriority - }, - character, - frequency - }); - } - return results; - } - - _getTermDefinition(dictionaryEntry, context, resultOutputMode) { - const self = this; - - let type = 'term'; - switch (resultOutputMode) { - case 'group': type = 'termGrouped'; break; - case 'merge': type = 'termMerged'; break; - } - - const {inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, definitions} = dictionaryEntry; - - let {url} = this._asObject(context); - if (typeof url !== 'string') { url = ''; } - - const primarySource = this._getPrimarySource(dictionaryEntry); - - const dictionaryNames = this.createCachedValue(this._getTermDictionaryNames.bind(this, dictionaryEntry)); - const commonInfo = this.createCachedValue(this._getTermDictionaryEntryCommonInfo.bind(this, dictionaryEntry, type)); - const termTags = this.createCachedValue(this._getTermTags.bind(this, dictionaryEntry, type)); - const expressions = this.createCachedValue(this._getTermExpressions.bind(this, dictionaryEntry)); - const frequencies = this.createCachedValue(this._getTermFrequencies.bind(this, dictionaryEntry)); - const pitches = this.createCachedValue(this._getTermPitches.bind(this, dictionaryEntry)); - const glossary = this.createCachedValue(this._getTermGlossaryArray.bind(this, dictionaryEntry, type)); - const cloze = this.createCachedValue(this._getCloze.bind(this, dictionaryEntry, context)); - const furiganaSegments = this.createCachedValue(this._getTermFuriganaSegments.bind(this, dictionaryEntry, type)); - const sequence = this.createCachedValue(this._getTermDictionaryEntrySequence.bind(this, dictionaryEntry)); - - return { - type, - id: (type === 'term' && definitions.length > 0 ? definitions[0].id : void 0), - source: (primarySource !== null ? primarySource.transformedText : null), - rawSource: (primarySource !== null ? primarySource.originalText : null), - sourceTerm: (type !== 'termMerged' ? (primarySource !== null ? primarySource.deinflectedText : null) : void 0), - reasons: inflections, - score, - isPrimary: (type === 'term' ? dictionaryEntry.isPrimary : void 0), - get sequence() { return self.getCachedValue(sequence); }, - get dictionary() { return self.getCachedValue(dictionaryNames)[0]; }, - dictionaryOrder: { - index: dictionaryIndex, - priority: dictionaryPriority - }, - get dictionaryNames() { return self.getCachedValue(dictionaryNames); }, - get expression() { - const {uniqueTerms} = self.getCachedValue(commonInfo); - return (type === 'term' || type === 'termGrouped' ? uniqueTerms[0] : uniqueTerms); - }, - get reading() { - const {uniqueReadings} = self.getCachedValue(commonInfo); - return (type === 'term' || type === 'termGrouped' ? uniqueReadings[0] : uniqueReadings); - }, - get expressions() { return self.getCachedValue(expressions); }, - get glossary() { return self.getCachedValue(glossary); }, - get definitionTags() { return type === 'term' ? self.getCachedValue(commonInfo).definitionTags : void 0; }, - get termTags() { return self.getCachedValue(termTags); }, - get definitions() { return self.getCachedValue(commonInfo).definitions; }, - get frequencies() { return self.getCachedValue(frequencies); }, - get pitches() { return self.getCachedValue(pitches); }, - sourceTermExactMatchCount, - url, - get cloze() { return self.getCachedValue(cloze); }, - get furiganaSegments() { return self.getCachedValue(furiganaSegments); } - }; - } - - _getTermDictionaryNames(dictionaryEntry) { - const dictionaryNames = new Set(); - for (const {dictionary} of dictionaryEntry.definitions) { - dictionaryNames.add(dictionary); - } - return [...dictionaryNames]; - } - - _getTermDictionaryEntryCommonInfo(dictionaryEntry, type) { - const merged = (type === 'termMerged'); - const hasDefinitions = (type !== 'term'); - - const allTermsSet = new Set(); - const allReadingsSet = new Set(); - for (const {term, reading} of dictionaryEntry.headwords) { - allTermsSet.add(term); - allReadingsSet.add(reading); - } - const uniqueTerms = [...allTermsSet]; - const uniqueReadings = [...allReadingsSet]; - - const definitions = []; - const definitionTags = []; - for (const {tags, headwordIndices, entries, dictionary, sequences} of dictionaryEntry.definitions) { - const definitionTags2 = []; - for (const tag of tags) { - definitionTags.push(this._convertTag(tag)); - definitionTags2.push(this._convertTag(tag)); - } - if (!hasDefinitions) { continue; } - const only = merged ? DictionaryDataUtil.getDisambiguations(dictionaryEntry.headwords, headwordIndices, allTermsSet, allReadingsSet) : void 0; - definitions.push({ - sequence: sequences[0], - dictionary, - glossary: entries, - definitionTags: definitionTags2, - only - }); - } - - return { - uniqueTerms, - uniqueReadings, - definitionTags, - definitions: hasDefinitions ? definitions : void 0 - }; - } - - _getTermFrequencies(dictionaryEntry) { - const results = []; - const {headwords} = dictionaryEntry; - for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency} of dictionaryEntry.frequencies) { - const {term, reading} = headwords[headwordIndex]; - results.push({ - index: results.length, - expressionIndex: headwordIndex, - dictionary, - dictionaryOrder: { - index: dictionaryIndex, - priority: dictionaryPriority - }, - expression: term, - reading, - hasReading, - frequency - }); - } - return results; - } - - _getTermPitches(dictionaryEntry) { - const self = this; - const results = []; - const {headwords} = dictionaryEntry; - for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches} of dictionaryEntry.pronunciations) { - const {term, reading} = headwords[headwordIndex]; - const cachedPitches = this.createCachedValue(this._getTermPitchesInner.bind(this, pitches)); - results.push({ - index: results.length, - expressionIndex: headwordIndex, - dictionary, - dictionaryOrder: { - index: dictionaryIndex, - priority: dictionaryPriority - }, - expression: term, - reading, - get pitches() { return self.getCachedValue(cachedPitches); } - }); - } - return results; - } - - _getTermPitchesInner(pitches) { - const self = this; - const results = []; - for (const {position, tags} of pitches) { - const cachedTags = this.createCachedValue(this._convertTags.bind(this, tags)); - results.push({ - position, - get tags() { return self.getCachedValue(cachedTags); } - }); - } - return results; - } - - _getTermExpressions(dictionaryEntry) { - const self = this; - const results = []; - const {headwords} = dictionaryEntry; - for (let i = 0, ii = headwords.length; i < ii; ++i) { - const {term, reading, tags, sources: [{deinflectedText}], wordClasses} = headwords[i]; - const termTags = this.createCachedValue(this._convertTags.bind(this, tags)); - const frequencies = this.createCachedValue(this._getTermExpressionFrequencies.bind(this, dictionaryEntry, i)); - const pitches = this.createCachedValue(this._getTermExpressionPitches.bind(this, dictionaryEntry, i)); - const termFrequency = this.createCachedValue(this._getTermExpressionTermFrequency.bind(this, termTags)); - const furiganaSegments = this.createCachedValue(this._getTermHeadwordFuriganaSegments.bind(this, term, reading)); - const item = { - sourceTerm: deinflectedText, - expression: term, - reading, - get termTags() { return self.getCachedValue(termTags); }, - get frequencies() { return self.getCachedValue(frequencies); }, - get pitches() { return self.getCachedValue(pitches); }, - get furiganaSegments() { return self.getCachedValue(furiganaSegments); }, - get termFrequency() { return self.getCachedValue(termFrequency); }, - wordClasses - }; - results.push(item); - } - return results; - } - - _getTermExpressionFrequencies(dictionaryEntry, i) { - const results = []; - const {headwords, frequencies} = dictionaryEntry; - for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency} of frequencies) { - if (headwordIndex !== i) { continue; } - const {term, reading} = headwords[headwordIndex]; - results.push({ - index: results.length, - expressionIndex: headwordIndex, - dictionary, - dictionaryOrder: { - index: dictionaryIndex, - priority: dictionaryPriority - }, - expression: term, - reading, - hasReading, - frequency - }); - } - return results; - } - - _getTermExpressionPitches(dictionaryEntry, i) { - const self = this; - const results = []; - const {headwords, pronunciations} = dictionaryEntry; - for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches} of pronunciations) { - if (headwordIndex !== i) { continue; } - const {term, reading} = headwords[headwordIndex]; - const cachedPitches = this.createCachedValue(this._getTermPitchesInner.bind(this, pitches)); - results.push({ - index: results.length, - expressionIndex: headwordIndex, - dictionary, - dictionaryOrder: { - index: dictionaryIndex, - priority: dictionaryPriority - }, - expression: term, - reading, - get pitches() { return self.getCachedValue(cachedPitches); } - }); - } - return results; - } - - _getTermExpressionTermFrequency(cachedTermTags) { - const termTags = this.getCachedValue(cachedTermTags); - return DictionaryDataUtil.getTermFrequency(termTags); - } - - _getTermGlossaryArray(dictionaryEntry, type) { - if (type === 'term') { - const results = []; - for (const {entries} of dictionaryEntry.definitions) { - results.push(...entries); - } - return results; - } - return void 0; - } - - _getTermTags(dictionaryEntry, type) { - if (type !== 'termMerged') { - const results = []; - for (const {tag} of DictionaryDataUtil.groupTermTags(dictionaryEntry)) { - results.push(this._convertTag(tag)); - } - return results; - } - return void 0; - } - - _convertTags(tags) { - const results = []; - for (const tag of tags) { - results.push(this._convertTag(tag)); - } - return results; - } - - _convertTag({name, category, content, order, score, dictionaries, redundant}) { - return { - name, - category, - notes: (content.length > 0 ? content[0] : ''), - order, - score, - dictionary: (dictionaries.length > 0 ? dictionaries[0] : ''), - redundant - }; - } - - _getCloze(dictionaryEntry, context) { - let originalText = ''; - switch (dictionaryEntry.type) { - case 'term': - { - const primarySource = this._getPrimarySource(dictionaryEntry); - if (primarySource !== null) { originalText = primarySource.originalText; } - } - break; - case 'kanji': - originalText = dictionaryEntry.character; - break; - } - - const {sentence} = this._asObject(context); - let {text, offset} = this._asObject(sentence); - if (typeof text !== 'string') { text = ''; } - if (typeof offset !== 'number') { offset = 0; } - - return { - sentence: text, - prefix: text.substring(0, offset), - body: text.substring(offset, offset + originalText.length), - suffix: text.substring(offset + originalText.length) - }; - } - - _getTermFuriganaSegments(dictionaryEntry, type) { - if (type === 'term') { - for (const {term, reading} of dictionaryEntry.headwords) { - return this._getTermHeadwordFuriganaSegments(term, reading); - } - } - return void 0; - } - - _getTermHeadwordFuriganaSegments(term, reading) { - const result = []; - for (const {text, reading: reading2} of this._japaneseUtil.distributeFurigana(term, reading)) { - result.push({text, furigana: reading2}); - } - return result; - } - - _getTermDictionaryEntrySequence(dictionaryEntry) { - let hasSequence = false; - let mainSequence = -1; - for (const {sequences, isPrimary} of dictionaryEntry.definitions) { - if (!isPrimary) { continue; } - const sequence = sequences[0]; - if (!hasSequence) { - mainSequence = sequence; - hasSequence = true; - if (mainSequence === -1) { break; } - } else if (mainSequence !== sequence) { - mainSequence = -1; - break; - } - } - return mainSequence; - } -} diff --git a/ext/js/data/sandbox/anki-note-data-creator.js b/ext/js/data/sandbox/anki-note-data-creator.js new file mode 100644 index 00000000..3622e837 --- /dev/null +++ b/ext/js/data/sandbox/anki-note-data-creator.js @@ -0,0 +1,612 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* global + * DictionaryDataUtil + */ + +/** + * This class is used to convert the internal dictionary entry format to the + * format used by Anki, for backwards compatibility. + */ +class AnkiNoteDataCreator { + /** + * Creates a new instance. + * @param japaneseUtil An instance of `JapaneseUtil`. + */ + constructor(japaneseUtil) { + this._japaneseUtil = japaneseUtil; + } + + /** + * Creates a compatibility representation of the specified data. + * @param marker The marker that is being used for template rendering. + * @returns An object used for rendering Anki templates. + */ + create(marker, { + dictionaryEntry, + resultOutputMode, + mode, + glossaryLayoutMode, + compactTags, + context, + media + }) { + const self = this; + const definition = this.createCachedValue(this._getDefinition.bind(this, dictionaryEntry, context, resultOutputMode)); + const uniqueExpressions = this.createCachedValue(this._getUniqueExpressions.bind(this, dictionaryEntry)); + const uniqueReadings = this.createCachedValue(this._getUniqueReadings.bind(this, dictionaryEntry)); + const context2 = this.createCachedValue(this._getPublicContext.bind(this, context)); + const pitches = this.createCachedValue(this._getPitches.bind(this, dictionaryEntry)); + const pitchCount = this.createCachedValue(this._getPitchCount.bind(this, pitches)); + if (typeof media !== 'object' || media === null || Array.isArray(media)) { media = {}; } + const result = { + marker, + get definition() { return self.getCachedValue(definition); }, + glossaryLayoutMode, + compactTags, + group: (resultOutputMode === 'group'), + merge: (resultOutputMode === 'merge'), + modeTermKanji: (mode === 'term-kanji'), + modeTermKana: (mode === 'term-kana'), + modeKanji: (mode === 'kanji'), + compactGlossaries: (glossaryLayoutMode === 'compact'), + get uniqueExpressions() { return self.getCachedValue(uniqueExpressions); }, + get uniqueReadings() { return self.getCachedValue(uniqueReadings); }, + get pitches() { return self.getCachedValue(pitches); }, + get pitchCount() { return self.getCachedValue(pitchCount); }, + get context() { return self.getCachedValue(context2); }, + media + }; + Object.defineProperty(result, 'dictionaryEntry', { + configurable: false, + enumerable: false, + writable: false, + value: dictionaryEntry + }); + return result; + } + + /** + * Creates a deferred-evaluation value. + * @param getter The function to invoke to get the return value. + * @returns An object which can be passed into `getCachedValue`. + */ + createCachedValue(getter) { + return {getter, hasValue: false, value: void 0}; + } + + /** + * Gets the value of a cached object. + * @param item An object that was returned from `createCachedValue`. + * @returns The result of evaluating the getter, which is cached after the first invocation. + */ + getCachedValue(item) { + if (item.hasValue) { return item.value; } + const value = item.getter(); + item.value = value; + item.hasValue = true; + return value; + } + + // Private + + _asObject(value) { + return (typeof value === 'object' && value !== null ? value : {}); + } + + _getPrimarySource(dictionaryEntry) { + for (const headword of dictionaryEntry.headwords) { + for (const source of headword.sources) { + if (source.isPrimary) { return source; } + } + } + return null; + } + + _getUniqueExpressions(dictionaryEntry) { + if (dictionaryEntry.type === 'term') { + const results = new Set(); + for (const {term} of dictionaryEntry.headwords) { + results.add(term); + } + return [...results]; + } else { + return []; + } + } + + _getUniqueReadings(dictionaryEntry) { + if (dictionaryEntry.type === 'term') { + const results = new Set(); + for (const {reading} of dictionaryEntry.headwords) { + results.add(reading); + } + return [...results]; + } else { + return []; + } + } + + _getPublicContext(context) { + let {documentTitle, query, fullQuery} = this._asObject(context); + if (typeof documentTitle !== 'string') { documentTitle = ''; } + return { + query, + fullQuery, + document: { + title: documentTitle + } + }; + } + + _getPitches(dictionaryEntry) { + const results = []; + if (dictionaryEntry.type === 'term') { + for (const {dictionary, pitches} of DictionaryDataUtil.getPitchAccentInfos(dictionaryEntry)) { + const pitches2 = []; + for (const {terms, reading, position, tags, exclusiveTerms, exclusiveReadings} of pitches) { + pitches2.push({ + expressions: terms, + reading, + position, + tags, + exclusiveExpressions: exclusiveTerms, + exclusiveReadings + }); + } + results.push({dictionary, pitches: pitches2}); + } + } + return results; + } + + _getPitchCount(cachedPitches) { + const pitches = this.getCachedValue(cachedPitches); + return pitches.reduce((i, v) => i + v.pitches.length, 0); + } + + _getDefinition(dictionaryEntry, context, resultOutputMode) { + switch (dictionaryEntry.type) { + case 'term': + return this._getTermDefinition(dictionaryEntry, context, resultOutputMode); + case 'kanji': + return this._getKanjiDefinition(dictionaryEntry, context); + default: + return {}; + } + } + + _getKanjiDefinition(dictionaryEntry, context) { + const self = this; + + const {character, dictionary, onyomi, kunyomi, definitions} = dictionaryEntry; + + let {url} = this._asObject(context); + if (typeof url !== 'string') { url = ''; } + + const stats = this.createCachedValue(this._getKanjiStats.bind(this, dictionaryEntry)); + const tags = this.createCachedValue(this._convertTags.bind(this, dictionaryEntry.tags)); + const frequencies = this.createCachedValue(this._getKanjiFrequencies.bind(this, dictionaryEntry)); + const cloze = this.createCachedValue(this._getCloze.bind(this, dictionaryEntry, context)); + + return { + type: 'kanji', + character, + dictionary, + onyomi, + kunyomi, + glossary: definitions, + get tags() { return self.getCachedValue(tags); }, + get stats() { return self.getCachedValue(stats); }, + get frequencies() { return self.getCachedValue(frequencies); }, + url, + get cloze() { return self.getCachedValue(cloze); } + }; + } + + _getKanjiStats(dictionaryEntry) { + const results = {}; + for (const [key, value] of Object.entries(dictionaryEntry.stats)) { + results[key] = value.map(this._convertKanjiStat.bind(this)); + } + return results; + } + + _convertKanjiStat({name, category, content, order, score, dictionary, value}) { + return { + name, + category, + notes: content, + order, + score, + dictionary, + value + }; + } + + _getKanjiFrequencies(dictionaryEntry) { + const results = []; + for (const {index, dictionary, dictionaryIndex, dictionaryPriority, character, frequency} of dictionaryEntry.frequencies) { + results.push({ + index, + dictionary, + dictionaryOrder: { + index: dictionaryIndex, + priority: dictionaryPriority + }, + character, + frequency + }); + } + return results; + } + + _getTermDefinition(dictionaryEntry, context, resultOutputMode) { + const self = this; + + let type = 'term'; + switch (resultOutputMode) { + case 'group': type = 'termGrouped'; break; + case 'merge': type = 'termMerged'; break; + } + + const {inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, definitions} = dictionaryEntry; + + let {url} = this._asObject(context); + if (typeof url !== 'string') { url = ''; } + + const primarySource = this._getPrimarySource(dictionaryEntry); + + const dictionaryNames = this.createCachedValue(this._getTermDictionaryNames.bind(this, dictionaryEntry)); + const commonInfo = this.createCachedValue(this._getTermDictionaryEntryCommonInfo.bind(this, dictionaryEntry, type)); + const termTags = this.createCachedValue(this._getTermTags.bind(this, dictionaryEntry, type)); + const expressions = this.createCachedValue(this._getTermExpressions.bind(this, dictionaryEntry)); + const frequencies = this.createCachedValue(this._getTermFrequencies.bind(this, dictionaryEntry)); + const pitches = this.createCachedValue(this._getTermPitches.bind(this, dictionaryEntry)); + const glossary = this.createCachedValue(this._getTermGlossaryArray.bind(this, dictionaryEntry, type)); + const cloze = this.createCachedValue(this._getCloze.bind(this, dictionaryEntry, context)); + const furiganaSegments = this.createCachedValue(this._getTermFuriganaSegments.bind(this, dictionaryEntry, type)); + const sequence = this.createCachedValue(this._getTermDictionaryEntrySequence.bind(this, dictionaryEntry)); + + return { + type, + id: (type === 'term' && definitions.length > 0 ? definitions[0].id : void 0), + source: (primarySource !== null ? primarySource.transformedText : null), + rawSource: (primarySource !== null ? primarySource.originalText : null), + sourceTerm: (type !== 'termMerged' ? (primarySource !== null ? primarySource.deinflectedText : null) : void 0), + reasons: inflections, + score, + isPrimary: (type === 'term' ? dictionaryEntry.isPrimary : void 0), + get sequence() { return self.getCachedValue(sequence); }, + get dictionary() { return self.getCachedValue(dictionaryNames)[0]; }, + dictionaryOrder: { + index: dictionaryIndex, + priority: dictionaryPriority + }, + get dictionaryNames() { return self.getCachedValue(dictionaryNames); }, + get expression() { + const {uniqueTerms} = self.getCachedValue(commonInfo); + return (type === 'term' || type === 'termGrouped' ? uniqueTerms[0] : uniqueTerms); + }, + get reading() { + const {uniqueReadings} = self.getCachedValue(commonInfo); + return (type === 'term' || type === 'termGrouped' ? uniqueReadings[0] : uniqueReadings); + }, + get expressions() { return self.getCachedValue(expressions); }, + get glossary() { return self.getCachedValue(glossary); }, + get definitionTags() { return type === 'term' ? self.getCachedValue(commonInfo).definitionTags : void 0; }, + get termTags() { return self.getCachedValue(termTags); }, + get definitions() { return self.getCachedValue(commonInfo).definitions; }, + get frequencies() { return self.getCachedValue(frequencies); }, + get pitches() { return self.getCachedValue(pitches); }, + sourceTermExactMatchCount, + url, + get cloze() { return self.getCachedValue(cloze); }, + get furiganaSegments() { return self.getCachedValue(furiganaSegments); } + }; + } + + _getTermDictionaryNames(dictionaryEntry) { + const dictionaryNames = new Set(); + for (const {dictionary} of dictionaryEntry.definitions) { + dictionaryNames.add(dictionary); + } + return [...dictionaryNames]; + } + + _getTermDictionaryEntryCommonInfo(dictionaryEntry, type) { + const merged = (type === 'termMerged'); + const hasDefinitions = (type !== 'term'); + + const allTermsSet = new Set(); + const allReadingsSet = new Set(); + for (const {term, reading} of dictionaryEntry.headwords) { + allTermsSet.add(term); + allReadingsSet.add(reading); + } + const uniqueTerms = [...allTermsSet]; + const uniqueReadings = [...allReadingsSet]; + + const definitions = []; + const definitionTags = []; + for (const {tags, headwordIndices, entries, dictionary, sequences} of dictionaryEntry.definitions) { + const definitionTags2 = []; + for (const tag of tags) { + definitionTags.push(this._convertTag(tag)); + definitionTags2.push(this._convertTag(tag)); + } + if (!hasDefinitions) { continue; } + const only = merged ? DictionaryDataUtil.getDisambiguations(dictionaryEntry.headwords, headwordIndices, allTermsSet, allReadingsSet) : void 0; + definitions.push({ + sequence: sequences[0], + dictionary, + glossary: entries, + definitionTags: definitionTags2, + only + }); + } + + return { + uniqueTerms, + uniqueReadings, + definitionTags, + definitions: hasDefinitions ? definitions : void 0 + }; + } + + _getTermFrequencies(dictionaryEntry) { + const results = []; + const {headwords} = dictionaryEntry; + for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency} of dictionaryEntry.frequencies) { + const {term, reading} = headwords[headwordIndex]; + results.push({ + index: results.length, + expressionIndex: headwordIndex, + dictionary, + dictionaryOrder: { + index: dictionaryIndex, + priority: dictionaryPriority + }, + expression: term, + reading, + hasReading, + frequency + }); + } + return results; + } + + _getTermPitches(dictionaryEntry) { + const self = this; + const results = []; + const {headwords} = dictionaryEntry; + for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches} of dictionaryEntry.pronunciations) { + const {term, reading} = headwords[headwordIndex]; + const cachedPitches = this.createCachedValue(this._getTermPitchesInner.bind(this, pitches)); + results.push({ + index: results.length, + expressionIndex: headwordIndex, + dictionary, + dictionaryOrder: { + index: dictionaryIndex, + priority: dictionaryPriority + }, + expression: term, + reading, + get pitches() { return self.getCachedValue(cachedPitches); } + }); + } + return results; + } + + _getTermPitchesInner(pitches) { + const self = this; + const results = []; + for (const {position, tags} of pitches) { + const cachedTags = this.createCachedValue(this._convertTags.bind(this, tags)); + results.push({ + position, + get tags() { return self.getCachedValue(cachedTags); } + }); + } + return results; + } + + _getTermExpressions(dictionaryEntry) { + const self = this; + const results = []; + const {headwords} = dictionaryEntry; + for (let i = 0, ii = headwords.length; i < ii; ++i) { + const {term, reading, tags, sources: [{deinflectedText}], wordClasses} = headwords[i]; + const termTags = this.createCachedValue(this._convertTags.bind(this, tags)); + const frequencies = this.createCachedValue(this._getTermExpressionFrequencies.bind(this, dictionaryEntry, i)); + const pitches = this.createCachedValue(this._getTermExpressionPitches.bind(this, dictionaryEntry, i)); + const termFrequency = this.createCachedValue(this._getTermExpressionTermFrequency.bind(this, termTags)); + const furiganaSegments = this.createCachedValue(this._getTermHeadwordFuriganaSegments.bind(this, term, reading)); + const item = { + sourceTerm: deinflectedText, + expression: term, + reading, + get termTags() { return self.getCachedValue(termTags); }, + get frequencies() { return self.getCachedValue(frequencies); }, + get pitches() { return self.getCachedValue(pitches); }, + get furiganaSegments() { return self.getCachedValue(furiganaSegments); }, + get termFrequency() { return self.getCachedValue(termFrequency); }, + wordClasses + }; + results.push(item); + } + return results; + } + + _getTermExpressionFrequencies(dictionaryEntry, i) { + const results = []; + const {headwords, frequencies} = dictionaryEntry; + for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency} of frequencies) { + if (headwordIndex !== i) { continue; } + const {term, reading} = headwords[headwordIndex]; + results.push({ + index: results.length, + expressionIndex: headwordIndex, + dictionary, + dictionaryOrder: { + index: dictionaryIndex, + priority: dictionaryPriority + }, + expression: term, + reading, + hasReading, + frequency + }); + } + return results; + } + + _getTermExpressionPitches(dictionaryEntry, i) { + const self = this; + const results = []; + const {headwords, pronunciations} = dictionaryEntry; + for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches} of pronunciations) { + if (headwordIndex !== i) { continue; } + const {term, reading} = headwords[headwordIndex]; + const cachedPitches = this.createCachedValue(this._getTermPitchesInner.bind(this, pitches)); + results.push({ + index: results.length, + expressionIndex: headwordIndex, + dictionary, + dictionaryOrder: { + index: dictionaryIndex, + priority: dictionaryPriority + }, + expression: term, + reading, + get pitches() { return self.getCachedValue(cachedPitches); } + }); + } + return results; + } + + _getTermExpressionTermFrequency(cachedTermTags) { + const termTags = this.getCachedValue(cachedTermTags); + return DictionaryDataUtil.getTermFrequency(termTags); + } + + _getTermGlossaryArray(dictionaryEntry, type) { + if (type === 'term') { + const results = []; + for (const {entries} of dictionaryEntry.definitions) { + results.push(...entries); + } + return results; + } + return void 0; + } + + _getTermTags(dictionaryEntry, type) { + if (type !== 'termMerged') { + const results = []; + for (const {tag} of DictionaryDataUtil.groupTermTags(dictionaryEntry)) { + results.push(this._convertTag(tag)); + } + return results; + } + return void 0; + } + + _convertTags(tags) { + const results = []; + for (const tag of tags) { + results.push(this._convertTag(tag)); + } + return results; + } + + _convertTag({name, category, content, order, score, dictionaries, redundant}) { + return { + name, + category, + notes: (content.length > 0 ? content[0] : ''), + order, + score, + dictionary: (dictionaries.length > 0 ? dictionaries[0] : ''), + redundant + }; + } + + _getCloze(dictionaryEntry, context) { + let originalText = ''; + switch (dictionaryEntry.type) { + case 'term': + { + const primarySource = this._getPrimarySource(dictionaryEntry); + if (primarySource !== null) { originalText = primarySource.originalText; } + } + break; + case 'kanji': + originalText = dictionaryEntry.character; + break; + } + + const {sentence} = this._asObject(context); + let {text, offset} = this._asObject(sentence); + if (typeof text !== 'string') { text = ''; } + if (typeof offset !== 'number') { offset = 0; } + + return { + sentence: text, + prefix: text.substring(0, offset), + body: text.substring(offset, offset + originalText.length), + suffix: text.substring(offset + originalText.length) + }; + } + + _getTermFuriganaSegments(dictionaryEntry, type) { + if (type === 'term') { + for (const {term, reading} of dictionaryEntry.headwords) { + return this._getTermHeadwordFuriganaSegments(term, reading); + } + } + return void 0; + } + + _getTermHeadwordFuriganaSegments(term, reading) { + const result = []; + for (const {text, reading: reading2} of this._japaneseUtil.distributeFurigana(term, reading)) { + result.push({text, furigana: reading2}); + } + return result; + } + + _getTermDictionaryEntrySequence(dictionaryEntry) { + let hasSequence = false; + let mainSequence = -1; + for (const {sequences, isPrimary} of dictionaryEntry.definitions) { + if (!isPrimary) { continue; } + const sequence = sequences[0]; + if (!hasSequence) { + mainSequence = sequence; + hasSequence = true; + if (mainSequence === -1) { break; } + } else if (mainSequence !== sequence) { + mainSequence = -1; + break; + } + } + return mainSequence; + } +} diff --git a/ext/js/display/pronunciation-generator.js b/ext/js/display/pronunciation-generator.js deleted file mode 100644 index bab36add..00000000 --- a/ext/js/display/pronunciation-generator.js +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright (C) 2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -class PronunciationGenerator { - constructor(japaneseUtil) { - this._japaneseUtil = japaneseUtil; - } - - createPronunciationText(morae, downstepPosition, nasalPositions, devoicePositions) { - const jp = this._japaneseUtil; - const nasalPositionsSet = nasalPositions.length > 0 ? new Set(nasalPositions) : null; - const devoicePositionsSet = devoicePositions.length > 0 ? new Set(devoicePositions) : null; - const container = document.createElement('span'); - container.className = 'pronunciation-text'; - for (let i = 0, ii = morae.length; i < ii; ++i) { - const i1 = i + 1; - const mora = morae[i]; - const highPitch = jp.isMoraPitchHigh(i, downstepPosition); - const highPitchNext = jp.isMoraPitchHigh(i1, downstepPosition); - const nasal = nasalPositionsSet !== null && nasalPositionsSet.has(i1); - const devoice = devoicePositionsSet !== null && devoicePositionsSet.has(i1); - - const n1 = document.createElement('span'); - n1.className = 'pronunciation-mora'; - n1.dataset.position = `${i}`; - n1.dataset.pitch = highPitch ? 'high' : 'low'; - n1.dataset.pitchNext = highPitchNext ? 'high' : 'low'; - - const characterNodes = []; - for (const character of mora) { - const n2 = document.createElement('span'); - n2.className = 'pronunciation-character'; - n2.textContent = character; - n1.appendChild(n2); - characterNodes.push(n2); - } - - if (devoice) { - n1.dataset.devoice = 'true'; - const n3 = document.createElement('span'); - n3.className = 'pronunciation-devoice-indicator'; - n1.appendChild(n3); - } - if (nasal && characterNodes.length > 0) { - n1.dataset.nasal = 'true'; - - const group = document.createElement('span'); - group.className = 'pronunciation-character-group'; - - const n2 = characterNodes[0]; - const character = n2.textContent; - - const characterInfo = jp.getKanaDiacriticInfo(character); - if (characterInfo !== null) { - n1.dataset.originalText = mora; - n2.dataset.originalText = character; - n2.textContent = characterInfo.character; - } - - let n3 = document.createElement('span'); - n3.className = 'pronunciation-nasal-diacritic'; - n3.textContent = '\u309a'; // Combining handakuten - group.appendChild(n3); - - n3 = document.createElement('span'); - n3.className = 'pronunciation-nasal-indicator'; - group.appendChild(n3); - - n2.parentNode.replaceChild(group, n2); - group.insertBefore(n2, group.firstChild); - } - - const line = document.createElement('span'); - line.className = 'pronunciation-mora-line'; - n1.appendChild(line); - - container.appendChild(n1); - } - return container; - } - - createPronunciationGraph(morae, downstepPosition) { - const jp = this._japaneseUtil; - const ii = morae.length; - - const svgns = 'http://www.w3.org/2000/svg'; - const svg = document.createElementNS(svgns, 'svg'); - svg.setAttribute('xmlns', svgns); - svg.setAttribute('class', 'pronunciation-graph'); - svg.setAttribute('focusable', 'false'); - svg.setAttribute('viewBox', `0 0 ${50 * (ii + 1)} 100`); - - if (ii <= 0) { return svg; } - - const path1 = document.createElementNS(svgns, 'path'); - svg.appendChild(path1); - - const path2 = document.createElementNS(svgns, 'path'); - svg.appendChild(path2); - - const pathPoints = []; - for (let i = 0; i < ii; ++i) { - const highPitch = jp.isMoraPitchHigh(i, downstepPosition); - const highPitchNext = jp.isMoraPitchHigh(i + 1, downstepPosition); - const x = i * 50 + 25; - const y = highPitch ? 25 : 75; - if (highPitch && !highPitchNext) { - this._addGraphDotDownstep(svg, svgns, x, y); - } else { - this._addGraphDot(svg, svgns, x, y); - } - pathPoints.push(`${x} ${y}`); - } - - path1.setAttribute('class', 'pronunciation-graph-line'); - path1.setAttribute('d', `M${pathPoints.join(' L')}`); - - pathPoints.splice(0, ii - 1); - { - const highPitch = jp.isMoraPitchHigh(ii, downstepPosition); - const x = ii * 50 + 25; - const y = highPitch ? 25 : 75; - this._addGraphTriangle(svg, svgns, x, y); - pathPoints.push(`${x} ${y}`); - } - - path2.setAttribute('class', 'pronunciation-graph-line-tail'); - path2.setAttribute('d', `M${pathPoints.join(' L')}`); - - return svg; - } - - createPronunciationDownstepNotation(downstepPosition) { - downstepPosition = `${downstepPosition}`; - - const n1 = document.createElement('span'); - n1.className = 'pronunciation-downstep-notation'; - n1.dataset.downstepPosition = downstepPosition; - - let n2 = document.createElement('span'); - n2.className = 'pronunciation-downstep-notation-prefix'; - n2.textContent = '['; - n1.appendChild(n2); - - n2 = document.createElement('span'); - n2.className = 'pronunciation-downstep-notation-number'; - n2.textContent = downstepPosition; - n1.appendChild(n2); - - n2 = document.createElement('span'); - n2.className = 'pronunciation-downstep-notation-suffix'; - n2.textContent = ']'; - n1.appendChild(n2); - - return n1; - } - - // Private - - _addGraphDot(container, svgns, x, y) { - container.appendChild(this._createGraphCircle(svgns, 'pronunciation-graph-dot', x, y, '15')); - } - - _addGraphDotDownstep(container, svgns, x, y) { - container.appendChild(this._createGraphCircle(svgns, 'pronunciation-graph-dot-downstep1', x, y, '15')); - container.appendChild(this._createGraphCircle(svgns, 'pronunciation-graph-dot-downstep2', x, y, '5')); - } - - _addGraphTriangle(container, svgns, x, y) { - const node = document.createElementNS(svgns, 'path'); - node.setAttribute('class', 'pronunciation-graph-triangle'); - node.setAttribute('d', 'M0 13 L15 -13 L-15 -13 Z'); - node.setAttribute('transform', `translate(${x},${y})`); - container.appendChild(node); - } - - _createGraphCircle(svgns, className, x, y, radius) { - const node = document.createElementNS(svgns, 'circle'); - node.setAttribute('class', className); - node.setAttribute('cx', `${x}`); - node.setAttribute('cy', `${y}`); - node.setAttribute('r', radius); - return node; - } -} diff --git a/ext/js/display/sandbox/pronunciation-generator.js b/ext/js/display/sandbox/pronunciation-generator.js new file mode 100644 index 00000000..bab36add --- /dev/null +++ b/ext/js/display/sandbox/pronunciation-generator.js @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +class PronunciationGenerator { + constructor(japaneseUtil) { + this._japaneseUtil = japaneseUtil; + } + + createPronunciationText(morae, downstepPosition, nasalPositions, devoicePositions) { + const jp = this._japaneseUtil; + const nasalPositionsSet = nasalPositions.length > 0 ? new Set(nasalPositions) : null; + const devoicePositionsSet = devoicePositions.length > 0 ? new Set(devoicePositions) : null; + const container = document.createElement('span'); + container.className = 'pronunciation-text'; + for (let i = 0, ii = morae.length; i < ii; ++i) { + const i1 = i + 1; + const mora = morae[i]; + const highPitch = jp.isMoraPitchHigh(i, downstepPosition); + const highPitchNext = jp.isMoraPitchHigh(i1, downstepPosition); + const nasal = nasalPositionsSet !== null && nasalPositionsSet.has(i1); + const devoice = devoicePositionsSet !== null && devoicePositionsSet.has(i1); + + const n1 = document.createElement('span'); + n1.className = 'pronunciation-mora'; + n1.dataset.position = `${i}`; + n1.dataset.pitch = highPitch ? 'high' : 'low'; + n1.dataset.pitchNext = highPitchNext ? 'high' : 'low'; + + const characterNodes = []; + for (const character of mora) { + const n2 = document.createElement('span'); + n2.className = 'pronunciation-character'; + n2.textContent = character; + n1.appendChild(n2); + characterNodes.push(n2); + } + + if (devoice) { + n1.dataset.devoice = 'true'; + const n3 = document.createElement('span'); + n3.className = 'pronunciation-devoice-indicator'; + n1.appendChild(n3); + } + if (nasal && characterNodes.length > 0) { + n1.dataset.nasal = 'true'; + + const group = document.createElement('span'); + group.className = 'pronunciation-character-group'; + + const n2 = characterNodes[0]; + const character = n2.textContent; + + const characterInfo = jp.getKanaDiacriticInfo(character); + if (characterInfo !== null) { + n1.dataset.originalText = mora; + n2.dataset.originalText = character; + n2.textContent = characterInfo.character; + } + + let n3 = document.createElement('span'); + n3.className = 'pronunciation-nasal-diacritic'; + n3.textContent = '\u309a'; // Combining handakuten + group.appendChild(n3); + + n3 = document.createElement('span'); + n3.className = 'pronunciation-nasal-indicator'; + group.appendChild(n3); + + n2.parentNode.replaceChild(group, n2); + group.insertBefore(n2, group.firstChild); + } + + const line = document.createElement('span'); + line.className = 'pronunciation-mora-line'; + n1.appendChild(line); + + container.appendChild(n1); + } + return container; + } + + createPronunciationGraph(morae, downstepPosition) { + const jp = this._japaneseUtil; + const ii = morae.length; + + const svgns = 'http://www.w3.org/2000/svg'; + const svg = document.createElementNS(svgns, 'svg'); + svg.setAttribute('xmlns', svgns); + svg.setAttribute('class', 'pronunciation-graph'); + svg.setAttribute('focusable', 'false'); + svg.setAttribute('viewBox', `0 0 ${50 * (ii + 1)} 100`); + + if (ii <= 0) { return svg; } + + const path1 = document.createElementNS(svgns, 'path'); + svg.appendChild(path1); + + const path2 = document.createElementNS(svgns, 'path'); + svg.appendChild(path2); + + const pathPoints = []; + for (let i = 0; i < ii; ++i) { + const highPitch = jp.isMoraPitchHigh(i, downstepPosition); + const highPitchNext = jp.isMoraPitchHigh(i + 1, downstepPosition); + const x = i * 50 + 25; + const y = highPitch ? 25 : 75; + if (highPitch && !highPitchNext) { + this._addGraphDotDownstep(svg, svgns, x, y); + } else { + this._addGraphDot(svg, svgns, x, y); + } + pathPoints.push(`${x} ${y}`); + } + + path1.setAttribute('class', 'pronunciation-graph-line'); + path1.setAttribute('d', `M${pathPoints.join(' L')}`); + + pathPoints.splice(0, ii - 1); + { + const highPitch = jp.isMoraPitchHigh(ii, downstepPosition); + const x = ii * 50 + 25; + const y = highPitch ? 25 : 75; + this._addGraphTriangle(svg, svgns, x, y); + pathPoints.push(`${x} ${y}`); + } + + path2.setAttribute('class', 'pronunciation-graph-line-tail'); + path2.setAttribute('d', `M${pathPoints.join(' L')}`); + + return svg; + } + + createPronunciationDownstepNotation(downstepPosition) { + downstepPosition = `${downstepPosition}`; + + const n1 = document.createElement('span'); + n1.className = 'pronunciation-downstep-notation'; + n1.dataset.downstepPosition = downstepPosition; + + let n2 = document.createElement('span'); + n2.className = 'pronunciation-downstep-notation-prefix'; + n2.textContent = '['; + n1.appendChild(n2); + + n2 = document.createElement('span'); + n2.className = 'pronunciation-downstep-notation-number'; + n2.textContent = downstepPosition; + n1.appendChild(n2); + + n2 = document.createElement('span'); + n2.className = 'pronunciation-downstep-notation-suffix'; + n2.textContent = ']'; + n1.appendChild(n2); + + return n1; + } + + // Private + + _addGraphDot(container, svgns, x, y) { + container.appendChild(this._createGraphCircle(svgns, 'pronunciation-graph-dot', x, y, '15')); + } + + _addGraphDotDownstep(container, svgns, x, y) { + container.appendChild(this._createGraphCircle(svgns, 'pronunciation-graph-dot-downstep1', x, y, '15')); + container.appendChild(this._createGraphCircle(svgns, 'pronunciation-graph-dot-downstep2', x, y, '5')); + } + + _addGraphTriangle(container, svgns, x, y) { + const node = document.createElementNS(svgns, 'path'); + node.setAttribute('class', 'pronunciation-graph-triangle'); + node.setAttribute('d', 'M0 13 L15 -13 L-15 -13 Z'); + node.setAttribute('transform', `translate(${x},${y})`); + container.appendChild(node); + } + + _createGraphCircle(svgns, className, x, y, radius) { + const node = document.createElementNS(svgns, 'circle'); + node.setAttribute('class', className); + node.setAttribute('cx', `${x}`); + node.setAttribute('cy', `${y}`); + node.setAttribute('r', radius); + return node; + } +} diff --git a/ext/js/display/sandbox/structured-content-generator.js b/ext/js/display/sandbox/structured-content-generator.js new file mode 100644 index 00000000..833df6f6 --- /dev/null +++ b/ext/js/display/sandbox/structured-content-generator.js @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +class StructuredContentGenerator { + constructor(mediaLoader, document) { + this._mediaLoader = mediaLoader; + this._document = document; + } + + createStructuredContent(content, dictionary) { + if (typeof content === 'string') { + return this._createTextNode(content); + } + if (!(typeof content === 'object' && content !== null)) { + return null; + } + if (Array.isArray(content)) { + const fragment = this._createDocumentFragment(); + for (const item of content) { + const child = this.createStructuredContent(item, dictionary); + if (child !== null) { fragment.appendChild(child); } + } + return fragment; + } + const {tag} = content; + switch (tag) { + case 'br': + return this._createStructuredContentElement(tag, content, dictionary, 'simple', false, false); + case 'ruby': + case 'rt': + case 'rp': + return this._createStructuredContentElement(tag, content, dictionary, 'simple', true, false); + case 'table': + return this._createStructuredContentTableElement(tag, content, dictionary); + case 'thead': + case 'tbody': + case 'tfoot': + case 'tr': + return this._createStructuredContentElement(tag, content, dictionary, 'table', true, false); + case 'th': + case 'td': + return this._createStructuredContentElement(tag, content, dictionary, 'table-cell', true, true); + case 'div': + case 'span': + return this._createStructuredContentElement(tag, content, dictionary, 'simple', true, true); + case 'img': + return this.createDefinitionImage(content, dictionary); + } + return null; + } + + createDefinitionImage(data, dictionary) { + const { + path, + width, + height, + preferredWidth, + preferredHeight, + title, + pixelated, + imageRendering, + appearance, + background, + collapsed, + collapsible, + verticalAlign, + sizeUnits + } = data; + + const hasPreferredWidth = (typeof preferredWidth === 'number'); + const hasPreferredHeight = (typeof preferredHeight === 'number'); + const invAspectRatio = ( + hasPreferredWidth && hasPreferredHeight ? + preferredHeight / preferredWidth : + height / width + ); + const usedWidth = ( + hasPreferredWidth ? + preferredWidth : + (hasPreferredHeight ? preferredHeight * invAspectRatio : width) + ); + + const node = this._createElement('a', 'gloss-image-link'); + node.target = '_blank'; + node.rel = 'noreferrer noopener'; + + const imageContainer = this._createElement('span', 'gloss-image-container'); + node.appendChild(imageContainer); + + const aspectRatioSizer = this._createElement('span', 'gloss-image-sizer'); + imageContainer.appendChild(aspectRatioSizer); + + const imageBackground = this._createElement('span', 'gloss-image-background'); + imageContainer.appendChild(imageBackground); + + const image = this._createElement('img', 'gloss-image'); + image.alt = ''; + imageContainer.appendChild(image); + + const overlay = this._createElement('span', 'gloss-image-container-overlay'); + imageContainer.appendChild(overlay); + + const linkText = this._createElement('span', 'gloss-image-link-text'); + linkText.textContent = 'Image'; + node.appendChild(linkText); + + node.dataset.path = path; + node.dataset.dictionary = dictionary; + node.dataset.imageLoadState = 'not-loaded'; + node.dataset.hasAspectRatio = 'true'; + node.dataset.imageRendering = typeof imageRendering === 'string' ? imageRendering : (pixelated ? 'pixelated' : 'auto'); + node.dataset.appearance = typeof appearance === 'string' ? appearance : 'auto'; + node.dataset.background = typeof background === 'boolean' ? `${background}` : 'true'; + node.dataset.collapsed = typeof collapsed === 'boolean' ? `${collapsed}` : 'false'; + node.dataset.collapsible = typeof collapsible === 'boolean' ? `${collapsible}` : 'true'; + if (typeof verticalAlign === 'string') { + node.dataset.verticalAlign = verticalAlign; + } + if (typeof sizeUnits === 'string' && (hasPreferredWidth || hasPreferredHeight)) { + node.dataset.sizeUnits = sizeUnits; + } + + imageContainer.style.width = `${usedWidth}em`; + if (typeof title === 'string') { + imageContainer.title = title; + } + + aspectRatioSizer.style.paddingTop = `${invAspectRatio * 100.0}%`; + + if (this._mediaLoader !== null) { + this._mediaLoader.loadMedia( + path, + dictionary, + (url) => this._setImageData(node, image, imageBackground, url, false), + () => this._setImageData(node, image, imageBackground, null, true) + ); + } + + return node; + } + + // Private + + _createElement(tagName, className) { + const node = this._document.createElement(tagName); + node.className = className; + return node; + } + + _createTextNode(data) { + return this._document.createTextNode(data); + } + + _createDocumentFragment() { + return this._document.createDocumentFragment(); + } + + _setImageData(node, image, imageBackground, url, unloaded) { + if (url !== null) { + image.src = url; + node.href = url; + node.dataset.imageLoadState = 'loaded'; + imageBackground.style.setProperty('--image', `url("${url}")`); + } else { + image.removeAttribute('src'); + node.removeAttribute('href'); + node.dataset.imageLoadState = unloaded ? 'unloaded' : 'load-error'; + imageBackground.style.removeProperty('--image'); + } + } + + _createStructuredContentTableElement(tag, content, dictionary) { + const container = this._createElement('div', 'gloss-sc-table-container'); + const table = this._createStructuredContentElement(tag, content, dictionary, 'table', true, false); + container.appendChild(table); + return container; + } + + _createStructuredContentElement(tag, content, dictionary, type, hasChildren, hasStyle) { + const node = this._createElement(tag, `gloss-sc-${tag}`); + switch (type) { + case 'table-cell': + { + const {colSpan, rowSpan} = content; + if (typeof colSpan === 'number') { node.colSpan = colSpan; } + if (typeof rowSpan === 'number') { node.rowSpan = rowSpan; } + } + break; + } + if (hasStyle) { + const {style} = content; + if (typeof style === 'object' && style !== null) { + this._setStructuredContentElementStyle(node, style); + } + } + if (hasChildren) { + const child = this.createStructuredContent(content.content, dictionary); + if (child !== null) { node.appendChild(child); } + } + return node; + } + + _setStructuredContentElementStyle(node, contentStyle) { + const {style} = node; + const {fontStyle, fontWeight, fontSize, textDecorationLine, verticalAlign} = contentStyle; + if (typeof fontStyle === 'string') { style.fontStyle = fontStyle; } + if (typeof fontWeight === 'string') { style.fontWeight = fontWeight; } + if (typeof fontSize === 'string') { style.fontSize = fontSize; } + if (typeof verticalAlign === 'string') { style.verticalAlign = verticalAlign; } + if (typeof textDecorationLine === 'string') { + style.textDecoration = textDecorationLine; + } else if (Array.isArray(textDecorationLine)) { + style.textDecoration = textDecorationLine.join(' '); + } + } +} diff --git a/ext/js/display/structured-content-generator.js b/ext/js/display/structured-content-generator.js deleted file mode 100644 index 833df6f6..00000000 --- a/ext/js/display/structured-content-generator.js +++ /dev/null @@ -1,230 +0,0 @@ -/* - * Copyright (C) 2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -class StructuredContentGenerator { - constructor(mediaLoader, document) { - this._mediaLoader = mediaLoader; - this._document = document; - } - - createStructuredContent(content, dictionary) { - if (typeof content === 'string') { - return this._createTextNode(content); - } - if (!(typeof content === 'object' && content !== null)) { - return null; - } - if (Array.isArray(content)) { - const fragment = this._createDocumentFragment(); - for (const item of content) { - const child = this.createStructuredContent(item, dictionary); - if (child !== null) { fragment.appendChild(child); } - } - return fragment; - } - const {tag} = content; - switch (tag) { - case 'br': - return this._createStructuredContentElement(tag, content, dictionary, 'simple', false, false); - case 'ruby': - case 'rt': - case 'rp': - return this._createStructuredContentElement(tag, content, dictionary, 'simple', true, false); - case 'table': - return this._createStructuredContentTableElement(tag, content, dictionary); - case 'thead': - case 'tbody': - case 'tfoot': - case 'tr': - return this._createStructuredContentElement(tag, content, dictionary, 'table', true, false); - case 'th': - case 'td': - return this._createStructuredContentElement(tag, content, dictionary, 'table-cell', true, true); - case 'div': - case 'span': - return this._createStructuredContentElement(tag, content, dictionary, 'simple', true, true); - case 'img': - return this.createDefinitionImage(content, dictionary); - } - return null; - } - - createDefinitionImage(data, dictionary) { - const { - path, - width, - height, - preferredWidth, - preferredHeight, - title, - pixelated, - imageRendering, - appearance, - background, - collapsed, - collapsible, - verticalAlign, - sizeUnits - } = data; - - const hasPreferredWidth = (typeof preferredWidth === 'number'); - const hasPreferredHeight = (typeof preferredHeight === 'number'); - const invAspectRatio = ( - hasPreferredWidth && hasPreferredHeight ? - preferredHeight / preferredWidth : - height / width - ); - const usedWidth = ( - hasPreferredWidth ? - preferredWidth : - (hasPreferredHeight ? preferredHeight * invAspectRatio : width) - ); - - const node = this._createElement('a', 'gloss-image-link'); - node.target = '_blank'; - node.rel = 'noreferrer noopener'; - - const imageContainer = this._createElement('span', 'gloss-image-container'); - node.appendChild(imageContainer); - - const aspectRatioSizer = this._createElement('span', 'gloss-image-sizer'); - imageContainer.appendChild(aspectRatioSizer); - - const imageBackground = this._createElement('span', 'gloss-image-background'); - imageContainer.appendChild(imageBackground); - - const image = this._createElement('img', 'gloss-image'); - image.alt = ''; - imageContainer.appendChild(image); - - const overlay = this._createElement('span', 'gloss-image-container-overlay'); - imageContainer.appendChild(overlay); - - const linkText = this._createElement('span', 'gloss-image-link-text'); - linkText.textContent = 'Image'; - node.appendChild(linkText); - - node.dataset.path = path; - node.dataset.dictionary = dictionary; - node.dataset.imageLoadState = 'not-loaded'; - node.dataset.hasAspectRatio = 'true'; - node.dataset.imageRendering = typeof imageRendering === 'string' ? imageRendering : (pixelated ? 'pixelated' : 'auto'); - node.dataset.appearance = typeof appearance === 'string' ? appearance : 'auto'; - node.dataset.background = typeof background === 'boolean' ? `${background}` : 'true'; - node.dataset.collapsed = typeof collapsed === 'boolean' ? `${collapsed}` : 'false'; - node.dataset.collapsible = typeof collapsible === 'boolean' ? `${collapsible}` : 'true'; - if (typeof verticalAlign === 'string') { - node.dataset.verticalAlign = verticalAlign; - } - if (typeof sizeUnits === 'string' && (hasPreferredWidth || hasPreferredHeight)) { - node.dataset.sizeUnits = sizeUnits; - } - - imageContainer.style.width = `${usedWidth}em`; - if (typeof title === 'string') { - imageContainer.title = title; - } - - aspectRatioSizer.style.paddingTop = `${invAspectRatio * 100.0}%`; - - if (this._mediaLoader !== null) { - this._mediaLoader.loadMedia( - path, - dictionary, - (url) => this._setImageData(node, image, imageBackground, url, false), - () => this._setImageData(node, image, imageBackground, null, true) - ); - } - - return node; - } - - // Private - - _createElement(tagName, className) { - const node = this._document.createElement(tagName); - node.className = className; - return node; - } - - _createTextNode(data) { - return this._document.createTextNode(data); - } - - _createDocumentFragment() { - return this._document.createDocumentFragment(); - } - - _setImageData(node, image, imageBackground, url, unloaded) { - if (url !== null) { - image.src = url; - node.href = url; - node.dataset.imageLoadState = 'loaded'; - imageBackground.style.setProperty('--image', `url("${url}")`); - } else { - image.removeAttribute('src'); - node.removeAttribute('href'); - node.dataset.imageLoadState = unloaded ? 'unloaded' : 'load-error'; - imageBackground.style.removeProperty('--image'); - } - } - - _createStructuredContentTableElement(tag, content, dictionary) { - const container = this._createElement('div', 'gloss-sc-table-container'); - const table = this._createStructuredContentElement(tag, content, dictionary, 'table', true, false); - container.appendChild(table); - return container; - } - - _createStructuredContentElement(tag, content, dictionary, type, hasChildren, hasStyle) { - const node = this._createElement(tag, `gloss-sc-${tag}`); - switch (type) { - case 'table-cell': - { - const {colSpan, rowSpan} = content; - if (typeof colSpan === 'number') { node.colSpan = colSpan; } - if (typeof rowSpan === 'number') { node.rowSpan = rowSpan; } - } - break; - } - if (hasStyle) { - const {style} = content; - if (typeof style === 'object' && style !== null) { - this._setStructuredContentElementStyle(node, style); - } - } - if (hasChildren) { - const child = this.createStructuredContent(content.content, dictionary); - if (child !== null) { node.appendChild(child); } - } - return node; - } - - _setStructuredContentElementStyle(node, contentStyle) { - const {style} = node; - const {fontStyle, fontWeight, fontSize, textDecorationLine, verticalAlign} = contentStyle; - if (typeof fontStyle === 'string') { style.fontStyle = fontStyle; } - if (typeof fontWeight === 'string') { style.fontWeight = fontWeight; } - if (typeof fontSize === 'string') { style.fontSize = fontSize; } - if (typeof verticalAlign === 'string') { style.verticalAlign = verticalAlign; } - if (typeof textDecorationLine === 'string') { - style.textDecoration = textDecorationLine; - } else if (Array.isArray(textDecorationLine)) { - style.textDecoration = textDecorationLine.join(' '); - } - } -} diff --git a/ext/js/dom/css-style-applier.js b/ext/js/dom/css-style-applier.js deleted file mode 100644 index c617fead..00000000 --- a/ext/js/dom/css-style-applier.js +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (C) 2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** - * This class is used to apply CSS styles to elements using a consistent method - * that is the same across different browsers. - */ -class CssStyleApplier { - /** - * Creates a new instance of the class. - * @param styleDataUrl The local URL to the JSON file continaing the style rules. - * The style rules should be of the format: - * [ - * { - * selectors: [(selector:string)...], - * styles: [ - * [(property:string), (value:string)]... - * ] - * }... - * ] - */ - constructor(styleDataUrl) { - this._styleDataUrl = styleDataUrl; - this._styleData = []; - this._cachedRules = new Map(); - } - - /** - * Loads the data file for use. - */ - async prepare() { - this._styleData = await this._fetchJsonAsset(this._styleDataUrl); - } - - /** - * Applies CSS styles directly to the "style" attribute using the "class" attribute. - * This only works for elements with a single class. - * @param elements An iterable collection of HTMLElement objects. - */ - applyClassStyles(elements) { - const elementStyles = []; - for (const element of elements) { - const {className} = element; - if (className.length === 0) { continue; } - let cssTextNew = ''; - for (const {selectorText, styles} of this._getRulesForClass(className)) { - if (!element.matches(selectorText)) { continue; } - cssTextNew += this._getCssText(styles); - } - cssTextNew += element.style.cssText; - elementStyles.push({element, style: cssTextNew}); - } - for (const {element, style} of elementStyles) { - element.removeAttribute('class'); - if (style.length > 0) { - element.setAttribute('style', style); - } else { - element.removeAttribute('style'); - } - } - } - - // Private - - async _fetchJsonAsset(url) { - const response = await fetch(url, { - method: 'GET', - mode: 'no-cors', - cache: 'default', - credentials: 'omit', - redirect: 'follow', - referrerPolicy: 'no-referrer' - }); - if (!response.ok) { - throw new Error(`Failed to fetch ${url}: ${response.status}`); - } - return await response.json(); - } - - _getRulesForClass(className) { - let rules = this._cachedRules.get(className); - if (typeof rules !== 'undefined') { return rules; } - - rules = []; - this._cachedRules.set(className, rules); - - const classNamePattern = new RegExp(`.${className}(?![0-9a-zA-Z-])`, ''); - for (const {selectors, styles} of this._styleData) { - const selectorText = selectors.join(','); - if (!classNamePattern.test(selectorText)) { continue; } - rules.push({selectorText, styles}); - } - - return rules; - } - - _getCssText(styles) { - let cssText = ''; - for (const [property, value] of styles) { - cssText += `${property}:${value};`; - } - return cssText; - } -} diff --git a/ext/js/dom/sandbox/css-style-applier.js b/ext/js/dom/sandbox/css-style-applier.js new file mode 100644 index 00000000..c617fead --- /dev/null +++ b/ext/js/dom/sandbox/css-style-applier.js @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * This class is used to apply CSS styles to elements using a consistent method + * that is the same across different browsers. + */ +class CssStyleApplier { + /** + * Creates a new instance of the class. + * @param styleDataUrl The local URL to the JSON file continaing the style rules. + * The style rules should be of the format: + * [ + * { + * selectors: [(selector:string)...], + * styles: [ + * [(property:string), (value:string)]... + * ] + * }... + * ] + */ + constructor(styleDataUrl) { + this._styleDataUrl = styleDataUrl; + this._styleData = []; + this._cachedRules = new Map(); + } + + /** + * Loads the data file for use. + */ + async prepare() { + this._styleData = await this._fetchJsonAsset(this._styleDataUrl); + } + + /** + * Applies CSS styles directly to the "style" attribute using the "class" attribute. + * This only works for elements with a single class. + * @param elements An iterable collection of HTMLElement objects. + */ + applyClassStyles(elements) { + const elementStyles = []; + for (const element of elements) { + const {className} = element; + if (className.length === 0) { continue; } + let cssTextNew = ''; + for (const {selectorText, styles} of this._getRulesForClass(className)) { + if (!element.matches(selectorText)) { continue; } + cssTextNew += this._getCssText(styles); + } + cssTextNew += element.style.cssText; + elementStyles.push({element, style: cssTextNew}); + } + for (const {element, style} of elementStyles) { + element.removeAttribute('class'); + if (style.length > 0) { + element.setAttribute('style', style); + } else { + element.removeAttribute('style'); + } + } + } + + // Private + + async _fetchJsonAsset(url) { + const response = await fetch(url, { + method: 'GET', + mode: 'no-cors', + cache: 'default', + credentials: 'omit', + redirect: 'follow', + referrerPolicy: 'no-referrer' + }); + if (!response.ok) { + throw new Error(`Failed to fetch ${url}: ${response.status}`); + } + return await response.json(); + } + + _getRulesForClass(className) { + let rules = this._cachedRules.get(className); + if (typeof rules !== 'undefined') { return rules; } + + rules = []; + this._cachedRules.set(className, rules); + + const classNamePattern = new RegExp(`.${className}(?![0-9a-zA-Z-])`, ''); + for (const {selectors, styles} of this._styleData) { + const selectorText = selectors.join(','); + if (!classNamePattern.test(selectorText)) { continue; } + rules.push({selectorText, styles}); + } + + return rules; + } + + _getCssText(styles) { + let cssText = ''; + for (const [property, value] of styles) { + cssText += `${property}:${value};`; + } + return cssText; + } +} diff --git a/ext/js/language/dictionary-data-util.js b/ext/js/language/dictionary-data-util.js deleted file mode 100644 index 951e10ff..00000000 --- a/ext/js/language/dictionary-data-util.js +++ /dev/null @@ -1,299 +0,0 @@ -/* - * Copyright (C) 2020-2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -class DictionaryDataUtil { - static groupTermTags(dictionaryEntry) { - const {headwords} = dictionaryEntry; - const headwordCount = headwords.length; - const uniqueCheck = (headwordCount > 1); - const resultsIndexMap = new Map(); - const results = []; - for (let i = 0; i < headwordCount; ++i) { - const {tags} = headwords[i]; - for (const tag of tags) { - if (uniqueCheck) { - const {name, category, notes, dictionary} = tag; - const key = this._createMapKey([name, category, notes, dictionary]); - const index = resultsIndexMap.get(key); - if (typeof index !== 'undefined') { - const existingItem = results[index]; - existingItem.headwordIndices.push(i); - continue; - } - resultsIndexMap.set(key, results.length); - } - - const item = {tag, headwordIndices: [i]}; - results.push(item); - } - } - return results; - } - - static groupTermFrequencies(dictionaryEntry) { - const {headwords, frequencies} = dictionaryEntry; - - const map1 = new Map(); - for (const {headwordIndex, dictionary, hasReading, frequency} of frequencies) { - const {term, reading} = headwords[headwordIndex]; - - let map2 = map1.get(dictionary); - if (typeof map2 === 'undefined') { - map2 = new Map(); - map1.set(dictionary, map2); - } - - const readingKey = hasReading ? reading : null; - const key = this._createMapKey([term, readingKey]); - let frequencyData = map2.get(key); - if (typeof frequencyData === 'undefined') { - frequencyData = {term, reading: readingKey, values: new Set()}; - map2.set(key, frequencyData); - } - - frequencyData.values.add(frequency); - } - return this._createFrequencyGroupsFromMap(map1); - } - - static groupKanjiFrequencies(frequencies) { - const map1 = new Map(); - for (const {dictionary, character, frequency} of frequencies) { - let map2 = map1.get(dictionary); - if (typeof map2 === 'undefined') { - map2 = new Map(); - map1.set(dictionary, map2); - } - - let frequencyData = map2.get(character); - if (typeof frequencyData === 'undefined') { - frequencyData = {character, values: new Set()}; - map2.set(character, frequencyData); - } - - frequencyData.values.add(frequency); - } - return this._createFrequencyGroupsFromMap(map1); - } - - static getPitchAccentInfos(dictionaryEntry) { - const {headwords, pronunciations} = dictionaryEntry; - - const allTerms = new Set(); - const allReadings = new Set(); - for (const {term, reading} of headwords) { - allTerms.add(term); - allReadings.add(reading); - } - - const pitchAccentInfoMap = new Map(); - for (const {headwordIndex, dictionary, pitches} of pronunciations) { - const {term, reading} = headwords[headwordIndex]; - let dictionaryPitchAccentInfoList = pitchAccentInfoMap.get(dictionary); - if (typeof dictionaryPitchAccentInfoList === 'undefined') { - dictionaryPitchAccentInfoList = []; - pitchAccentInfoMap.set(dictionary, dictionaryPitchAccentInfoList); - } - for (const {position, nasalPositions, devoicePositions, tags} of pitches) { - let pitchAccentInfo = this._findExistingPitchAccentInfo(reading, position, nasalPositions, devoicePositions, tags, dictionaryPitchAccentInfoList); - if (pitchAccentInfo === null) { - pitchAccentInfo = { - terms: new Set(), - reading, - position, - nasalPositions, - devoicePositions, - tags, - exclusiveTerms: [], - exclusiveReadings: [] - }; - dictionaryPitchAccentInfoList.push(pitchAccentInfo); - } - pitchAccentInfo.terms.add(term); - } - } - - const multipleReadings = (allReadings.size > 1); - for (const dictionaryPitchAccentInfoList of pitchAccentInfoMap.values()) { - for (const pitchAccentInfo of dictionaryPitchAccentInfoList) { - const {terms, reading, exclusiveTerms, exclusiveReadings} = pitchAccentInfo; - if (!this._areSetsEqual(terms, allTerms)) { - exclusiveTerms.push(...this._getSetIntersection(terms, allTerms)); - } - if (multipleReadings) { - exclusiveReadings.push(reading); - } - pitchAccentInfo.terms = [...terms]; - } - } - - const results2 = []; - for (const [dictionary, pitches] of pitchAccentInfoMap.entries()) { - results2.push({dictionary, pitches}); - } - return results2; - } - - static getTermFrequency(termTags) { - let totalScore = 0; - for (const {score} of termTags) { - totalScore += score; - } - if (totalScore > 0) { - return 'popular'; - } else if (totalScore < 0) { - return 'rare'; - } else { - return 'normal'; - } - } - - static getDisambiguations(headwords, headwordIndices, allTermsSet, allReadingsSet) { - if (allTermsSet.size <= 1 && allReadingsSet.size <= 1) { return []; } - - const terms = new Set(); - const readings = new Set(); - for (const headwordIndex of headwordIndices) { - const {term, reading} = headwords[headwordIndex]; - terms.add(term); - readings.add(reading); - } - - const disambiguations = []; - const addTerms = !this._areSetsEqual(terms, allTermsSet); - const addReadings = !this._areSetsEqual(readings, allReadingsSet); - if (addTerms) { - disambiguations.push(...this._getSetIntersection(terms, allTermsSet)); - } - if (addReadings) { - if (addTerms) { - for (const term of terms) { - readings.delete(term); - } - } - disambiguations.push(...this._getSetIntersection(readings, allReadingsSet)); - } - return disambiguations; - } - - static isNonNounVerbOrAdjective(wordClasses) { - let isVerbOrAdjective = false; - let isSuruVerb = false; - let isNoun = false; - for (const wordClass of wordClasses) { - switch (wordClass) { - case 'v1': - case 'v5': - case 'vk': - case 'vz': - case 'adj-i': - isVerbOrAdjective = true; - break; - case 'vs': - isVerbOrAdjective = true; - isSuruVerb = true; - break; - case 'n': - isNoun = true; - break; - } - } - return isVerbOrAdjective && !(isSuruVerb && isNoun); - } - - // Private - - static _createFrequencyGroupsFromMap(map) { - const results = []; - for (const [dictionary, map2] of map.entries()) { - const frequencies = []; - for (const frequencyData of map2.values()) { - frequencyData.values = [...frequencyData.values]; - frequencies.push(frequencyData); - } - results.push({dictionary, frequencies}); - } - return results; - } - - static _findExistingPitchAccentInfo(reading, position, nasalPositions, devoicePositions, tags, pitchAccentInfoList) { - for (const pitchInfo of pitchAccentInfoList) { - if ( - pitchInfo.reading === reading && - pitchInfo.position === position && - this._areArraysEqual(pitchInfo.nasalPositions, nasalPositions) && - this._areArraysEqual(pitchInfo.devoicePositions, devoicePositions) && - this._areTagListsEqual(pitchInfo.tags, tags) - ) { - return pitchInfo; - } - } - return null; - } - - static _areArraysEqual(array1, array2) { - const ii = array1.length; - if (ii !== array2.length) { return false; } - for (let i = 0; i < ii; ++i) { - if (array1[i] !== array2[i]) { return false; } - } - return true; - } - - static _areTagListsEqual(tagList1, tagList2) { - const ii = tagList1.length; - if (tagList2.length !== ii) { return false; } - - for (let i = 0; i < ii; ++i) { - const tag1 = tagList1[i]; - const tag2 = tagList2[i]; - if (tag1.name !== tag2.name || tag1.dictionary !== tag2.dictionary) { - return false; - } - } - - return true; - } - - static _areSetsEqual(set1, set2) { - if (set1.size !== set2.size) { - return false; - } - - for (const value of set1) { - if (!set2.has(value)) { - return false; - } - } - - return true; - } - - static _getSetIntersection(set1, set2) { - const result = []; - for (const value of set1) { - if (set2.has(value)) { - result.push(value); - } - } - return result; - } - - static _createMapKey(array) { - return JSON.stringify(array); - } -} diff --git a/ext/js/language/japanese-util.js b/ext/js/language/japanese-util.js deleted file mode 100644 index c7f79751..00000000 --- a/ext/js/language/japanese-util.js +++ /dev/null @@ -1,716 +0,0 @@ -/* - * Copyright (C) 2020-2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -const JapaneseUtil = (() => { - const ITERATION_MARK_CODE_POINT = 0x3005; - const HIRAGANA_SMALL_TSU_CODE_POINT = 0x3063; - const KATAKANA_SMALL_TSU_CODE_POINT = 0x30c3; - const KATAKANA_SMALL_KA_CODE_POINT = 0x30f5; - const KATAKANA_SMALL_KE_CODE_POINT = 0x30f6; - const KANA_PROLONGED_SOUND_MARK_CODE_POINT = 0x30fc; - - const HIRAGANA_RANGE = [0x3040, 0x309f]; - const KATAKANA_RANGE = [0x30a0, 0x30ff]; - - const HIRAGANA_CONVERSION_RANGE = [0x3041, 0x3096]; - const KATAKANA_CONVERSION_RANGE = [0x30a1, 0x30f6]; - - const KANA_RANGES = [HIRAGANA_RANGE, KATAKANA_RANGE]; - - const CJK_UNIFIED_IDEOGRAPHS_RANGE = [0x4e00, 0x9fff]; - const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A_RANGE = [0x3400, 0x4dbf]; - const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B_RANGE = [0x20000, 0x2a6df]; - const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C_RANGE = [0x2a700, 0x2b73f]; - const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D_RANGE = [0x2b740, 0x2b81f]; - const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_E_RANGE = [0x2b820, 0x2ceaf]; - const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_F_RANGE = [0x2ceb0, 0x2ebef]; - const CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT_RANGE = [0x2f800, 0x2fa1f]; - const CJK_UNIFIED_IDEOGRAPHS_RANGES = [ - CJK_UNIFIED_IDEOGRAPHS_RANGE, - CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A_RANGE, - CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B_RANGE, - CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C_RANGE, - CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D_RANGE, - CJK_UNIFIED_IDEOGRAPHS_EXTENSION_E_RANGE, - CJK_UNIFIED_IDEOGRAPHS_EXTENSION_F_RANGE, - CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT_RANGE - ]; - - // Japanese character ranges, roughly ordered in order of expected frequency - const JAPANESE_RANGES = [ - HIRAGANA_RANGE, - KATAKANA_RANGE, - - ...CJK_UNIFIED_IDEOGRAPHS_RANGES, - - [0xff66, 0xff9f], // Halfwidth katakana - - [0x30fb, 0x30fc], // Katakana punctuation - [0xff61, 0xff65], // Kana punctuation - [0x3000, 0x303f], // CJK punctuation - - [0xff10, 0xff19], // Fullwidth numbers - [0xff21, 0xff3a], // Fullwidth upper case Latin letters - [0xff41, 0xff5a], // Fullwidth lower case Latin letters - - [0xff01, 0xff0f], // Fullwidth punctuation 1 - [0xff1a, 0xff1f], // Fullwidth punctuation 2 - [0xff3b, 0xff3f], // Fullwidth punctuation 3 - [0xff5b, 0xff60], // Fullwidth punctuation 4 - [0xffe0, 0xffee] // Currency markers - ]; - - const SMALL_KANA_SET = new Set(Array.from('ぁぃぅぇぉゃゅょゎァィゥェォャュョヮ')); - - const HALFWIDTH_KATAKANA_MAPPING = new Map([ - ['ヲ', 'ヲヺ-'], - ['ァ', 'ァ--'], - ['ィ', 'ィ--'], - ['ゥ', 'ゥ--'], - ['ェ', 'ェ--'], - ['ォ', 'ォ--'], - ['ャ', 'ャ--'], - ['ュ', 'ュ--'], - ['ョ', 'ョ--'], - ['ッ', 'ッ--'], - ['ー', 'ー--'], - ['ア', 'ア--'], - ['イ', 'イ--'], - ['ウ', 'ウヴ-'], - ['エ', 'エ--'], - ['オ', 'オ--'], - ['カ', 'カガ-'], - ['キ', 'キギ-'], - ['ク', 'クグ-'], - ['ケ', 'ケゲ-'], - ['コ', 'コゴ-'], - ['サ', 'サザ-'], - ['シ', 'シジ-'], - ['ス', 'スズ-'], - ['セ', 'セゼ-'], - ['ソ', 'ソゾ-'], - ['タ', 'タダ-'], - ['チ', 'チヂ-'], - ['ツ', 'ツヅ-'], - ['テ', 'テデ-'], - ['ト', 'トド-'], - ['ナ', 'ナ--'], - ['ニ', 'ニ--'], - ['ヌ', 'ヌ--'], - ['ネ', 'ネ--'], - ['ノ', 'ノ--'], - ['ハ', 'ハバパ'], - ['ヒ', 'ヒビピ'], - ['フ', 'フブプ'], - ['ヘ', 'ヘベペ'], - ['ホ', 'ホボポ'], - ['マ', 'マ--'], - ['ミ', 'ミ--'], - ['ム', 'ム--'], - ['メ', 'メ--'], - ['モ', 'モ--'], - ['ヤ', 'ヤ--'], - ['ユ', 'ユ--'], - ['ヨ', 'ヨ--'], - ['ラ', 'ラ--'], - ['リ', 'リ--'], - ['ル', 'ル--'], - ['レ', 'レ--'], - ['ロ', 'ロ--'], - ['ワ', 'ワ--'], - ['ン', 'ン--'] - ]); - - const VOWEL_TO_KANA_MAPPING = new Map([ - ['a', 'ぁあかがさざただなはばぱまゃやらゎわヵァアカガサザタダナハバパマャヤラヮワヵヷ'], - ['i', 'ぃいきぎしじちぢにひびぴみりゐィイキギシジチヂニヒビピミリヰヸ'], - ['u', 'ぅうくぐすずっつづぬふぶぷむゅゆるゥウクグスズッツヅヌフブプムュユルヴ'], - ['e', 'ぇえけげせぜてでねへべぺめれゑヶェエケゲセゼテデネヘベペメレヱヶヹ'], - ['o', 'ぉおこごそぞとどのほぼぽもょよろをォオコゴソゾトドノホボポモョヨロヲヺ'], - ['', 'のノ'] - ]); - - const KANA_TO_VOWEL_MAPPING = (() => { - const map = new Map(); - for (const [vowel, characters] of VOWEL_TO_KANA_MAPPING) { - for (const character of characters) { - map.set(character, vowel); - } - } - return map; - })(); - - const DIACRITIC_MAPPING = (() => { - const kana = 'うゔ-かが-きぎ-くぐ-けげ-こご-さざ-しじ-すず-せぜ-そぞ-ただ-ちぢ-つづ-てで-とど-はばぱひびぴふぶぷへべぺほぼぽワヷ-ヰヸ-ウヴ-ヱヹ-ヲヺ-カガ-キギ-クグ-ケゲ-コゴ-サザ-シジ-スズ-セゼ-ソゾ-タダ-チヂ-ツヅ-テデ-トド-ハバパヒビピフブプヘベペホボポ'; - const map = new Map(); - for (let i = 0, ii = kana.length; i < ii; i += 3) { - const character = kana[i]; - const dakuten = kana[i + 1]; - const handakuten = kana[i + 2]; - map.set(dakuten, {character, type: 'dakuten'}); - if (handakuten !== '-') { - map.set(handakuten, {character, type: 'handakuten'}); - } - } - return map; - })(); - - - function isCodePointInRange(codePoint, [min, max]) { - return (codePoint >= min && codePoint <= max); - } - - function isCodePointInRanges(codePoint, ranges) { - for (const [min, max] of ranges) { - if (codePoint >= min && codePoint <= max) { - return true; - } - } - return false; - } - - function getProlongedHiragana(previousCharacter) { - switch (KANA_TO_VOWEL_MAPPING.get(previousCharacter)) { - case 'a': return 'あ'; - case 'i': return 'い'; - case 'u': return 'う'; - case 'e': return 'え'; - case 'o': return 'う'; - default: return null; - } - } - - - // eslint-disable-next-line no-shadow - class JapaneseUtil { - constructor(wanakana=null) { - this._wanakana = wanakana; - } - - // Character code testing functions - - isCodePointKanji(codePoint) { - return isCodePointInRanges(codePoint, CJK_UNIFIED_IDEOGRAPHS_RANGES); - } - - isCodePointKana(codePoint) { - return isCodePointInRanges(codePoint, KANA_RANGES); - } - - isCodePointJapanese(codePoint) { - return isCodePointInRanges(codePoint, JAPANESE_RANGES); - } - - // String testing functions - - isStringEntirelyKana(str) { - if (str.length === 0) { return false; } - for (const c of str) { - if (!isCodePointInRanges(c.codePointAt(0), KANA_RANGES)) { - return false; - } - } - return true; - } - - isStringPartiallyJapanese(str) { - if (str.length === 0) { return false; } - for (const c of str) { - if (isCodePointInRanges(c.codePointAt(0), JAPANESE_RANGES)) { - return true; - } - } - return false; - } - - // Mora functions - - isMoraPitchHigh(moraIndex, pitchAccentPosition) { - switch (pitchAccentPosition) { - case 0: return (moraIndex > 0); - case 1: return (moraIndex < 1); - default: return (moraIndex > 0 && moraIndex < pitchAccentPosition); - } - } - - getPitchCategory(text, pitchAccentPosition, isVerbOrAdjective) { - if (pitchAccentPosition === 0) { - return 'heiban'; - } - if (isVerbOrAdjective) { - return pitchAccentPosition > 0 ? 'kifuku' : null; - } - if (pitchAccentPosition === 1) { - return 'atamadaka'; - } - if (pitchAccentPosition > 1) { - return pitchAccentPosition >= this.getKanaMoraCount(text) ? 'odaka' : 'nakadaka'; - } - return null; - } - - getKanaMorae(text) { - const morae = []; - let i; - for (const c of text) { - if (SMALL_KANA_SET.has(c) && (i = morae.length) > 0) { - morae[i - 1] += c; - } else { - morae.push(c); - } - } - return morae; - } - - getKanaMoraCount(text) { - let moraCount = 0; - for (const c of text) { - if (!(SMALL_KANA_SET.has(c) && moraCount > 0)) { - ++moraCount; - } - } - return moraCount; - } - - // Conversion functions - - convertToKana(text) { - return this._getWanakana().toKana(text); - } - - convertToKanaSupported() { - return this._wanakana !== null; - } - - convertKatakanaToHiragana(text) { - let result = ''; - const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]); - for (let char of text) { - const codePoint = char.codePointAt(0); - if (codePoint === KATAKANA_SMALL_KA_CODE_POINT || codePoint === KATAKANA_SMALL_KE_CODE_POINT) { - // No change - } else if (codePoint === KANA_PROLONGED_SOUND_MARK_CODE_POINT) { - if (result.length > 0) { - const char2 = getProlongedHiragana(result[result.length - 1]); - if (char2 !== null) { char = char2; } - } - } else if (isCodePointInRange(codePoint, KATAKANA_CONVERSION_RANGE)) { - char = String.fromCodePoint(codePoint + offset); - } - result += char; - } - return result; - } - - convertHiraganaToKatakana(text) { - let result = ''; - const offset = (KATAKANA_CONVERSION_RANGE[0] - HIRAGANA_CONVERSION_RANGE[0]); - for (let char of text) { - const codePoint = char.codePointAt(0); - if (isCodePointInRange(codePoint, HIRAGANA_CONVERSION_RANGE)) { - char = String.fromCodePoint(codePoint + offset); - } - result += char; - } - return result; - } - - convertToRomaji(text) { - const wanakana = this._getWanakana(); - return wanakana.toRomaji(text); - } - - convertToRomajiSupported() { - return this._wanakana !== null; - } - - convertNumericToFullWidth(text) { - let result = ''; - for (const char of text) { - let c = char.codePointAt(0); - if (c >= 0x30 && c <= 0x39) { // ['0', '9'] - c += 0xff10 - 0x30; // 0xff10 = '0' full width - result += String.fromCodePoint(c); - } else { - result += char; - } - } - return result; - } - - convertHalfWidthKanaToFullWidth(text, sourceMap=null) { - let result = ''; - - // This function is safe to use charCodeAt instead of codePointAt, since all - // the relevant characters are represented with a single UTF-16 character code. - for (let i = 0, ii = text.length; i < ii; ++i) { - const c = text[i]; - const mapping = HALFWIDTH_KATAKANA_MAPPING.get(c); - if (typeof mapping !== 'string') { - result += c; - continue; - } - - let index = 0; - switch (text.charCodeAt(i + 1)) { - case 0xff9e: // dakuten - index = 1; - break; - case 0xff9f: // handakuten - index = 2; - break; - } - - let c2 = mapping[index]; - if (index > 0) { - if (c2 === '-') { // invalid - index = 0; - c2 = mapping[0]; - } else { - ++i; - } - } - - if (sourceMap !== null && index > 0) { - sourceMap.combine(result.length, 1); - } - result += c2; - } - - return result; - } - - convertAlphabeticToKana(text, sourceMap=null) { - let part = ''; - let result = ''; - - for (const char of text) { - // Note: 0x61 is the character code for 'a' - let c = char.codePointAt(0); - if (c >= 0x41 && c <= 0x5a) { // ['A', 'Z'] - c += (0x61 - 0x41); - } else if (c >= 0x61 && c <= 0x7a) { // ['a', 'z'] - // NOP; c += (0x61 - 0x61); - } else if (c >= 0xff21 && c <= 0xff3a) { // ['A', 'Z'] fullwidth - c += (0x61 - 0xff21); - } else if (c >= 0xff41 && c <= 0xff5a) { // ['a', 'z'] fullwidth - c += (0x61 - 0xff41); - } else if (c === 0x2d || c === 0xff0d) { // '-' or fullwidth dash - c = 0x2d; // '-' - } else { - if (part.length > 0) { - result += this._convertAlphabeticPartToKana(part, sourceMap, result.length); - part = ''; - } - result += char; - continue; - } - part += String.fromCodePoint(c); - } - - if (part.length > 0) { - result += this._convertAlphabeticPartToKana(part, sourceMap, result.length); - } - return result; - } - - convertAlphabeticToKanaSupported() { - return this._wanakana !== null; - } - - getKanaDiacriticInfo(character) { - const info = DIACRITIC_MAPPING.get(character); - return typeof info !== 'undefined' ? {character: info.character, type: info.type} : null; - } - - // Furigana distribution - - distributeFurigana(term, reading) { - if (reading === term) { - // Same - return [this._createFuriganaSegment(term, '')]; - } - - const groups = []; - let groupPre = null; - let isKanaPre = null; - for (const c of term) { - const codePoint = c.codePointAt(0); - const isKana = !(this.isCodePointKanji(codePoint) || codePoint === ITERATION_MARK_CODE_POINT); - if (isKana === isKanaPre) { - groupPre.text += c; - } else { - groupPre = {isKana, text: c, textNormalized: null}; - groups.push(groupPre); - isKanaPre = isKana; - } - } - for (const group of groups) { - if (group.isKana) { - group.textNormalized = this.convertKatakanaToHiragana(group.text); - } - } - - const readingNormalized = this.convertKatakanaToHiragana(reading); - const segments = this._segmentizeFurigana(reading, readingNormalized, groups, 0); - if (segments !== null) { - return segments; - } - - // Fallback - return [this._createFuriganaSegment(term, reading)]; - } - - distributeFuriganaInflected(term, reading, source) { - const termNormalized = this.convertKatakanaToHiragana(term); - const readingNormalized = this.convertKatakanaToHiragana(reading); - const sourceNormalized = this.convertKatakanaToHiragana(source); - - let mainText = term; - let stemLength = this._getStemLength(termNormalized, sourceNormalized); - - // Check if source is derived from the reading instead of the term - const readingStemLength = this._getStemLength(readingNormalized, sourceNormalized); - if (readingStemLength > 0 && readingStemLength >= stemLength) { - mainText = reading; - stemLength = readingStemLength; - reading = `${source.substring(0, stemLength)}${reading.substring(stemLength)}`; - } - - const segments = []; - if (stemLength > 0) { - mainText = `${source.substring(0, stemLength)}${mainText.substring(stemLength)}`; - const segments2 = this.distributeFurigana(mainText, reading); - let consumed = 0; - for (const segment of segments2) { - const {text} = segment; - const start = consumed; - consumed += text.length; - if (consumed < stemLength) { - segments.push(segment); - } else if (consumed === stemLength) { - segments.push(segment); - break; - } else { - if (start < stemLength) { - segments.push(this._createFuriganaSegment(mainText.substring(start, stemLength), '')); - } - break; - } - } - } - - if (stemLength < source.length) { - const remainder = source.substring(stemLength); - const segmentCount = segments.length; - if (segmentCount > 0 && segments[segmentCount - 1].reading.length === 0) { - // Append to the last segment if it has an empty reading - segments[segmentCount - 1].text += remainder; - } else { - // Otherwise, create a new segment - segments.push(this._createFuriganaSegment(remainder, '')); - } - } - - return segments; - } - - // Miscellaneous - - collapseEmphaticSequences(text, fullCollapse, sourceMap=null) { - let result = ''; - let collapseCodePoint = -1; - const hasSourceMap = (sourceMap !== null); - for (const char of text) { - const c = char.codePointAt(0); - if ( - c === HIRAGANA_SMALL_TSU_CODE_POINT || - c === KATAKANA_SMALL_TSU_CODE_POINT || - c === KANA_PROLONGED_SOUND_MARK_CODE_POINT - ) { - if (collapseCodePoint !== c) { - collapseCodePoint = c; - if (!fullCollapse) { - result += char; - continue; - } - } - } else { - collapseCodePoint = -1; - result += char; - continue; - } - - if (hasSourceMap) { - sourceMap.combine(Math.max(0, result.length - 1), 1); - } - } - return result; - } - - // Private - - _createFuriganaSegment(text, reading) { - return {text, reading}; - } - - _segmentizeFurigana(reading, readingNormalized, groups, groupsStart) { - const groupCount = groups.length - groupsStart; - if (groupCount <= 0) { - return reading.length === 0 ? [] : null; - } - - const group = groups[groupsStart]; - const {isKana, text} = group; - const textLength = text.length; - if (isKana) { - const {textNormalized} = group; - if (readingNormalized.startsWith(textNormalized)) { - const segments = this._segmentizeFurigana( - reading.substring(textLength), - readingNormalized.substring(textLength), - groups, - groupsStart + 1 - ); - if (segments !== null) { - if (reading.startsWith(text)) { - segments.unshift(this._createFuriganaSegment(text, '')); - } else { - segments.unshift(...this._getFuriganaKanaSegments(text, reading)); - } - return segments; - } - } - return null; - } else { - let result = null; - for (let i = reading.length; i >= textLength; --i) { - const segments = this._segmentizeFurigana( - reading.substring(i), - readingNormalized.substring(i), - groups, - groupsStart + 1 - ); - if (segments !== null) { - if (result !== null) { - // More than one way to segmentize the tail; mark as ambiguous - return null; - } - const segmentReading = reading.substring(0, i); - segments.unshift(this._createFuriganaSegment(text, segmentReading)); - result = segments; - } - // There is only one way to segmentize the last non-kana group - if (groupCount === 1) { - break; - } - } - return result; - } - } - - _getFuriganaKanaSegments(text, reading) { - const textLength = text.length; - const newSegments = []; - let start = 0; - let state = (reading[0] === text[0]); - for (let i = 1; i < textLength; ++i) { - const newState = (reading[i] === text[i]); - if (state === newState) { continue; } - newSegments.push(this._createFuriganaSegment(text.substring(start, i), state ? '' : reading.substring(start, i))); - state = newState; - start = i; - } - newSegments.push(this._createFuriganaSegment(text.substring(start, textLength), state ? '' : reading.substring(start, textLength))); - return newSegments; - } - - _getWanakana() { - const wanakana = this._wanakana; - if (wanakana === null) { throw new Error('Functions which use WanaKana are not supported in this context'); } - return wanakana; - } - - _convertAlphabeticPartToKana(text, sourceMap, sourceMapStart) { - const wanakana = this._getWanakana(); - const result = wanakana.toHiragana(text); - - // Generate source mapping - if (sourceMap !== null) { - let i = 0; - let resultPos = 0; - const ii = text.length; - while (i < ii) { - // Find smallest matching substring - let iNext = i + 1; - let resultPosNext = result.length; - while (iNext < ii) { - const t = wanakana.toHiragana(text.substring(0, iNext)); - if (t === result.substring(0, t.length)) { - resultPosNext = t.length; - break; - } - ++iNext; - } - - // Merge characters - const removals = iNext - i - 1; - if (removals > 0) { - sourceMap.combine(sourceMapStart, removals); - } - ++sourceMapStart; - - // Empty elements - const additions = resultPosNext - resultPos - 1; - for (let j = 0; j < additions; ++j) { - sourceMap.insert(sourceMapStart, 0); - ++sourceMapStart; - } - - i = iNext; - resultPos = resultPosNext; - } - } - - return result; - } - - _getStemLength(text1, text2) { - const minLength = Math.min(text1.length, text2.length); - if (minLength === 0) { return 0; } - - let i = 0; - while (true) { - const char1 = text1.codePointAt(i); - const char2 = text2.codePointAt(i); - if (char1 !== char2) { break; } - const charLength = String.fromCodePoint(char1).length; - i += charLength; - if (i >= minLength) { - if (i > minLength) { - i -= charLength; // Don't consume partial UTF16 surrogate characters - } - break; - } - } - return i; - } - } - - - return JapaneseUtil; -})(); diff --git a/ext/js/language/sandbox/dictionary-data-util.js b/ext/js/language/sandbox/dictionary-data-util.js new file mode 100644 index 00000000..951e10ff --- /dev/null +++ b/ext/js/language/sandbox/dictionary-data-util.js @@ -0,0 +1,299 @@ +/* + * Copyright (C) 2020-2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +class DictionaryDataUtil { + static groupTermTags(dictionaryEntry) { + const {headwords} = dictionaryEntry; + const headwordCount = headwords.length; + const uniqueCheck = (headwordCount > 1); + const resultsIndexMap = new Map(); + const results = []; + for (let i = 0; i < headwordCount; ++i) { + const {tags} = headwords[i]; + for (const tag of tags) { + if (uniqueCheck) { + const {name, category, notes, dictionary} = tag; + const key = this._createMapKey([name, category, notes, dictionary]); + const index = resultsIndexMap.get(key); + if (typeof index !== 'undefined') { + const existingItem = results[index]; + existingItem.headwordIndices.push(i); + continue; + } + resultsIndexMap.set(key, results.length); + } + + const item = {tag, headwordIndices: [i]}; + results.push(item); + } + } + return results; + } + + static groupTermFrequencies(dictionaryEntry) { + const {headwords, frequencies} = dictionaryEntry; + + const map1 = new Map(); + for (const {headwordIndex, dictionary, hasReading, frequency} of frequencies) { + const {term, reading} = headwords[headwordIndex]; + + let map2 = map1.get(dictionary); + if (typeof map2 === 'undefined') { + map2 = new Map(); + map1.set(dictionary, map2); + } + + const readingKey = hasReading ? reading : null; + const key = this._createMapKey([term, readingKey]); + let frequencyData = map2.get(key); + if (typeof frequencyData === 'undefined') { + frequencyData = {term, reading: readingKey, values: new Set()}; + map2.set(key, frequencyData); + } + + frequencyData.values.add(frequency); + } + return this._createFrequencyGroupsFromMap(map1); + } + + static groupKanjiFrequencies(frequencies) { + const map1 = new Map(); + for (const {dictionary, character, frequency} of frequencies) { + let map2 = map1.get(dictionary); + if (typeof map2 === 'undefined') { + map2 = new Map(); + map1.set(dictionary, map2); + } + + let frequencyData = map2.get(character); + if (typeof frequencyData === 'undefined') { + frequencyData = {character, values: new Set()}; + map2.set(character, frequencyData); + } + + frequencyData.values.add(frequency); + } + return this._createFrequencyGroupsFromMap(map1); + } + + static getPitchAccentInfos(dictionaryEntry) { + const {headwords, pronunciations} = dictionaryEntry; + + const allTerms = new Set(); + const allReadings = new Set(); + for (const {term, reading} of headwords) { + allTerms.add(term); + allReadings.add(reading); + } + + const pitchAccentInfoMap = new Map(); + for (const {headwordIndex, dictionary, pitches} of pronunciations) { + const {term, reading} = headwords[headwordIndex]; + let dictionaryPitchAccentInfoList = pitchAccentInfoMap.get(dictionary); + if (typeof dictionaryPitchAccentInfoList === 'undefined') { + dictionaryPitchAccentInfoList = []; + pitchAccentInfoMap.set(dictionary, dictionaryPitchAccentInfoList); + } + for (const {position, nasalPositions, devoicePositions, tags} of pitches) { + let pitchAccentInfo = this._findExistingPitchAccentInfo(reading, position, nasalPositions, devoicePositions, tags, dictionaryPitchAccentInfoList); + if (pitchAccentInfo === null) { + pitchAccentInfo = { + terms: new Set(), + reading, + position, + nasalPositions, + devoicePositions, + tags, + exclusiveTerms: [], + exclusiveReadings: [] + }; + dictionaryPitchAccentInfoList.push(pitchAccentInfo); + } + pitchAccentInfo.terms.add(term); + } + } + + const multipleReadings = (allReadings.size > 1); + for (const dictionaryPitchAccentInfoList of pitchAccentInfoMap.values()) { + for (const pitchAccentInfo of dictionaryPitchAccentInfoList) { + const {terms, reading, exclusiveTerms, exclusiveReadings} = pitchAccentInfo; + if (!this._areSetsEqual(terms, allTerms)) { + exclusiveTerms.push(...this._getSetIntersection(terms, allTerms)); + } + if (multipleReadings) { + exclusiveReadings.push(reading); + } + pitchAccentInfo.terms = [...terms]; + } + } + + const results2 = []; + for (const [dictionary, pitches] of pitchAccentInfoMap.entries()) { + results2.push({dictionary, pitches}); + } + return results2; + } + + static getTermFrequency(termTags) { + let totalScore = 0; + for (const {score} of termTags) { + totalScore += score; + } + if (totalScore > 0) { + return 'popular'; + } else if (totalScore < 0) { + return 'rare'; + } else { + return 'normal'; + } + } + + static getDisambiguations(headwords, headwordIndices, allTermsSet, allReadingsSet) { + if (allTermsSet.size <= 1 && allReadingsSet.size <= 1) { return []; } + + const terms = new Set(); + const readings = new Set(); + for (const headwordIndex of headwordIndices) { + const {term, reading} = headwords[headwordIndex]; + terms.add(term); + readings.add(reading); + } + + const disambiguations = []; + const addTerms = !this._areSetsEqual(terms, allTermsSet); + const addReadings = !this._areSetsEqual(readings, allReadingsSet); + if (addTerms) { + disambiguations.push(...this._getSetIntersection(terms, allTermsSet)); + } + if (addReadings) { + if (addTerms) { + for (const term of terms) { + readings.delete(term); + } + } + disambiguations.push(...this._getSetIntersection(readings, allReadingsSet)); + } + return disambiguations; + } + + static isNonNounVerbOrAdjective(wordClasses) { + let isVerbOrAdjective = false; + let isSuruVerb = false; + let isNoun = false; + for (const wordClass of wordClasses) { + switch (wordClass) { + case 'v1': + case 'v5': + case 'vk': + case 'vz': + case 'adj-i': + isVerbOrAdjective = true; + break; + case 'vs': + isVerbOrAdjective = true; + isSuruVerb = true; + break; + case 'n': + isNoun = true; + break; + } + } + return isVerbOrAdjective && !(isSuruVerb && isNoun); + } + + // Private + + static _createFrequencyGroupsFromMap(map) { + const results = []; + for (const [dictionary, map2] of map.entries()) { + const frequencies = []; + for (const frequencyData of map2.values()) { + frequencyData.values = [...frequencyData.values]; + frequencies.push(frequencyData); + } + results.push({dictionary, frequencies}); + } + return results; + } + + static _findExistingPitchAccentInfo(reading, position, nasalPositions, devoicePositions, tags, pitchAccentInfoList) { + for (const pitchInfo of pitchAccentInfoList) { + if ( + pitchInfo.reading === reading && + pitchInfo.position === position && + this._areArraysEqual(pitchInfo.nasalPositions, nasalPositions) && + this._areArraysEqual(pitchInfo.devoicePositions, devoicePositions) && + this._areTagListsEqual(pitchInfo.tags, tags) + ) { + return pitchInfo; + } + } + return null; + } + + static _areArraysEqual(array1, array2) { + const ii = array1.length; + if (ii !== array2.length) { return false; } + for (let i = 0; i < ii; ++i) { + if (array1[i] !== array2[i]) { return false; } + } + return true; + } + + static _areTagListsEqual(tagList1, tagList2) { + const ii = tagList1.length; + if (tagList2.length !== ii) { return false; } + + for (let i = 0; i < ii; ++i) { + const tag1 = tagList1[i]; + const tag2 = tagList2[i]; + if (tag1.name !== tag2.name || tag1.dictionary !== tag2.dictionary) { + return false; + } + } + + return true; + } + + static _areSetsEqual(set1, set2) { + if (set1.size !== set2.size) { + return false; + } + + for (const value of set1) { + if (!set2.has(value)) { + return false; + } + } + + return true; + } + + static _getSetIntersection(set1, set2) { + const result = []; + for (const value of set1) { + if (set2.has(value)) { + result.push(value); + } + } + return result; + } + + static _createMapKey(array) { + return JSON.stringify(array); + } +} diff --git a/ext/js/language/sandbox/japanese-util.js b/ext/js/language/sandbox/japanese-util.js new file mode 100644 index 00000000..c7f79751 --- /dev/null +++ b/ext/js/language/sandbox/japanese-util.js @@ -0,0 +1,716 @@ +/* + * Copyright (C) 2020-2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +const JapaneseUtil = (() => { + const ITERATION_MARK_CODE_POINT = 0x3005; + const HIRAGANA_SMALL_TSU_CODE_POINT = 0x3063; + const KATAKANA_SMALL_TSU_CODE_POINT = 0x30c3; + const KATAKANA_SMALL_KA_CODE_POINT = 0x30f5; + const KATAKANA_SMALL_KE_CODE_POINT = 0x30f6; + const KANA_PROLONGED_SOUND_MARK_CODE_POINT = 0x30fc; + + const HIRAGANA_RANGE = [0x3040, 0x309f]; + const KATAKANA_RANGE = [0x30a0, 0x30ff]; + + const HIRAGANA_CONVERSION_RANGE = [0x3041, 0x3096]; + const KATAKANA_CONVERSION_RANGE = [0x30a1, 0x30f6]; + + const KANA_RANGES = [HIRAGANA_RANGE, KATAKANA_RANGE]; + + const CJK_UNIFIED_IDEOGRAPHS_RANGE = [0x4e00, 0x9fff]; + const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A_RANGE = [0x3400, 0x4dbf]; + const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B_RANGE = [0x20000, 0x2a6df]; + const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C_RANGE = [0x2a700, 0x2b73f]; + const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D_RANGE = [0x2b740, 0x2b81f]; + const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_E_RANGE = [0x2b820, 0x2ceaf]; + const CJK_UNIFIED_IDEOGRAPHS_EXTENSION_F_RANGE = [0x2ceb0, 0x2ebef]; + const CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT_RANGE = [0x2f800, 0x2fa1f]; + const CJK_UNIFIED_IDEOGRAPHS_RANGES = [ + CJK_UNIFIED_IDEOGRAPHS_RANGE, + CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A_RANGE, + CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B_RANGE, + CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C_RANGE, + CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D_RANGE, + CJK_UNIFIED_IDEOGRAPHS_EXTENSION_E_RANGE, + CJK_UNIFIED_IDEOGRAPHS_EXTENSION_F_RANGE, + CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT_RANGE + ]; + + // Japanese character ranges, roughly ordered in order of expected frequency + const JAPANESE_RANGES = [ + HIRAGANA_RANGE, + KATAKANA_RANGE, + + ...CJK_UNIFIED_IDEOGRAPHS_RANGES, + + [0xff66, 0xff9f], // Halfwidth katakana + + [0x30fb, 0x30fc], // Katakana punctuation + [0xff61, 0xff65], // Kana punctuation + [0x3000, 0x303f], // CJK punctuation + + [0xff10, 0xff19], // Fullwidth numbers + [0xff21, 0xff3a], // Fullwidth upper case Latin letters + [0xff41, 0xff5a], // Fullwidth lower case Latin letters + + [0xff01, 0xff0f], // Fullwidth punctuation 1 + [0xff1a, 0xff1f], // Fullwidth punctuation 2 + [0xff3b, 0xff3f], // Fullwidth punctuation 3 + [0xff5b, 0xff60], // Fullwidth punctuation 4 + [0xffe0, 0xffee] // Currency markers + ]; + + const SMALL_KANA_SET = new Set(Array.from('ぁぃぅぇぉゃゅょゎァィゥェォャュョヮ')); + + const HALFWIDTH_KATAKANA_MAPPING = new Map([ + ['ヲ', 'ヲヺ-'], + ['ァ', 'ァ--'], + ['ィ', 'ィ--'], + ['ゥ', 'ゥ--'], + ['ェ', 'ェ--'], + ['ォ', 'ォ--'], + ['ャ', 'ャ--'], + ['ュ', 'ュ--'], + ['ョ', 'ョ--'], + ['ッ', 'ッ--'], + ['ー', 'ー--'], + ['ア', 'ア--'], + ['イ', 'イ--'], + ['ウ', 'ウヴ-'], + ['エ', 'エ--'], + ['オ', 'オ--'], + ['カ', 'カガ-'], + ['キ', 'キギ-'], + ['ク', 'クグ-'], + ['ケ', 'ケゲ-'], + ['コ', 'コゴ-'], + ['サ', 'サザ-'], + ['シ', 'シジ-'], + ['ス', 'スズ-'], + ['セ', 'セゼ-'], + ['ソ', 'ソゾ-'], + ['タ', 'タダ-'], + ['チ', 'チヂ-'], + ['ツ', 'ツヅ-'], + ['テ', 'テデ-'], + ['ト', 'トド-'], + ['ナ', 'ナ--'], + ['ニ', 'ニ--'], + ['ヌ', 'ヌ--'], + ['ネ', 'ネ--'], + ['ノ', 'ノ--'], + ['ハ', 'ハバパ'], + ['ヒ', 'ヒビピ'], + ['フ', 'フブプ'], + ['ヘ', 'ヘベペ'], + ['ホ', 'ホボポ'], + ['マ', 'マ--'], + ['ミ', 'ミ--'], + ['ム', 'ム--'], + ['メ', 'メ--'], + ['モ', 'モ--'], + ['ヤ', 'ヤ--'], + ['ユ', 'ユ--'], + ['ヨ', 'ヨ--'], + ['ラ', 'ラ--'], + ['リ', 'リ--'], + ['ル', 'ル--'], + ['レ', 'レ--'], + ['ロ', 'ロ--'], + ['ワ', 'ワ--'], + ['ン', 'ン--'] + ]); + + const VOWEL_TO_KANA_MAPPING = new Map([ + ['a', 'ぁあかがさざただなはばぱまゃやらゎわヵァアカガサザタダナハバパマャヤラヮワヵヷ'], + ['i', 'ぃいきぎしじちぢにひびぴみりゐィイキギシジチヂニヒビピミリヰヸ'], + ['u', 'ぅうくぐすずっつづぬふぶぷむゅゆるゥウクグスズッツヅヌフブプムュユルヴ'], + ['e', 'ぇえけげせぜてでねへべぺめれゑヶェエケゲセゼテデネヘベペメレヱヶヹ'], + ['o', 'ぉおこごそぞとどのほぼぽもょよろをォオコゴソゾトドノホボポモョヨロヲヺ'], + ['', 'のノ'] + ]); + + const KANA_TO_VOWEL_MAPPING = (() => { + const map = new Map(); + for (const [vowel, characters] of VOWEL_TO_KANA_MAPPING) { + for (const character of characters) { + map.set(character, vowel); + } + } + return map; + })(); + + const DIACRITIC_MAPPING = (() => { + const kana = 'うゔ-かが-きぎ-くぐ-けげ-こご-さざ-しじ-すず-せぜ-そぞ-ただ-ちぢ-つづ-てで-とど-はばぱひびぴふぶぷへべぺほぼぽワヷ-ヰヸ-ウヴ-ヱヹ-ヲヺ-カガ-キギ-クグ-ケゲ-コゴ-サザ-シジ-スズ-セゼ-ソゾ-タダ-チヂ-ツヅ-テデ-トド-ハバパヒビピフブプヘベペホボポ'; + const map = new Map(); + for (let i = 0, ii = kana.length; i < ii; i += 3) { + const character = kana[i]; + const dakuten = kana[i + 1]; + const handakuten = kana[i + 2]; + map.set(dakuten, {character, type: 'dakuten'}); + if (handakuten !== '-') { + map.set(handakuten, {character, type: 'handakuten'}); + } + } + return map; + })(); + + + function isCodePointInRange(codePoint, [min, max]) { + return (codePoint >= min && codePoint <= max); + } + + function isCodePointInRanges(codePoint, ranges) { + for (const [min, max] of ranges) { + if (codePoint >= min && codePoint <= max) { + return true; + } + } + return false; + } + + function getProlongedHiragana(previousCharacter) { + switch (KANA_TO_VOWEL_MAPPING.get(previousCharacter)) { + case 'a': return 'あ'; + case 'i': return 'い'; + case 'u': return 'う'; + case 'e': return 'え'; + case 'o': return 'う'; + default: return null; + } + } + + + // eslint-disable-next-line no-shadow + class JapaneseUtil { + constructor(wanakana=null) { + this._wanakana = wanakana; + } + + // Character code testing functions + + isCodePointKanji(codePoint) { + return isCodePointInRanges(codePoint, CJK_UNIFIED_IDEOGRAPHS_RANGES); + } + + isCodePointKana(codePoint) { + return isCodePointInRanges(codePoint, KANA_RANGES); + } + + isCodePointJapanese(codePoint) { + return isCodePointInRanges(codePoint, JAPANESE_RANGES); + } + + // String testing functions + + isStringEntirelyKana(str) { + if (str.length === 0) { return false; } + for (const c of str) { + if (!isCodePointInRanges(c.codePointAt(0), KANA_RANGES)) { + return false; + } + } + return true; + } + + isStringPartiallyJapanese(str) { + if (str.length === 0) { return false; } + for (const c of str) { + if (isCodePointInRanges(c.codePointAt(0), JAPANESE_RANGES)) { + return true; + } + } + return false; + } + + // Mora functions + + isMoraPitchHigh(moraIndex, pitchAccentPosition) { + switch (pitchAccentPosition) { + case 0: return (moraIndex > 0); + case 1: return (moraIndex < 1); + default: return (moraIndex > 0 && moraIndex < pitchAccentPosition); + } + } + + getPitchCategory(text, pitchAccentPosition, isVerbOrAdjective) { + if (pitchAccentPosition === 0) { + return 'heiban'; + } + if (isVerbOrAdjective) { + return pitchAccentPosition > 0 ? 'kifuku' : null; + } + if (pitchAccentPosition === 1) { + return 'atamadaka'; + } + if (pitchAccentPosition > 1) { + return pitchAccentPosition >= this.getKanaMoraCount(text) ? 'odaka' : 'nakadaka'; + } + return null; + } + + getKanaMorae(text) { + const morae = []; + let i; + for (const c of text) { + if (SMALL_KANA_SET.has(c) && (i = morae.length) > 0) { + morae[i - 1] += c; + } else { + morae.push(c); + } + } + return morae; + } + + getKanaMoraCount(text) { + let moraCount = 0; + for (const c of text) { + if (!(SMALL_KANA_SET.has(c) && moraCount > 0)) { + ++moraCount; + } + } + return moraCount; + } + + // Conversion functions + + convertToKana(text) { + return this._getWanakana().toKana(text); + } + + convertToKanaSupported() { + return this._wanakana !== null; + } + + convertKatakanaToHiragana(text) { + let result = ''; + const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]); + for (let char of text) { + const codePoint = char.codePointAt(0); + if (codePoint === KATAKANA_SMALL_KA_CODE_POINT || codePoint === KATAKANA_SMALL_KE_CODE_POINT) { + // No change + } else if (codePoint === KANA_PROLONGED_SOUND_MARK_CODE_POINT) { + if (result.length > 0) { + const char2 = getProlongedHiragana(result[result.length - 1]); + if (char2 !== null) { char = char2; } + } + } else if (isCodePointInRange(codePoint, KATAKANA_CONVERSION_RANGE)) { + char = String.fromCodePoint(codePoint + offset); + } + result += char; + } + return result; + } + + convertHiraganaToKatakana(text) { + let result = ''; + const offset = (KATAKANA_CONVERSION_RANGE[0] - HIRAGANA_CONVERSION_RANGE[0]); + for (let char of text) { + const codePoint = char.codePointAt(0); + if (isCodePointInRange(codePoint, HIRAGANA_CONVERSION_RANGE)) { + char = String.fromCodePoint(codePoint + offset); + } + result += char; + } + return result; + } + + convertToRomaji(text) { + const wanakana = this._getWanakana(); + return wanakana.toRomaji(text); + } + + convertToRomajiSupported() { + return this._wanakana !== null; + } + + convertNumericToFullWidth(text) { + let result = ''; + for (const char of text) { + let c = char.codePointAt(0); + if (c >= 0x30 && c <= 0x39) { // ['0', '9'] + c += 0xff10 - 0x30; // 0xff10 = '0' full width + result += String.fromCodePoint(c); + } else { + result += char; + } + } + return result; + } + + convertHalfWidthKanaToFullWidth(text, sourceMap=null) { + let result = ''; + + // This function is safe to use charCodeAt instead of codePointAt, since all + // the relevant characters are represented with a single UTF-16 character code. + for (let i = 0, ii = text.length; i < ii; ++i) { + const c = text[i]; + const mapping = HALFWIDTH_KATAKANA_MAPPING.get(c); + if (typeof mapping !== 'string') { + result += c; + continue; + } + + let index = 0; + switch (text.charCodeAt(i + 1)) { + case 0xff9e: // dakuten + index = 1; + break; + case 0xff9f: // handakuten + index = 2; + break; + } + + let c2 = mapping[index]; + if (index > 0) { + if (c2 === '-') { // invalid + index = 0; + c2 = mapping[0]; + } else { + ++i; + } + } + + if (sourceMap !== null && index > 0) { + sourceMap.combine(result.length, 1); + } + result += c2; + } + + return result; + } + + convertAlphabeticToKana(text, sourceMap=null) { + let part = ''; + let result = ''; + + for (const char of text) { + // Note: 0x61 is the character code for 'a' + let c = char.codePointAt(0); + if (c >= 0x41 && c <= 0x5a) { // ['A', 'Z'] + c += (0x61 - 0x41); + } else if (c >= 0x61 && c <= 0x7a) { // ['a', 'z'] + // NOP; c += (0x61 - 0x61); + } else if (c >= 0xff21 && c <= 0xff3a) { // ['A', 'Z'] fullwidth + c += (0x61 - 0xff21); + } else if (c >= 0xff41 && c <= 0xff5a) { // ['a', 'z'] fullwidth + c += (0x61 - 0xff41); + } else if (c === 0x2d || c === 0xff0d) { // '-' or fullwidth dash + c = 0x2d; // '-' + } else { + if (part.length > 0) { + result += this._convertAlphabeticPartToKana(part, sourceMap, result.length); + part = ''; + } + result += char; + continue; + } + part += String.fromCodePoint(c); + } + + if (part.length > 0) { + result += this._convertAlphabeticPartToKana(part, sourceMap, result.length); + } + return result; + } + + convertAlphabeticToKanaSupported() { + return this._wanakana !== null; + } + + getKanaDiacriticInfo(character) { + const info = DIACRITIC_MAPPING.get(character); + return typeof info !== 'undefined' ? {character: info.character, type: info.type} : null; + } + + // Furigana distribution + + distributeFurigana(term, reading) { + if (reading === term) { + // Same + return [this._createFuriganaSegment(term, '')]; + } + + const groups = []; + let groupPre = null; + let isKanaPre = null; + for (const c of term) { + const codePoint = c.codePointAt(0); + const isKana = !(this.isCodePointKanji(codePoint) || codePoint === ITERATION_MARK_CODE_POINT); + if (isKana === isKanaPre) { + groupPre.text += c; + } else { + groupPre = {isKana, text: c, textNormalized: null}; + groups.push(groupPre); + isKanaPre = isKana; + } + } + for (const group of groups) { + if (group.isKana) { + group.textNormalized = this.convertKatakanaToHiragana(group.text); + } + } + + const readingNormalized = this.convertKatakanaToHiragana(reading); + const segments = this._segmentizeFurigana(reading, readingNormalized, groups, 0); + if (segments !== null) { + return segments; + } + + // Fallback + return [this._createFuriganaSegment(term, reading)]; + } + + distributeFuriganaInflected(term, reading, source) { + const termNormalized = this.convertKatakanaToHiragana(term); + const readingNormalized = this.convertKatakanaToHiragana(reading); + const sourceNormalized = this.convertKatakanaToHiragana(source); + + let mainText = term; + let stemLength = this._getStemLength(termNormalized, sourceNormalized); + + // Check if source is derived from the reading instead of the term + const readingStemLength = this._getStemLength(readingNormalized, sourceNormalized); + if (readingStemLength > 0 && readingStemLength >= stemLength) { + mainText = reading; + stemLength = readingStemLength; + reading = `${source.substring(0, stemLength)}${reading.substring(stemLength)}`; + } + + const segments = []; + if (stemLength > 0) { + mainText = `${source.substring(0, stemLength)}${mainText.substring(stemLength)}`; + const segments2 = this.distributeFurigana(mainText, reading); + let consumed = 0; + for (const segment of segments2) { + const {text} = segment; + const start = consumed; + consumed += text.length; + if (consumed < stemLength) { + segments.push(segment); + } else if (consumed === stemLength) { + segments.push(segment); + break; + } else { + if (start < stemLength) { + segments.push(this._createFuriganaSegment(mainText.substring(start, stemLength), '')); + } + break; + } + } + } + + if (stemLength < source.length) { + const remainder = source.substring(stemLength); + const segmentCount = segments.length; + if (segmentCount > 0 && segments[segmentCount - 1].reading.length === 0) { + // Append to the last segment if it has an empty reading + segments[segmentCount - 1].text += remainder; + } else { + // Otherwise, create a new segment + segments.push(this._createFuriganaSegment(remainder, '')); + } + } + + return segments; + } + + // Miscellaneous + + collapseEmphaticSequences(text, fullCollapse, sourceMap=null) { + let result = ''; + let collapseCodePoint = -1; + const hasSourceMap = (sourceMap !== null); + for (const char of text) { + const c = char.codePointAt(0); + if ( + c === HIRAGANA_SMALL_TSU_CODE_POINT || + c === KATAKANA_SMALL_TSU_CODE_POINT || + c === KANA_PROLONGED_SOUND_MARK_CODE_POINT + ) { + if (collapseCodePoint !== c) { + collapseCodePoint = c; + if (!fullCollapse) { + result += char; + continue; + } + } + } else { + collapseCodePoint = -1; + result += char; + continue; + } + + if (hasSourceMap) { + sourceMap.combine(Math.max(0, result.length - 1), 1); + } + } + return result; + } + + // Private + + _createFuriganaSegment(text, reading) { + return {text, reading}; + } + + _segmentizeFurigana(reading, readingNormalized, groups, groupsStart) { + const groupCount = groups.length - groupsStart; + if (groupCount <= 0) { + return reading.length === 0 ? [] : null; + } + + const group = groups[groupsStart]; + const {isKana, text} = group; + const textLength = text.length; + if (isKana) { + const {textNormalized} = group; + if (readingNormalized.startsWith(textNormalized)) { + const segments = this._segmentizeFurigana( + reading.substring(textLength), + readingNormalized.substring(textLength), + groups, + groupsStart + 1 + ); + if (segments !== null) { + if (reading.startsWith(text)) { + segments.unshift(this._createFuriganaSegment(text, '')); + } else { + segments.unshift(...this._getFuriganaKanaSegments(text, reading)); + } + return segments; + } + } + return null; + } else { + let result = null; + for (let i = reading.length; i >= textLength; --i) { + const segments = this._segmentizeFurigana( + reading.substring(i), + readingNormalized.substring(i), + groups, + groupsStart + 1 + ); + if (segments !== null) { + if (result !== null) { + // More than one way to segmentize the tail; mark as ambiguous + return null; + } + const segmentReading = reading.substring(0, i); + segments.unshift(this._createFuriganaSegment(text, segmentReading)); + result = segments; + } + // There is only one way to segmentize the last non-kana group + if (groupCount === 1) { + break; + } + } + return result; + } + } + + _getFuriganaKanaSegments(text, reading) { + const textLength = text.length; + const newSegments = []; + let start = 0; + let state = (reading[0] === text[0]); + for (let i = 1; i < textLength; ++i) { + const newState = (reading[i] === text[i]); + if (state === newState) { continue; } + newSegments.push(this._createFuriganaSegment(text.substring(start, i), state ? '' : reading.substring(start, i))); + state = newState; + start = i; + } + newSegments.push(this._createFuriganaSegment(text.substring(start, textLength), state ? '' : reading.substring(start, textLength))); + return newSegments; + } + + _getWanakana() { + const wanakana = this._wanakana; + if (wanakana === null) { throw new Error('Functions which use WanaKana are not supported in this context'); } + return wanakana; + } + + _convertAlphabeticPartToKana(text, sourceMap, sourceMapStart) { + const wanakana = this._getWanakana(); + const result = wanakana.toHiragana(text); + + // Generate source mapping + if (sourceMap !== null) { + let i = 0; + let resultPos = 0; + const ii = text.length; + while (i < ii) { + // Find smallest matching substring + let iNext = i + 1; + let resultPosNext = result.length; + while (iNext < ii) { + const t = wanakana.toHiragana(text.substring(0, iNext)); + if (t === result.substring(0, t.length)) { + resultPosNext = t.length; + break; + } + ++iNext; + } + + // Merge characters + const removals = iNext - i - 1; + if (removals > 0) { + sourceMap.combine(sourceMapStart, removals); + } + ++sourceMapStart; + + // Empty elements + const additions = resultPosNext - resultPos - 1; + for (let j = 0; j < additions; ++j) { + sourceMap.insert(sourceMapStart, 0); + ++sourceMapStart; + } + + i = iNext; + resultPos = resultPosNext; + } + } + + return result; + } + + _getStemLength(text1, text2) { + const minLength = Math.min(text1.length, text2.length); + if (minLength === 0) { return 0; } + + let i = 0; + while (true) { + const char1 = text1.codePointAt(i); + const char2 = text2.codePointAt(i); + if (char1 !== char2) { break; } + const charLength = String.fromCodePoint(char1).length; + i += charLength; + if (i >= minLength) { + if (i > minLength) { + i -= charLength; // Don't consume partial UTF16 surrogate characters + } + break; + } + } + return i; + } + } + + + return JapaneseUtil; +})(); diff --git a/ext/js/templates/sandbox/template-renderer-frame-api.js b/ext/js/templates/sandbox/template-renderer-frame-api.js new file mode 100644 index 00000000..f234c8e0 --- /dev/null +++ b/ext/js/templates/sandbox/template-renderer-frame-api.js @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2020-2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +class TemplateRendererFrameApi { + constructor(templateRenderer) { + this._templateRenderer = templateRenderer; + this._windowMessageHandlers = new Map([ + ['render', {async: false, handler: this._onRender.bind(this)}], + ['renderMulti', {async: false, handler: this._onRenderMulti.bind(this)}], + ['getModifiedData', {async: false, handler: this._onGetModifiedData.bind(this)}] + ]); + } + + prepare() { + window.addEventListener('message', this._onWindowMessage.bind(this), false); + this._postMessage(window.parent, 'ready', {}, null); + } + + // Private + + _onWindowMessage(e) { + const {source, data: {action, params, id}} = e; + const messageHandler = this._windowMessageHandlers.get(action); + if (typeof messageHandler === 'undefined') { return; } + + this._onWindowMessageInner(messageHandler, action, params, source, id); + } + + async _onWindowMessageInner({handler, async}, action, params, source, id) { + let response; + try { + let result = handler(params); + if (async) { + result = await result; + } + response = {result}; + } catch (error) { + response = {error: this._serializeError(error)}; + } + + if (typeof id === 'undefined') { return; } + this._postMessage(source, `${action}.response`, response, id); + } + + _onRender({template, data, type}) { + return this._templateRenderer.render(template, data, type); + } + + _onRenderMulti({items}) { + return this._serializeMulti(this._templateRenderer.renderMulti(items)); + } + + _onGetModifiedData({data, type}) { + const result = this._templateRenderer.getModifiedData(data, type); + return this._clone(result); + } + + _serializeError(error) { + try { + if (typeof error === 'object' && error !== null) { + return { + name: error.name, + message: error.message, + stack: error.stack, + data: error.data + }; + } + } catch (e) { + // NOP + } + return { + value: error, + hasValue: true + }; + } + + _serializeMulti(array) { + for (let i = 0, ii = array.length; i < ii; ++i) { + const value = array[i]; + const {error} = value; + if (typeof error !== 'undefined') { + value.error = this._serializeError(error); + } + } + return array; + } + + _clone(value) { + return JSON.parse(JSON.stringify(value)); + } + + _postMessage(target, action, params, id) { + return target.postMessage({action, params, id}, '*'); + } +} diff --git a/ext/js/templates/sandbox/template-renderer-frame-main.js b/ext/js/templates/sandbox/template-renderer-frame-main.js new file mode 100644 index 00000000..bb9cac3a --- /dev/null +++ b/ext/js/templates/sandbox/template-renderer-frame-main.js @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2020-2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* globals + * AnkiNoteDataCreator + * CssStyleApplier + * JapaneseUtil + * TemplateRenderer + * TemplateRendererFrameApi + */ + +(async () => { + const cssStyleApplier = new CssStyleApplier('/data/structured-content-style.json'); + await cssStyleApplier.prepare(); + const japaneseUtil = new JapaneseUtil(null); + const templateRenderer = new TemplateRenderer(japaneseUtil, cssStyleApplier); + const ankiNoteDataCreator = new AnkiNoteDataCreator(japaneseUtil); + templateRenderer.registerDataType('ankiNote', { + modifier: ({marker, commonData}) => ankiNoteDataCreator.create(marker, commonData), + composeData: (marker, commonData) => ({marker, commonData}) + }); + const templateRendererFrameApi = new TemplateRendererFrameApi(templateRenderer); + templateRendererFrameApi.prepare(); +})(); diff --git a/ext/js/templates/sandbox/template-renderer-media-provider.js b/ext/js/templates/sandbox/template-renderer-media-provider.js new file mode 100644 index 00000000..114d3387 --- /dev/null +++ b/ext/js/templates/sandbox/template-renderer-media-provider.js @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* global + * Handlebars + */ + +class TemplateRendererMediaProvider { + constructor() { + this._requirements = null; + } + + get requirements() { + return this._requirements; + } + + set requirements(value) { + this._requirements = value; + } + + hasMedia(root, args, namedArgs) { + const {media} = root; + const data = this._getMediaData(media, args, namedArgs); + return (data !== null); + } + + getMedia(root, args, namedArgs) { + const {media} = root; + const data = this._getMediaData(media, args, namedArgs); + if (data !== null) { + const result = this._getFormattedValue(data, namedArgs); + if (typeof result === 'string') { return result; } + } + const defaultValue = namedArgs.default; + return typeof defaultValue !== 'undefined' ? defaultValue : ''; + } + + // Private + + _addRequirement(value) { + if (this._requirements === null) { return; } + this._requirements.push(value); + } + + _getFormattedValue(data, namedArgs) { + let {value} = data; + const {escape=true} = namedArgs; + if (escape) { + value = Handlebars.Utils.escapeExpression(value); + } + return value; + } + + _getMediaData(media, args, namedArgs) { + const type = args[0]; + switch (type) { + case 'audio': return this._getSimpleMediaData(media, 'audio'); + case 'screenshot': return this._getSimpleMediaData(media, 'screenshot'); + case 'clipboardImage': return this._getSimpleMediaData(media, 'clipboardImage'); + case 'clipboardText': return this._getSimpleMediaData(media, 'clipboardText'); + case 'selectionText': return this._getSimpleMediaData(media, 'selectionText'); + case 'textFurigana': return this._getTextFurigana(media, args[1], namedArgs); + case 'dictionaryMedia': return this._getDictionaryMedia(media, args[1], namedArgs); + default: return null; + } + } + + _getSimpleMediaData(media, type) { + const result = media[type]; + if (typeof result === 'object' && result !== null) { return result; } + this._addRequirement({type}); + return null; + } + + _getDictionaryMedia(media, path, namedArgs) { + const {dictionaryMedia} = media; + const {dictionary} = namedArgs; + if ( + typeof dictionaryMedia !== 'undefined' && + typeof dictionary === 'string' && + Object.prototype.hasOwnProperty.call(dictionaryMedia, dictionary) + ) { + const dictionaryMedia2 = dictionaryMedia[dictionary]; + if (Object.prototype.hasOwnProperty.call(dictionaryMedia2, path)) { + const result = dictionaryMedia2[path]; + if (typeof result === 'object' && result !== null) { + return result; + } + } + } + this._addRequirement({ + type: 'dictionaryMedia', + dictionary, + path + }); + return null; + } + + _getTextFurigana(media, text, namedArgs) { + const {readingMode=null} = namedArgs; + const {textFurigana} = media; + if (Array.isArray(textFurigana)) { + for (const entry of textFurigana) { + if (entry.text !== text || entry.readingMode !== readingMode) { continue; } + return entry.details; + } + } + this._addRequirement({ + type: 'textFurigana', + text, + readingMode + }); + return null; + } +} diff --git a/ext/js/templates/sandbox/template-renderer.js b/ext/js/templates/sandbox/template-renderer.js new file mode 100644 index 00000000..02471c97 --- /dev/null +++ b/ext/js/templates/sandbox/template-renderer.js @@ -0,0 +1,620 @@ +/* + * Copyright (C) 2016-2021 Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* global + * DictionaryDataUtil + * Handlebars + * StructuredContentGenerator + * TemplateRendererMediaProvider + */ + +class TemplateRenderer { + constructor(japaneseUtil, cssStyleApplier) { + this._japaneseUtil = japaneseUtil; + this._cssStyleApplier = cssStyleApplier; + this._mediaProvider = new TemplateRendererMediaProvider(); + this._cache = new Map(); + this._cacheMaxSize = 5; + this._helpersRegistered = false; + this._stateStack = null; + this._dataTypes = new Map(); + this._requirements = null; + this._cleanupCallbacks = null; + this._temporaryElement = null; + } + + registerDataType(name, {modifier=null, composeData=null}) { + this._dataTypes.set(name, {modifier, composeData}); + } + + render(template, data, type) { + const instance = this._getTemplateInstance(template); + data = this._getModifiedData(data, void 0, type); + return this._renderTemplate(instance, data); + } + + renderMulti(items) { + const results = []; + for (const {template, templateItems} of items) { + const instance = this._getTemplateInstance(template); + for (const {type, commonData, datas} of templateItems) { + for (let data of datas) { + let result; + try { + data = this._getModifiedData(data, commonData, type); + result = this._renderTemplate(instance, data); + result = {result}; + } catch (error) { + result = {error}; + } + results.push(result); + } + } + } + return results; + } + + getModifiedData(data, type) { + return this._getModifiedData(data, void 0, type); + } + + // Private + + _getTemplateInstance(template) { + if (!this._helpersRegistered) { + this._registerHelpers(); + } + + const cache = this._cache; + let instance = cache.get(template); + if (typeof instance === 'undefined') { + this._updateCacheSize(this._cacheMaxSize - 1); + instance = Handlebars.compile(template); + cache.set(template, instance); + } + + return instance; + } + + _renderTemplate(instance, data) { + const cleanupCallbacks = []; + const requirements = []; + try { + this._stateStack = [new Map()]; + this._requirements = requirements; + this._mediaProvider.requirements = requirements; + this._cleanupCallbacks = cleanupCallbacks; + const result = instance(data).trim(); + return {result, requirements}; + } finally { + for (const callback of cleanupCallbacks) { callback(); } + this._stateStack = null; + this._requirements = null; + this._mediaProvider.requirements = null; + this._cleanupCallbacks = null; + } + } + + _getModifiedData(data, commonData, type) { + if (typeof type === 'string') { + const typeInfo = this._dataTypes.get(type); + if (typeof typeInfo !== 'undefined') { + if (typeof commonData !== 'undefined') { + const {composeData} = typeInfo; + if (typeof composeData === 'function') { + data = composeData(data, commonData); + } + } + const {modifier} = typeInfo; + if (typeof modifier === 'function') { + data = modifier(data); + } + } + } + return data; + } + + _updateCacheSize(maxSize) { + const cache = this._cache; + let removeCount = cache.size - maxSize; + if (removeCount <= 0) { return; } + + for (const key of cache.keys()) { + cache.delete(key); + if (--removeCount <= 0) { break; } + } + } + + _registerHelpers() { + Handlebars.partials = Handlebars.templates; + + const helpers = [ + ['dumpObject', this._dumpObject.bind(this)], + ['furigana', this._furigana.bind(this)], + ['furiganaPlain', this._furiganaPlain.bind(this)], + ['kanjiLinks', this._kanjiLinks.bind(this)], + ['multiLine', this._multiLine.bind(this)], + ['sanitizeCssClass', this._sanitizeCssClass.bind(this)], + ['regexReplace', this._regexReplace.bind(this)], + ['regexMatch', this._regexMatch.bind(this)], + ['mergeTags', this._mergeTags.bind(this)], + ['eachUpTo', this._eachUpTo.bind(this)], + ['spread', this._spread.bind(this)], + ['op', this._op.bind(this)], + ['get', this._get.bind(this)], + ['set', this._set.bind(this)], + ['scope', this._scope.bind(this)], + ['property', this._property.bind(this)], + ['noop', this._noop.bind(this)], + ['isMoraPitchHigh', this._isMoraPitchHigh.bind(this)], + ['getKanaMorae', this._getKanaMorae.bind(this)], + ['typeof', this._getTypeof.bind(this)], + ['join', this._join.bind(this)], + ['concat', this._concat.bind(this)], + ['pitchCategories', this._pitchCategories.bind(this)], + ['formatGlossary', this._formatGlossary.bind(this)], + ['hasMedia', this._hasMedia.bind(this)], + ['getMedia', this._getMedia.bind(this)] + ]; + + for (const [name, helper] of helpers) { + this._registerHelper(name, helper); + } + + this._helpersRegistered = true; + } + + _registerHelper(name, helper) { + function wrapper(...args) { + return helper(this, ...args); + } + Handlebars.registerHelper(name, wrapper); + } + + _escape(text) { + return Handlebars.Utils.escapeExpression(text); + } + + _dumpObject(context, options) { + const dump = JSON.stringify(options.fn(context), null, 4); + return this._escape(dump); + } + + _furigana(context, ...args) { + const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); + const segs = this._japaneseUtil.distributeFurigana(expression, reading); + + let result = ''; + for (const {text, reading: reading2} of segs) { + if (reading2.length > 0) { + result += `${text}${reading2}`; + } else { + result += text; + } + } + + return result; + } + + _furiganaPlain(context, ...args) { + const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); + const segs = this._japaneseUtil.distributeFurigana(expression, reading); + + let result = ''; + for (const {text, reading: reading2} of segs) { + if (reading2.length > 0) { + if (result.length > 0) { result += ' '; } + result += `${text}[${reading2}]`; + } else { + result += text; + } + } + + return result; + } + + _getFuriganaExpressionAndReading(context, ...args) { + const options = args[args.length - 1]; + if (args.length >= 3) { + return {expression: args[0], reading: args[1]}; + } else { + const {expression, reading} = options.fn(context); + return {expression, reading}; + } + } + + _kanjiLinks(context, options) { + const jp = this._japaneseUtil; + let result = ''; + for (const c of options.fn(context)) { + if (jp.isCodePointKanji(c.codePointAt(0))) { + result += `${c}`; + } else { + result += c; + } + } + + return result; + } + + _stringToMultiLineHtml(string) { + return string.split('\n').join('
'); + } + + _multiLine(context, options) { + return this._stringToMultiLineHtml(options.fn(context)); + } + + _sanitizeCssClass(context, options) { + return options.fn(context).replace(/[^_a-z0-9\u00a0-\uffff]/ig, '_'); + } + + _regexReplace(context, ...args) { + // Usage: + // {{#regexReplace regex string [flags]}}content{{/regexReplace}} + // regex: regular expression string + // string: string to replace + // flags: optional flags for regular expression + // e.g. "i" for case-insensitive, "g" for replace all + let value = args[args.length - 1].fn(context); + if (args.length >= 3) { + try { + const flags = args.length > 3 ? args[2] : 'g'; + const regex = new RegExp(args[0], flags); + value = value.replace(regex, args[1]); + } catch (e) { + return `${e}`; + } + } + return value; + } + + _regexMatch(context, ...args) { + // Usage: + // {{#regexMatch regex [flags]}}content{{/regexMatch}} + // regex: regular expression string + // flags: optional flags for regular expression + // e.g. "i" for case-insensitive, "g" for match all + let value = args[args.length - 1].fn(context); + if (args.length >= 2) { + try { + const flags = args.length > 2 ? args[1] : ''; + const regex = new RegExp(args[0], flags); + const parts = []; + value.replace(regex, (g0) => parts.push(g0)); + value = parts.join(''); + } catch (e) { + return `${e}`; + } + } + return value; + } + + _mergeTags(context, object, isGroupMode, isMergeMode) { + const tagSources = []; + if (isGroupMode || isMergeMode) { + for (const definition of object.definitions) { + tagSources.push(definition.definitionTags); + } + } else { + tagSources.push(object.definitionTags); + } + + const tags = new Set(); + for (const tagSource of tagSources) { + if (!Array.isArray(tagSource)) { continue; } + for (const tag of tagSource) { + tags.add(tag.name); + } + } + + return [...tags].join(', '); + } + + _eachUpTo(context, iterable, maxCount, options) { + if (iterable) { + const results = []; + let any = false; + for (const entry of iterable) { + any = true; + if (results.length >= maxCount) { break; } + const processedEntry = options.fn(entry); + results.push(processedEntry); + } + if (any) { + return results.join(''); + } + } + return options.inverse(context); + } + + _spread(context, ...args) { + const result = []; + for (let i = 0, ii = args.length - 1; i < ii; ++i) { + try { + result.push(...args[i]); + } catch (e) { + // NOP + } + } + return result; + } + + _op(context, ...args) { + switch (args.length) { + case 3: return this._evaluateUnaryExpression(args[0], args[1]); + case 4: return this._evaluateBinaryExpression(args[0], args[1], args[2]); + case 5: return this._evaluateTernaryExpression(args[0], args[1], args[2], args[3]); + default: return void 0; + } + } + + _evaluateUnaryExpression(operator, operand1) { + switch (operator) { + case '+': return +operand1; + case '-': return -operand1; + case '~': return ~operand1; + case '!': return !operand1; + default: return void 0; + } + } + + _evaluateBinaryExpression(operator, operand1, operand2) { + switch (operator) { + case '+': return operand1 + operand2; + case '-': return operand1 - operand2; + case '/': return operand1 / operand2; + case '*': return operand1 * operand2; + case '%': return operand1 % operand2; + case '**': return operand1 ** operand2; + case '==': return operand1 == operand2; // eslint-disable-line eqeqeq + case '!=': return operand1 != operand2; // eslint-disable-line eqeqeq + case '===': return operand1 === operand2; + case '!==': return operand1 !== operand2; + case '<': return operand1 < operand2; + case '<=': return operand1 <= operand2; + case '>': return operand1 > operand2; + case '>=': return operand1 >= operand2; + case '<<': return operand1 << operand2; + case '>>': return operand1 >> operand2; + case '>>>': return operand1 >>> operand2; + case '&': return operand1 & operand2; + case '|': return operand1 | operand2; + case '^': return operand1 ^ operand2; + case '&&': return operand1 && operand2; + case '||': return operand1 || operand2; + default: return void 0; + } + } + + _evaluateTernaryExpression(operator, operand1, operand2, operand3) { + switch (operator) { + case '?:': return operand1 ? operand2 : operand3; + default: return void 0; + } + } + + _get(context, key) { + for (let i = this._stateStack.length; --i >= 0;) { + const map = this._stateStack[i]; + if (map.has(key)) { + return map.get(key); + } + } + return void 0; + } + + _set(context, ...args) { + switch (args.length) { + case 2: + { + const [key, options] = args; + const value = options.fn(context); + this._stateStack[this._stateStack.length - 1].set(key, value); + } + break; + case 3: + { + const [key, value] = args; + this._stateStack[this._stateStack.length - 1].set(key, value); + } + break; + } + return ''; + } + + _scope(context, options) { + try { + this._stateStack.push(new Map()); + return options.fn(context); + } finally { + if (this._stateStack.length > 1) { + this._stateStack.pop(); + } + } + } + + _property(context, ...args) { + const ii = args.length - 1; + if (ii <= 0) { return void 0; } + + try { + let value = args[0]; + for (let i = 1; i < ii; ++i) { + value = value[args[i]]; + } + return value; + } catch (e) { + return void 0; + } + } + + _noop(context, options) { + return options.fn(context); + } + + _isMoraPitchHigh(context, index, position) { + return this._japaneseUtil.isMoraPitchHigh(index, position); + } + + _getKanaMorae(context, text) { + return this._japaneseUtil.getKanaMorae(`${text}`); + } + + _getTypeof(context, ...args) { + const ii = args.length - 1; + const value = (ii > 0 ? args[0] : args[ii].fn(context)); + return typeof value; + } + + _join(context, ...args) { + return args.length > 1 ? args.slice(1, args.length - 1).flat().join(args[0]) : ''; + } + + _concat(context, ...args) { + let result = ''; + for (let i = 0, ii = args.length - 1; i < ii; ++i) { + result += args[i]; + } + return result; + } + + _pitchCategories(context, data) { + const {pronunciations, headwords} = data.dictionaryEntry; + const categories = new Set(); + for (const {headwordIndex, pitches} of pronunciations) { + const {reading, wordClasses} = headwords[headwordIndex]; + const isVerbOrAdjective = DictionaryDataUtil.isNonNounVerbOrAdjective(wordClasses); + for (const {position} of pitches) { + const category = this._japaneseUtil.getPitchCategory(reading, position, isVerbOrAdjective); + if (category !== null) { + categories.add(category); + } + } + } + return [...categories]; + } + + _getTemporaryElement() { + let element = this._temporaryElement; + if (element === null) { + element = document.createElement('div'); + this._temporaryElement = element; + } + return element; + } + + _getHtml(node) { + const container = this._getTemporaryElement(); + container.appendChild(node); + this._normalizeHtml(container); + const result = container.innerHTML; + container.textContent = ''; + return result; + } + + _normalizeHtml(root) { + const {ELEMENT_NODE, TEXT_NODE} = Node; + const treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT); + const elements = []; + const textNodes = []; + while (true) { + const node = treeWalker.nextNode(); + if (node === null) { break; } + switch (node.nodeType) { + case ELEMENT_NODE: + elements.push(node); + break; + case TEXT_NODE: + textNodes.push(node); + break; + } + } + this._cssStyleApplier.applyClassStyles(elements); + for (const element of elements) { + const {dataset} = element; + for (const key of Object.keys(dataset)) { + delete dataset[key]; + } + } + for (const textNode of textNodes) { + this._replaceNewlines(textNode); + } + } + + _replaceNewlines(textNode) { + const parts = textNode.nodeValue.split('\n'); + if (parts.length <= 1) { return; } + const {parentNode} = textNode; + if (parentNode === null) { return; } + const fragment = document.createDocumentFragment(); + for (let i = 0, ii = parts.length; i < ii; ++i) { + if (i > 0) { fragment.appendChild(document.createElement('br')); } + fragment.appendChild(document.createTextNode(parts[i])); + } + parentNode.replaceChild(fragment, textNode); + } + + _createStructuredContentGenerator(data) { + const mediaLoader = { + loadMedia: async (path, dictionary, onLoad, onUnload) => { + const imageUrl = this._mediaProvider.getMedia(data, ['dictionaryMedia', path], {dictionary, format: 'fileName', default: null}); + if (imageUrl !== null) { + onLoad(imageUrl); + this._cleanupCallbacks.push(() => onUnload(true)); + } + } + }; + return new StructuredContentGenerator(mediaLoader, document); + } + + _formatGlossary(context, dictionary, options) { + const data = options.data.root; + const content = options.fn(context); + if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); } + if (!(typeof content === 'object' && content !== null)) { return ''; } + switch (content.type) { + case 'image': return this._formatGlossaryImage(content, dictionary, data); + case 'structured-content': return this._formatStructuredContent(content, dictionary, data); + } + return ''; + } + + _formatGlossaryImage(content, dictionary, data) { + const structuredContentGenerator = this._createStructuredContentGenerator(data); + const node = structuredContentGenerator.createDefinitionImage(content, dictionary); + return this._getHtml(node); + } + + _formatStructuredContent(content, dictionary, data) { + const structuredContentGenerator = this._createStructuredContentGenerator(data); + const node = structuredContentGenerator.createStructuredContent(content.content, dictionary); + return node !== null ? this._getHtml(node) : ''; + } + + _hasMedia(context, ...args) { + const ii = args.length - 1; + const options = args[ii]; + return this._mediaProvider.hasMedia(options.data.root, args.slice(0, ii), options.hash); + } + + _getMedia(context, ...args) { + const ii = args.length - 1; + const options = args[ii]; + return this._mediaProvider.getMedia(options.data.root, args.slice(0, ii), options.hash); + } +} diff --git a/ext/js/templates/template-renderer-frame-api.js b/ext/js/templates/template-renderer-frame-api.js deleted file mode 100644 index f234c8e0..00000000 --- a/ext/js/templates/template-renderer-frame-api.js +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2020-2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -class TemplateRendererFrameApi { - constructor(templateRenderer) { - this._templateRenderer = templateRenderer; - this._windowMessageHandlers = new Map([ - ['render', {async: false, handler: this._onRender.bind(this)}], - ['renderMulti', {async: false, handler: this._onRenderMulti.bind(this)}], - ['getModifiedData', {async: false, handler: this._onGetModifiedData.bind(this)}] - ]); - } - - prepare() { - window.addEventListener('message', this._onWindowMessage.bind(this), false); - this._postMessage(window.parent, 'ready', {}, null); - } - - // Private - - _onWindowMessage(e) { - const {source, data: {action, params, id}} = e; - const messageHandler = this._windowMessageHandlers.get(action); - if (typeof messageHandler === 'undefined') { return; } - - this._onWindowMessageInner(messageHandler, action, params, source, id); - } - - async _onWindowMessageInner({handler, async}, action, params, source, id) { - let response; - try { - let result = handler(params); - if (async) { - result = await result; - } - response = {result}; - } catch (error) { - response = {error: this._serializeError(error)}; - } - - if (typeof id === 'undefined') { return; } - this._postMessage(source, `${action}.response`, response, id); - } - - _onRender({template, data, type}) { - return this._templateRenderer.render(template, data, type); - } - - _onRenderMulti({items}) { - return this._serializeMulti(this._templateRenderer.renderMulti(items)); - } - - _onGetModifiedData({data, type}) { - const result = this._templateRenderer.getModifiedData(data, type); - return this._clone(result); - } - - _serializeError(error) { - try { - if (typeof error === 'object' && error !== null) { - return { - name: error.name, - message: error.message, - stack: error.stack, - data: error.data - }; - } - } catch (e) { - // NOP - } - return { - value: error, - hasValue: true - }; - } - - _serializeMulti(array) { - for (let i = 0, ii = array.length; i < ii; ++i) { - const value = array[i]; - const {error} = value; - if (typeof error !== 'undefined') { - value.error = this._serializeError(error); - } - } - return array; - } - - _clone(value) { - return JSON.parse(JSON.stringify(value)); - } - - _postMessage(target, action, params, id) { - return target.postMessage({action, params, id}, '*'); - } -} diff --git a/ext/js/templates/template-renderer-frame-main.js b/ext/js/templates/template-renderer-frame-main.js deleted file mode 100644 index bb9cac3a..00000000 --- a/ext/js/templates/template-renderer-frame-main.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2020-2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* globals - * AnkiNoteDataCreator - * CssStyleApplier - * JapaneseUtil - * TemplateRenderer - * TemplateRendererFrameApi - */ - -(async () => { - const cssStyleApplier = new CssStyleApplier('/data/structured-content-style.json'); - await cssStyleApplier.prepare(); - const japaneseUtil = new JapaneseUtil(null); - const templateRenderer = new TemplateRenderer(japaneseUtil, cssStyleApplier); - const ankiNoteDataCreator = new AnkiNoteDataCreator(japaneseUtil); - templateRenderer.registerDataType('ankiNote', { - modifier: ({marker, commonData}) => ankiNoteDataCreator.create(marker, commonData), - composeData: (marker, commonData) => ({marker, commonData}) - }); - const templateRendererFrameApi = new TemplateRendererFrameApi(templateRenderer); - templateRendererFrameApi.prepare(); -})(); diff --git a/ext/js/templates/template-renderer-media-provider.js b/ext/js/templates/template-renderer-media-provider.js deleted file mode 100644 index 114d3387..00000000 --- a/ext/js/templates/template-renderer-media-provider.js +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* global - * Handlebars - */ - -class TemplateRendererMediaProvider { - constructor() { - this._requirements = null; - } - - get requirements() { - return this._requirements; - } - - set requirements(value) { - this._requirements = value; - } - - hasMedia(root, args, namedArgs) { - const {media} = root; - const data = this._getMediaData(media, args, namedArgs); - return (data !== null); - } - - getMedia(root, args, namedArgs) { - const {media} = root; - const data = this._getMediaData(media, args, namedArgs); - if (data !== null) { - const result = this._getFormattedValue(data, namedArgs); - if (typeof result === 'string') { return result; } - } - const defaultValue = namedArgs.default; - return typeof defaultValue !== 'undefined' ? defaultValue : ''; - } - - // Private - - _addRequirement(value) { - if (this._requirements === null) { return; } - this._requirements.push(value); - } - - _getFormattedValue(data, namedArgs) { - let {value} = data; - const {escape=true} = namedArgs; - if (escape) { - value = Handlebars.Utils.escapeExpression(value); - } - return value; - } - - _getMediaData(media, args, namedArgs) { - const type = args[0]; - switch (type) { - case 'audio': return this._getSimpleMediaData(media, 'audio'); - case 'screenshot': return this._getSimpleMediaData(media, 'screenshot'); - case 'clipboardImage': return this._getSimpleMediaData(media, 'clipboardImage'); - case 'clipboardText': return this._getSimpleMediaData(media, 'clipboardText'); - case 'selectionText': return this._getSimpleMediaData(media, 'selectionText'); - case 'textFurigana': return this._getTextFurigana(media, args[1], namedArgs); - case 'dictionaryMedia': return this._getDictionaryMedia(media, args[1], namedArgs); - default: return null; - } - } - - _getSimpleMediaData(media, type) { - const result = media[type]; - if (typeof result === 'object' && result !== null) { return result; } - this._addRequirement({type}); - return null; - } - - _getDictionaryMedia(media, path, namedArgs) { - const {dictionaryMedia} = media; - const {dictionary} = namedArgs; - if ( - typeof dictionaryMedia !== 'undefined' && - typeof dictionary === 'string' && - Object.prototype.hasOwnProperty.call(dictionaryMedia, dictionary) - ) { - const dictionaryMedia2 = dictionaryMedia[dictionary]; - if (Object.prototype.hasOwnProperty.call(dictionaryMedia2, path)) { - const result = dictionaryMedia2[path]; - if (typeof result === 'object' && result !== null) { - return result; - } - } - } - this._addRequirement({ - type: 'dictionaryMedia', - dictionary, - path - }); - return null; - } - - _getTextFurigana(media, text, namedArgs) { - const {readingMode=null} = namedArgs; - const {textFurigana} = media; - if (Array.isArray(textFurigana)) { - for (const entry of textFurigana) { - if (entry.text !== text || entry.readingMode !== readingMode) { continue; } - return entry.details; - } - } - this._addRequirement({ - type: 'textFurigana', - text, - readingMode - }); - return null; - } -} diff --git a/ext/js/templates/template-renderer.js b/ext/js/templates/template-renderer.js deleted file mode 100644 index 02471c97..00000000 --- a/ext/js/templates/template-renderer.js +++ /dev/null @@ -1,620 +0,0 @@ -/* - * Copyright (C) 2016-2021 Yomichan Authors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* global - * DictionaryDataUtil - * Handlebars - * StructuredContentGenerator - * TemplateRendererMediaProvider - */ - -class TemplateRenderer { - constructor(japaneseUtil, cssStyleApplier) { - this._japaneseUtil = japaneseUtil; - this._cssStyleApplier = cssStyleApplier; - this._mediaProvider = new TemplateRendererMediaProvider(); - this._cache = new Map(); - this._cacheMaxSize = 5; - this._helpersRegistered = false; - this._stateStack = null; - this._dataTypes = new Map(); - this._requirements = null; - this._cleanupCallbacks = null; - this._temporaryElement = null; - } - - registerDataType(name, {modifier=null, composeData=null}) { - this._dataTypes.set(name, {modifier, composeData}); - } - - render(template, data, type) { - const instance = this._getTemplateInstance(template); - data = this._getModifiedData(data, void 0, type); - return this._renderTemplate(instance, data); - } - - renderMulti(items) { - const results = []; - for (const {template, templateItems} of items) { - const instance = this._getTemplateInstance(template); - for (const {type, commonData, datas} of templateItems) { - for (let data of datas) { - let result; - try { - data = this._getModifiedData(data, commonData, type); - result = this._renderTemplate(instance, data); - result = {result}; - } catch (error) { - result = {error}; - } - results.push(result); - } - } - } - return results; - } - - getModifiedData(data, type) { - return this._getModifiedData(data, void 0, type); - } - - // Private - - _getTemplateInstance(template) { - if (!this._helpersRegistered) { - this._registerHelpers(); - } - - const cache = this._cache; - let instance = cache.get(template); - if (typeof instance === 'undefined') { - this._updateCacheSize(this._cacheMaxSize - 1); - instance = Handlebars.compile(template); - cache.set(template, instance); - } - - return instance; - } - - _renderTemplate(instance, data) { - const cleanupCallbacks = []; - const requirements = []; - try { - this._stateStack = [new Map()]; - this._requirements = requirements; - this._mediaProvider.requirements = requirements; - this._cleanupCallbacks = cleanupCallbacks; - const result = instance(data).trim(); - return {result, requirements}; - } finally { - for (const callback of cleanupCallbacks) { callback(); } - this._stateStack = null; - this._requirements = null; - this._mediaProvider.requirements = null; - this._cleanupCallbacks = null; - } - } - - _getModifiedData(data, commonData, type) { - if (typeof type === 'string') { - const typeInfo = this._dataTypes.get(type); - if (typeof typeInfo !== 'undefined') { - if (typeof commonData !== 'undefined') { - const {composeData} = typeInfo; - if (typeof composeData === 'function') { - data = composeData(data, commonData); - } - } - const {modifier} = typeInfo; - if (typeof modifier === 'function') { - data = modifier(data); - } - } - } - return data; - } - - _updateCacheSize(maxSize) { - const cache = this._cache; - let removeCount = cache.size - maxSize; - if (removeCount <= 0) { return; } - - for (const key of cache.keys()) { - cache.delete(key); - if (--removeCount <= 0) { break; } - } - } - - _registerHelpers() { - Handlebars.partials = Handlebars.templates; - - const helpers = [ - ['dumpObject', this._dumpObject.bind(this)], - ['furigana', this._furigana.bind(this)], - ['furiganaPlain', this._furiganaPlain.bind(this)], - ['kanjiLinks', this._kanjiLinks.bind(this)], - ['multiLine', this._multiLine.bind(this)], - ['sanitizeCssClass', this._sanitizeCssClass.bind(this)], - ['regexReplace', this._regexReplace.bind(this)], - ['regexMatch', this._regexMatch.bind(this)], - ['mergeTags', this._mergeTags.bind(this)], - ['eachUpTo', this._eachUpTo.bind(this)], - ['spread', this._spread.bind(this)], - ['op', this._op.bind(this)], - ['get', this._get.bind(this)], - ['set', this._set.bind(this)], - ['scope', this._scope.bind(this)], - ['property', this._property.bind(this)], - ['noop', this._noop.bind(this)], - ['isMoraPitchHigh', this._isMoraPitchHigh.bind(this)], - ['getKanaMorae', this._getKanaMorae.bind(this)], - ['typeof', this._getTypeof.bind(this)], - ['join', this._join.bind(this)], - ['concat', this._concat.bind(this)], - ['pitchCategories', this._pitchCategories.bind(this)], - ['formatGlossary', this._formatGlossary.bind(this)], - ['hasMedia', this._hasMedia.bind(this)], - ['getMedia', this._getMedia.bind(this)] - ]; - - for (const [name, helper] of helpers) { - this._registerHelper(name, helper); - } - - this._helpersRegistered = true; - } - - _registerHelper(name, helper) { - function wrapper(...args) { - return helper(this, ...args); - } - Handlebars.registerHelper(name, wrapper); - } - - _escape(text) { - return Handlebars.Utils.escapeExpression(text); - } - - _dumpObject(context, options) { - const dump = JSON.stringify(options.fn(context), null, 4); - return this._escape(dump); - } - - _furigana(context, ...args) { - const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); - const segs = this._japaneseUtil.distributeFurigana(expression, reading); - - let result = ''; - for (const {text, reading: reading2} of segs) { - if (reading2.length > 0) { - result += `${text}${reading2}`; - } else { - result += text; - } - } - - return result; - } - - _furiganaPlain(context, ...args) { - const {expression, reading} = this._getFuriganaExpressionAndReading(context, ...args); - const segs = this._japaneseUtil.distributeFurigana(expression, reading); - - let result = ''; - for (const {text, reading: reading2} of segs) { - if (reading2.length > 0) { - if (result.length > 0) { result += ' '; } - result += `${text}[${reading2}]`; - } else { - result += text; - } - } - - return result; - } - - _getFuriganaExpressionAndReading(context, ...args) { - const options = args[args.length - 1]; - if (args.length >= 3) { - return {expression: args[0], reading: args[1]}; - } else { - const {expression, reading} = options.fn(context); - return {expression, reading}; - } - } - - _kanjiLinks(context, options) { - const jp = this._japaneseUtil; - let result = ''; - for (const c of options.fn(context)) { - if (jp.isCodePointKanji(c.codePointAt(0))) { - result += `${c}`; - } else { - result += c; - } - } - - return result; - } - - _stringToMultiLineHtml(string) { - return string.split('\n').join('
'); - } - - _multiLine(context, options) { - return this._stringToMultiLineHtml(options.fn(context)); - } - - _sanitizeCssClass(context, options) { - return options.fn(context).replace(/[^_a-z0-9\u00a0-\uffff]/ig, '_'); - } - - _regexReplace(context, ...args) { - // Usage: - // {{#regexReplace regex string [flags]}}content{{/regexReplace}} - // regex: regular expression string - // string: string to replace - // flags: optional flags for regular expression - // e.g. "i" for case-insensitive, "g" for replace all - let value = args[args.length - 1].fn(context); - if (args.length >= 3) { - try { - const flags = args.length > 3 ? args[2] : 'g'; - const regex = new RegExp(args[0], flags); - value = value.replace(regex, args[1]); - } catch (e) { - return `${e}`; - } - } - return value; - } - - _regexMatch(context, ...args) { - // Usage: - // {{#regexMatch regex [flags]}}content{{/regexMatch}} - // regex: regular expression string - // flags: optional flags for regular expression - // e.g. "i" for case-insensitive, "g" for match all - let value = args[args.length - 1].fn(context); - if (args.length >= 2) { - try { - const flags = args.length > 2 ? args[1] : ''; - const regex = new RegExp(args[0], flags); - const parts = []; - value.replace(regex, (g0) => parts.push(g0)); - value = parts.join(''); - } catch (e) { - return `${e}`; - } - } - return value; - } - - _mergeTags(context, object, isGroupMode, isMergeMode) { - const tagSources = []; - if (isGroupMode || isMergeMode) { - for (const definition of object.definitions) { - tagSources.push(definition.definitionTags); - } - } else { - tagSources.push(object.definitionTags); - } - - const tags = new Set(); - for (const tagSource of tagSources) { - if (!Array.isArray(tagSource)) { continue; } - for (const tag of tagSource) { - tags.add(tag.name); - } - } - - return [...tags].join(', '); - } - - _eachUpTo(context, iterable, maxCount, options) { - if (iterable) { - const results = []; - let any = false; - for (const entry of iterable) { - any = true; - if (results.length >= maxCount) { break; } - const processedEntry = options.fn(entry); - results.push(processedEntry); - } - if (any) { - return results.join(''); - } - } - return options.inverse(context); - } - - _spread(context, ...args) { - const result = []; - for (let i = 0, ii = args.length - 1; i < ii; ++i) { - try { - result.push(...args[i]); - } catch (e) { - // NOP - } - } - return result; - } - - _op(context, ...args) { - switch (args.length) { - case 3: return this._evaluateUnaryExpression(args[0], args[1]); - case 4: return this._evaluateBinaryExpression(args[0], args[1], args[2]); - case 5: return this._evaluateTernaryExpression(args[0], args[1], args[2], args[3]); - default: return void 0; - } - } - - _evaluateUnaryExpression(operator, operand1) { - switch (operator) { - case '+': return +operand1; - case '-': return -operand1; - case '~': return ~operand1; - case '!': return !operand1; - default: return void 0; - } - } - - _evaluateBinaryExpression(operator, operand1, operand2) { - switch (operator) { - case '+': return operand1 + operand2; - case '-': return operand1 - operand2; - case '/': return operand1 / operand2; - case '*': return operand1 * operand2; - case '%': return operand1 % operand2; - case '**': return operand1 ** operand2; - case '==': return operand1 == operand2; // eslint-disable-line eqeqeq - case '!=': return operand1 != operand2; // eslint-disable-line eqeqeq - case '===': return operand1 === operand2; - case '!==': return operand1 !== operand2; - case '<': return operand1 < operand2; - case '<=': return operand1 <= operand2; - case '>': return operand1 > operand2; - case '>=': return operand1 >= operand2; - case '<<': return operand1 << operand2; - case '>>': return operand1 >> operand2; - case '>>>': return operand1 >>> operand2; - case '&': return operand1 & operand2; - case '|': return operand1 | operand2; - case '^': return operand1 ^ operand2; - case '&&': return operand1 && operand2; - case '||': return operand1 || operand2; - default: return void 0; - } - } - - _evaluateTernaryExpression(operator, operand1, operand2, operand3) { - switch (operator) { - case '?:': return operand1 ? operand2 : operand3; - default: return void 0; - } - } - - _get(context, key) { - for (let i = this._stateStack.length; --i >= 0;) { - const map = this._stateStack[i]; - if (map.has(key)) { - return map.get(key); - } - } - return void 0; - } - - _set(context, ...args) { - switch (args.length) { - case 2: - { - const [key, options] = args; - const value = options.fn(context); - this._stateStack[this._stateStack.length - 1].set(key, value); - } - break; - case 3: - { - const [key, value] = args; - this._stateStack[this._stateStack.length - 1].set(key, value); - } - break; - } - return ''; - } - - _scope(context, options) { - try { - this._stateStack.push(new Map()); - return options.fn(context); - } finally { - if (this._stateStack.length > 1) { - this._stateStack.pop(); - } - } - } - - _property(context, ...args) { - const ii = args.length - 1; - if (ii <= 0) { return void 0; } - - try { - let value = args[0]; - for (let i = 1; i < ii; ++i) { - value = value[args[i]]; - } - return value; - } catch (e) { - return void 0; - } - } - - _noop(context, options) { - return options.fn(context); - } - - _isMoraPitchHigh(context, index, position) { - return this._japaneseUtil.isMoraPitchHigh(index, position); - } - - _getKanaMorae(context, text) { - return this._japaneseUtil.getKanaMorae(`${text}`); - } - - _getTypeof(context, ...args) { - const ii = args.length - 1; - const value = (ii > 0 ? args[0] : args[ii].fn(context)); - return typeof value; - } - - _join(context, ...args) { - return args.length > 1 ? args.slice(1, args.length - 1).flat().join(args[0]) : ''; - } - - _concat(context, ...args) { - let result = ''; - for (let i = 0, ii = args.length - 1; i < ii; ++i) { - result += args[i]; - } - return result; - } - - _pitchCategories(context, data) { - const {pronunciations, headwords} = data.dictionaryEntry; - const categories = new Set(); - for (const {headwordIndex, pitches} of pronunciations) { - const {reading, wordClasses} = headwords[headwordIndex]; - const isVerbOrAdjective = DictionaryDataUtil.isNonNounVerbOrAdjective(wordClasses); - for (const {position} of pitches) { - const category = this._japaneseUtil.getPitchCategory(reading, position, isVerbOrAdjective); - if (category !== null) { - categories.add(category); - } - } - } - return [...categories]; - } - - _getTemporaryElement() { - let element = this._temporaryElement; - if (element === null) { - element = document.createElement('div'); - this._temporaryElement = element; - } - return element; - } - - _getHtml(node) { - const container = this._getTemporaryElement(); - container.appendChild(node); - this._normalizeHtml(container); - const result = container.innerHTML; - container.textContent = ''; - return result; - } - - _normalizeHtml(root) { - const {ELEMENT_NODE, TEXT_NODE} = Node; - const treeWalker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT); - const elements = []; - const textNodes = []; - while (true) { - const node = treeWalker.nextNode(); - if (node === null) { break; } - switch (node.nodeType) { - case ELEMENT_NODE: - elements.push(node); - break; - case TEXT_NODE: - textNodes.push(node); - break; - } - } - this._cssStyleApplier.applyClassStyles(elements); - for (const element of elements) { - const {dataset} = element; - for (const key of Object.keys(dataset)) { - delete dataset[key]; - } - } - for (const textNode of textNodes) { - this._replaceNewlines(textNode); - } - } - - _replaceNewlines(textNode) { - const parts = textNode.nodeValue.split('\n'); - if (parts.length <= 1) { return; } - const {parentNode} = textNode; - if (parentNode === null) { return; } - const fragment = document.createDocumentFragment(); - for (let i = 0, ii = parts.length; i < ii; ++i) { - if (i > 0) { fragment.appendChild(document.createElement('br')); } - fragment.appendChild(document.createTextNode(parts[i])); - } - parentNode.replaceChild(fragment, textNode); - } - - _createStructuredContentGenerator(data) { - const mediaLoader = { - loadMedia: async (path, dictionary, onLoad, onUnload) => { - const imageUrl = this._mediaProvider.getMedia(data, ['dictionaryMedia', path], {dictionary, format: 'fileName', default: null}); - if (imageUrl !== null) { - onLoad(imageUrl); - this._cleanupCallbacks.push(() => onUnload(true)); - } - } - }; - return new StructuredContentGenerator(mediaLoader, document); - } - - _formatGlossary(context, dictionary, options) { - const data = options.data.root; - const content = options.fn(context); - if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); } - if (!(typeof content === 'object' && content !== null)) { return ''; } - switch (content.type) { - case 'image': return this._formatGlossaryImage(content, dictionary, data); - case 'structured-content': return this._formatStructuredContent(content, dictionary, data); - } - return ''; - } - - _formatGlossaryImage(content, dictionary, data) { - const structuredContentGenerator = this._createStructuredContentGenerator(data); - const node = structuredContentGenerator.createDefinitionImage(content, dictionary); - return this._getHtml(node); - } - - _formatStructuredContent(content, dictionary, data) { - const structuredContentGenerator = this._createStructuredContentGenerator(data); - const node = structuredContentGenerator.createStructuredContent(content.content, dictionary); - return node !== null ? this._getHtml(node) : ''; - } - - _hasMedia(context, ...args) { - const ii = args.length - 1; - const options = args[ii]; - return this._mediaProvider.hasMedia(options.data.root, args.slice(0, ii), options.hash); - } - - _getMedia(context, ...args) { - const ii = args.length - 1; - const options = args[ii]; - return this._mediaProvider.getMedia(options.data.root, args.slice(0, ii), options.hash); - } -} diff --git a/ext/popup.html b/ext/popup.html index 065bcfa1..1f336943 100644 --- a/ext/popup.html +++ b/ext/popup.html @@ -110,9 +110,9 @@ - - + + @@ -125,8 +125,8 @@ - - + + diff --git a/ext/search.html b/ext/search.html index beceeb18..f32b5412 100644 --- a/ext/search.html +++ b/ext/search.html @@ -94,12 +94,12 @@ - + + - @@ -111,8 +111,8 @@ - - + + diff --git a/ext/settings.html b/ext/settings.html index b45d8c22..9d2d9791 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -3449,10 +3449,10 @@ - - + + diff --git a/ext/sw.js b/ext/sw.js index a1fb7ea1..6394260e 100644 --- a/ext/sw.js +++ b/ext/sw.js @@ -39,9 +39,9 @@ self.importScripts( '/js/general/regex-util.js', '/js/general/text-source-map.js', '/js/language/deinflector.js', - '/js/language/dictionary-data-util.js', '/js/language/dictionary-database.js', - '/js/language/japanese-util.js', + '/js/language/sandbox/dictionary-data-util.js', + '/js/language/sandbox/japanese-util.js', '/js/language/translator.js', '/js/media/audio-downloader.js', '/js/media/media-util.js', diff --git a/ext/template-renderer.html b/ext/template-renderer.html index a3812b37..9e0769f9 100644 --- a/ext/template-renderer.html +++ b/ext/template-renderer.html @@ -17,16 +17,16 @@ - - - - - - - - + + + + + + + + - + -- cgit v1.2.3