/navigation/examples/compileReadme.js
https://gitlab.com/pgksunilkumar/screencasts · JavaScript · 125 lines · 81 code · 16 blank · 28 comment · 11 complexity · c3d11fd04bb9b9526b6023915d523c16 MD5 · raw file
- // This script compiles README_template.md
- // into README.md by
- //
- // * compiling metadata about examples
- // * dynamically generating a markdown list of example entries
- // * injecting the list into the template
- //
- // Created by Curran Kelleher 3/2/2014
- //
- // Updated July 2014 to point to the "code" subdirectory,
- // so the URL looks like modelDrivenDataVis/examples/code/index.html
- // rather than modelDrivenDataVis/examples/examples/index.html
- //
- // Updated October 2014
- //
- // * put the README in the parent directory
- // * stop producing README.html
- //
- var _ = require('underscore'),
- marked = require('marked'),
- fs = require('fs'),
- inputFile = './README_template.md',
- outputFile = '../README.md',
- outputJSONFile = './examples.json',
- entryDir = 'code/',
- snapshotsPath = entryDir,
- snapshotURL = 'https://github.com/curran/screencasts/tree/gh-pages/navigation/examples/code/',
- snapshotRunURL = 'http://curran.github.io/screencasts/navigation/examples/code/',
- messageFile = '/message.txt',
- entryTemplate = _.template(' * [Example <%= number %>](<%= url %>) - ([run it!](<%= runUrl %>) | [index.html](<%= url %>/index.html)) - <%= message %>'),
- // These files are not excluded from the example code viewer.
- irrelevantFiles = ['message.txt', 'README.md', 'server.js', 'backbone.js', 'underscore.js'];
- // Read the template for README.md
- fs.readFile(inputFile, 'utf8', function (err, template) {
- // Generate the data describing the examples.
- var data = generateExampleJSON(),
- outputJSON = JSON.stringify(data, null, 2),
- // Generate the model for the README.md template.
- model = generateREADMEmodel(data),
- // Evaluate the README.md template.
- output = _.template(template, model);
- // Write README.md
- write(outputFile, output);
- // Write examples.json (used by the example viewer app)
- write(outputJSONFile, outputJSON);
- });
- function write(outputFile, output){
- fs.writeFile(outputFile, output, function(err) {
- if(err) {
- console.log(err);
- } else {
- console.log("Wrote file " + outputFile);
- }
- });
- }
- function generateREADMEmodel(data){
- return { examples: data.map(entryTemplate).join('\n') };
- }
- function generateExampleJSON(){
- var files = fs.readdirSync(snapshotsPath);
- return files
- .filter(function (file) {
- return file !== "latest";
- })
- .map(function (file) {
- return {
- name: file,
- number: extractNumber(file),
- message: getMessage(file),
- files: listFilesForExample(file),
- url: snapshotURL + file,
- runUrl: snapshotRunURL + file
- };
- });
- }
- // Computes the list of files for each example.
- // Sorts files by:
- // index.html > *.html > *.js > *.json > *
- function listFilesForExample(file){
- var path = snapshotsPath + file,
- allFiles = fs.readdirSync(path),
- files =_.difference(allFiles, irrelevantFiles);
- return _.sortBy(files, filePrecedence);
- }
- function filePrecedence(name){
- var ext = name.substr(name.lastIndexOf('.'));
- if(name === 'index.html'){
- return 0;
- } else if (ext === '.html') {
- return 1;
- } else if (ext === '.js') {
- return 2;
- } else if (ext === '.json') {
- return 3;
- } else {
- return 4;
- }
- }
- function extractNumber(name){
- return parseInt(name.substr(8), 10);
- }
- function getMessage(file){
- var msgFile = snapshotsPath + file + messageFile;
- if(fs.existsSync(msgFile)){
- var msg = fs.readFileSync(msgFile, 'utf8');
- return msg.replace('\n','');
- }
- return '';
- }