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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AnkiNoteBuilder {
|
2020-04-12 16:46:32 +00:00
|
|
|
constructor({anki, audioSystem, renderTemplate}) {
|
|
|
|
this._anki = anki;
|
2020-04-05 23:34:31 +00:00
|
|
|
this._audioSystem = audioSystem;
|
2020-03-07 20:20:45 +00:00
|
|
|
this._renderTemplate = renderTemplate;
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 21:13:00 +00:00
|
|
|
async createNote(definition, mode, context, options, templates) {
|
2020-03-07 20:14:05 +00:00
|
|
|
const isKanji = (mode === 'kanji');
|
|
|
|
const tags = options.anki.tags;
|
|
|
|
const modeOptions = isKanji ? options.anki.kanji : options.anki.terms;
|
|
|
|
const modeOptionsFieldEntries = Object.entries(modeOptions.fields);
|
|
|
|
|
2020-06-21 19:54:34 +00:00
|
|
|
const fields = {};
|
2020-03-07 20:14:05 +00:00
|
|
|
const note = {
|
2020-06-21 19:54:34 +00:00
|
|
|
fields,
|
2020-03-07 20:14:05 +00:00
|
|
|
tags,
|
|
|
|
deckName: modeOptions.deck,
|
2020-04-27 22:10:59 +00:00
|
|
|
modelName: modeOptions.model,
|
|
|
|
options: {
|
|
|
|
duplicateScope: options.anki.duplicateScope
|
|
|
|
}
|
2020-03-07 20:14:05 +00:00
|
|
|
};
|
|
|
|
|
2020-06-21 19:54:34 +00:00
|
|
|
const formattedFieldValuePromises = [];
|
|
|
|
for (const [, fieldValue] of modeOptionsFieldEntries) {
|
|
|
|
const formattedFieldValuePromise = this.formatField(fieldValue, definition, mode, context, options, templates, null);
|
|
|
|
formattedFieldValuePromises.push(formattedFieldValuePromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
|
|
|
|
for (let i = 0, ii = modeOptionsFieldEntries.length; i < ii; ++i) {
|
|
|
|
const fieldName = modeOptionsFieldEntries[i][0];
|
|
|
|
const formattedFieldValue = formattedFieldValues[i];
|
|
|
|
fields[fieldName] = formattedFieldValue;
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2020-03-15 21:13:00 +00:00
|
|
|
async formatField(field, definition, mode, context, options, templates, errors=null) {
|
2020-03-07 20:14:05 +00:00
|
|
|
const data = {
|
|
|
|
marker: null,
|
|
|
|
definition,
|
|
|
|
group: options.general.resultOutputMode === 'group',
|
|
|
|
merge: options.general.resultOutputMode === 'merge',
|
|
|
|
modeTermKanji: mode === 'term-kanji',
|
|
|
|
modeTermKana: mode === 'term-kana',
|
|
|
|
modeKanji: mode === 'kanji',
|
2020-03-15 21:13:00 +00:00
|
|
|
compactGlossaries: options.general.compactGlossaries,
|
|
|
|
context
|
2020-03-07 20:14:05 +00:00
|
|
|
};
|
|
|
|
const pattern = /\{([\w-]+)\}/g;
|
2020-03-07 20:23:32 +00:00
|
|
|
return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => {
|
2020-03-07 20:14:05 +00:00
|
|
|
data.marker = marker;
|
|
|
|
try {
|
2020-03-07 20:20:45 +00:00
|
|
|
return await this._renderTemplate(templates, data);
|
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-05-02 16:50:16 +00:00
|
|
|
async injectAudio(definition, fields, sources, customSourceUrl) {
|
2020-04-02 01:12:59 +00:00
|
|
|
if (!this._containsMarker(fields, 'audio')) { return; }
|
2020-04-02 01:09:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const expressions = definition.expressions;
|
|
|
|
const audioSourceDefinition = Array.isArray(expressions) ? expressions[0] : definition;
|
|
|
|
|
2020-05-06 23:25:56 +00:00
|
|
|
let fileName = this._createInjectedAudioFileName(audioSourceDefinition);
|
|
|
|
if (fileName === null) { return; }
|
|
|
|
fileName = AnkiNoteBuilder.replaceInvalidFileNameCharacters(fileName);
|
2020-05-02 16:50:16 +00:00
|
|
|
|
|
|
|
const {audio} = await this._audioSystem.getDefinitionAudio(
|
|
|
|
audioSourceDefinition,
|
|
|
|
sources,
|
|
|
|
{
|
|
|
|
textToSpeechVoice: null,
|
|
|
|
customSourceUrl,
|
|
|
|
binary: true,
|
|
|
|
disableCache: true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const data = AnkiNoteBuilder.arrayBufferToBase64(audio);
|
2020-05-06 23:25:56 +00:00
|
|
|
await this._anki.storeMediaFile(fileName, data);
|
2020-05-02 16:50:16 +00:00
|
|
|
|
2020-05-06 23:25:56 +00:00
|
|
|
definition.audioFileName = fileName;
|
2020-04-02 01:09:49 +00:00
|
|
|
} catch (e) {
|
2020-04-02 01:13:55 +00:00
|
|
|
// NOP
|
2020-04-02 01:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:46:32 +00:00
|
|
|
async injectScreenshot(definition, fields, screenshot) {
|
2020-04-02 01:12:59 +00:00
|
|
|
if (!this._containsMarker(fields, 'screenshot')) { return; }
|
2020-04-02 01:09:49 +00:00
|
|
|
|
|
|
|
const now = new Date(Date.now());
|
2020-05-06 23:25:56 +00:00
|
|
|
let fileName = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`;
|
|
|
|
fileName = AnkiNoteBuilder.replaceInvalidFileNameCharacters(fileName);
|
2020-04-02 01:09:49 +00:00
|
|
|
const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, '');
|
|
|
|
|
|
|
|
try {
|
2020-05-06 23:25:56 +00:00
|
|
|
await this._anki.storeMediaFile(fileName, data);
|
2020-04-02 01:09:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:25:56 +00:00
|
|
|
definition.screenshotFileName = fileName;
|
2020-04-02 01:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_createInjectedAudioFileName(definition) {
|
|
|
|
const {reading, expression} = definition;
|
|
|
|
if (!reading && !expression) { return null; }
|
|
|
|
|
2020-05-06 23:25:56 +00:00
|
|
|
let fileName = 'yomichan';
|
|
|
|
if (reading) { fileName += `_${reading}`; }
|
|
|
|
if (expression) { fileName += `_${expression}`; }
|
|
|
|
fileName += '.mp3';
|
|
|
|
fileName = fileName.replace(/\]/g, '');
|
|
|
|
return fileName;
|
2020-04-02 01:09:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 01:11:01 +00:00
|
|
|
_dateToString(date) {
|
|
|
|
const year = date.getUTCFullYear();
|
|
|
|
const month = date.getUTCMonth().toString().padStart(2, '0');
|
|
|
|
const day = date.getUTCDate().toString().padStart(2, '0');
|
|
|
|
const hours = date.getUTCHours().toString().padStart(2, '0');
|
|
|
|
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
|
|
|
|
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
|
|
|
|
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:12:59 +00:00
|
|
|
_containsMarker(fields, marker) {
|
|
|
|
marker = `{${marker}}`;
|
|
|
|
for (const fieldValue of Object.values(fields)) {
|
|
|
|
if (fieldValue.includes(marker)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:25:56 +00:00
|
|
|
static replaceInvalidFileNameCharacters(fileName) {
|
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
return fileName.replace(/[<>:"/\\|?*\x00-\x1F]/g, '-');
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:50:16 +00:00
|
|
|
static arrayBufferToBase64(arrayBuffer) {
|
2020-05-24 18:01:21 +00:00
|
|
|
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
2020-05-02 16:50:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 20:23:32 +00:00
|
|
|
static stringReplaceAsync(str, regex, replacer) {
|
|
|
|
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
|
|
|
}
|