2020-03-07 20:14:05 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2020 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
|
|
|
|
*/
|
|
|
|
|
2020-03-07 20:14:05 +00:00
|
|
|
class AnkiNoteBuilder {
|
2020-09-10 19:04:54 +00:00
|
|
|
constructor({renderTemplate}) {
|
2020-03-07 20:20:45 +00:00
|
|
|
this._renderTemplate = renderTemplate;
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 15:54:40 +00:00
|
|
|
async createNote({
|
2020-09-09 20:57:35 +00:00
|
|
|
anki=null,
|
2020-09-09 15:54:40 +00:00
|
|
|
definition,
|
|
|
|
mode,
|
|
|
|
context,
|
|
|
|
templates,
|
|
|
|
tags=[],
|
|
|
|
duplicateScope='collection',
|
|
|
|
resultOutputMode='split',
|
|
|
|
compactGlossaries=false,
|
|
|
|
modeOptions: {fields, deck, model},
|
2020-09-09 20:57:35 +00:00
|
|
|
audioDetails=null,
|
|
|
|
screenshotDetails=null,
|
2020-09-26 17:45:48 +00:00
|
|
|
clipboardDetails=null,
|
2020-09-09 15:54:40 +00:00
|
|
|
errors=null
|
|
|
|
}) {
|
2020-09-09 20:57:35 +00:00
|
|
|
if (anki !== null) {
|
2020-09-26 17:45:48 +00:00
|
|
|
await this._injectMedia(anki, definition, fields, mode, audioDetails, screenshotDetails, clipboardDetails);
|
2020-09-09 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 23:03:45 +00:00
|
|
|
let duplicateScopeDeckName = null;
|
|
|
|
let duplicateScopeCheckChildren = false;
|
|
|
|
if (duplicateScope === 'deck-root') {
|
|
|
|
duplicateScope = 'deck';
|
|
|
|
duplicateScopeDeckName = this.getRootDeckName(deck);
|
|
|
|
duplicateScopeCheckChildren = true;
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:54:40 +00:00
|
|
|
const fieldEntries = Object.entries(fields);
|
|
|
|
const noteFields = {};
|
2020-03-07 20:14:05 +00:00
|
|
|
const note = {
|
2020-09-09 15:54:40 +00:00
|
|
|
fields: noteFields,
|
2020-03-07 20:14:05 +00:00
|
|
|
tags,
|
2020-09-09 15:54:40 +00:00
|
|
|
deckName: deck,
|
|
|
|
modelName: model,
|
2020-10-31 23:03:45 +00:00
|
|
|
options: {
|
|
|
|
duplicateScope,
|
|
|
|
duplicateScopeOptions: {
|
|
|
|
deckName: duplicateScopeDeckName,
|
|
|
|
checkChildren: duplicateScopeCheckChildren
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 20:14:05 +00:00
|
|
|
};
|
|
|
|
|
2020-09-09 20:57:35 +00:00
|
|
|
const data = this._createNoteData(definition, mode, context, resultOutputMode, compactGlossaries);
|
2020-06-21 19:54:34 +00:00
|
|
|
const formattedFieldValuePromises = [];
|
2020-09-09 15:54:40 +00:00
|
|
|
for (const [, fieldValue] of fieldEntries) {
|
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);
|
2020-09-09 15:54:40 +00:00
|
|
|
for (let i = 0, ii = fieldEntries.length; i < ii; ++i) {
|
|
|
|
const fieldName = fieldEntries[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
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2020-09-10 19:04:54 +00:00
|
|
|
containsMarker(fields, marker) {
|
|
|
|
marker = `{${marker}}`;
|
|
|
|
for (const fieldValue of Object.values(fields)) {
|
|
|
|
if (fieldValue.includes(marker)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
_createNoteData(definition, mode, context, resultOutputMode, compactGlossaries) {
|
2020-08-01 20:23:33 +00:00
|
|
|
const pitches = DictionaryDataUtil.getPitchAccentInfos(definition);
|
|
|
|
const pitchCount = pitches.reduce((i, v) => i + v.pitches.length, 0);
|
|
|
|
return {
|
2020-03-07 20:14:05 +00:00
|
|
|
marker: null,
|
|
|
|
definition,
|
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-09-09 15:54:40 +00:00
|
|
|
compactGlossaries,
|
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-03-07 20:14:05 +00:00
|
|
|
const pattern = /\{([\w-]+)\}/g;
|
2020-09-09 20:57:35 +00:00
|
|
|
return await this._stringReplaceAsync(field, pattern, 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
|
|
|
|
2020-09-09 20:57:35 +00:00
|
|
|
_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) {
|
|
|
|
return Promise.resolve(str);
|
|
|
|
}
|
|
|
|
parts.push(str.substring(index));
|
|
|
|
return Promise.all(parts).then((v) => v.join(''));
|
|
|
|
}
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|