-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
109 lines (93 loc) · 3.49 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require.paths.unshift(__dirname);
require('showdown');
var http = require('node_compat/http');
var global = {
__doc__: "Below you can find a list of pre-packaged bundles [Beam.js](http://beamjs.org) provides. Please note that you can combine them.\n\n" +
beamjs.bundles.__doc__ +
"Once bundles are loaded using one of the above methods, their modules become available to a program. Happy hacking!",
"default": { __doc__: "This bundle provides core Beam.js functionality and is always loaded", beamjs: beamjs, require: require, console: console, match: match },
"node_compat": { __doc__: "Node.js compatibility layer", events: require('events'), sys: require('sys'), fs: require('fs'), dns: require('dns'), util: require('util'), os: require('os') },
"erlang": { __doc__: "This bundle exposes core Erlang functionality", gen_event: require('gen_event'), messaging: require('messaging'), dist: require('dist') },
"stdlib": { __doc__: "Standard Library", osys: require('osys'), dns: require('dns') },
"commonjs": { __doc__: "CommonJS compatibility layer", test: require('test'), assert: require('assert') }
};
var YAJET = require('yajet');
var Template = function (v) { return v};
var STATIC = {
'/js/code_highlighter.js': undefined,
'/js/javascript.js': undefined,
'/js/breadcrumbs.js': undefined,
'/css/main.css': undefined
};
handler = function(request, response) {
match([[{path: '/favicon.ico', _: match._}, match._], function() {
response.writeHead(404,{});
response.end();
return;
}]).
match([[match._, match._], function() {
if (STATIC[request.path] != undefined) {
response.writeHead(200,{"Content-Type": "text/css"});
response.end(STATIC[request.path]);
return;
}
var tokens = request.path.split("/");
if (request.path=="/") tokens.shift(); // get rid of empty string before the slash
var current = {"": global};
var key="", lkey;
var path = "";
while (tokens.length > 0) {
lkey = tokens.shift();
if (lkey == "") {
key += "Bundles";
} else {
key += "." + lkey;
}
path += lkey + "/" ;
if ((current = current[lkey]) == undefined) {
response.writeHead(200,{"Content-Type": "text/html"});
response.end("<h1>Not found</h1>");
return;
}
}
var templateData = {
doc: "",
key: key,
topics: {},
obj: current,
path: path,
version: beamjs.version
};
if ((d = current['__doc__']) != undefined) {
templateData.doc= d;
}
for (_export in current) {
if ((_export != "__doc__") && ((typeof current[_export] == 'object') || (typeof current[_export] == 'function'))) {
templateData.topics[_export] = current[_export];
}
}
if ((typeof current == 'function') && (current.prototype != undefined) && (current.prototype != {})) {
templateData.topics['prototype'] = current.prototype;
}
try {
response.writeHead(200, {"Content-Type": "text/html"});
response.end(Template(templateData));
} catch (e) {
console.log(e);
}
}])([request, response]);
}
var showdown = new Showdown.converter();
for (file in STATIC) {
require('fs').readFile(__dirname + file, function(err, data) {
STATIC[file] = data;
});
}
require('fs').readFile(__dirname + "/template.html", function(err, template) {
Template = new YAJET({reader_char: "$", filters : {
showdown: function(val) { return showdown.makeHtml(val) },
typeOf: function(val) { if (typeof val == 'function') return 'function'; if (typeof val == 'object') return ''; }
}}).compile(template);
server = http.createServer(handler)
server.listen(listenPort || 8080);
});