Skip to content

Commit

Permalink
the url /update downloads the latest for the git repo in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
bennage committed Mar 23, 2012
1 parent de11732 commit 9697715
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 62 deletions.
50 changes: 21 additions & 29 deletions lib/app.js
@@ -1,37 +1,19 @@
var fs = require('fs'),
x = require('express'),
stylus = require('stylus'),
path = require('path');

var auth = require('./auth'),
config = require('./config'),
context = require('./context'),
convert = require('./convert')(),
github = require('./github')(),
viewhelpers = require('./viewhelpers'),
routes = require('./routes'),
auth = require('./auth');
viewhelpers = require('./viewhelpers');

var app = module.exports = x.createServer();

function context(req, res, next) {

var session, auth;

// if we already have the context, next
if (req.context && req.context.user) {
return next();
}

session = req.session;
req.context = {};

// if there's not session or auth, then set an empty context
if (!session || !(session.auth && session.auth.loggedIn)) {
return next();
}

req.context.user = session.auth.github.user;
req.context.accessToken = session.auth.github.accessToken;
// github.init(req.context.accessToken);
next();
}
__dirname = __dirname + '/..';

app.configure('development', function() {
app.use(x.logger('tiny'));
Expand Down Expand Up @@ -70,15 +52,25 @@ app.configure('production', function() {
viewhelpers.initialize(app);

app.get('/', routes.index);

app.get('/update', function(req, res) {
github(config.source.repo, './source/', config.source.ignore, function(err, sha) {
console.log('downloading complete');

var src = path.join('./source'),
tgt = path.join('./markdown');

github(config.source.repo, src, config.source.ignore, function(err, sha) {

console.log('downloading complete: ' + sha);

var imageRoot = 'https://raw.github.com/' + config.source.repo + '/' + sha;
convert('./source', './markdown', imageRoot);
res.end();
convert(src, tgt, imageRoot, function() {
res.redirect('/index');
});
});

});

app.get('/reset', function(req, res) {});

app.get('/export', routes['export']);
app.post('/comment/:file', routes.saveComment);
app.get('/comments/:file', routes.getComments);
Expand Down
22 changes: 22 additions & 0 deletions lib/context.js
@@ -0,0 +1,22 @@
module.exports = function context(req, res, next) {

var session, auth;

// if we already have the context, next
if (req.context && req.context.user) {
return next();
}

session = req.session;
req.context = {};

// if there's not session or auth, then set an empty context
if (!session || !(session.auth && session.auth.loggedIn)) {
return next();
}

req.context.user = session.auth.github.user;
req.context.accessToken = session.auth.github.accessToken;

next();
}
30 changes: 24 additions & 6 deletions lib/convert.js
@@ -1,12 +1,13 @@
module.exports = function(fs, marked) {
module.exports = function(fs, marked, path) {

fs = fs || require('fs');
marked = marked || require('marked');
path = path || require('path');

marked.setOptions({
gfm: true
});


function ensureDirectory(folder, callback) {
fs.stat(folder, function(err, stats) {
if (err) {
Expand All @@ -17,14 +18,15 @@ module.exports = function(fs, marked) {
});
}

return function(source, target, rootUrl) {
return function(source, target, rootUrl, callback) {

function toHTML(file) {
function toHTML(file, callback) {
fs.readFile(source + '/' + file, 'utf8', function(err, input) {
var file_path = path.join(target, file.replace('.markdown', '.html'));
var i = convertImageRefs(input);
var output = marked(i);
console.log('=> ' + file);
fs.writeFile(target + '/' + file.replace('.markdown', '.html'), output);
fs.writeFile(file_path, output, callback);
});
}

Expand All @@ -36,9 +38,25 @@ module.exports = function(fs, marked) {
return revised;
}

var counter = 0;

ensureDirectory(target, function() {
console.log('attempting to read ' + source);
fs.readdir(source, function(err, files) {
files.forEach(toHTML);
if(err || !files){
console.dir(err || 'no files in ' + source);
callback();
return;
}
files.forEach(function(file) {
counter++;
toHTML(file, function() {
counter--;
if (counter === 0) {
callback();
}
});
});
});
});
};
Expand Down
41 changes: 24 additions & 17 deletions lib/github.js
@@ -1,14 +1,12 @@
module.exports = function(fs, https) {
module.exports = function(fs, https, path) {

fs = fs || require('fs');
https = https || require('https');
path = path || require('path');

var raw_root = 'raw.github.com',
api_root = 'api.github.com';

// var repo = '/mspnp/cqrs-journey-doc/';
// var output_path = './source/';
// var ignore = ['.gitignore', 'Copyright.markdown', 'LICENSE.txt', 'README.markdown'];
return function(repo, output_path, ignore, callback) {

function https_get(options, success) {
Expand Down Expand Up @@ -39,19 +37,19 @@ module.exports = function(fs, https) {
});
}

function downloadBlob(blob) {

var path = repo + blob.branch + '/' + blob.path;
function downloadBlob(blob, callback) {
var file_path = path.join(output_path, blob.path);

git({
host: raw_root,
path: path
path: repo + blob.branch + '/' + blob.path
}, function(err, buffer) {
fs.writeFile(output_path + blob.path, buffer, 'binary');
fs.writeFile(file_path, buffer, 'binary', callback);
});
}

function getTree(sha, branch) {
function getTree(sha, branch, callback) {

var recurse = false;

Expand All @@ -61,44 +59,53 @@ module.exports = function(fs, https) {
}, function(err, buffer) {

var o = JSON.parse(buffer);
var counter = 0;

o.tree.filter(function(x) {
return x.type === 'blob' && x.path.indexOf('.markdown' !== -1) && !ignore.some(function(i) {
return x.path.indexOf(i) > -1;
});
}).map(function(x) {
counter++;
return {
sha: x.sha,
path: x.path,
size: x.size,
branch: branch
};
}).forEach(downloadBlob);
}).forEach(function(x) {
downloadBlob(x, function(err) {
counter--;
if (counter === 0) {
callback();
}
});
});
});
}

function getCommit(commit) {
function getCommit(commit, callback) {

git({
host: api_root,
path: '/repos' + repo + 'git/commits/' + commit
}, function(err, buffer) {
var o = JSON.parse(buffer);
getTree(o.tree.sha, commit);
getTree(o.tree.sha, commit, callback);
});
}

git({
host: api_root,
path: '/repos' + repo + 'git/refs/heads/master'
}, function(err, buffer) {

var o = JSON.parse(buffer),
sha = o.object.sha;

getCommit(sha);

if (callback) callback(null, sha);
getCommit(sha, function() {
if (callback) callback(null, sha);
});
});
};
};
22 changes: 14 additions & 8 deletions lib/routes.js
Expand Up @@ -4,7 +4,7 @@ var db = require('./data');
module.exports = {

index: function(req, res) {
fs.readdir('./source', function(err, files) {
fs.readdir('./markdown', function(err, files) {
res.render('index', {
files: files
});
Expand Down Expand Up @@ -39,24 +39,30 @@ module.exports = {
},

getDocuments: function(req, res) {
fs.readdir('./source/', function(err, files) {
var html = files.filter(function(x) {
return x.indexOf('.html') !== -1;
});
res.send(html);
fs.readdir('./markdown/', function(err, files) {
if (err) {
console.dir(err);
res.send(err);
} else {
var html = files.filter(function(x) {
return x.indexOf('.html') !== -1;
});
res.send(html);
}

});
},

getDocument: function(req, res) {
var file = req.params.file;
fs.readFile('./source/' + file, 'utf8', function(err, content) {
fs.readFile('./markdown/' + file, 'utf8', function(err, content) {
res.send({
content: content
});
});
},

'export': function(req,res){
'export': function(req, res) {
fs.readFile('./data/data', 'utf8', function(err, content) {
res.json(JSON.parse(content));
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -4,6 +4,7 @@
, "private": true
, "dependencies": {
"express": ">= 2.5.8"
, "jade": ">= 0.22.0"
, "stylus": ">= 0.0.1"
, "everyauth": ">= 0.2.18"
, "marked": "0.2.3"
Expand Down
12 changes: 11 additions & 1 deletion public/stylesheets/style.css
Expand Up @@ -351,12 +351,22 @@ body footer .pad {
#for-review h3 {
border: 1px solid transparent;
}
#for-review img {
max-width: 100%;
}
#comments-spacer {
width: 400px;
margin: 1em 1em 1em 0em;
}
#comments {
width: 400px;
background-color: #ededed;
margin: 1em;
border: 1px solid #ddd;
padding: 1em;
margin: 1em 1em 1em 0em;
position: fixed;
top: 78px;
right: 0;
}
#comments .comment-body {
min-height: 200px;
Expand Down
12 changes: 11 additions & 1 deletion public/stylesheets/style.styl
Expand Up @@ -76,12 +76,22 @@ body
p, h1, h2, h3
border 1px solid transparent

img
max-width 100%

#comments-spacer
width 400px
margin 1em 1em 1em 0em

#comments
width 400px
background-color #ededed
margin 1em
border 1px solid #ddd
padding 1em
margin 1em 1em 1em 0em
position fixed
top 78px
right 0

.comment-body
min-height 200px
Expand Down
1 change: 1 addition & 0 deletions views/index.jade
Expand Up @@ -11,6 +11,7 @@ script#tmpl-comment(type='text/html')
#for-review

aside
#comments-spacer
#comments
header
h1 Comments
Expand Down

0 comments on commit 9697715

Please sign in to comment.