Removing FancyFirstChar plugin

- requires removal of most of the plugin tests
This commit is contained in:
Hannah Wolfe 2013-09-18 19:49:10 +01:00
parent 0ea290df96
commit c7a91ffff1
3 changed files with 4 additions and 273 deletions

View File

@ -1,101 +0,0 @@
var util = require('util'),
_ = require('underscore'),
fancifyPlugin,
whiteSpace = [
'',
' ',
'\t',
'\n',
'\r'
];
fancifyPlugin = {
// Fancify a single post body
fancify: function (originalContent) {
var newContent,
firstCharIndex = 0,
firstChar,
getIndexOfNextCharacter = function (beginFrom) {
var currIndex = beginFrom,
nextChar;
nextChar = originalContent.substr(currIndex, 1);
while (_.contains(whiteSpace, nextChar) && currIndex !== originalContent.length) {
currIndex += 1;
nextChar = originalContent.substr(currIndex, 1);
}
return currIndex;
},
getAfterNextClosingTag = function (beginFrom) {
return originalContent.indexOf('>', beginFrom) + 1;
};
// Skip any leading white space until we get a character
firstCharIndex = getIndexOfNextCharacter(firstCharIndex);
firstChar = originalContent.substr(firstCharIndex, 1);
while (firstChar === '<') {
// Get after the close of the tag
firstCharIndex = getAfterNextClosingTag(firstCharIndex);
// Skip any white space until we get a character
firstCharIndex = getIndexOfNextCharacter(firstCharIndex);
// Grab the character
firstChar = originalContent.substr(firstCharIndex, 1);
}
// Do nothing if we found no characters
if (firstCharIndex === originalContent.length) {
return originalContent;
}
newContent = originalContent.substr(0, firstCharIndex);
newContent += '<span class="fancyChar">';
newContent += originalContent.substr(firstCharIndex, 1);
newContent += '</span>';
newContent += originalContent.substr(firstCharIndex + 1, originalContent.length - firstCharIndex - 1);
return newContent;
},
// Fancify a collection of posts
fancifyPosts: function (posts) {
var self = this;
if (_.isArray(posts)) {
_.each(posts, function (post) {
post.html = self.fancify(post.html);
});
} else if (posts.hasOwnProperty('html')) {
posts.html = this.fancify(posts.html);
}
return posts;
},
install: function () {
},
uninstall: function () {
},
// Registers the prePostsRender filter to alter the html.
activate: function (ghost) {
ghost.registerFilter('prePostsRender', this.fancifyPosts);
},
// Unregister any filters.
deactivate: function (ghost) {
ghost.unregisterFilter("prePostsRender", this.fancifyPosts);
}
};
// Ensure our this context in the important methods
_.bindAll(fancifyPlugin, "fancifyPosts", "fancify", "activate", "deactivate");
module.exports = fancifyPlugin;

View File

@ -0,0 +1,3 @@
# Content / Plugins
Coming soon, Ghost plugins will appear here

View File

@ -9,8 +9,7 @@ var testUtils = require('./testUtils'),
// Stuff we are testing
plugins = require('../../server/plugins'),
GhostPlugin = plugins.GhostPlugin,
loader = require('../../server/plugins/loader'),
FancyFirstChar = require('../../../content/plugins/FancyFirstChar');
loader = require('../../server/plugins/loader');
describe('Plugins', function () {
@ -60,174 +59,4 @@ describe('Plugins', function () {
_.isFunction(plugin.deactivate).should.equal(true);
});
});
describe('loader', function () {
// TODO: These depend heavily on the FancyFirstChar plugin being present.
it('can load FancyFirstChar by name and unload', function (done) {
var fancyPlugin = require("../../../content/plugins/FancyFirstChar"),
fakeGhost = {
registerFilter: function () { return; },
unregisterFilter: function () { return; }
},
installMock = sinon.stub(fancyPlugin, "install"),
uninstallMock = sinon.stub(fancyPlugin, "uninstall"),
registerMock = sinon.stub(fakeGhost, "registerFilter"),
unregisterMock = sinon.stub(fakeGhost, "unregisterFilter");
loader.installPluginByName("FancyFirstChar", fakeGhost).then(function (loadedPlugin) {
should.exist(loadedPlugin);
installMock.called.should.equal(true);
loadedPlugin.activate(fakeGhost);
// Registers the filter
registerMock.called.should.equal(true);
loadedPlugin.deactivate(fakeGhost);
// Unregisters the filter
unregisterMock.called.should.equal(true);
loadedPlugin.uninstall(fakeGhost);
done();
}, done);
});
});
it("can initialize an array of plugins", function (done) {
var fakeGhost = {
registerFilter: sandbox.stub(),
unregisterFilter: sandbox.stub()
},
installSpy = sinon.spy(loader, "installPluginByName"),
activateSpy = sinon.spy(loader, "activatePluginByName");
plugins.init(fakeGhost, ["FancyFirstChar"]).then(function (loadedPlugins) {
should.exist(loadedPlugins);
should.exist(loadedPlugins["FancyFirstChar"]);
installSpy.called.should.equal(true);
activateSpy.called.should.equal(true);
var api = require("../../server/api");
return api.settings.read("installedPlugins").then(function (setting) {
should.exist(setting);
setting.value.should.equal('["FancyFirstChar"]');
done();
});
}, done);
});
describe("FancyFirstChar", function () {
it("has install and uninstall handlers", function () {
should.exist(FancyFirstChar.install);
should.exist(FancyFirstChar.uninstall);
});
it("activates and deactivates properly", function () {
var fakeGhost = {
registerFilter: sandbox.stub(),
unregisterFilter: sandbox.stub()
};
FancyFirstChar.activate(fakeGhost);
fakeGhost.registerFilter.called.should.equal(true);
FancyFirstChar.deactivate(fakeGhost);
fakeGhost.unregisterFilter.called.should.equal(true);
});
it("fancifies simple text", function () {
var original = "Some text to fancify",
expect = '<span class="fancyChar">S</span>ome text to fancify',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies in single tag", function () {
var original = "<p>Some text to fancify</p>",
expect = '<p><span class="fancyChar">S</span>ome text to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies in nested tag", function () {
var original = "<p><strong>Some text</strong> to fancify</p>",
expect = '<p><strong><span class="fancyChar">S</span>ome text</strong> to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies in nested nested tag", function () {
var original = "<p><strong><em>Some</em> text</strong> to fancify</p>",
expect = '<p><strong><em><span class="fancyChar">S</span>ome</em> text</strong> to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("fancifies with tags before first text", function () {
var original = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div><p><strong>Some text</strong> to fancify</p>",
expect = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div><p><strong><span class=\"fancyChar\">S</span>ome text</strong> to fancify</p>",
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("does nothing if no text found", function () {
var original = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div>",
expect = "<div class='someSpecialDiv'><img src='/kitty.jpg' alt='Kitteh' title='Kitteh'></div>",
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("skips leading white space", function () {
var original = "\n\t <p>Some text to fancify</p>",
expect = '\n\t <p><span class="fancyChar">S</span>ome text to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
it("skips white space in inner tags", function () {
var original = "\n\t <p>\n\t\t Some text to fancify</p>",
expect = '\n\t <p>\n\t\t <span class="fancyChar">S</span>ome text to fancify</p>',
result;
result = FancyFirstChar.fancify(original);
result.should.equal(expect);
});
});
});