/introToChiasm/examples/compileReadme.js

https://gitlab.com/pgksunilkumar/screencasts · JavaScript · 159 lines · 90 code · 27 blank · 42 comment · 17 complexity · 7884a9786ec644323da454cd53118ae1 MD5 · raw file

  1. // This script compiles README_template.md
  2. //
  3. // * compiling metadata about examples
  4. // * dynamically generating a markdown list of example entries
  5. // * injecting the list into the template
  6. //
  7. // Created by Curran Kelleher 3/2/2014
  8. //
  9. // Updated July 2014 to point to the "code" subdirectory,
  10. // so the URL looks like modelDrivenDataVis/examples/code/index.html
  11. // rather than modelDrivenDataVis/examples/examples/index.html
  12. //
  13. // Updated October 2014
  14. //
  15. // * put the README in the parent directory
  16. // * stop producing README.html
  17. //
  18. var _ = require('underscore'),
  19. fs = require('fs'),
  20. // This file contains data that changes for each different presentation.
  21. // This includes fields like project title, and base GitHub URL.
  22. projectFile = './project.json',
  23. inputFile = './README_template.md',
  24. outputFile = '../README.md',
  25. outputJSONFile = './examples.json',
  26. entryDir = 'code/',
  27. snapshotsPath = entryDir,
  28. messageFile = '/message.txt',
  29. entryTemplate = _.template(' * [Example <%= number %>](<%= url %>) - <%= message %>'),
  30. // These files are not excluded from the example code viewer.
  31. irrelevantFiles = ['message.txt', 'README.md', 'server.js', 'backbone.js', 'underscore.js', 'main-build.js'];
  32. // Read the project description file.
  33. fs.readFile(projectFile, 'utf8', function (err, project) {
  34. // Parse the JSON text into an object.
  35. project = JSON.parse(project);
  36. // Read the template for README.md
  37. fs.readFile(inputFile, 'utf8', function (err, template) {
  38. // Generate the data describing the examples.
  39. var entries = generateExampleJSON(project),
  40. // The raw data about each example is output as examples.json.
  41. outputJSON = JSON.stringify(entries, null, 2),
  42. // The same data is used to generate the model for the README.md template.
  43. model = generateREADMEmodel(entries),
  44. // Evaluate the README.md template.
  45. output = _.template(template)(model);
  46. // Write README.md
  47. write(outputFile, output);
  48. // Write examples.json (used by the example viewer app)
  49. write(outputJSONFile, outputJSON);
  50. });
  51. });
  52. function write(outputFile, output){
  53. fs.writeFile(outputFile, output, function(err) {
  54. if(err) {
  55. console.log(err);
  56. } else {
  57. console.log("Wrote file " + outputFile);
  58. }
  59. });
  60. }
  61. // Generates the object passed into the README template.
  62. function generateREADMEmodel(entries){
  63. // Return an object with a property "examples",
  64. // which is referenced from within the README template.
  65. return {
  66. // Apply the entry template to each example entry.
  67. examples: entries.map(entryTemplate).join('\n')
  68. };
  69. }
  70. // Generates data about all examples by reading
  71. // a directory listing from the file system.
  72. function generateExampleJSON(project){
  73. // Read the list of examples from the file system.
  74. var files = fs.readdirSync(snapshotsPath);
  75. return _.sortBy(files
  76. // Exclude the "latest" subdirectory.
  77. .filter(function (file) {
  78. return file !== "latest";
  79. })
  80. // Assemble data about each example.
  81. .map(function (file) {
  82. return {
  83. name: file,
  84. number: extractNumber(file),
  85. message: getMessage(file),
  86. files: listFilesForExample(file),
  87. url: project.snapshotURL + extractNumber(file),
  88. runUrl: project.snapshotRunURL + file
  89. };
  90. }), "number");
  91. }
  92. // Computes the list of files for each example.
  93. // Sorts files by:
  94. // index.html > *.html > *.js > *.json > *
  95. function listFilesForExample(file){
  96. var path = snapshotsPath + file,
  97. allFiles = fs.readdirSync(path),
  98. files =_.difference(allFiles, irrelevantFiles);
  99. return _.sortBy(files, filePrecedence);
  100. }
  101. function filePrecedence(name){
  102. var ext = name.substr(name.lastIndexOf('.'));
  103. // TODO fix this nasty code.
  104. if(name === 'main.js'){
  105. return 0;
  106. } else if (name === 'myComponent.js') {
  107. return 0.5;
  108. } else if (name === 'mixins.js') {
  109. return 1;
  110. } else if (name === 'index.html') {
  111. return 1.5;
  112. } else if (ext === '.html') {
  113. return 2;
  114. } else if (ext === '.js') {
  115. return 3;
  116. } else if (ext === '.json') {
  117. return 4;
  118. } else {
  119. return 5;
  120. }
  121. }
  122. function extractNumber(name){
  123. return parseInt(name.substr(8), 10);
  124. }
  125. function getMessage(file){
  126. var msgFile = snapshotsPath + file + messageFile;
  127. if(fs.existsSync(msgFile)){
  128. var msg = fs.readFileSync(msgFile, 'utf8');
  129. return msg.replace('\n','');
  130. }
  131. return '';
  132. }