mirror of
/repos/Prototyper.git
synced 2025-12-30 06:31:32 +01:00
working importer
This commit is contained in:
parent
efe0ff63de
commit
4d55fb65f2
151
importer.js
151
importer.js
@ -1,45 +1,59 @@
|
||||
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) {
|
||||
|
||||
var import_leftovers_tag = 'import_leftovers__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
|
||||
var import_leftovers_regexp = new RegExp(import_leftovers_tag);
|
||||
var import_leftovers_regexp = new RegExp(helpers.marker_prefix + import_leftovers_tag + helpers.marker_postfix);
|
||||
|
||||
var importer = function (doc, options, cb) {
|
||||
when.any(
|
||||
helpers.replace(doc, import_leftovers_tag, function getReplacement(result, callback) {
|
||||
var parts = import_leftovers_regexp.exec(result);
|
||||
var context = {
|
||||
collection: parts[1],
|
||||
name: parts[2],
|
||||
attribute: parts[3]
|
||||
};
|
||||
return callback(null, {
|
||||
regExp: new RegExp(result, 'gmi'),
|
||||
value: context
|
||||
});
|
||||
},
|
||||
var parts = import_leftovers_regexp.exec(result);
|
||||
var context = {
|
||||
collection: parts[1],
|
||||
name: parts[2],
|
||||
attribute: parts[3]
|
||||
};
|
||||
return callback(null, {
|
||||
regExp: new RegExp(result, 'gmi'),
|
||||
value: context
|
||||
});
|
||||
},
|
||||
// there can be only one import_leftovers
|
||||
true
|
||||
),
|
||||
function onSuccess(leftover) {
|
||||
handleImportMarkers(doc, options, function handleLeftover(err, remainder) {
|
||||
if (leftover) {
|
||||
remainder = remainder.replace(leftover.regExp, "");
|
||||
mongoInstance.setMongoAttribute(remainder, leftover.value, function (err,result) {
|
||||
return cb(err);
|
||||
})
|
||||
} else {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR importer.importer handleImportMarkers', err);
|
||||
return cb(err);
|
||||
}
|
||||
if (leftover) {
|
||||
mongoInstance.ensureContent(leftover.replacement, function parent(err, parent_result) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR importer.importer ensureContent', err);
|
||||
return cb(err);
|
||||
}
|
||||
leftover.replacement.query = { _id: parent_result._id };
|
||||
|
||||
remainder = remainder.replace(leftover.regExp, "");
|
||||
mongoInstance.setMongoAttribute(remainder, leftover.replacement, function (err, attribute_result) {
|
||||
return cb(err, remainder);
|
||||
})
|
||||
})
|
||||
} else {
|
||||
console.log('no import_leftover tag found');
|
||||
return cb(null, remainder);
|
||||
}
|
||||
});
|
||||
},
|
||||
function onFailure(err) {
|
||||
config.errors && console.log('ERR importer.importer onFailure', err);
|
||||
return cb(err);
|
||||
}
|
||||
)
|
||||
@ -47,9 +61,12 @@ module.exports = function (config, mongoInstance) {
|
||||
|
||||
var handleImportMarkers = function (doc, options, callback) {
|
||||
var promises = replaceMarkers(doc, options);
|
||||
function handler( text, result) {
|
||||
return text.replace(result.regExp, result.replacement);
|
||||
|
||||
function handler(text, result) {
|
||||
var new_text = text.replace(result.regExp, result.replacement);
|
||||
return new_text;
|
||||
}
|
||||
|
||||
helpers.handTextManipulation(doc,
|
||||
promises,
|
||||
handler,
|
||||
@ -57,72 +74,100 @@ module.exports = function (config, mongoInstance) {
|
||||
);
|
||||
};
|
||||
|
||||
var replaceMarkers = function(doc, options) {
|
||||
var replaceMarkers = function (doc, options) {
|
||||
var promises = [];
|
||||
/* markers:
|
||||
import__[collection]_[name]_[attribute]
|
||||
end_import__[collection]_[name]_[attribute]
|
||||
import__[collection]_[name]_[attribute]_
|
||||
_end_import__[collection]_[name]_[attribute]
|
||||
|
||||
moves content between tags into /collection/name/attribute
|
||||
moves content between tags into /collection/name/attribute
|
||||
|
||||
|
||||
import_file__[filename]__into__[collection]_[name]_[attribute]
|
||||
import_file__[filename]__into__[collection]_[name]_[attribute]
|
||||
|
||||
read filename into /collection/name/attribute and process it.
|
||||
read filename into /collection/name/attribute and process it.
|
||||
*/
|
||||
var import_tag = 'import__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)([\\w\\W]*)_end_import_([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
|
||||
var import_regexp = new RegExp(import_tag);
|
||||
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 (result, callback) {
|
||||
var parts = import_regexp.exec(result);
|
||||
if (parts[1] != parts[5]
|
||||
|| parts[2] != parts[6]
|
||||
|| parts[3] != parts[7]
|
||||
) {
|
||||
callback(new Error('no closing tag found for import__'+parts[1]+'_'+parts[2]+'_'+parts[3]))
|
||||
}
|
||||
var context = {
|
||||
collection: parts[1],
|
||||
name: parts[2],
|
||||
attribute: parts[3]
|
||||
};
|
||||
var sub_doc = parts[4];
|
||||
var striped_parts = import_strip_regexp.exec(parts[4]);
|
||||
var sub_doc = striped_parts[1];
|
||||
|
||||
handleImportMarkers(sub_doc, options, function handleLeftover(err, remainder) {
|
||||
mongoInstance.setMongoAttribute(remainder, context, function (err,attribute_result) {
|
||||
return callback(err, {
|
||||
regExp: new RegExp(result, 'gmi'),
|
||||
value: ""
|
||||
mongoInstance.ensureContent(context, function parent(err, parent_result) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR importer.replaceMarkers ensureContent', err);
|
||||
return callback(err);
|
||||
}
|
||||
context.query = { _id: parent_result._id };
|
||||
|
||||
mongoInstance.setMongoAttribute(remainder, context, function (err, attribute_result) {
|
||||
var replacement = {
|
||||
regExp: result,
|
||||
value: ""
|
||||
};
|
||||
return callback(err, replacement);
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
var import_file_tag = 'import_file__(\\w\\W)__into__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
|
||||
var import_file_regexp = new RegExp(import_file_tag);
|
||||
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 (result, callback) {
|
||||
var parts = import_file_regexp.exec(result);
|
||||
var filename = parts[0];
|
||||
var filename = path.resolve(config.public_path, parts[1]);
|
||||
var context = {
|
||||
collection: parts[1],
|
||||
name: parts[2],
|
||||
attribute: parts[3]
|
||||
collection: parts[2],
|
||||
name: parts[3],
|
||||
attribute: parts[4]
|
||||
};
|
||||
var sub_doc = fs.readFile(filename, 'utf-8');
|
||||
// process with leftover marker support
|
||||
importer(sub_doc, context, function handleLeftover(err, attribute_result) {
|
||||
// remove import_file marker from source
|
||||
return callback(err, {
|
||||
regExp: new RegExp(result, 'gmi'),
|
||||
value: ""
|
||||
fs.readFile(filename, 'utf-8', function (err, sub_doc) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR importer.replaceMarkers readFile', err);
|
||||
return callback(err);
|
||||
}
|
||||
// process with leftover marker support
|
||||
importer(sub_doc, context, function handleLeftover(err, remainder) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR importer.replaceMarkers importer', err);
|
||||
return callback(err);
|
||||
}
|
||||
mongoInstance.ensureContent(context, function parent(err, parent_result) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR importer.importer ensureContent', err);
|
||||
return callback(err);
|
||||
}
|
||||
context.query = { _id: parent_result._id };
|
||||
|
||||
mongoInstance.setMongoAttribute(remainder, context, function (err, attribute_result) {
|
||||
// remove import_file marker from source
|
||||
var replacement = {
|
||||
regExp: result,
|
||||
value: ""
|
||||
};
|
||||
return callback(err, replacement);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
return promises;
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@ -8,11 +8,12 @@
|
||||
<!-- @@remove_ -->
|
||||
<link href="/style.css" rel="stylesheet" type="text/css">
|
||||
<!-- @@_end_remove -->
|
||||
<!-- @@style_app_main_style -->
|
||||
<!-- @@style__app_main_style -->
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<!-- @@import__app_main_body -->
|
||||
<!-- @@template__app_main_body__context__app_main -->
|
||||
<!-- @@import__app_main_body_ -->
|
||||
<div id="header">
|
||||
<div id="htext">
|
||||
Editing <b>index</b>
|
||||
@ -35,7 +36,8 @@
|
||||
|
||||
<div id="editor"></div>
|
||||
|
||||
<!-- @@import__app_main_scripts -->
|
||||
<!-- @@template__app_main_scripts__context__app_main -->
|
||||
<!-- @@import__app_main_scripts_ -->
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
||||
<!-- @@remove_ -->
|
||||
<!--<script src="//knockoutjs.com/downloads/knockout-2.2.1.debug.js"></script>-->
|
||||
@ -60,8 +62,9 @@
|
||||
<script src="/share/share.uncompressed.js"></script>
|
||||
<script src="/share/json.js"></script>
|
||||
<script src="/share/ace.js"></script>
|
||||
<!-- @@end_import__app_main_scripts -->
|
||||
<!-- @@import__app_main_behaviour -->
|
||||
<!-- @@_end_import__app_main_scripts -->
|
||||
<!-- @@template__app_main_behaviour__context__app_main -->
|
||||
<!-- @@import__app_main_behaviour_ -->
|
||||
<script>
|
||||
var attribute = 'index';
|
||||
var editor = ace.edit("editor");
|
||||
@ -218,8 +221,8 @@
|
||||
}
|
||||
})
|
||||
</script>
|
||||
<!-- @@import__app_main_behaviour -->
|
||||
<!-- @@end_import__app_main_body -->
|
||||
<!-- @@_end_import__app_main_behaviour -->
|
||||
<!-- @@_end_import__app_main_body -->
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
25
routes.js
25
routes.js
@ -3,6 +3,8 @@ var mongodata = require('./mongodata.js');
|
||||
var responder = require('./responder.js');
|
||||
var preview = require('./preview.js');
|
||||
var importer = require('./importer.js');
|
||||
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
|
||||
module.exports = function (app, config) {
|
||||
@ -185,6 +187,7 @@ module.exports = function (app, config) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return handleResult;
|
||||
}
|
||||
|
||||
@ -201,7 +204,7 @@ module.exports = function (app, config) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR2 applyOp', err);
|
||||
}
|
||||
}));
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -220,7 +223,7 @@ module.exports = function (app, config) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR1 applyOp', err);
|
||||
}
|
||||
}));
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -315,12 +318,18 @@ module.exports = function (app, config) {
|
||||
|
||||
var importerInstance = importer(config, mongodataInstance);
|
||||
app.get('/importer/:filename', function importFile(req, res, next) {
|
||||
config.debug && console.log('/importer/:filename');
|
||||
var sub_doc = fs.readFile( req.params.filename, 'utf-8');
|
||||
// process with leftover marker support
|
||||
var options = {};
|
||||
importerInstance.importer(sub_doc, options, function handleLeftover(err, result) {
|
||||
responder(options, res, next);
|
||||
var filename = path.resolve(config.public_path, req.params.filename);
|
||||
config.debug && console.log('/importer/:filename', filename);
|
||||
fs.readFile(filename, 'utf-8', function (err, sub_doc) {
|
||||
if (err) {
|
||||
config.errors && console.log('ERR readFile', filename, err);
|
||||
next(err);
|
||||
}
|
||||
// process with leftover marker support
|
||||
var options = {};
|
||||
importerInstance.importer(sub_doc, options,
|
||||
responder(options, res, next)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -21,17 +21,18 @@ var config = {
|
||||
share: {
|
||||
sockjs: {},
|
||||
db: {type: 'none'}
|
||||
}
|
||||
},
|
||||
public_path: __dirname + '/public'
|
||||
};
|
||||
|
||||
var app = express();
|
||||
config.debug && app.use(connect.logger());
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
app.use(express.static(config.public_path));
|
||||
|
||||
|
||||
var server = instance(app, config);
|
||||
|
||||
server.listen(config.port, function (err) {
|
||||
config.debug && console.log('routes',server.routes);
|
||||
config.debug && console.log('routes', server.routes);
|
||||
console.log('Server running at http://127.0.0.1:', config.port);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user