hiveExtractor.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 (element.attr('href') && element.attr('href').value().indexOf('/links') == 0) {
  86. // Internal link
  87. let ref = element.attr('href').value().replace(/\/links\/_[0-9]+\//, '');
  88. extractorUtils.removeAllAttributes(element);
  89. const anchorMatch = ref.match(/.html#(.+)$/);
  90. if (anchorMatch) {
  91. element.attr('data-doc-anchor-id', anchorMatch[1]);
  92. ref = ref.replace('#' + anchorMatch[1], '');
  93. }
  94. ref = '_' + ref.replace('.html', '');
  95. element.attr({
  96. class: 'hue-doc-internal-link',
  97. href: 'javascript:void(0);',
  98. 'data-doc-ref': ref
  99. });
  100. } else if (element.attr('href')) {
  101. // External link
  102. const href = element.attr('href').value();
  103. extractorUtils.removeAllAttributes(element);
  104. element.attr({class: 'hue-doc-external-link', href: href, target: '_blank'});
  105. }
  106. break;
  107. default:
  108. }
  109. element.childNodes().forEach(adaptElement);
  110. };
  111. epub.on('end', () => {
  112. const rootTopics = [];
  113. const lastTopicPerLevel = {};
  114. const promises = [];
  115. epub.flow.forEach(chapter => {
  116. promises.push(
  117. new Promise((resolve, reject) => {
  118. const topic = new Topic('/', chapter.id);
  119. topic.fragment = {
  120. title: {
  121. text: () => chapter.title.replace(/LanguageManual\s(.+)/, '$1')
  122. }
  123. };
  124. epub.getChapter(chapter.id, (error, text) => {
  125. try {
  126. const contents = libxml.parseHtmlFragment('<div>' + text + '</div>');
  127. topic.domXml = contents.root();
  128. adaptElement(topic.domXml);
  129. resolve();
  130. } catch (error) {
  131. reject();
  132. }
  133. });
  134. if (lastTopicPerLevel[chapter.level - 1]) {
  135. lastTopicPerLevel[chapter.level - 1].children.push(topic);
  136. }
  137. if (chapter.level === 0) {
  138. rootTopics.push(topic);
  139. }
  140. lastTopicPerLevel[chapter.level] = topic;
  141. })
  142. );
  143. });
  144. Promise.all(promises).then(() => {
  145. const rootTopic = rootTopics[0];
  146. rootTopic.children.forEach(childTopic => {
  147. rootTopics.push(childTopic);
  148. });
  149. rootTopic.children = [];
  150. jsonHandler
  151. .saveTopics(rootTopics, outputPath, mako, false)
  152. .then(() => {
  153. console.log('Done.');
  154. })
  155. .catch(() => {
  156. console.log('Fail.');
  157. });
  158. });
  159. });
  160. epub.parse();