Skip to content

Commit

Permalink
Switch to two spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianheine committed Feb 6, 2013
1 parent 0577de1 commit da3631d
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 316 deletions.
103 changes: 53 additions & 50 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,80 @@
'use strict';

var express = require('express'),
http = require('http'),
fs = require('fs'),
lib = require('./lib'),
http = require('http'),
fs = require('fs'),
lib = require('./lib'),

staticDir = '/static',
lessConfig = {
src: __dirname + staticDir + '/style',
dstRoot: __dirname
};
staticDir = '/static',
lessConfig = {
src: __dirname + staticDir + '/style',
dstRoot: __dirname
};

var app = module.exports = express();

// Configuration
if (app.get('env') === 'production') {
lessConfig.compress = true;
lessConfig.compress = true;
} else {
lessConfig.compress = false;
app.use(express.errorHandler());
lessConfig.compress = false;
app.use(express.errorHandler());
}

app.configure(function () {
app.set('port', process.env.PORT || 9002);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon(__dirname + '/favicon.ico'));
app.set('port', process.env.PORT || 9002);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon(__dirname + '/favicon.ico'));

app.use(require('connect-less')(lessConfig));
app.use(require('connect-less')(lessConfig));

app.use(staticDir, express['static'](__dirname + staticDir, {maxAge: 1000 * 60 * 60 * 24 * 365}));
app.use(express.logger('dev'));
app.use(app.router);
app.use(staticDir, express['static'](__dirname + staticDir, {maxAge: 1000 * 60 * 60 * 24 * 365}));
app.use(express.logger('dev'));
app.use(app.router);
});

// Routes
var tabs = lib.buildSubs({'Intro': {}, 'Skills': {}, 'Examples': {}, 'CV': {}, 'Contact': {}});

lib.each(tabs, function (tab, tabid) {
if (fs.existsSync(__dirname + '/subs/' + tabid + '.js')) {
tab.subs = lib.buildSubs(tab.title, require('./subs/' + tabid));
}
if (fs.existsSync(__dirname + '/subs/' + tabid + '.js')) {
tab.subs = lib.buildSubs(tab.title, require('./subs/' + tabid));
}
});

app.get('/:tab?/:sub?', function (req, res, next) {
var cur_t = req.params.tab || 'intro',
sub = req.params.sub,
fragment = false;

if (!tabs.hasOwnProperty(cur_t) ||
(sub && !tabs[cur_t].subs.hasOwnProperty(sub))) {
return next();
}

if (!req.accepts('html')) {
fragment = lib.firstRes(['focus', 'tab'], function (type) {
return req.accepts(lib.mimeType(type)) && type;
});
}

res.header('Vary', 'Accept');

if (fragment) {
// Do not use res.contentType since it performs a mime lookup which
// resolves my personal mime type with application/octet-stream.
res.header('Content-Type', lib.mimeType(fragment));
}

res.render(cur_t + (fragment === 'focus' && sub ? '_sub' : ''),
{tabs: tabs, cur_t: cur_t, sub: sub,
longTitle: sub ? tabs[cur_t].subs[sub].longTitle : tabs[cur_t].longTitle,
linkTo: lib.linkTo.bind(undefined, tabs[cur_t].title),
layout: !fragment});
var cur_t = req.params.tab || 'intro',
sub = req.params.sub,
fragment = false;

if (!tabs.hasOwnProperty(cur_t) ||
(sub && !tabs[cur_t].subs.hasOwnProperty(sub))) {
return next();
}

if (!req.accepts('html')) {
fragment = lib.firstRes(['focus', 'tab'], function (type) {
return req.accepts(lib.mimeType(type)) && type;
});
}

res.header('Vary', 'Accept');

if (fragment) {
// Do not use res.contentType since it performs a mime lookup which
// resolves my personal mime type with application/octet-stream.
res.header('Content-Type', lib.mimeType(fragment));
}

res.render(cur_t + (fragment === 'focus' && sub ? '_sub' : ''), {
tabs: tabs,
cur_t: cur_t,
sub: sub,
longTitle: sub ? tabs[cur_t].subs[sub].longTitle : tabs[cur_t].longTitle,
linkTo: lib.linkTo.bind(undefined, tabs[cur_t].title),
layout: !fragment
});
});

http.createServer(app).listen(app.get('port'), function(){
Expand Down
86 changes: 43 additions & 43 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,69 @@
var lib = module.exports = require('underscore');

lib.longTitle = function (tab, title) {
var parts = ['Hire Adrian Lang'];
if (tab && tab !== 'Intro') {
parts.push(tab);
}
if (title) {
parts.push(title);
}
return parts.join(' | ');
var parts = ['Hire Adrian Lang'];
if (tab && tab !== 'Intro') {
parts.push(tab);
}
if (title) {
parts.push(title);
}
return parts.join(' | ');
};

lib.mimeType = function (subtype) {
return 'application/prs.de.adrianlang.hire.' + subtype;
return 'application/prs.de.adrianlang.hire.' + subtype;
};

lib.html = function (str) {
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;')
.replace(/>/g, '&gt;').replace(/"/g, '&quot;');
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;')
.replace(/>/g, '&gt;').replace(/"/g, '&quot;');
};

lib.id = function (item) {
return item.toLowerCase().replace(/ /g, '_').replace(/[\/.]/g, '-');
return item.toLowerCase().replace(/ /g, '_').replace(/[\/.]/g, '-');
};

lib.linkTo = function (def, path, txt) {
if (typeof path === 'string') {
path = [path];
}
if (path.length === 1) {
path.unshift(def);
}
if (typeof txt === 'undefined') {
txt = path[path.length - 1];
}
return '<a href="/' + path.map(lib.id).map(lib.html).join('/') + '" ' +
'title="' + lib.html(lib.longTitle(path[0], path[1])) + '">' +
lib.html(txt) + '</a>';
if (typeof path === 'string') {
path = [path];
}
if (path.length === 1) {
path.unshift(def);
}
if (typeof txt === 'undefined') {
txt = path[path.length - 1];
}
return '<a href="/' + path.map(lib.id).map(lib.html).join('/') + '" ' +
'title="' + lib.html(lib.longTitle(path[0], path[1])) + '">' +
lib.html(txt) + '</a>';
};

lib.buildSubs = function (tab, items, defaults) {
var longTitle = lib.longTitle;
var longTitle = lib.longTitle;

defaults = defaults || {};
defaults = defaults || {};

if (items) {
longTitle = longTitle.bind(undefined, tab);
} else {
items = tab;
}
if (items) {
longTitle = longTitle.bind(undefined, tab);
} else {
items = tab;
}

return lib.reduce(items, function (ret, data, item) {
ret[lib.id(item)] = lib.extend({
title: item,
longTitle: longTitle(item)
}, defaults, data);
return ret;
}, {});
return lib.reduce(items, function (ret, data, item) {
ret[lib.id(item)] = lib.extend({
title: item,
longTitle: longTitle(item)
}, defaults, data);
return ret;
}, {});
};

lib.firstRes = function (items, test_func) {
var res = null;
items.some(function () {
res = test_func.apply(this, Array.prototype.slice.call(arguments));
return res;
});
var res = null;
items.some(function () {
res = test_func.apply(this, Array.prototype.slice.call(arguments));
return res;
});
return res;
};
16 changes: 8 additions & 8 deletions list_skills.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
"use strict";

var skills = require('./subs/skills'),
_ = require('underscore');
_ = require('underscore');

skills = _.map(_.groupBy(_.map(skills, function (data, skill) {
return {
score: data.score,
title: skill + (data.desc === 'No description yet, sorry.' ? '(!)' : '')
};
return {
score: data.score,
title: skill + (data.desc === 'No description yet, sorry.' ? '(!)' : '')
};
}), function (skill) {
return skill.score;
return skill.score;
}), function (group) {
return _.pluck(group, 'title');
return _.pluck(group, 'title');
});

_.each(skills, function (g) {
console.log(String(g.length), ':', g.join(', '), '\n');
console.log(String(g.length), ':', g.join(', '), '\n');
});
34 changes: 17 additions & 17 deletions subs/examples.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit da3631d

Please sign in to comment.