ConvertBase.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const fs = require('fs-extra');
  2. const iconv = require('iconv-lite');
  3. const chardet = require('chardet');
  4. const textUtils = require('./textUtils');
  5. class ConvertBase {
  6. constructor(config) {
  7. this.config = config;
  8. this.calibrePath = `${config.dataDir}/calibre/ebook-convert`;
  9. this.sofficePath = '/usr/bin/soffice';
  10. this.pdfToHtmlPath = '/usr/bin/pdftohtml';
  11. }
  12. async run(data, opts) {// eslint-disable-line no-unused-vars
  13. //override
  14. }
  15. async checkExternalConverterPresent() {
  16. if (!await fs.pathExists(this.calibrePath))
  17. throw new Error('Внешний конвертер calibre не найден');
  18. if (!await fs.pathExists(this.sofficePath))
  19. throw new Error('Внешний конвертер LibreOffice не найден');
  20. if (!await fs.pathExists(this.pdfToHtmlPath))
  21. throw new Error('Внешний конвертер pdftohtml не найден');
  22. }
  23. decode(data) {
  24. let selected = textUtils.getEncoding(data);
  25. if (selected == 'ISO-8859-5') {
  26. const charsetAll = chardet.detectAll(data.slice(0, 20000));
  27. for (const charset of charsetAll) {
  28. if (charset.name.indexOf('ISO-8859') < 0) {
  29. selected = charset.name;
  30. break;
  31. }
  32. }
  33. }
  34. if (selected.toLowerCase() != 'utf-8')
  35. return iconv.decode(data, selected);
  36. else
  37. return data;
  38. }
  39. repSpaces(text) {
  40. return text.replace(/&nbsp;|[\t\n\r]/g, ' ');
  41. }
  42. formatFb2(fb2) {
  43. let out = '<?xml version="1.0" encoding="utf-8"?>';
  44. out += '<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">';
  45. out += this.formatFb2Node(fb2);
  46. out += '</FictionBook>';
  47. return out;
  48. }
  49. formatFb2Node(node, name) {
  50. let out = '';
  51. if (Array.isArray(node)) {
  52. for (const n of node) {
  53. out += this.formatFb2Node(n);
  54. }
  55. } else if (typeof node == 'string') {
  56. if (name)
  57. out += `<${name}>${this.repSpaces(node)}</${name}>`;
  58. else
  59. out += this.repSpaces(node);
  60. } else {
  61. if (node._n)
  62. name = node._n;
  63. let attrs = '';
  64. if (node._attrs) {
  65. for (let attrName in node._attrs) {
  66. attrs += ` ${attrName}="${node._attrs[attrName]}"`;
  67. }
  68. }
  69. let tOpen = '';
  70. let tBody = '';
  71. let tClose = '';
  72. if (name)
  73. tOpen += `<${name}${attrs}>`;
  74. if (node.hasOwnProperty('_t'))
  75. tBody += this.repSpaces(node._t);
  76. for (let nodeName in node) {
  77. if (nodeName && nodeName[0] == '_' && nodeName != '_a')
  78. continue;
  79. const n = node[nodeName];
  80. tBody += this.formatFb2Node(n, nodeName);
  81. }
  82. if (name)
  83. tClose += `</${name}>`;
  84. if (attrs == '' && name == 'p' && tBody.trim() == '')
  85. out += '<empty-line/>'
  86. else
  87. out += `${tOpen}${tBody}${tClose}`;
  88. }
  89. return out;
  90. }
  91. }
  92. module.exports = ConvertBase;