2020-03-07 20:14:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* 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-05 23:34:31 +00:00
|
|
|
constructor({audioSystem, renderTemplate}) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
const note = {
|
|
|
|
fields: {},
|
|
|
|
tags,
|
|
|
|
deckName: modeOptions.deck,
|
|
|
|
modelName: modeOptions.model
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const [fieldName, fieldValue] of modeOptionsFieldEntries) {
|
2020-03-15 21:13:00 +00:00
|
|
|
note.fields[fieldName] = await this.formatField(fieldValue, definition, mode, context, options, templates, null);
|
2020-03-07 20:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isKanji && definition.audio) {
|
|
|
|
const audioFields = [];
|
|
|
|
|
|
|
|
for (const [fieldName, fieldValue] of modeOptionsFieldEntries) {
|
|
|
|
if (fieldValue.includes('{audio}')) {
|
|
|
|
audioFields.push(fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audioFields.length > 0) {
|
|
|
|
note.audio = {
|
|
|
|
url: definition.audio.url,
|
|
|
|
filename: definition.audio.filename,
|
|
|
|
skipHash: '7e2c2f954ef6051373ba916f000168dc', // hash of audio data that should be skipped
|
|
|
|
fields: audioFields
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-05 23:34:31 +00:00
|
|
|
async injectAudio(definition, fields, sources, optionsContext) {
|
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-04-06 21:34:36 +00:00
|
|
|
const {uri} = await this._audioSystem.getDefinitionAudio(audioSourceDefinition, sources, {tts: false, optionsContext});
|
2020-04-02 01:09:49 +00:00
|
|
|
const filename = this._createInjectedAudioFileName(audioSourceDefinition);
|
|
|
|
if (filename !== null) {
|
|
|
|
definition.audio = {url: uri, filename};
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2020-04-02 01:13:55 +00:00
|
|
|
// NOP
|
2020-04-02 01:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async injectScreenshot(definition, fields, screenshot, anki) {
|
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-04-02 01:11:01 +00:00
|
|
|
const filename = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`;
|
2020-04-02 01:09:49 +00:00
|
|
|
const data = screenshot.dataUrl.replace(/^data:[\w\W]*?,/, '');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await anki.storeMediaFile(filename, data);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
definition.screenshotFileName = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createInjectedAudioFileName(definition) {
|
|
|
|
const {reading, expression} = definition;
|
|
|
|
if (!reading && !expression) { return null; }
|
|
|
|
|
|
|
|
let filename = 'yomichan';
|
|
|
|
if (reading) { filename += `_${reading}`; }
|
|
|
|
if (expression) { filename += `_${expression}`; }
|
|
|
|
filename += '.mp3';
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|