hiveExtractor.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. const fs = require('fs');
  17. const mkdirp = require('mkdirp');
  18. const path = require('path');
  19. const program = require('commander');
  20. const EPub = require('epub');
  21. const Topic = require('./Topic');
  22. const libxml = require('libxmljs');
  23. const outputPath = './desktop/core/src/desktop/static/desktop/docs/hive/';
  24. const mako = './desktop/core/src/desktop/templates/sql_doc_index.mako';
  25. const jsonHandler = require('./jsonHandler');
  26. // Handle command line arguments
  27. program
  28. .version('1.0')
  29. .option('-e, --epub [path]', 'the path to the Hive epub file')
  30. .parse(process.argv);
  31. const epub = new EPub(program.epub);
  32. const convertToPre = (element, fragments) => {
  33. switch (element.name()) {
  34. case 'div':
  35. element.childNodes().forEach(node => {
  36. convertToPre(node, fragments);
  37. });
  38. break;
  39. case 'text':
  40. if (fragments.length === 1) {
  41. fragments.push(element.text().replace(/^\n/, ''));
  42. } else {
  43. fragments.push(element.text());
  44. }
  45. break;
  46. case 'code':
  47. if (element.attr('class') && element.attr('class').value().indexOf('value') !== -1) {
  48. fragments.push('<span class="hue-doc-varname">');
  49. element.childNodes().forEach(node => {
  50. convertToPre(node, fragments);
  51. });
  52. fragments.push('</span>');
  53. break;
  54. }
  55. default:
  56. element.childNodes().forEach(node => {
  57. convertToPre(node, fragments);
  58. });
  59. }
  60. };
  61. const adaptElement = (element) => {
  62. if (element.attr('class') && element.attr('class').value().indexOf('syntaxhighlighter') !== -1) {
  63. let fragments = ['<div class="hue-doc-codeblock">'];
  64. element.childNodes().forEach(childNode => {
  65. convertToPre(childNode, fragments);
  66. });
  67. fragments.push('</div>');
  68. let replacement = fragments.join('');
  69. element.replace(libxml.parseHtmlFragment(replacement).root());
  70. } else if (element.attr('class')) {
  71. element.attr('class').remove();
  72. }
  73. element.childNodes().forEach(adaptElement);
  74. };
  75. epub.on("end", function(){
  76. let savePromises = [];
  77. let rootTopics = [];
  78. let topicStack = [];
  79. let lastTopicPerLevel = {};
  80. let promises = [];
  81. epub.flow.forEach(chapter => {
  82. promises.push(new Promise((resolve, reject) => {
  83. let topic = new Topic('/', chapter.id);
  84. topic.fragment = {
  85. title : {
  86. text: () => chapter.title.replace(/LanguageManual\s(.+)/, '$1')
  87. }
  88. };
  89. epub.getChapter(chapter.id, (error, text) => {
  90. try {
  91. let contents = libxml.parseHtmlFragment('<div>' + text + '</div>');
  92. topic.domXml = contents.root();
  93. adaptElement(topic.domXml);
  94. resolve();
  95. } catch (error) {
  96. reject();
  97. }
  98. });
  99. if (lastTopicPerLevel[chapter.level - 1]) {
  100. lastTopicPerLevel[chapter.level - 1].children.push(topic);
  101. }
  102. if (chapter.level === 0) {
  103. rootTopics.push(topic);
  104. }
  105. lastTopicPerLevel[chapter.level] = topic;
  106. }));
  107. });
  108. Promise.all(promises).then(() => {
  109. jsonHandler.saveTopics(rootTopics, outputPath, mako, false).then(() => {
  110. console.log('Done.');
  111. }).catch(() => {
  112. console.log('Fail.');
  113. });
  114. });
  115. });
  116. epub.parse();