docExtractor.js 3.0 KB

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