gen_pages_dict.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 yaml = require('js-yaml');
  21. var glob = require('glob');
  22. var optimist = require('optimist');
  23. var util = require('./util');
  24. // constants
  25. var LATEST_ALIAS_URI = '/latest/';
  26. // helpers
  27. function pathToURI (filePath, rootPath) {
  28. return filePath
  29. .replace(new RegExp('^' + rootPath), '')
  30. .replace(new RegExp('\\.md$'), '.html');
  31. }
  32. function pagesFromRedirects (redirects, languages) {
  33. var pages = {};
  34. // add docs redirects
  35. if (typeof redirects.docs !== 'undefined') {
  36. for (var redirectSource in redirects.docs) {
  37. // add an entry for the redirect's source, once for each language
  38. for (var i = 0; i < languages.length; i++) {
  39. var language = languages[i];
  40. var pagePath = '/docs/' + language + '/' + redirectSource;
  41. pages[pagePath] = true;
  42. }
  43. }
  44. }
  45. return pages;
  46. }
  47. function isInLatestDocs (uri, latestVersion) {
  48. return uri.indexOf('/' + latestVersion + '/') !== (-1);
  49. }
  50. // main
  51. function main () {
  52. // get args
  53. var argv = optimist
  54. .usage('Usage: $0 [options]')
  55. .demand('languages').describe('languages', 'comma-separated list of docs languages')
  56. .demand('latestVersion').describe('latestVersion', 'the current latest docs version')
  57. .demand('siteRoot').describe('siteRoot', 'the source ToC for the given directory')
  58. .string('redirectsFile').describe('redirectsFile', 'file containing redirects for the website').default('redirectsFile', null)
  59. .argv;
  60. var siteRootPath = argv.siteRoot;
  61. var redirectsFilePath = argv.redirectsFile;
  62. var latestVersion = argv.latestVersion;
  63. var languages = argv.languages.split(',');
  64. // pages to return
  65. var pages = {};
  66. // add pages for redirects if a redirects file was passed
  67. if (redirectsFilePath !== null) {
  68. var redirectsString = fs.readFileSync(redirectsFilePath);
  69. var redirects = yaml.load(redirectsString);
  70. var redirectsPages = pagesFromRedirects(redirects, languages);
  71. pages = redirectsPages;
  72. }
  73. // add entries for all Markdown files in the site root
  74. var allMarkdownFiles = path.join(siteRootPath, '**/*.md');
  75. glob(allMarkdownFiles, function (error, filePaths) {
  76. if (error) throw error;
  77. for (var i = 0; i < filePaths.length; i++) {
  78. var filePath = filePaths[i];
  79. var fileURI = pathToURI(filePath, siteRootPath);
  80. // add the page
  81. pages[fileURI] = true;
  82. // also add /latest/ version for pages in latest docs
  83. if (isInLatestDocs(fileURI, latestVersion)) {
  84. var latestURI = fileURI.replace('/' + latestVersion + '/', LATEST_ALIAS_URI);
  85. pages[latestURI] = true;
  86. }
  87. }
  88. // print output
  89. console.log(util.generatedBy(__filename));
  90. console.log(yaml.dump(pages));
  91. });
  92. }
  93. if (require.main === module) {
  94. main();
  95. }