Добавлен парсинг оглавления из djvu, добавлено отображение атрибута alt изображений в ContentsPage

This commit is contained in:
Book Pauk
2020-12-17 20:57:29 +07:00
parent 9492f85d80
commit f5c06ce420
4 changed files with 43 additions and 11 deletions

View File

@@ -22,6 +22,10 @@ class ConvertDjvu extends ConvertJpegPng {
if (!await fs.pathExists(ddjvuPath))
throw new Error('Внешний конвертер ddjvu не найден');
const djvusedPath = '/usr/bin/djvused';
if (!await fs.pathExists(djvusedPath))
throw new Error('Внешний конвертер djvused не найден');
const tiffsplitPath = '/usr/bin/tiffsplit';
if (!await fs.pathExists(tiffsplitPath))
throw new Error('Внешний конвертер tiffsplitPath не найден');
@@ -67,8 +71,27 @@ class ConvertDjvu extends ConvertJpegPng {
files.sort((a, b) => a.base.localeCompare(b.base));
//схема документа (outline)
const djvusedResult = await this.execConverter(djvusedPath, ['-u', '-e', 'print-outline', inputFiles.sourceFile]);
const outline = [];
const lines = djvusedResult.stdout.match(/\(".*"\s*?"#\d+".*?\)/g);
if (lines) {
lines.forEach(l => {
const m = l.match(/"(.*)"\s*?"#(\d+)"/);
if (m) {
outline[m[2]] = m[1];
}
});
}
await utils.sleep(100);
return await super.run(data, Object.assign({}, opts, {imageFiles: files.map(f => f.name)}));
let i = 0;
const imageFiles = files.map(f => {
i++;
let alt = (outline[i] ? outline[i] : '');
return {src: f.name, alt};
});
return await super.run(data, Object.assign({}, opts, {imageFiles}));
}
}

View File

@@ -27,7 +27,7 @@ class ConvertJpegPng extends ConvertBase {
} else {
const imageFile = `${inputFiles.filesDir}/${path.basename(inputFiles.sourceFile)}.${inputFiles.sourceFileType.ext}`;
await fs.copy(inputFiles.sourceFile, imageFile);
files.push(imageFile);
files.push({src: imageFile});
}
//читаем изображения
@@ -55,10 +55,9 @@ class ConvertJpegPng extends ConvertBase {
let images = [];
let loading = [];
files.forEach(f => {
const image = {src: f};
images.push(image);
loading.push(loadImage(image));
files.forEach(img => {
images.push(img);
loading.push(loadImage(img));
});
await Promise.all(loading);
@@ -82,8 +81,12 @@ class ConvertJpegPng extends ConvertBase {
const img = {_n: 'binary', _attrs: {id: image.name, 'content-type': image.type}, _t: image.data};
binary.push(img);
const attrs = {'l:href': `#${image.name}`};
if (image.alt)
attrs.alt = image.alt;
pars.push({_n: 'p', _t: ''});
pars.push({_n: 'image', _attrs: {'l:href': `#${image.name}`}});
pars.push({_n: 'image', _attrs: attrs});
}
}
pars.push({_n: 'p', _t: ''});