toc.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. var fs = require('fs');
  19. var path = require('path');
  20. var argv = require('optimist').argv;
  21. var augment = require('./augment_toc');
  22. var util = require('./util');
  23. function main () {
  24. var docsRoot = argv._[0];
  25. var tocRoot = argv._[1];
  26. // validate args
  27. if ((!docsRoot) || (!tocRoot)) {
  28. var scriptName = path.basename(process.argv[1]);
  29. console.log('usage: ' + scriptName + ' docsRoot tocRoot');
  30. console.log(scriptName + ': error: too few arguments');
  31. return 1;
  32. }
  33. // go through all the languages
  34. util.listdirsSync(docsRoot).forEach(function (languageName) {
  35. var languagePath = path.join(docsRoot, languageName);
  36. // go through all the versions
  37. util.listdirsSync(languagePath).forEach(function (versionName) {
  38. var versionPath = path.join(languagePath, versionName);
  39. var srcTocName = util.srcTocfileName(languageName, versionName);
  40. var destTocName = util.genTocfileName(languageName, versionName);
  41. var srcTocPath = path.join(tocRoot, srcTocName);
  42. var destTocPath = path.join(tocRoot, destTocName);
  43. // read the input
  44. fs.readFile(srcTocPath, function (error, data) {
  45. if (error) throw error;
  46. // augment the ToC
  47. var originalTocString = data.toString();
  48. var augmentedTocString = augment.augmentString(originalTocString, versionPath);
  49. var warningComment = util.generatedBy(__filename);
  50. var output = warningComment + '\n' + augmentedTocString;
  51. // write the output
  52. fs.writeFile(destTocPath, output, function (error, data) {
  53. if (error) throw error;
  54. console.log(srcTocPath + ' -> ' + destTocPath);
  55. });
  56. });
  57. });
  58. });
  59. }
  60. main();