Permalink
Browse files

somewhat stablized after switching to azure tables

  • Loading branch information...
1 parent 66f3001 commit 8d51b931363459e267f580a254c93dce89bcb7f3 @bennage committed Mar 23, 2012
Showing with 162 additions and 64 deletions.
  1. +1 −2 .gitignore
  2. +30 −11 lib/app.js
  3. +1 −1 lib/context.js
  4. +33 −18 lib/convert.js
  5. +14 −28 lib/routes.js
  6. +80 −0 lib/table.js
  7. +1 −0 package.json
  8. +2 −4 public/javascripts/client.js
View
@@ -6,8 +6,7 @@ node_modules/*
*.sublime-workspace
*.logs/*
source/*
-data/*
-markdown/*
+pages/*
*.publishsettings
node.exe
View
@@ -9,12 +9,11 @@ var auth = require('./auth'),
convert = require('./convert')(),
github = require('./github')(),
routes = require('./routes'),
+ table = require('./table'),
viewhelpers = require('./viewhelpers');
var app = module.exports = x.createServer();
-var current_sha;
-
__dirname = __dirname + '/..';
app.configure('development', function() {
@@ -53,29 +52,49 @@ app.configure('production', function() {
});
function add_sha(req, res, next) {
- req.sha = current_sha;
+ req.sha = table.data.sha;
next();
}
-viewhelpers.initialize(app);
-
-app.get('/', routes.index);
+table.initialize(function(err) {
+ if (!table.data.sha) {
+ initialize(function() {
+ console.log('initialized');
+ });
+ }
+});
-app.get('/update', function(req, res) {
+function initialize(callback) {
var src = path.join('./source'),
- tgt = path.join('./markdown');
+ tgt = path.join('./pages');
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(src, tgt, imageRoot, function() {
- current_sha = sha;
- res.redirect('/index');
+
+ convert(src, tgt, imageRoot, function(err, files) {
+
+ //todo: this feels pretty wierd
+ table.update({
+ sha: sha,
+ files: files
+ }, callback);
+
});
});
+}
+
+viewhelpers.initialize(app);
+
+app.get('/', routes.index);
+
+app.get('/update', function(req, res) {
+ initialize(function() {
+ res.redirect('/index');
+ });
});
app.get('/reset', function(req, res) {});
View
@@ -19,4 +19,4 @@ module.exports = function context(req, res, next) {
req.context.accessToken = session.auth.github.accessToken;
next();
-}
+};
View
@@ -8,7 +8,12 @@ module.exports = function(fs, marked, path) {
gfm: true
});
+ var ref_pattern = /(\[\w+\]:\s+)(http)?(.+)/g;
+
+
function ensureDirectory(folder, callback) {
+
+ //todo: switch to use the path.exists api
fs.stat(folder, function(err, stats) {
if (err) {
fs.mkdir(folder, '0777', callback);
@@ -20,43 +25,53 @@ module.exports = function(fs, marked, path) {
return function(source, target, rootUrl, callback) {
+ var counter = 0,
+ output = [];
+
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);
+ var output = marked(convertImageRefs(input));
+
console.log('=> ' + file);
- fs.writeFile(file_path, output, callback);
+
+ fs.writeFile(file_path, output, function(err) {
+ callback(err, {
+ name: file.replace('.markdown', '.doc'),
+ content: output
+ });
+ });
});
}
function convertImageRefs(content) {
- var re = /(\[\w+\]:\s+)(http)?(.+)/g;
- var revised = content.replace(re, function($0, $1, $2, $3) {
+ return content.replace(ref_pattern, function($0, $1, $2, $3) {
return $2 ? $0 : $1 + rootUrl + $3;
});
- return revised;
}
- var counter = 0;
+ function convert(file) {
+ counter++;
+ toHTML(file, function(err, fileInfo) {
+
+ output.push(fileInfo);
+
+ counter--;
+ if (counter === 0) {
+ callback(null, output);
+ }
+ });
+ }
ensureDirectory(target, function() {
- console.log('attempting to read ' + source);
fs.readdir(source, function(err, files) {
- if(err || !files){
+ 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();
- }
- });
- });
+ files.forEach(convert);
});
});
};
View
@@ -1,13 +1,11 @@
-var fs = require('fs');
-var db = require('./data');
+var store = require('./table');
module.exports = {
index: function(req, res) {
- fs.readdir('./markdown', function(err, files) {
- res.render('index', {
- files: files
- });
+ console.log('index');
+ res.render('index', {
+ files: store.fileNames()
});
},
@@ -33,38 +31,26 @@ module.exports = {
getComments: function(req, res) {
var file = req.params.file;
- db.getCommentsFor(file, function(err, data) {
- res.send(data);
- });
+ // db.getCommentsFor(file, function(err, data) {
+ // res.send(data);
+ // });
+ res.send([]);
},
getDocuments: function(req, res) {
- 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);
- }
-
- });
+ res.send(store.fileNames());
},
getDocument: function(req, res) {
var file = req.params.file;
- fs.readFile('./markdown/' + file, 'utf8', function(err, content) {
- res.send({
- content: content
- });
+ res.json({
+ content: store.contentFor(file)
});
},
'export': function(req, res) {
- fs.readFile('./data/data', 'utf8', function(err, content) {
- res.json(JSON.parse(content));
- });
+ // fs.readFile('./data/data', 'utf8', function(err, content) {
+ // res.json(JSON.parse(content));
+ // });
}
};
View
@@ -0,0 +1,80 @@
+var uuid = require('node-uuid'),
+ azure = require('azure');
+
+var settings = {},
+ fileNames = [],
+ fileContents = {};
+
+var service = azure.ServiceClient,
+ TableQuery = azure.TableQuery;
+
+var partition = 'part1';
+var tableName = 'config';
+var rowKey = 'default';
+
+var client = azure.createTableService(service.DEVSTORE_STORAGE_ACCOUNT, service.DEVSTORE_STORAGE_ACCESS_KEY, service.DEVSTORE_TABLE_HOST);
+
+function load(callback) {
+
+ var configQuery = TableQuery.select().from(tableName);
+
+ client.queryEntity(tableName, partition, rowKey, function(err, data) {
+ if (err) {
+ console.log(err.message);
+ } else {
+ settings = data;
+ }
+ callback(err);
+ });
+}
+
+function update(data, callback) {
+ settings.sha = data.sha;
+ data.files.forEach(function(item) {
+ fileNames.push(item.name);
+ fileContents[item.name] = item.content;
+ });
+
+ client.updateEntity(tableName, settings, function(err) {
+ if (callback) callback();
+ });
+}
+
+function initialize(callback) {
+
+ client.createTableIfNotExists(tableName, function(err, created) {
+ if (created) {
+
+ settings = {
+ PartitionKey: partition,
+ RowKey: rowKey,
+ sha: null
+ };
+
+ client.beginBatch();
+
+ client.insertEntity(tableName, settings);
+
+ client.commitBatch(function() {
+ console.log('created config table');
+ callback(null);
+ });
+ } else {
+ load(callback);
+ }
+ });
+}
+
+module.exports = {
+ initialize: initialize,
+ update: update,
+ data: function() {
+ return settings;
+ },
+ fileNames: function() {
+ return fileNames;
+ },
+ contentFor: function(file) {
+ return fileContents[file];
+ }
+};
View
@@ -9,6 +9,7 @@
, "everyauth": ">= 0.2.18"
, "marked": "0.2.3"
, "azure":">=0.5.2"
+ , "node-uuid":">=1.3.3"
}
, "devDependencies": {
"mocha": "*"
@@ -174,7 +174,7 @@
function setup_document_list(documents) {
var el = $('#docs select');
- documents.forEach(function(doc) {
+ documents.sort().forEach(function(doc) {
el.append('<option>' + doc + '</option>');
});
@@ -228,15 +228,13 @@
render_comments();
});
});
-
-
}
function set_context_from_url() {
context = null;
- var target = window.location.href.match(/\/#?([\w-]*\.html)/);
+ var target = window.location.href.match(/\/#?([\w-]*\.doc)/);
if (target !== null && target.length > 0) context = target[1];
if (!context) {

0 comments on commit 8d51b93

Please sign in to comment.