ConvertHtml.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const ConvertBase = require('./ConvertBase');
  2. const sax = require('./sax');
  3. const textUtils = require('./textUtils');
  4. class ConvertHtml extends ConvertBase {
  5. check(data, opts) {
  6. const {fileType} = opts;
  7. if (fileType && (fileType.ext == 'html' || fileType.ext == 'xml'))
  8. return {isText: false};
  9. //может это чистый текст?
  10. if (textUtils.checkIfText(data)) {
  11. return {isText: true};
  12. }
  13. return false;
  14. }
  15. run(data, opts) {
  16. const checkResult = this.check(data, opts);
  17. if (!checkResult)
  18. return false;
  19. let {isText} = checkResult;
  20. let titleInfo = {};
  21. let desc = {_n: 'description', 'title-info': titleInfo};
  22. let pars = [];
  23. let body = {_n: 'body', section: {_a: []}};
  24. let fb2 = [desc, body];
  25. let title = '';
  26. let inTitle = false;
  27. let spaceCounter = [];
  28. const repCrLfTab = (text) => text.replace(/[\n\r]/g, '').replace(/\t/g, ' ');
  29. const newParagraph = () => {
  30. pars.push({_n: 'p', _t: ''});
  31. };
  32. const growParagraph = (text) => {
  33. if (!pars.length)
  34. newParagraph();
  35. const l = pars.length;
  36. if (pars[l - 1]._t == '')
  37. text = text.trimLeft();
  38. pars[l - 1]._t += text;
  39. //посчитаем отступы у текста, чтобы выделить потом параграфы
  40. const lines = text.split('\n');
  41. for (let line of lines) {
  42. line = repCrLfTab(line)
  43. let l = 0;
  44. while (l < line.length && line[l] == ' ') {
  45. l++;
  46. }
  47. if (!spaceCounter[l])
  48. spaceCounter[l] = 0;
  49. spaceCounter[l]++;
  50. }
  51. };
  52. const newPara = new Set(['tr', 'br', 'br/', 'dd', 'p', 'title', '/title', 'h1', 'h2', 'h3', '/h1', '/h2', '/h3']);
  53. const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  54. if (!cutCounter) {
  55. growParagraph(text);
  56. }
  57. if (inTitle && !title)
  58. title = text;
  59. };
  60. const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  61. if (!cutCounter) {
  62. if (newPara.has(tag))
  63. newParagraph();
  64. }
  65. if (tag == 'title')
  66. inTitle = true;
  67. };
  68. const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
  69. if (tag == 'title')
  70. inTitle = false;
  71. };
  72. let buf = this.decode(data).toString();
  73. sax.parseSync(buf, {
  74. onStartNode, onEndNode, onTextNode,
  75. innerCut: new Set(['head', 'script', 'style', 'binary'])
  76. });
  77. titleInfo['book-title'] = title;
  78. //подозрение на чистый текст, надо разбить на параграфы
  79. if (isText || pars.length < buf.length/2000) {
  80. let total = 0;
  81. for (let i = 0; i < spaceCounter.length; i++) {
  82. total += (spaceCounter[i] ? spaceCounter[i] : 0);
  83. }
  84. total /= 10;
  85. let i = spaceCounter.length - 1;
  86. while (i > 0 && (!spaceCounter[i] || spaceCounter[i] < total)) i--;
  87. const parIndent = (i > 0 ? i : 0);
  88. let newPars = [];
  89. const newPar = () => {
  90. newPars.push({_n: 'p', _t: ''});
  91. };
  92. const growPar = (text) => {
  93. if (!newPars.length)
  94. newPar();
  95. const l = newPars.length;
  96. newPars[l - 1]._t += text;
  97. }
  98. i = 0;
  99. for (const par of pars) {
  100. if (i > 0)
  101. newPar();
  102. i++;
  103. const lines = par._t.split('\n');
  104. for (let line of lines) {
  105. line = repCrLfTab(line);
  106. let l = 0;
  107. while (l < line.length && line[l] == ' ') {
  108. l++;
  109. }
  110. if (l >= parIndent)
  111. newPar();
  112. growPar(line.trim() + ' ');
  113. }
  114. }
  115. body.section._a[0] = newPars;
  116. } else {
  117. body.section._a[0] = pars;
  118. }
  119. //убираем лишнее
  120. for (let i = 0; i < pars.length; i++)
  121. pars[i]._t = this.repSpaces(pars[i]._t).trim();
  122. return this.formatFb2(fb2);
  123. }
  124. }
  125. module.exports = ConvertHtml;