-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
executable file
·171 lines (130 loc) · 4.87 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var express = require('express'),
favicon = require('serve-favicon'),
path = require('path'),
oboe = require('oboe'),
consolidate = require('consolidate'),
readContent = require('./read-content.js'),
readPagesList = require('./read-pages-list.js'),
barrier = require('./barrier.js'),
environment = require('optimist')
.demand(['env'])
.argv.env,
PORT = '8888',
isProd = (environment == 'prod'),
SCRIPTS = isProd ? ['/js-concat/all.js'] : require('./sourceList.js'),
CSS_STYLESHEETS = isProd ? ["all-min.css"] : ["all.css"],
latestTag = '',
ANALYTICS_ID = 'UA-47871814-1',
RAW_REPO_LOCATION = 'https://raw.github.com/jimhigson/oboe.js',
REPO_LOCATION = 'https://github.com/jimhigson/oboe.js',
GITHUB_TAGS_URL = 'https://api.github.com/repos/jimhigson/oboe.js/tags',
USER_AGENT = 'http://github.com/jimhigson/oboe.js-website',
app = express();
require('colors');
console.log('starting up for environment', environment.blue);
// Load the latest tag from Github. This will be unavailable for a second or two
// when the site starts up.
var FIFTEEN_MINUTES = 1000 * 60 * 15;
function getLatestTag() {
oboe({ url: GITHUB_TAGS_URL, headers: { 'User-Agent': USER_AGENT } })
.node('![0].name', function (tagName) {
console.log('latest Oboe version is', tagName.blue);
latestTag = tagName;
this.abort();
})
.fail(console.error);
}
setInterval(getLatestTag, FIFTEEN_MINUTES);
getLatestTag();
app.engine('handlebars', consolidate.handlebars);
app.set('view engine', 'handlebars');
app.set('views', __dirname + '/views');
/* create <template> elements to send to the client side so it can
* make visualisations by cloning them */
function renderClientSideDemoTemplates(res, callback) {
var DEMO_TEMPLATE_OPTIONS = { packetRadius: 15 };
res.render('demoTemplate', DEMO_TEMPLATE_OPTIONS,
function (err, demoContentHtml) {
callback(demoContentHtml);
});
}
function defaultOpts(opts) {
opts = opts || {};
opts.scripts = SCRIPTS;
opts.stylesheets = CSS_STYLESHEETS;
opts.latestTag = latestTag;
opts.analyticsId = ANALYTICS_ID;
opts.repo = REPO_LOCATION;
opts.rawRepo = RAW_REPO_LOCATION;
opts.logoSize = 64;
opts.releasedJs = RAW_REPO_LOCATION + '/' + latestTag + '/dist';
opts.production = isProd;
return opts;
}
function respondWithMarkdown(req, res, getContentFn, opts) {
var view = (req.query.mode == 'raw' ? 'raw' : 'page');
opts = defaultOpts(opts);
var bar = barrier(function () {
res.render(view, opts);
console.log('The HTML page for', req.url.blue, 'was created in', String(bar.duration).blue, 'ms');
});
readPagesList(bar.add(function (pages) {
// if any page in the pages list is the current, mark as such:
pages.forEach(function (page) {
var pageUrl = '/' + page.path;
page.current = (pageUrl == req.url);
});
opts.pages = pages;
}));
getContentFn(req, opts, bar.add(function (outline) {
opts.content = outline.content;
opts.heading = outline.heading;
opts.sections = outline.sections;
opts.multipleSections = outline.multipleSections;
res.status(outline.status);
}));
renderClientSideDemoTemplates(res, bar.add(function (templateHtml) {
opts.templates = templateHtml;
}));
}
function readMarkdownFromFile(filename) {
return function (req, opts, callback) {
readContent(filename, opts, callback);
};
}
app
.use(favicon(path.join(__dirname, '/statics/favicons/favicon.ico')))
.use(express.static('statics'))
.use(express.static('pdf'))
.use(express.static('bower_components'))
.get('/', function (req, res) {
respondWithMarkdown(req, res, readMarkdownFromFile('index'), {
home: true,
twitter: true
});
})
.get('/:page', function (req, res) {
respondWithMarkdown(req, res, readMarkdownFromFile(req.params.page));
});
// allow single demos to be viewed but only if we are in dev:
if (environment == 'dev') {
app.get('/demo/:demo', function (req, res) {
function generateMarkdownForSingleDemo(req, _opts, callback) {
var demoName = req.params.demo;
callback({
content: '<figure data-demo="' + demoName + '"></figure>'
, heading: { text: 'Demo: ' + demoName }
, sections: []
, status: 200
});
}
respondWithMarkdown(req, res, generateMarkdownForSingleDemo);
})
}
// As a catch-all generate a 404.
app.use(function (req, res) {
console.warn('Unrecognised path; catch-all serving 404:'.red, req.url);
respondWithMarkdown(req, res, readMarkdownFromFile('404'));
});
app.listen(PORT);
console.log('started on port', PORT.blue);