docExtractor.js 3.0 KB

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