Matt Godbolt | 15ea5e1 | 2012-05-22 21:07:40 -0500 | [diff] [blame] | 1 | (function() { |
| 2 | |
| 3 | var props = require('../properties.js'), |
| 4 | path = require('path'), |
| 5 | fs = require('fs'); |
| 6 | |
Matt Godbolt | 43a2173 | 2014-04-30 21:55:02 -0500 | [diff] [blame^] | 7 | var sourcePath = props.get('builtin', 'sourcepath', './examples/c++'); |
| 8 | var sourceMatch = new RegExp(props.get('builtin', 'extensionRe', '.*\.cpp$')); |
Matt Godbolt | 15ea5e1 | 2012-05-22 21:07:40 -0500 | [diff] [blame] | 9 | var examples = fs.readdirSync(sourcePath) |
Matt Godbolt | 43a2173 | 2014-04-30 21:55:02 -0500 | [diff] [blame^] | 10 | .filter(function(file) { return file.match(sourceMatch); }) |
Matt Godbolt | 15ea5e1 | 2012-05-22 21:07:40 -0500 | [diff] [blame] | 11 | .map(function(file) { |
| 12 | var nicename = file.replace(/\.cpp$/, ''); |
| 13 | return { urlpart: nicename, name: nicename.replace(/_/g, ' '), path: path.join(sourcePath, file) }; |
| 14 | }).sort(function(x,y) { return y.name < x.name; }); |
| 15 | var byUrlpart = {}; |
| 16 | examples.forEach(function(e) { byUrlpart[e.urlpart] = e.path }); |
| 17 | |
| 18 | function load(filename, callback) { |
| 19 | var path = byUrlpart[filename]; |
| 20 | if (!path) { callback("No such path"); return; } |
| 21 | fs.readFile(path, 'utf-8', function(err, res) { |
| 22 | if (err) { callback(err); return; } |
| 23 | callback(null, {file: res}); |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | function list(callback) { |
| 28 | callback(null, examples.map(function(example) { |
| 29 | return {urlpart: example.urlpart, name: example.name}; |
| 30 | })); |
| 31 | } |
| 32 | |
| 33 | exports.load = load; |
| 34 | exports.save = null; |
| 35 | exports.list = list; |
| 36 | exports.name = "Examples"; |
| 37 | exports.urlpart = "builtin"; |
| 38 | |
| 39 | }).call(this); |