blob: f830eb37a514dec60a69b3a21bd274846f31f402 [file] [log] [blame] [raw]
Matt Godbolt15ea5e12012-05-22 21:07:40 -05001(function() {
2
3var props = require('../properties.js'),
4 path = require('path'),
5 fs = require('fs');
6
Matt Godbolt43a21732014-04-30 21:55:02 -05007var sourcePath = props.get('builtin', 'sourcepath', './examples/c++');
8var sourceMatch = new RegExp(props.get('builtin', 'extensionRe', '.*\.cpp$'));
Matt Godbolt15ea5e12012-05-22 21:07:40 -05009var examples = fs.readdirSync(sourcePath)
Matt Godbolt43a21732014-04-30 21:55:02 -050010 .filter(function(file) { return file.match(sourceMatch); })
Matt Godbolt15ea5e12012-05-22 21:07:40 -050011 .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; });
15var byUrlpart = {};
16examples.forEach(function(e) { byUrlpart[e.urlpart] = e.path });
17
18function 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
27function list(callback) {
28 callback(null, examples.map(function(example) {
29 return {urlpart: example.urlpart, name: example.name};
30 }));
31}
32
33exports.load = load;
34exports.save = null;
35exports.list = list;
36exports.name = "Examples";
37exports.urlpart = "builtin";
38
39}).call(this);