Last active
May 21, 2017 23:04
-
-
Save paulredmond/31e5b656a2bc63b93a2d3d939ecd38d8 to your computer and use it in GitHub Desktop.
Revisions
-
paulredmond renamed this gist
Feb 14, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
paulredmond revised this gist
Feb 14, 2017 . 1 changed file with 0 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +0,0 @@ -
paulredmond revised this gist
Feb 14, 2017 . 1 changed file with 15 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, 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, key)); }); }); }); -
paulredmond created this gist
Feb 14, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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"); }); }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" } }