A string name (only) can now be given for bundled extensions

This commit is contained in:
Titus 2012-06-20 21:17:58 -06:00
parent 0218913efa
commit 05453b5042
2 changed files with 51 additions and 43 deletions

View File

@ -80,6 +80,13 @@ var forEach = Showdown.forEach = function(obj, callback) {
}
};
//
// Standard extension naming
//
var stdExtName = function(s) {
return s.replace(/[_-]||\s/g, '').toLowerCase();
};
//
// converter
//
@ -106,6 +113,28 @@ var g_lang_extensions = [];
var g_output_modifiers = [];
//
// Automatic Extension Loading (node only):
//
if (typeof module !== 'undefind' && typeof exports !== 'undefined' && typeof require !== 'undefind') {
var fs = require('fs');
if (fs) {
// Search extensions folder
var extensions = fs.readdirSync('./src/extensions').filter(function(file){
return ~file.indexOf('.js');
}).map(function(file){
return file.replace('.js', '');
});
// Load extensions into Showdown namespace
extensions.forEach(function(ext){
var name = stdExtName(ext);
Showdown.extensions[name] = require('./extensions/' + ext);
});
}
}
//
// Options:
//
@ -116,21 +145,29 @@ if (converter_options && converter_options.extensions) {
// Iterate over each plugin
converter_options.extensions.forEach(function(plugin){
// Iterate over each extensino within that plugin
plugin(this).forEach(function(ext){
// Sort extensions by type
if (ext.type) {
if (ext.type === 'language' || ext.type === 'lang') {
g_lang_extensions.push(ext);
} else if (ext.type === 'output' || ext.type === 'html') {
// Assume it's a bundled plugin if a string is given
if (typeof plugin === 'string') {
plugin = Showdown.extensions[stdExtName(plugin)];
}
if (typeof plugin === 'function') {
// Iterate over each extension within that plugin
plugin(this).forEach(function(ext){
// Sort extensions by type
if (ext.type) {
if (ext.type === 'language' || ext.type === 'lang') {
g_lang_extensions.push(ext);
} else if (ext.type === 'output' || ext.type === 'html') {
g_output_modifiers.push(ext);
}
} else {
// Assume language extension
g_output_modifiers.push(ext);
}
} else {
// Assume language extension
g_output_modifiers.push(ext);
}
});
});
} else {
throw "Extension '" + plugin + "' could not be loaded. It was either not found or is not a valid extension.";
}
});
}
@ -211,29 +248,6 @@ this.makeHtml = function(text) {
};
//
// Scan extensions folder for extensions
// (if in a node environment)
//
if (typeof module !== 'undefind' && typeof exports !== 'undefined' && typeof require !== 'undefind') {
var fs = require('fs');
if (fs) {
// Search extensions folder
var extensions = fs.readdirSync('./src/extensions').filter(function(file){
return ~file.indexOf('.js');
}).map(function(file){
return file.replace('.js', '');
});
// Load extensions into Showdown namespace
extensions.forEach(function(ext){
var name = ext.replace(/[_-]|\\s/g, '').toLowerCase();
Showdown.extensions[name] = require('./extensions/' + ext);
});
}
}
var _ExecuteExtension = function(ext, text) {
if (ext.regex) {
var re = new RegExp(ext.regex, 'g');

View File

@ -77,13 +77,7 @@ if (path.existsSync('test/extensions')) {
throw "Attempting tests for '" + ext + "' but sourc file (" + src + ") was not found.";
}
// Build converter with extension included
var extension = showdown.extensions[ext.replace(/[_-]||\s/g, '').toLowerCase()];
if (!extension) {
throw "Could not load extension '" + ext + "'. Did you forget module.exports?";
}
var converter = new showdown.converter({ extensions: [ extension ] });
var converter = new showdown.converter({ extensions: [ ext ] });
var dir = 'test/extensions/' + ext;
runTestsInDir(dir, converter);
});