2016-08-24 05:28:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
/*
|
|
|
|
* Promise
|
|
|
|
*/
|
2017-01-10 03:42:54 +00:00
|
|
|
|
2016-10-22 03:08:16 +00:00
|
|
|
function promiseCallback(promise, callback) {
|
|
|
|
return promise.then(result => {
|
2017-03-05 03:16:19 +00:00
|
|
|
callback({result});
|
2016-10-22 03:08:16 +00:00
|
|
|
}).catch(error => {
|
|
|
|
callback({error});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
|
2017-04-22 20:02:06 +00:00
|
|
|
/*
|
|
|
|
* Japanese
|
|
|
|
*/
|
|
|
|
|
|
|
|
function jpIsKanji(c) {
|
|
|
|
const code = c.charCodeAt(0);
|
|
|
|
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function jpIsKana(c) {
|
|
|
|
return wanakana.isKana(c);
|
|
|
|
}
|
|
|
|
|
2017-07-09 23:29:52 +00:00
|
|
|
function jpKatakanaToHiragana(text) {
|
|
|
|
return wanakana._katakanaToHiragana(text);
|
|
|
|
}
|
|
|
|
|
2017-04-22 20:02:06 +00:00
|
|
|
|
2017-03-18 20:00:29 +00:00
|
|
|
/*
|
|
|
|
* Commands
|
|
|
|
*/
|
|
|
|
|
|
|
|
function commandExec(command) {
|
|
|
|
instYomi().onCommand(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
/*
|
|
|
|
* Instance
|
|
|
|
*/
|
|
|
|
|
|
|
|
function instYomi() {
|
2017-03-03 04:23:56 +00:00
|
|
|
return chrome.extension.getBackgroundPage().yomichan;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function instDb() {
|
|
|
|
return instYomi().translator.database;
|
2017-03-03 04:23:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function instAnki() {
|
|
|
|
return instYomi().anki;
|
2017-03-03 04:23:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
|
2017-03-05 03:16:19 +00:00
|
|
|
/*
|
|
|
|
* Foreground
|
|
|
|
*/
|
|
|
|
|
|
|
|
function fgBroadcast(action, params) {
|
|
|
|
chrome.tabs.query({}, tabs => {
|
|
|
|
for (const tab of tabs) {
|
|
|
|
chrome.tabs.sendMessage(tab.id, {action, params}, () => null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function fgOptionsSet(options) {
|
|
|
|
fgBroadcast('optionsSet', options);
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Options
|
|
|
|
*/
|
|
|
|
|
|
|
|
function optionsSetDefaults(options) {
|
|
|
|
const defaults = {
|
|
|
|
general: {
|
|
|
|
enable: true,
|
2017-04-02 19:08:15 +00:00
|
|
|
audioSource: 'jpod101',
|
2017-04-01 19:01:34 +00:00
|
|
|
audioVolume: 100,
|
2017-03-03 05:01:49 +00:00
|
|
|
groupResults: true,
|
2017-05-24 03:54:03 +00:00
|
|
|
debugInfo: false,
|
2017-03-03 05:01:49 +00:00
|
|
|
maxResults: 32,
|
2017-03-12 01:48:30 +00:00
|
|
|
showAdvanced: false,
|
|
|
|
popupWidth: 400,
|
|
|
|
popupHeight: 250,
|
2017-04-09 01:01:36 +00:00
|
|
|
popupOffset: 10,
|
|
|
|
showGuide: true
|
2017-03-03 05:01:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
scanning: {
|
2017-03-06 00:03:14 +00:00
|
|
|
middleMouse: true,
|
2017-03-03 05:01:49 +00:00
|
|
|
selectText: true,
|
2017-04-22 20:02:06 +00:00
|
|
|
alphanumeric: true,
|
2017-03-03 05:01:49 +00:00
|
|
|
delay: 15,
|
2017-05-26 03:56:08 +00:00
|
|
|
length: 10,
|
|
|
|
modifier: 'shift'
|
2017-03-03 05:01:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
dictionaries: {},
|
|
|
|
|
|
|
|
anki: {
|
|
|
|
enable: false,
|
|
|
|
server: 'http://127.0.0.1:8765',
|
|
|
|
tags: ['yomichan'],
|
|
|
|
htmlCards: true,
|
|
|
|
sentenceExt: 200,
|
|
|
|
terms: {deck: '', model: '', fields: {}},
|
|
|
|
kanji: {deck: '', model: '', fields: {}}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const combine = (target, source) => {
|
|
|
|
for (const key in source) {
|
|
|
|
if (!target.hasOwnProperty(key)) {
|
|
|
|
target[key] = source[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
combine(options, defaults);
|
|
|
|
combine(options.general, defaults.general);
|
|
|
|
combine(options.scanning, defaults.scanning);
|
|
|
|
combine(options.anki, defaults.anki);
|
|
|
|
combine(options.anki.terms, defaults.anki.terms);
|
|
|
|
combine(options.anki.kanji, defaults.anki.kanji);
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionsVersion(options) {
|
|
|
|
const fixups = [
|
2017-05-26 03:56:08 +00:00
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
() => {},
|
2017-03-03 05:01:49 +00:00
|
|
|
() => {
|
2017-04-02 19:08:15 +00:00
|
|
|
if (options.general.audioPlayback) {
|
|
|
|
options.general.audioSource = 'jpod101';
|
|
|
|
} else {
|
|
|
|
options.general.audioSource = 'disabled';
|
2017-03-05 19:39:07 +00:00
|
|
|
}
|
2017-04-09 01:01:36 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
options.general.showGuide = false;
|
2017-05-26 03:56:08 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
if (options.scanning.requireShift) {
|
|
|
|
options.scanning.modifier = 'shift';
|
|
|
|
} else {
|
|
|
|
options.scanning.modifier = 'none';
|
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
optionsSetDefaults(options);
|
|
|
|
if (!options.hasOwnProperty('version')) {
|
|
|
|
options.version = fixups.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (options.version < fixups.length) {
|
|
|
|
fixups[options.version++]();
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionsLoad() {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-03-05 19:29:28 +00:00
|
|
|
chrome.storage.local.get(null, store => resolve(store.options));
|
|
|
|
}).then(optionsStr => {
|
2017-03-12 02:39:01 +00:00
|
|
|
return optionsStr ? JSON.parse(optionsStr) : {};
|
2017-03-05 19:29:28 +00:00
|
|
|
}).catch(error => {
|
|
|
|
return {};
|
2017-03-05 04:56:54 +00:00
|
|
|
}).then(options => {
|
2017-03-05 19:29:28 +00:00
|
|
|
return optionsVersion(options);
|
2017-03-03 05:01:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionsSave(options) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-03-05 19:12:48 +00:00
|
|
|
chrome.storage.local.set({options: JSON.stringify(options)}, resolve);
|
2017-03-05 03:16:19 +00:00
|
|
|
}).then(() => {
|
|
|
|
instYomi().optionsSet(options);
|
|
|
|
fgOptionsSet(options);
|
2017-03-03 05:01:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dictionary
|
|
|
|
*/
|
|
|
|
|
2017-03-27 00:14:50 +00:00
|
|
|
function dictEnabledSet(options) {
|
2017-03-03 05:01:49 +00:00
|
|
|
const dictionaries = {};
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
const dictionary = options.dictionaries[title];
|
|
|
|
if (dictionary.enabled) {
|
|
|
|
dictionaries[title] = dictionary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
2017-03-27 00:14:50 +00:00
|
|
|
function dictConfigured(options) {
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
if (options.dictionaries[title].enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:54:03 +00:00
|
|
|
function dictRowsSort(rows, options) {
|
|
|
|
return rows.sort((ra, rb) => {
|
2017-03-06 00:27:29 +00:00
|
|
|
const pa = (options.dictionaries[ra.title] || {}).priority || 0;
|
|
|
|
const pb = (options.dictionaries[rb.title] || {}).priority || 0;
|
2017-03-05 23:54:03 +00:00
|
|
|
if (pa > pb) {
|
|
|
|
return -1;
|
|
|
|
} else if (pa < pb) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTermsSort(definitions, dictionaries=null) {
|
2016-09-20 03:17:58 +00:00
|
|
|
return definitions.sort((v1, v2) => {
|
|
|
|
const sl1 = v1.source.length;
|
|
|
|
const sl2 = v2.source.length;
|
|
|
|
if (sl1 > sl2) {
|
|
|
|
return -1;
|
|
|
|
} else if (sl1 < sl2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-17 04:54:01 +00:00
|
|
|
if (dictionaries !== null) {
|
|
|
|
const p1 = (dictionaries[v1.dictionary] || {}).priority || 0;
|
|
|
|
const p2 = (dictionaries[v2.dictionary] || {}).priority || 0;
|
|
|
|
if (p1 > p2) {
|
|
|
|
return -1;
|
|
|
|
} else if (p1 < p2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:17:58 +00:00
|
|
|
const s1 = v1.score;
|
|
|
|
const s2 = v2.score;
|
|
|
|
if (s1 > s2) {
|
|
|
|
return -1;
|
|
|
|
} else if (s1 < s2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-18 02:45:19 +00:00
|
|
|
const rl1 = v1.reasons.length;
|
|
|
|
const rl2 = v2.reasons.length;
|
2016-09-20 03:17:58 +00:00
|
|
|
if (rl1 < rl2) {
|
|
|
|
return -1;
|
|
|
|
} else if (rl1 > rl2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v2.expression.localeCompare(v1.expression);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTermsUndupe(definitions) {
|
2016-12-23 23:44:16 +00:00
|
|
|
const definitionGroups = {};
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const definition of definitions) {
|
2016-12-23 23:44:16 +00:00
|
|
|
const definitionExisting = definitionGroups[definition.id];
|
|
|
|
if (!definitionGroups.hasOwnProperty(definition.id) || definition.expression.length > definitionExisting.expression.length) {
|
|
|
|
definitionGroups[definition.id] = definition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const definitionsUnique = [];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const key in definitionGroups) {
|
2016-12-23 23:44:16 +00:00
|
|
|
definitionsUnique.push(definitionGroups[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return definitionsUnique;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTermsGroup(definitions, dictionaries) {
|
2017-01-08 23:08:18 +00:00
|
|
|
const groups = {};
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const definition of definitions) {
|
2017-01-08 23:08:18 +00:00
|
|
|
const key = [definition.source, definition.expression].concat(definition.reasons);
|
|
|
|
if (definition.reading) {
|
|
|
|
key.push(definition.reading);
|
|
|
|
}
|
|
|
|
|
|
|
|
const group = groups[key];
|
|
|
|
if (group) {
|
|
|
|
group.push(definition);
|
|
|
|
} else {
|
|
|
|
groups[key] = [definition];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = [];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const key in groups) {
|
2017-01-09 02:10:38 +00:00
|
|
|
const groupDefs = groups[key];
|
2017-01-08 23:08:18 +00:00
|
|
|
const firstDef = groupDefs[0];
|
2017-03-03 05:01:49 +00:00
|
|
|
dictTermsSort(groupDefs, dictionaries);
|
2017-01-08 23:08:18 +00:00
|
|
|
results.push({
|
|
|
|
definitions: groupDefs,
|
|
|
|
expression: firstDef.expression,
|
|
|
|
reading: firstDef.reading,
|
|
|
|
reasons: firstDef.reasons,
|
2017-04-01 18:05:28 +00:00
|
|
|
score: groupDefs.reduce((p, v) => v.score > p ? v.score : p, Number.MIN_SAFE_INTEGER),
|
2017-01-15 20:42:44 +00:00
|
|
|
source: firstDef.source
|
2017-01-08 23:08:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
return dictTermsSort(results);
|
2017-01-08 23:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagBuildSource(name) {
|
|
|
|
return dictTagSanitize({name, category: 'dictionary', order: 100});
|
2017-01-08 19:18:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagBuild(name, meta) {
|
2016-12-18 05:26:46 +00:00
|
|
|
const tag = {name};
|
|
|
|
const symbol = name.split(':')[0];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const prop in meta[symbol] || {}) {
|
2016-09-20 03:17:58 +00:00
|
|
|
tag[prop] = meta[symbol][prop];
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
return dictTagSanitize(tag);
|
2016-12-18 19:52:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagSanitize(tag) {
|
2016-12-18 19:52:11 +00:00
|
|
|
tag.name = tag.name || 'untitled';
|
|
|
|
tag.category = tag.category || 'default';
|
|
|
|
tag.notes = tag.notes || '';
|
|
|
|
tag.order = tag.order || 0;
|
2016-09-20 03:17:58 +00:00
|
|
|
return tag;
|
|
|
|
}
|
2016-10-22 04:33:54 +00:00
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagsSort(tags) {
|
2017-01-08 22:08:36 +00:00
|
|
|
return tags.sort((v1, v2) => {
|
|
|
|
const order1 = v1.order;
|
|
|
|
const order2 = v2.order;
|
|
|
|
if (order1 < order2) {
|
|
|
|
return -1;
|
|
|
|
} else if (order1 > order2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name1 = v1.name;
|
|
|
|
const name2 = v2.name;
|
|
|
|
if (name1 < name2) {
|
|
|
|
return -1;
|
|
|
|
} else if (name1 > name2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictFieldSplit(field) {
|
|
|
|
return field.length === 0 ? [] : field.split(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
function dictFieldFormat(field, definition, mode, options) {
|
2017-01-08 22:08:36 +00:00
|
|
|
const markers = [
|
|
|
|
'audio',
|
|
|
|
'character',
|
2017-03-29 03:49:26 +00:00
|
|
|
'cloze-body',
|
|
|
|
'cloze-prefix',
|
|
|
|
'cloze-suffix',
|
2017-01-08 22:08:36 +00:00
|
|
|
'dictionary',
|
|
|
|
'expression',
|
2017-01-22 03:30:01 +00:00
|
|
|
'furigana',
|
2017-01-08 22:08:36 +00:00
|
|
|
'glossary',
|
|
|
|
'kunyomi',
|
|
|
|
'onyomi',
|
|
|
|
'reading',
|
|
|
|
'sentence',
|
|
|
|
'tags',
|
2017-01-15 20:42:44 +00:00
|
|
|
'url'
|
2017-01-08 22:08:36 +00:00
|
|
|
];
|
|
|
|
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const marker of markers) {
|
2017-01-22 03:30:01 +00:00
|
|
|
const data = {
|
|
|
|
marker,
|
|
|
|
definition,
|
2017-01-27 04:22:52 +00:00
|
|
|
group: options.general.groupResults,
|
|
|
|
html: options.anki.htmlCards,
|
2017-03-05 03:53:28 +00:00
|
|
|
modeTermKanji: mode === 'term-kanji',
|
|
|
|
modeTermKana: mode === 'term-kana',
|
2017-01-22 03:30:01 +00:00
|
|
|
modeKanji: mode === 'kanji'
|
|
|
|
};
|
|
|
|
|
|
|
|
field = field.replace(
|
|
|
|
`{${marker}}`,
|
|
|
|
Handlebars.templates['fields.html'](data).trim()
|
|
|
|
);
|
2017-01-08 22:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Json
|
|
|
|
*/
|
|
|
|
|
|
|
|
function jsonLoad(url) {
|
2016-11-06 19:01:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
2017-01-29 04:36:58 +00:00
|
|
|
xhr.overrideMimeType('application/json');
|
2016-11-08 06:31:57 +00:00
|
|
|
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
2016-11-07 17:16:38 +00:00
|
|
|
xhr.addEventListener('error', () => reject('failed to execute network request'));
|
2016-11-07 17:22:57 +00:00
|
|
|
xhr.open('GET', url);
|
2016-11-06 19:01:35 +00:00
|
|
|
xhr.send();
|
2016-11-08 06:31:57 +00:00
|
|
|
}).then(responseText => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(responseText);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return Promise.reject('invalid JSON response');
|
|
|
|
}
|
2016-11-06 19:01:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function jsonLoadInt(url) {
|
|
|
|
return jsonLoad(chrome.extension.getURL(url));
|
2016-11-07 17:22:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 22:36:28 +00:00
|
|
|
/*
|
|
|
|
* Zip
|
|
|
|
*/
|
|
|
|
|
|
|
|
function zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded) {
|
2017-06-25 22:56:30 +00:00
|
|
|
return JSZip.loadAsync(archive).then(files => files.files).then(files => {
|
2017-06-25 22:36:28 +00:00
|
|
|
const indexFile = files['index.json'];
|
|
|
|
if (!indexFile) {
|
|
|
|
return Promise.reject('no dictionary index found in archive');
|
2016-11-08 06:31:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 22:36:28 +00:00
|
|
|
return indexFile.async('string').then(indexJson => {
|
|
|
|
const index = JSON.parse(indexJson);
|
|
|
|
if (!index.title || !index.version || !index.revision) {
|
|
|
|
return Promise.reject('unrecognized dictionary format');
|
|
|
|
}
|
|
|
|
|
2016-11-07 01:10:31 +00:00
|
|
|
return indexLoaded(
|
|
|
|
index.title,
|
|
|
|
index.version,
|
2016-12-24 05:59:19 +00:00
|
|
|
index.revision,
|
|
|
|
index.tagMeta || {},
|
2016-11-07 01:10:31 +00:00
|
|
|
index.termBanks > 0,
|
|
|
|
index.kanjiBanks > 0
|
|
|
|
).then(() => index);
|
2017-06-25 22:36:28 +00:00
|
|
|
}).then(index => {
|
|
|
|
const loaders = [];
|
|
|
|
const banksTotal = index.termBanks + index.kanjiBanks;
|
|
|
|
let banksLoaded = 0;
|
|
|
|
|
|
|
|
for (let i = 1; i <= index.termBanks; ++i) {
|
|
|
|
const bankFile = files[`term_bank_${i}.json`];
|
|
|
|
if (!bankFile) {
|
|
|
|
return Promise.reject('missing term bank file');
|
|
|
|
}
|
|
|
|
|
|
|
|
loaders.push(() => bankFile.async('string').then(bankJson => {
|
|
|
|
const bank = JSON.parse(bankJson);
|
|
|
|
return termsLoaded(index.title, bank, banksTotal, banksLoaded++);
|
|
|
|
}));
|
|
|
|
}
|
2016-11-05 23:44:29 +00:00
|
|
|
|
2017-06-25 22:36:28 +00:00
|
|
|
for (let i = 1; i <= index.kanjiBanks; ++i) {
|
|
|
|
const bankFile = files[`kanji_bank_${i}.json`];
|
|
|
|
if (!bankFile) {
|
|
|
|
return Promise.reject('missing kanji bank file');
|
|
|
|
}
|
2016-11-06 19:01:35 +00:00
|
|
|
|
2017-06-25 22:36:28 +00:00
|
|
|
loaders.push(() => bankFile.async('string').then(bankJson => {
|
|
|
|
const bank = JSON.parse(bankJson);
|
|
|
|
return kanjiLoaded(index.title, bank, banksTotal, banksLoaded++);
|
|
|
|
}));
|
|
|
|
}
|
2016-11-05 23:44:29 +00:00
|
|
|
|
2017-06-25 22:36:28 +00:00
|
|
|
let chain = Promise.resolve();
|
|
|
|
for (const loader of loaders) {
|
|
|
|
chain = chain.then(loader);
|
|
|
|
}
|
2016-11-05 23:44:29 +00:00
|
|
|
|
2017-06-25 22:36:28 +00:00
|
|
|
return chain;
|
|
|
|
});
|
2016-11-05 23:44:29 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
2017-05-25 02:13:56 +00:00
|
|
|
function handlebarsEscape(text) {
|
|
|
|
return Handlebars.Utils.escapeExpression(text);
|
|
|
|
}
|
|
|
|
|
2017-05-24 04:27:20 +00:00
|
|
|
function handlebarsDumpObject(options) {
|
|
|
|
const dump = JSON.stringify(options.fn(this), null, 4);
|
2017-05-25 02:13:56 +00:00
|
|
|
return handlebarsEscape(dump);
|
2017-05-24 04:27:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 03:16:19 +00:00
|
|
|
function handlebarsKanjiLinks(options) {
|
2017-03-03 05:01:49 +00:00
|
|
|
let result = '';
|
|
|
|
for (const c of options.fn(this)) {
|
2017-04-22 20:02:06 +00:00
|
|
|
if (jpIsKanji(c)) {
|
2017-03-03 05:01:49 +00:00
|
|
|
result += `<a href="#" class="kanji-link">${c}</a>`;
|
|
|
|
} else {
|
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-05 03:16:19 +00:00
|
|
|
function handlebarsMultiLine(options) {
|
2017-03-03 05:01:49 +00:00
|
|
|
return options.fn(this).split('\n').join('<br>');
|
|
|
|
}
|
2017-03-05 03:16:19 +00:00
|
|
|
|
|
|
|
function handlebarsRegister() {
|
|
|
|
Handlebars.partials = Handlebars.templates;
|
2017-05-24 04:27:20 +00:00
|
|
|
Handlebars.registerHelper('dumpObject', handlebarsDumpObject);
|
2017-03-05 03:16:19 +00:00
|
|
|
Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks);
|
|
|
|
Handlebars.registerHelper('multiLine', handlebarsMultiLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlebarsRender(template, data) {
|
|
|
|
return Handlebars.templates[template](data);
|
|
|
|
}
|