Ghost/content/plugins/FancyFirstChar/index.js
Hannah Wolfe d968495996 Mass renaming of things
Conflicts:
	core/client/views/settings.js
	core/server/models/user.js
2013-09-14 21:56:07 +01:00

101 lines
2.9 KiB
JavaScript

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;