/navigation/examples/compileReadme.js

https://gitlab.com/pgksunilkumar/screencasts · JavaScript · 125 lines · 81 code · 16 blank · 28 comment · 11 complexity · c3d11fd04bb9b9526b6023915d523c16 MD5 · raw file

  1. // This script compiles README_template.md
  2. // into README.md by
  3. //
  4. // * compiling metadata about examples
  5. // * dynamically generating a markdown list of example entries
  6. // * injecting the list into the template
  7. //
  8. // Created by Curran Kelleher 3/2/2014
  9. //
  10. // Updated July 2014 to point to the "code" subdirectory,
  11. // so the URL looks like modelDrivenDataVis/examples/code/index.html
  12. // rather than modelDrivenDataVis/examples/examples/index.html
  13. //
  14. // Updated October 2014
  15. //
  16. // * put the README in the parent directory
  17. // * stop producing README.html
  18. //
  19. var _ = require('underscore'),
  20. marked = require('marked'),
  21. fs = require('fs'),
  22. inputFile = './README_template.md',
  23. outputFile = '../README.md',
  24. outputJSONFile = './examples.json',
  25. entryDir = 'code/',
  26. snapshotsPath = entryDir,
  27. snapshotURL = 'https://github.com/curran/screencasts/tree/gh-pages/navigation/examples/code/',
  28. snapshotRunURL = 'http://curran.github.io/screencasts/navigation/examples/code/',
  29. messageFile = '/message.txt',
  30. entryTemplate = _.template(' * [Example <%= number %>](<%= url %>) - ([run it!](<%= runUrl %>) | [index.html](<%= url %>/index.html)) - <%= message %>'),
  31. // These files are not excluded from the example code viewer.
  32. irrelevantFiles = ['message.txt', 'README.md', 'server.js', 'backbone.js', 'underscore.js'];
  33. // Read the template for README.md
  34. fs.readFile(inputFile, 'utf8', function (err, template) {
  35. // Generate the data describing the examples.
  36. var data = generateExampleJSON(),
  37. outputJSON = JSON.stringify(data, null, 2),
  38. // Generate the model for the README.md template.
  39. model = generateREADMEmodel(data),
  40. // Evaluate the README.md template.
  41. output = _.template(template, model);
  42. // Write README.md
  43. write(outputFile, output);
  44. // Write examples.json (used by the example viewer app)
  45. write(outputJSONFile, outputJSON);
  46. });
  47. function write(outputFile, output){
  48. fs.writeFile(outputFile, output, function(err) {
  49. if(err) {
  50. console.log(err);
  51. } else {
  52. console.log("Wrote file " + outputFile);
  53. }
  54. });
  55. }
  56. function generateREADMEmodel(data){
  57. return { examples: data.map(entryTemplate).join('\n') };
  58. }
  59. function generateExampleJSON(){
  60. var files = fs.readdirSync(snapshotsPath);
  61. return files
  62. .filter(function (file) {
  63. return file !== "latest";
  64. })
  65. .map(function (file) {
  66. return {
  67. name: file,
  68. number: extractNumber(file),
  69. message: getMessage(file),
  70. files: listFilesForExample(file),
  71. url: snapshotURL + file,
  72. runUrl: snapshotRunURL + file
  73. };
  74. });
  75. }
  76. // Computes the list of files for each example.
  77. // Sorts files by:
  78. // index.html > *.html > *.js > *.json > *
  79. function listFilesForExample(file){
  80. var path = snapshotsPath + file,
  81. allFiles = fs.readdirSync(path),
  82. files =_.difference(allFiles, irrelevantFiles);
  83. return _.sortBy(files, filePrecedence);
  84. }
  85. function filePrecedence(name){
  86. var ext = name.substr(name.lastIndexOf('.'));
  87. if(name === 'index.html'){
  88. return 0;
  89. } else if (ext === '.html') {
  90. return 1;
  91. } else if (ext === '.js') {
  92. return 2;
  93. } else if (ext === '.json') {
  94. return 3;
  95. } else {
  96. return 4;
  97. }
  98. }
  99. function extractNumber(name){
  100. return parseInt(name.substr(8), 10);
  101. }
  102. function getMessage(file){
  103. var msgFile = snapshotsPath + file + messageFile;
  104. if(fs.existsSync(msgFile)){
  105. var msg = fs.readFileSync(msgFile, 'utf8');
  106. return msg.replace('\n','');
  107. }
  108. return '';
  109. }