hiveExtractor.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 program = require('commander');
  17. const EPub = require('epub');
  18. const Topic = require('./Topic');
  19. const libxml = require('libxmljs');
  20. const extractorUtils = require('./extractorUtils');
  21. const outputPath = './desktop/core/src/desktop/static/desktop/docs/hive/';
  22. const mako = './desktop/core/src/desktop/templates/sql_doc_index.mako';
  23. const jsonHandler = require('./jsonHandler');
  24. // Handle command line arguments
  25. program
  26. .version('1.0')
  27. .option('-e, --epub [path]', 'the path to the Hive epub file')
  28. .parse(process.argv);
  29. const epub = new EPub(program.epub);
  30. const convertToPre = (element, fragments) => {
  31. switch (element.name()) {
  32. case 'div':
  33. element.childNodes().forEach(node => {
  34. convertToPre(node, fragments);
  35. });
  36. break;
  37. case 'text':
  38. if (fragments.length === 1) {
  39. fragments.push(element.text().replace(/^\n/, ''));
  40. } else {
  41. fragments.push(element.text());
  42. }
  43. break;
  44. case 'code':
  45. if (
  46. element.attr('class') &&
  47. element
  48. .attr('class')
  49. .value()
  50. .indexOf('value') !== -1
  51. ) {
  52. fragments.push('<span class="hue-doc-varname">');
  53. element.childNodes().forEach(node => {
  54. convertToPre(node, fragments);
  55. });
  56. fragments.push('</span>');
  57. break;
  58. }
  59. default:
  60. element.childNodes().forEach(node => {
  61. convertToPre(node, fragments);
  62. });
  63. }
  64. };
  65. const adaptElement = element => {
  66. if (
  67. element.attr('class') &&
  68. element
  69. .attr('class')
  70. .value()
  71. .indexOf('syntaxhighlighter') !== -1
  72. ) {
  73. const fragments = ['<div class="hue-doc-codeblock">'];
  74. element.childNodes().forEach(childNode => {
  75. convertToPre(childNode, fragments);
  76. });
  77. fragments.push('</div>');
  78. const replacement = fragments.join('');
  79. element.replace(libxml.parseHtmlFragment(replacement).root());
  80. } else if (element.attr('class')) {
  81. element.attr('class').remove();
  82. }
  83. switch (element.name()) {
  84. case 'a':
  85. if (
  86. element.attr('href') &&
  87. element
  88. .attr('href')
  89. .value()
  90. .indexOf('/links') == 0
  91. ) {
  92. // Internal link
  93. let ref = element
  94. .attr('href')
  95. .value()
  96. .replace(/\/links\/_[0-9]+\//, '');
  97. extractorUtils.removeAllAttributes(element);
  98. const anchorMatch = ref.match(/.html#(.+)$/);
  99. if (anchorMatch) {
  100. element.attr('data-doc-anchor-id', anchorMatch[1]);
  101. ref = ref.replace('#' + anchorMatch[1], '');
  102. }
  103. ref = '_' + ref.replace('.html', '');
  104. element.attr({
  105. class: 'hue-doc-internal-link',
  106. href: 'javascript:void(0);',
  107. 'data-doc-ref': ref
  108. });
  109. } else if (element.attr('href')) {
  110. // External link
  111. const href = element.attr('href').value();
  112. extractorUtils.removeAllAttributes(element);
  113. element.attr({ class: 'hue-doc-external-link', href: href, target: '_blank' });
  114. }
  115. break;
  116. case 'h1':
  117. element.text(element.text().replace(/LanguageManual\s(.+)/, '$1'));
  118. break;
  119. default:
  120. }
  121. element.childNodes().forEach(adaptElement);
  122. };
  123. epub.on('end', () => {
  124. const rootTopics = [];
  125. const lastTopicPerLevel = {};
  126. const promises = [];
  127. epub.flow.forEach(chapter => {
  128. promises.push(
  129. new Promise((resolve, reject) => {
  130. const topic = new Topic('/', chapter.id);
  131. topic.fragment = {
  132. title: {
  133. text: () => chapter.title.replace(/LanguageManual\s(.+)/, '$1')
  134. }
  135. };
  136. epub.getChapter(chapter.id, (error, text) => {
  137. try {
  138. const contents = libxml.parseHtmlFragment('<div>' + text + '</div>');
  139. topic.domXml = contents.root();
  140. adaptElement(topic.domXml);
  141. resolve();
  142. } catch (error) {
  143. reject();
  144. }
  145. });
  146. if (lastTopicPerLevel[chapter.level - 1]) {
  147. lastTopicPerLevel[chapter.level - 1].children.push(topic);
  148. }
  149. if (chapter.level === 0) {
  150. rootTopics.push(topic);
  151. }
  152. lastTopicPerLevel[chapter.level] = topic;
  153. })
  154. );
  155. });
  156. Promise.all(promises).then(() => {
  157. const rootTopic = rootTopics[0];
  158. rootTopic.children.forEach(childTopic => {
  159. rootTopics.push(childTopic);
  160. });
  161. rootTopic.children = [];
  162. jsonHandler
  163. .saveTopics(rootTopics, outputPath, mako, false)
  164. .then(() => {
  165. console.log('Done.');
  166. })
  167. .catch(() => {
  168. console.log('Fail.');
  169. });
  170. });
  171. });
  172. epub.parse();