Skip to content

Instantly share code, notes, and snippets.

@paulredmond
Last active May 21, 2017 23:04
Show Gist options
  • Save paulredmond/31e5b656a2bc63b93a2d3d939ecd38d8 to your computer and use it in GitHub Desktop.
Save paulredmond/31e5b656a2bc63b93a2d3d939ecd38d8 to your computer and use it in GitHub Desktop.

Revisions

  1. paulredmond renamed this gist Feb 14, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. paulredmond revised this gist Feb 14, 2017. 1 changed file with 0 additions and 10 deletions.
    10 changes: 0 additions & 10 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    {
    "scripts": {
    "docs": "node docs.js"
    },
    "devDependencies": {
    "cson": "^4.0.0",
    "glob": "^7.1.1",
    "lodash": "^4.17.4"
    }
    }
  3. paulredmond revised this gist Feb 14, 2017. 1 changed file with 15 additions and 2 deletions.
    17 changes: 15 additions & 2 deletions doc.js
    Original file line number Diff line number Diff line change
    @@ -7,17 +7,30 @@ const path = require('path');
    const fs = require('fs');
    const _ = require('lodash');

    function Snippet(title, code, prefix, fileName) {
    function Snippet(title, code, prefix, fileName, lang) {
    this.title = title;
    this.code = code;
    this.prefix = prefix;
    this.fileName = fileName;
    this.lang = this.getCodeLanguage(lang);
    }

    Snippet.prototype.getFormattedCode = function () {
    return this.code.replace(/\\\\/g, '\\');
    };

    /**
    * Parse the language from the snippet's root source type (eg `.source.php`)
    */
    Snippet.prototype.getCodeLanguage = function (lang) {
    var lang = lang.split('.');

    lang = lang[lang.length - 1];
    lang = (lang == '*' || lang == '') ? '' : lang;

    return lang;
    }

    // Process snippets and generate markdown reference document
    glob('./snippets/**/*.cson', (err, files) => {
    var snippets = [];
    @@ -31,7 +44,7 @@ glob('./snippets/**/*.cson', (err, files) => {
    Object.keys(parsed).forEach(key => {
    Object.keys(parsed[key]).forEach(title => {
    var s = parsed[key][title];
    snippets.push(new Snippet(title, s.body, s.prefix, fileName));
    snippets.push(new Snippet(title, s.body, s.prefix, fileName, key));
    });
    });
    });
  4. paulredmond created this gist Feb 14, 2017.
    46 changes: 46 additions & 0 deletions doc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    /**
    * Convert cson snippets into a Markdown document for reference
    */
    const cson = require('cson');
    const glob = require ('glob');
    const path = require('path');
    const fs = require('fs');
    const _ = require('lodash');

    function Snippet(title, code, prefix, fileName) {
    this.title = title;
    this.code = code;
    this.prefix = prefix;
    this.fileName = fileName;
    }

    Snippet.prototype.getFormattedCode = function () {
    return this.code.replace(/\\\\/g, '\\');
    };

    // Process snippets and generate markdown reference document
    glob('./snippets/**/*.cson', (err, files) => {
    var snippets = [];
    var template = fs.readFileSync('./doc/snippet.jst').toString();
    var compiled = _.template(template);

    files.forEach(file => {
    var fileName = path.parse(file).base;
    var parsed = cson.parseCSONFile(file);

    Object.keys(parsed).forEach(key => {
    Object.keys(parsed[key]).forEach(title => {
    var s = parsed[key][title];
    snippets.push(new Snippet(title, s.body, s.prefix, fileName));
    });
    });
    });

    fs.writeFile('./doc/snippet-reference.markdown', compiled({ snippets: snippets }), (err) => {
    if (err) {
    return console.log(err);
    }
    console.log("The ./doc/snippet-reference.mardown file was saved");
    });

    });
    10 changes: 10 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    {
    "scripts": {
    "docs": "node docs.js"
    },
    "devDependencies": {
    "cson": "^4.0.0",
    "glob": "^7.1.1",
    "lodash": "^4.17.4"
    }
    }