util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. module.exports = (function () {
  19. var fs = require('fs');
  20. var path = require('path');
  21. function stripFrontMatter (text) {
  22. // get and replace front matter if it's there
  23. // NOTE:
  24. // String.replace() replaces only the first occurrence
  25. // of a string, which is what we want
  26. var rawFrontMatterString = getRawFrontMatterString(text);
  27. if (rawFrontMatterString !== null) {
  28. return text.replace(rawFrontMatterString, '');
  29. }
  30. return text;
  31. }
  32. function getFrontMatterString (text) {
  33. var rawFrontMatterString = getRawFrontMatterString(text);
  34. if (rawFrontMatterString !== null) {
  35. // strip out front matter markers
  36. var frontMatterString = rawFrontMatterString.replace(/^---\s*$/gm, '');
  37. return frontMatterString;
  38. }
  39. return null;
  40. }
  41. function setFrontMatterString (text, frontMatterString) {
  42. var textOnly = stripFrontMatter(text);
  43. var newText = '---\n' + frontMatterString + '---\n\n' + textOnly;
  44. return newText;
  45. }
  46. function getRawFrontMatterString (text) {
  47. // NOTE:
  48. // [\s\S] matches all characters
  49. // *? non-greedy *-match
  50. var match = text.match(/^(---\s*\r?\n[\s\S]*?\r?\n---\s*\r?\n)[\s\S]*$/);
  51. if (match === null) {
  52. return null;
  53. }
  54. return match[1];
  55. }
  56. function listdirsSync (root) {
  57. return fs.readdirSync(root).filter(function (fileName) {
  58. return fs.statSync(path.join(root, fileName)).isDirectory();
  59. });
  60. }
  61. function tocfileName (language, version, suffix) {
  62. var versionSlug = version.replace(/\./g, '-');
  63. if (suffix) {
  64. suffix = '-' + suffix;
  65. } else {
  66. suffix = '';
  67. }
  68. return language + '_' + versionSlug + suffix + '.yml';
  69. }
  70. function srcTocfileName (language, version) {
  71. return tocfileName(language, version, 'src');
  72. }
  73. function genTocfileName (language, version) {
  74. return tocfileName(language, version, 'gen');
  75. }
  76. function mergeObjects (a, b) {
  77. var c = {};
  78. // NOTE: b's properties override a's properties
  79. Object.keys(a).forEach(function (key) {
  80. c[key] = a[key];
  81. });
  82. Object.keys(b).forEach(function (key) {
  83. c[key] = b[key];
  84. });
  85. return c;
  86. }
  87. function generatedBy (scriptName) {
  88. return '# WARNING: This file is generated by ' + path.basename(scriptName);
  89. }
  90. return {
  91. stripFrontMatter: stripFrontMatter,
  92. getFrontMatterString: getFrontMatterString,
  93. setFrontMatterString: setFrontMatterString,
  94. getRawFrontMatterString: getRawFrontMatterString,
  95. listdirsSync: listdirsSync,
  96. tocfileName: tocfileName,
  97. srcTocfileName: srcTocfileName,
  98. genTocfileName: genTocfileName,
  99. mergeObjects: mergeObjects,
  100. generatedBy: generatedBy
  101. };
  102. })();