docExtractor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /* eslint-disable no-restricted-syntax */
  17. const program = require('commander');
  18. const extractorUtils = require('./extractorUtils');
  19. const ditamapParser = require('./ditamapParser');
  20. const docXmlParser = require('./docXmlParser');
  21. const topicLinker = require('./topicLinker');
  22. const jsonHandler = require('./jsonHandler');
  23. const LOG_NAME = 'docExtractor.js';
  24. // Handle command line arguments
  25. program
  26. .version('1.0')
  27. .option('-f, --folder [path]', 'the folder where the ditamap file(s) reside (required)')
  28. .option(
  29. '-d, --ditamap [path]',
  30. 'comma-separated ditamap file names, the first will define the topic ' +
  31. "tree (at least one file is required). Note that there should be no whitespace around the ','"
  32. )
  33. .option('-o, --output [path]', 'output folder where the json files will be written to (required)')
  34. .option('-c, --cssClassPrefix [prefix]', 'optional css class prefix')
  35. .option(
  36. '-m, --mako [path]',
  37. 'optional path to a .mako file where the index is written, ' +
  38. 'used for django if the output folder is a static resource'
  39. )
  40. .parse(process.argv);
  41. extractorUtils.checkArguments(program);
  42. const ensureTrailingSlash = path => {
  43. if (!path.endsWith('/')) {
  44. return path + '/';
  45. }
  46. return path;
  47. };
  48. const ditamapFiles = program.ditamap.split(',').map(file => file.trim());
  49. console.log('%s: Parsing ditamap file(s)...', LOG_NAME);
  50. const ditamapParsePromises = ditamapFiles.map(ditamapFile =>
  51. ditamapParser.parseDitamap(ditamapFile, ensureTrailingSlash(program.folder))
  52. );
  53. Promise.all(ditamapParsePromises).then(parseResults => {
  54. let cssClassPrefix = program.cssClassPrefix || '';
  55. if (cssClassPrefix && !/-$/.test(cssClassPrefix)) {
  56. cssClassPrefix += '-';
  57. }
  58. console.log('%s: Parsing topics...', LOG_NAME);
  59. docXmlParser.parseTopics(parseResults, cssClassPrefix).then(() => {
  60. console.log('%s: Linking topics...', LOG_NAME);
  61. topicLinker.linkTopics(parseResults, cssClassPrefix);
  62. console.log('%s: Saving topic tree json files...', LOG_NAME);
  63. jsonHandler
  64. .saveTopics(parseResults[0].topics, ensureTrailingSlash(program.output), program.mako)
  65. .then(savedFiles => {
  66. console.log('%s: Done! Saved %d files.', LOG_NAME, savedFiles.length);
  67. })
  68. .catch(err => {
  69. console.log('%s: Failed saving files!', LOG_NAME);
  70. console.log(err);
  71. });
  72. });
  73. });
  74. /* eslint-enable no-restricted-syntax */