2020-03-07 20:14:05 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2020-2021 Yomichan Authors
|
2020-03-07 20:14:05 +00:00
|
|
|
*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-08-01 20:23:33 +00:00
|
|
|
/* global
|
|
|
|
* DictionaryDataUtil
|
2021-01-11 00:28:50 +00:00
|
|
|
* TemplateRendererProxy
|
2020-08-01 20:23:33 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-07 20:14:05 +00:00
|
|
|
class AnkiNoteBuilder {
|
2021-01-11 00:28:50 +00:00
|
|
|
constructor(enabled) {
|
2020-12-20 17:20:29 +00:00
|
|
|
this._markerPattern = /\{([\w-]+)\}/g;
|
2021-01-11 00:28:50 +00:00
|
|
|
this._templateRenderer = enabled ? new TemplateRendererProxy() : null;
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 15:54:40 +00:00
|
|
|
async createNote({
|
|
|
|
definition,
|
|
|
|
mode,
|
|
|
|
context,
|
|
|
|
templates,
|
2021-01-04 00:40:12 +00:00
|
|
|
deckName,
|
|
|
|
modelName,
|
|
|
|
fields,
|
2020-09-09 15:54:40 +00:00
|
|
|
tags=[],
|
2020-11-08 21:25:07 +00:00
|
|
|
checkForDuplicates=true,
|
2020-09-09 15:54:40 +00:00
|
|
|
duplicateScope='collection',
|
|
|
|
resultOutputMode='split',
|
2020-11-26 04:22:05 +00:00
|
|
|
glossaryLayoutMode='default',
|
2020-11-13 01:34:11 +00:00
|
|
|
compactTags=false,
|
2020-09-09 15:54:40 +00:00
|
|
|
errors=null
|
|
|
|
}) {
|
2020-10-31 23:03:45 +00:00
|
|
|
let duplicateScopeDeckName = null;
|
|
|
|
let duplicateScopeCheckChildren = false;
|
|
|
|
if (duplicateScope === 'deck-root') {
|
|
|
|
duplicateScope = 'deck';
|
2021-01-04 00:40:12 +00:00
|
|
|
duplicateScopeDeckName = this.getRootDeckName(deckName);
|
2020-10-31 23:03:45 +00:00
|
|
|
duplicateScopeCheckChildren = true;
|
|
|
|
}
|
|
|
|
|
2020-11-26 04:22:05 +00:00
|
|
|
const data = this._createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags);
|
2020-06-21 19:54:34 +00:00
|
|
|
const formattedFieldValuePromises = [];
|
2021-01-04 00:40:12 +00:00
|
|
|
for (const [, fieldValue] of fields) {
|
2020-09-09 20:57:35 +00:00
|
|
|
const formattedFieldValuePromise = this._formatField(fieldValue, data, templates, errors);
|
2020-06-21 19:54:34 +00:00
|
|
|
formattedFieldValuePromises.push(formattedFieldValuePromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
|
2021-01-11 00:28:50 +00:00
|
|
|
const noteFields = {};
|
2021-01-04 00:40:12 +00:00
|
|
|
for (let i = 0, ii = fields.length; i < ii; ++i) {
|
|
|
|
const fieldName = fields[i][0];
|
2020-06-21 19:54:34 +00:00
|
|
|
const formattedFieldValue = formattedFieldValues[i];
|
2020-09-09 15:54:40 +00:00
|
|
|
noteFields[fieldName] = formattedFieldValue;
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 00:28:50 +00:00
|
|
|
return {
|
|
|
|
fields: noteFields,
|
|
|
|
tags,
|
|
|
|
deckName,
|
|
|
|
modelName,
|
|
|
|
options: {
|
|
|
|
allowDuplicate: !checkForDuplicates,
|
|
|
|
duplicateScope,
|
|
|
|
duplicateScopeOptions: {
|
|
|
|
deckName: duplicateScopeDeckName,
|
|
|
|
checkChildren: duplicateScopeCheckChildren
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 19:04:54 +00:00
|
|
|
containsMarker(fields, marker) {
|
|
|
|
marker = `{${marker}}`;
|
2021-01-04 00:40:12 +00:00
|
|
|
for (const [, fieldValue] of fields) {
|
2020-09-10 19:04:54 +00:00
|
|
|
if (fieldValue.includes(marker)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:20:29 +00:00
|
|
|
containsAnyMarker(field) {
|
|
|
|
const result = this._markerPattern.test(field);
|
|
|
|
this._markerPattern.lastIndex = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:03:45 +00:00
|
|
|
getRootDeckName(deckName) {
|
|
|
|
const index = deckName.indexOf('::');
|
|
|
|
return index >= 0 ? deckName.substring(0, index) : deckName;
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:57:35 +00:00
|
|
|
// Private
|
|
|
|
|
2020-11-26 04:22:05 +00:00
|
|
|
_createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags) {
|
2020-08-01 20:23:33 +00:00
|
|
|
const pitches = DictionaryDataUtil.getPitchAccentInfos(definition);
|
|
|
|
const pitchCount = pitches.reduce((i, v) => i + v.pitches.length, 0);
|
2020-11-28 19:30:50 +00:00
|
|
|
const uniqueExpressions = new Set();
|
|
|
|
const uniqueReadings = new Set();
|
|
|
|
if (definition.type !== 'kanji') {
|
|
|
|
for (const {expression, reading} of definition.expressions) {
|
|
|
|
uniqueExpressions.add(expression);
|
|
|
|
uniqueReadings.add(reading);
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 20:23:33 +00:00
|
|
|
return {
|
2020-03-07 20:14:05 +00:00
|
|
|
marker: null,
|
|
|
|
definition,
|
2020-11-28 19:30:50 +00:00
|
|
|
uniqueExpressions: [...uniqueExpressions],
|
|
|
|
uniqueReadings: [...uniqueReadings],
|
2020-08-01 20:23:33 +00:00
|
|
|
pitches,
|
|
|
|
pitchCount,
|
2020-09-09 15:54:40 +00:00
|
|
|
group: resultOutputMode === 'group',
|
|
|
|
merge: resultOutputMode === 'merge',
|
2020-03-07 20:14:05 +00:00
|
|
|
modeTermKanji: mode === 'term-kanji',
|
|
|
|
modeTermKana: mode === 'term-kana',
|
|
|
|
modeKanji: mode === 'kanji',
|
2020-11-26 04:22:05 +00:00
|
|
|
compactGlossaries: (glossaryLayoutMode === 'compact'),
|
|
|
|
glossaryLayoutMode,
|
2020-11-13 01:34:11 +00:00
|
|
|
compactTags,
|
2020-03-15 21:13:00 +00:00
|
|
|
context
|
2020-03-07 20:14:05 +00:00
|
|
|
};
|
2020-08-01 20:23:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 20:57:35 +00:00
|
|
|
async _formatField(field, data, templates, errors=null) {
|
2020-12-20 17:20:29 +00:00
|
|
|
return await this._stringReplaceAsync(field, this._markerPattern, async (g0, marker) => {
|
2020-03-07 20:14:05 +00:00
|
|
|
try {
|
2020-09-04 21:57:05 +00:00
|
|
|
return await this._renderTemplate(templates, data, marker);
|
2020-03-07 20:14:05 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (errors) { errors.push(e); }
|
|
|
|
return `{${marker}-render-error}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-03-07 20:23:32 +00:00
|
|
|
|
2021-01-11 00:28:50 +00:00
|
|
|
async _stringReplaceAsync(str, regex, replacer) {
|
2020-03-07 20:23:32 +00:00
|
|
|
let match;
|
|
|
|
let index = 0;
|
|
|
|
const parts = [];
|
|
|
|
while ((match = regex.exec(str)) !== null) {
|
|
|
|
parts.push(str.substring(index, match.index), replacer(...match, match.index, str));
|
|
|
|
index = regex.lastIndex;
|
|
|
|
}
|
|
|
|
if (parts.length === 0) {
|
2021-01-11 00:28:50 +00:00
|
|
|
return str;
|
2020-03-07 20:23:32 +00:00
|
|
|
}
|
|
|
|
parts.push(str.substring(index));
|
2021-01-11 00:28:50 +00:00
|
|
|
return (await Promise.all(parts)).join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
async _renderTemplate(template, data, marker) {
|
|
|
|
return await this._templateRenderer.render(template, {data, marker}, 'ankiNote');
|
2020-03-07 20:23:32 +00:00
|
|
|
}
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|