1
0
mirror of /repos/Prototyper.git synced 2026-02-27 16:12:00 +01:00

refactoring backend so it can be tested

This commit is contained in:
Aiko Mastboom
2013-05-12 00:24:07 +02:00
parent f5bece7728
commit 4ab2d7ff5f
7 changed files with 169 additions and 94 deletions

View File

@@ -1,70 +1,68 @@
var when = require('when');
var _ = require('underscore');
var marker_prefix = '<!--\\s*@@';
var marker_postfix = '\\s*-->';
module.exports = function (markers) {
function replace(text, marker, getReplacement, once) {
var deferred = when.defer();
var regExp = new RegExp(marker_prefix + marker + marker_postfix, 'gmi');
var matches = text.match(regExp);
if (matches) {
if (once) {
matches = [matches[0]];
}
var match_promises = [];
_.forEach(matches, function handleMatch(result) {
var deferred2 = when.defer();
match_promises.push(deferred2.promise);
getReplacement(result, function resolveReplacement(err, replacement) {
if (err) {
deferred2.reject(err);
} else {
var replace_result = {
regExp: replacement.regExp || regExp,
replacement: replacement.value
};
deferred2.resolve(replace_result)
function replace(text, marker, getReplacement, once) {
var deferred = when.defer();
var regExp = new RegExp(markers.prefix + marker + markers.postfix, 'gmi');
var matches = text.match(regExp);
if (matches) {
if (once) {
matches = [matches[0]];
}
var match_promises = [];
_.forEach(matches, function handleMatch(result) {
var deferred2 = when.defer();
match_promises.push(deferred2.promise);
getReplacement(result, function resolveReplacement(err, replacement) {
if (err) {
deferred2.reject(err);
} else {
var replace_result = {
regExp: replacement.regExp || regExp,
replacement: replacement.value
};
deferred2.resolve(replace_result)
}
})
});
when.all(
match_promises
).then(
function onSuccess(results) {
deferred.resolve(results);
},
function onFailure(err) {
deferred.reject(err);
}
})
});
);
} else {
deferred.resolve({});
}
return deferred.promise;
}
function handTextManipulation(text, promises, handler, callback) {
when.all(
match_promises
promises
).then(
function onSuccess(results) {
deferred.resolve(results);
function onSuccess(all_results) {
_.forEach(all_results, function loopResults(results) {
_.forEach(results, function handleResult(result) {
text = handler(text, result);
});
});
return callback(null, text);
},
function onFailure(err) {
deferred.reject(err);
return callback(err);
}
);
} else {
deferred.resolve({});
)
}
return deferred.promise;
}
function handTextManipulation(text, promises, handler, callback) {
when.all(
promises
).then(
function onSuccess(all_results) {
_.forEach(all_results, function loopResults(results) {
_.forEach(results, function handleResult(result) {
text = handler(text, result);
});
});
return callback(null, text);
},
function onFailure(err) {
return callback(err);
}
)
}
module.exports = {
marker_prefix: marker_prefix,
marker_postfix: marker_postfix,
replace: replace,
handTextManipulation: handTextManipulation
return {
replace: replace,
handTextManipulation: handTextManipulation
}
};

View File

@@ -1,14 +1,13 @@
var when = require('when');
var _ = require('underscore');
var helpers = require('./helpers.js');
var path = require('path');
var fs = require('fs');
module.exports = function (config, mongoInstance) {
module.exports = function (config, mongoInstance, helpers, markers) {
var import_leftovers_tag = 'import_leftovers__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_leftovers_regexp = new RegExp(helpers.marker_prefix + import_leftovers_tag + helpers.marker_postfix);
var import_leftovers_tag = markers.import_leftovers_tag;
var import_leftovers_regexp = markers.import_leftovers_regexp;
var importer = function (doc, options, cb) {
when.any(
@@ -80,11 +79,15 @@ module.exports = function (config, mongoInstance) {
);
};
var import_tag = markers.import_tag;
var import_regexp = markers.import_regexp;
var import_strip_regexp = markers.import_strip_regexp;
var import_file_tag = markers.import_file_tag;
var import_file_regexp = markers.import_file_regexp;
var replaceMarkers = function (doc, options) {
var promises = [];
var import_tag = 'import__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)_([\\w\\W]*)_end_import__\\1_\\2_\\3';
var import_regexp = new RegExp(helpers.marker_prefix + import_tag + helpers.marker_postfix);
var import_strip_regexp = new RegExp(helpers.marker_postfix + '([\\w\\W]*)' + helpers.marker_prefix);
promises.push(
helpers.replace(doc, import_tag, function handleImportMarker(result, callback) {
@@ -107,8 +110,6 @@ module.exports = function (config, mongoInstance) {
})
);
var import_file_tag = 'import_file__([A-Za-z0-9.\/]+)__into__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_file_regexp = new RegExp(helpers.marker_prefix + import_file_tag + helpers.marker_postfix);
promises.push(
helpers.replace(doc, import_file_tag, function handleImportFileMarker(result, callback) {

64
markers.js Normal file
View File

@@ -0,0 +1,64 @@
module.exports = function markers(config) {
var marker_prefix = '<!--\\s*@@';
var marker_postfix = '\\s*-->';
// Preview markers
var script_tag = 'script__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var script_regexp = new RegExp(marker_prefix + script_tag + marker_postfix);
var style_tag = 'style__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var style_regexp = new RegExp(marker_prefix + style_tag + marker_postfix);
var less_tag = 'less__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var less_regexp = new RegExp(marker_prefix + less_tag + marker_postfix);
var template_tag = 'template__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)__context__([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var template_regexp = new RegExp(marker_prefix + template_tag + marker_postfix);
var markdown_tag = 'markdown__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var markdown_regexp = new RegExp(marker_prefix + markdown_tag + marker_postfix);
var remove_tag = 'remove_([\\w\\W]*?)_end_remove';
//var remove_regexp = new RegExp(remove_tag);
// Importer markers
var import_leftovers_tag = 'import_leftovers__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_leftovers_regexp = new RegExp(marker_prefix + import_leftovers_tag + marker_postfix);
var import_tag = 'import__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)_([\\w\\W]*)_end_import__\\1_\\2_\\3';
var import_regexp = new RegExp(marker_prefix + import_tag + marker_postfix);
var import_strip_regexp = new RegExp(marker_postfix + '([\\w\\W]*)' + marker_prefix);
var import_file_tag = 'import_file__([A-Za-z0-9.\/]+)__into__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_file_regexp = new RegExp(marker_prefix + import_file_tag + marker_postfix);
function createTag(type, collection, name, attribute) {
return '<!-- @@' + type + '__' + collection + '_' + name + '_' + attribute + ' -->';
}
return {
prefix: marker_prefix,
postfix: marker_postfix,
createTag:createTag,
script_tag:script_tag,
script_regexp:script_regexp,
style_tag:style_tag,
style_regexp:style_regexp,
less_tag:less_tag,
less_regexp:less_regexp,
template_tag:template_tag,
template_regexp:template_regexp,
markdown_tag:markdown_tag,
markdown_regexp:markdown_regexp,
remove_tag:remove_tag,
// remove_regexp:remove_regexp,
import_leftovers_tag:import_leftovers_tag,
import_leftovers_regexp:import_leftovers_regexp,
import_tag:import_tag,
import_regexp:import_regexp,
import_strip_regexp:import_strip_regexp,
import_file_tag:import_file_tag,
import_file_regexp:import_file_regexp
}
};

View File

@@ -3,9 +3,8 @@ var markdown = require('markdown').markdown;
var _ = require('underscore');
var less = require('less');
var when = require('when');
var helpers = require('./helpers.js');
module.exports = function (config, mongoDataInstance) {
module.exports = function (config, mongoDataInstance, helpers, markers) {
var getPreviewHTML = function (content, options, callback) {
config.debug && console.log('getPreviewHTML', content);
@@ -22,12 +21,29 @@ module.exports = function (config, mongoDataInstance) {
);
};
var script_tag = markers.script_tag;
var script_regexp = markers.script_regexp;
var style_tag = markers.style_tag;
var style_regexp = markers.style_regexp;
var less_tag = markers.less_tag;
var less_regexp = markers.less_regexp;
var template_tag = markers.template_tag;
var template_regexp = markers.template_regexp;
var markdown_tag = markers.markdown_tag;
var markdown_regexp = markers.markdown_regexp;
var remove_tag = markers.remove_tag;
//var remove_regexp = markers.remove_regexp;
var replaceMarkers = function (html, options) {
var promises = [];
var script_tag = 'script__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var script_regexp = new RegExp(helpers.marker_prefix + script_tag + helpers.marker_postfix);
// Script tag handling
promises.push(
helpers.replace(html, script_tag, function handleScriptMarker(result, callback) {
var parts = script_regexp.exec(result);
@@ -42,8 +58,7 @@ module.exports = function (config, mongoDataInstance) {
});
}));
var style_tag = 'style__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var style_regexp = new RegExp(helpers.marker_prefix + style_tag + helpers.marker_postfix);
// Style tag handling
promises.push(
helpers.replace(html, style_tag, function handleStyleMarker(result, callback) {
var parts = style_regexp.exec(result);
@@ -58,8 +73,7 @@ module.exports = function (config, mongoDataInstance) {
});
}));
var less_tag = 'less__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var less_regexp = new RegExp(helpers.marker_prefix + less_tag + helpers.marker_postfix);
// Less tag handling
promises.push(
helpers.replace(html, less_tag, function handleStyleMarker(result, callback) {
var parts = less_regexp.exec(result);
@@ -74,8 +88,7 @@ module.exports = function (config, mongoDataInstance) {
});
}));
var template_tag = 'template__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)__context__([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var template_regexp = new RegExp(helpers.marker_prefix + template_tag + helpers.marker_postfix);
// Template tag handling
promises.push(
helpers.replace(html, template_tag, function handleTemplateMarker(result, callback) {
var parts = template_regexp.exec(result);
@@ -170,7 +183,7 @@ module.exports = function (config, mongoDataInstance) {
});
});
return when.all(
promises
promises
).then(
function onSuccess() {
var rendered = null;
@@ -202,8 +215,7 @@ module.exports = function (config, mongoDataInstance) {
});
}));
var markdown_tag = 'markdown__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var markdown_regexp = new RegExp(helpers.marker_prefix + markdown_tag + helpers.marker_postfix);
// Markdown tag handling
promises.push(
helpers.replace(html, markdown_tag, function handleMarkDownMarker(result, callback) {
var parts = markdown_regexp.exec(result);
@@ -225,8 +237,7 @@ module.exports = function (config, mongoDataInstance) {
});
}));
var remove_tag = 'remove_([\\w\\W]*?)_end_remove';
//var remove_regexp = new RegExp(remove_tag);
// Remove tag handling
promises.push(
helpers.replace(html, remove_tag, function handleRemoveMarker(result, callback) {
return callback(null, {
@@ -241,7 +252,7 @@ module.exports = function (config, mongoDataInstance) {
return {
getPreviewHTML: getPreviewHTML,
_replaceMarkers: replaceMarkers
_replaceMarkers: replaceMarkers
};
};

View File

@@ -3,7 +3,7 @@ var responder = require('./responder.js');
var path = require('path');
var fs = require('fs');
module.exports = function (app, handlers, config) {
module.exports = function (app, handlers, markers, config) {
var route;
route = config.api.data + '/:collection/:guid/:attribute.:ext(css|less|js|html)';
@@ -83,10 +83,7 @@ module.exports = function (app, handlers, config) {
};
if (options.ext == 'md') {
var attribute_parts = options.query.name.split('.');
var markdownTag = 'markdown__' + options.collection + '_' + attribute_parts[0] + '_' + attribute_parts[1];
//var markdownDocument=helpers.marker_prefix + markdownTag + helpers.marker_postfix;
// TODO: remove hardcoded marker
var markdownDocument = '<!-- @@' + markdownTag + ' -->';
var markdownDocument = markers.createTag('markdown',options.collection, attribute_parts[0], attribute_parts[1]);
return handlers.getPreviewHTML(markdownDocument, { req: options.req },
responder(options, res, next)
);

View File

@@ -10,7 +10,8 @@ var mongoData = require('./mongodata.js');
var preview = require('./preview.js');
var importer = require('./importer.js');
var handlers = require('./handlers.js');
var markers = require('./markers.js');
var helpers = require('./helpers.js');
var config = {
errors: true,
@@ -77,6 +78,9 @@ app.use('/lib/async', express.static(config.statics.async_client));
config.debug && console.log('static routes set');
var markerInstance = markers(config);
var helperInstance = helpers(markerInstance);
MongoClient.connect(config.mongo.server, config.mongo.options, function connection(err, db) {
if (err) {
config.errors && console.log('ERR connection to database', err);
@@ -84,7 +88,7 @@ MongoClient.connect(config.mongo.server, config.mongo.options, function connecti
}
config.debug && console.log('database connected');
var share = shareServer(app, db, config);
var share = shareServer(config, app, db);
var model = share.model;
var server = share.server;
@@ -96,13 +100,13 @@ MongoClient.connect(config.mongo.server, config.mongo.options, function connecti
shareHandlers(config, model, mongoDataInstance);
config.debug && console.log('sharehandlers attached');
config.debug && console.log('shareHandlers attached');
var previewInstance = preview(config, mongoDataInstance);
var previewInstance = preview(config, mongoDataInstance, helperInstance, markerInstance);
config.debug && console.log('previews initialized');
var importerInstance = importer(config, mongoDataInstance);
var importerInstance = importer(config, mongoDataInstance, helperInstance, markerInstance);
config.debug && console.log('importer initialized');
@@ -110,7 +114,7 @@ MongoClient.connect(config.mongo.server, config.mongo.options, function connecti
config.debug && console.log('handlers initialized');
app = addRoutes(app, handlerInstance, config);
app = addRoutes(app, handlerInstance, markers, config);
config.debug && console.log('routes added');

View File

@@ -1,6 +1,6 @@
var ShareJS = require('share');
module.exports = function( app, db, config) {
module.exports = function (config, app, db) {
// share wraps express app with http.Server
if (config
&& config.share