diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-05-16 15:24:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-16 15:24:38 -0400 |
commit | 66d048832f2dc30e11e6be4c68beab23c7d8adef (patch) | |
tree | 9cb1d83faacf133f61c3a96156ea6de1541e5233 /ext/js | |
parent | 41ee167dfdfcd794634e0459185a929e814b0493 (diff) |
Sentence termination character mode (#1682)
* Change enableTerminationCharacters to terminationCharacterMode
* Update settings
* Update sentence extraction
* Update tests
* Add tests
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/data/options-util.js | 14 | ||||
-rw-r--r-- | ext/js/dom/document-util.js | 7 | ||||
-rw-r--r-- | ext/js/language/text-scanner.js | 17 |
3 files changed, 30 insertions, 8 deletions
diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index cb7946f7..cb58206f 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -460,7 +460,8 @@ class OptionsUtil { {async: true, update: this._updateVersion8.bind(this)}, {async: false, update: this._updateVersion9.bind(this)}, {async: true, update: this._updateVersion10.bind(this)}, - {async: true, update: this._updateVersion11.bind(this)} + {async: false, update: this._updateVersion11.bind(this)}, + {async: false, update: this._updateVersion12.bind(this)} ]; } @@ -811,4 +812,15 @@ class OptionsUtil { } return options; } + + _updateVersion12(options) { + // Version 12 changes: + // Changed sentenceParsing.enableTerminationCharacters to sentenceParsing.terminationCharacterMode. + for (const profile of options.profiles) { + const {sentenceParsing} = profile.options; + sentenceParsing.terminationCharacterMode = sentenceParsing.enableTerminationCharacters ? 'custom' : 'newlines'; + delete sentenceParsing.enableTerminationCharacters; + } + return options; + } } diff --git a/ext/js/dom/document-util.js b/ext/js/dom/document-util.js index 8284ffa5..da4d3e61 100644 --- a/ext/js/dom/document-util.js +++ b/ext/js/dom/document-util.js @@ -69,6 +69,7 @@ class DocumentUtil { * @param source The text source object, either `TextSourceRange` or `TextSourceElement`. * @param layoutAwareScan Whether or not layout-aware scan mode should be used. * @param extent The length of the sentence to extract. + * @param terminateAtNewlines Whether or not a sentence should be terminated at newline characters. * @param terminatorMap A mapping of characters that terminate a sentence. * Format: * ```js @@ -87,7 +88,7 @@ class DocumentUtil { * ``` * @returns The sentence and the offset to the original source: `{sentence: string, offset: integer}`. */ - extractSentence(source, layoutAwareScan, extent, terminatorMap, forwardQuoteMap, backwardQuoteMap) { + extractSentence(source, layoutAwareScan, extent, terminateAtNewlines, terminatorMap, forwardQuoteMap, backwardQuoteMap) { // Scan text source = source.clone(); const startLength = source.setStartOffset(extent, layoutAwareScan); @@ -102,7 +103,7 @@ class DocumentUtil { let quoteStack = []; for (; pos1 > 0; --pos1) { const c = text[pos1 - 1]; - if (c === '\n') { break; } + if (c === '\n' && terminateAtNewlines) { break; } if (quoteStack.length === 0) { const terminatorInfo = terminatorMap.get(c); @@ -133,7 +134,7 @@ class DocumentUtil { quoteStack = []; for (; pos2 < textLength; ++pos2) { const c = text[pos2]; - if (c === '\n') { break; } + if (c === '\n' && terminateAtNewlines) { break; } if (quoteStack.length === 0) { const terminatorInfo = terminatorMap.get(c); diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index a49627f8..2ebf26e2 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -63,6 +63,7 @@ class TextScanner extends EventDispatcher { this._layoutAwareScan = false; this._preventMiddleMouse = false; this._sentenceScanExtent = 0; + this._sentenceTerminateAtNewlines = true; this._sentenceTerminatorMap = new Map(); this._sentenceForwardQuoteMap = new Map(); this._sentenceBackwardQuoteMap = new Map(); @@ -209,19 +210,23 @@ class TextScanner extends EventDispatcher { this._preventMiddleMouse = preventMiddleMouse; } if (typeof sentenceParsingOptions === 'object' && sentenceParsingOptions !== null) { - const {scanExtent, enableTerminationCharacters, terminationCharacters} = sentenceParsingOptions; - const hasTerminationCharacters = (typeof terminationCharacters === 'object' && Array.isArray(terminationCharacters)); + const {scanExtent, terminationCharacterMode, terminationCharacters} = sentenceParsingOptions; if (typeof scanExtent === 'number') { this._sentenceScanExtent = sentenceParsingOptions.scanExtent; } - if (typeof enableTerminationCharacters === 'boolean' || hasTerminationCharacters) { + if (typeof terminationCharacterMode === 'string') { + this._sentenceTerminateAtNewlines = (terminationCharacterMode === 'custom' || terminationCharacterMode === 'newlines'); const sentenceTerminatorMap = this._sentenceTerminatorMap; const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap; const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap; sentenceTerminatorMap.clear(); sentenceForwardQuoteMap.clear(); sentenceBackwardQuoteMap.clear(); - if (enableTerminationCharacters !== false && hasTerminationCharacters) { + if ( + typeof terminationCharacters === 'object' && + Array.isArray(terminationCharacters) && + (terminationCharacterMode === 'custom' || terminationCharacterMode === 'custom-no-newlines') + ) { for (const {enabled, character1, character2, includeCharacterAtStart, includeCharacterAtEnd} of terminationCharacters) { if (!enabled) { continue; } if (character2 === null) { @@ -841,6 +846,7 @@ class TextScanner extends EventDispatcher { async _findTermDictionaryEntries(textSource, optionsContext) { const scanLength = this._scanLength; const sentenceScanExtent = this._sentenceScanExtent; + const sentenceTerminateAtNewlines = this._sentenceTerminateAtNewlines; const sentenceTerminatorMap = this._sentenceTerminatorMap; const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap; const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap; @@ -856,6 +862,7 @@ class TextScanner extends EventDispatcher { textSource, layoutAwareScan, sentenceScanExtent, + sentenceTerminateAtNewlines, sentenceTerminatorMap, sentenceForwardQuoteMap, sentenceBackwardQuoteMap @@ -866,6 +873,7 @@ class TextScanner extends EventDispatcher { async _findKanjiDictionaryEntries(textSource, optionsContext) { const sentenceScanExtent = this._sentenceScanExtent; + const sentenceTerminateAtNewlines = this._sentenceTerminateAtNewlines; const sentenceTerminatorMap = this._sentenceTerminatorMap; const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap; const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap; @@ -881,6 +889,7 @@ class TextScanner extends EventDispatcher { textSource, layoutAwareScan, sentenceScanExtent, + sentenceTerminateAtNewlines, sentenceTerminatorMap, sentenceForwardQuoteMap, sentenceBackwardQuoteMap |