var CandyShop = (function(self) { return self; }(CandyShop || {}));
CandyShop.Emphasis = (function(self, Candy, $) {
// textile, bbcode, old html, escaped old html, new html, escaped new html
var _styles = {
textile: [
{ plain: '==*bold*==', regex: /((^|\s|\>)==\*(.*?)\*==(\s|\<|$))/gm, plain: "$2*$3*$4", xhtml: "$2*$3*$4" },
{ plain: '==_italic_==', regex: /((^|\s|\>)==\_(.*?)\_==(\s|\<|$))/gm, plain: "$2_$3_$4", xhtml: "$2_$3_$4" },
{ plain: '==-strikethrough-==', regex: /((^|\s|\>)==\-(.*?)\-==(\s|\<|$))/gm, plain: "$2-$3-$4", xhtml: "$2-$3-$4" },
{ plain: '==+underline+==', regex: /((^|\s|\>)==\+(.*?)\+==(\s|\<|$))/gm, plain: "$2+$3+$4", xhtml: "$2+$3+$4" },
{ plain: '*bold*', regex: /((^|\s|\>)\*(.*?)\*(\s|\<|$))/gm, plain: "$2*$3*$4", xhtml: "$2$3$4" },
{ plain: '_italic_', regex: /((^|\s|\>)\_(.*?)\_(\s|\<|$))/gm, plain: "$2_$3_$4", xhtml: "$2$3$4" },
{ plain: '-strikethrough-', regex: /((^|\s|\>)\-(.*?)\-(\s|\<|$))/gm, plain: "$2-$3-$4", xhtml: "$2$3$4" },
{ plain: '+underline+', regex: /((^|\s|\>)\+(.*?)\+(\s|\<|$))/gm, plain: "$2+$3+$4", xhtml: "$2$3$4" }
],
bbcode: [
{ plain: '[b]bold[/b]', regex: /(\[b\](.*?)\[\/b\])/igm, plain: "*$2*", xhtml: "$2" },
{ plain: '[i]italic[/i]', regex: /(\[i\](.*?)\[\/i\])/igm, plain: "_$2_", xhtml: "$2" },
{ plain: '[s]strikethrough[/s]', regex: /(\[s\](.*?)\[\/s\])/igm, plain: "-$2-", xhtml: "$2" },
{ plain: '[u]underline[/u]', regex: /(\[u\](.*?)\[\/u\])/igm, plain: "+$2+", xhtml: "$2" }
],
html: [
//handling both escaped an unescaped, just in case.
{ plain: '<b>bold</b>', regex: /(\<b\>(.*?)\<\/b\>)/igm, plain: "*$2*", xhtml: "$2" },
{ plain: '<strong>bold</strong>', regex: /(\<strong\>(.*?)\<\/strong\>)/igm, plain: "*$2*", xhtml: "$2" },
{ plain: '<i>italic</i>', regex: /(\<i\>(.*?)\<\/i\>)/igm, plain: "_$2_", xhtml: "$2" },
{ plain: '<em>italic</em>', regex: /(\<em\>(.*?)\<\/em\>)/igm, plain: "_$2_", xhtml: "$2" },
{ plain: '<s>strikethrough</s>', regex: /(\<s\>(.*?)\<\/s\>)/igm, plain: "-$2-", xhtml: "$2" },
{ plain: '<del>strikethrough</del>', regex: /(\<del\>(.*?)\<\/del\>)/igm, plain: "-$2-", xhtml: "$2" },
{ plain: '<u>underline</u>', regex: /(\<u\>(.*?)\<\/u\>)/igm, plain: "+$2+", xhtml: "$2" },
{ plain: '<ins>underline</ins>', regex: /(\<ins\>(.*?)\<\/ins\>)/igm, plain: "+$2+", xhtml: "$2" },
{ plain: 'bold', regex: /(\(.*?)\<\/b\>)/igm, plain: "*$2*", xhtml: "$2" },
{ plain: 'bold', regex: /(\(.*?)\<\/strong\>)/igm, plain: "*$2*", xhtml: "$2" },
{ plain: 'italic', regex: /(\(.*?)\<\/i\>)/igm, plain: "_$2_", xhtml: "$2" },
{ plain: 'italic', regex: /(\(.*?)\<\/em\>)/igm, plain: "_$2_", xhtml: "$2" },
{ plain: 'strikethrough', regex: /(\(.*?)\<\/s\>)/igm, plain: "-$2-", xhtml: "$2" },
{ plain: 'strikethrough', regex: /(\(.*?)\<\/del\>)/igm, plain: "-$2-", xhtml: "$2" },
{ plain: 'underline', regex: /(\(.*?)\<\/u\>)/igm, plain: "+$2+", xhtml: "$2" },
{ plain: 'underline', regex: /(\(.*?)\<\/ins\>)/igm, plain: "+$2+", xhtml: "$2" }
]
};
var _options = {
textile: true,
bbcode: true,
html: true
};
self.init = function( options ) {
// apply the supplied options to the defaults specified
$.extend( true, _options, options );
$(Candy).on( 'candy:view.message.before-send', function(e, args) {
var workingPlainMessage = args.message;
var workingXhtmlMessage = args.message;
if( args.xhtmlMessage ) {
var workingXhtmlMessage = args.xhtmlMessage;
}
$.each( _options, function( optionIndex, optionValue ){
if( optionValue === true ){
$.each( _styles[optionIndex], function( styleIndex, styleValue ){
workingPlainMessage = workingPlainMessage.replace( styleValue.regex, styleValue.plain );
workingXhtmlMessage = workingXhtmlMessage.replace( styleValue.regex, styleValue.xhtml );
});
}
});
if( workingPlainMessage != workingXhtmlMessage) {
// strophe currently requires that xhtml have a root element. Apparently.
// Force one node, with no external text
if( !workingXhtmlMessage.match( /^<.*>$/ ) || $( workingXhtmlMessage ).length != 1 ) {
workingXhtmlMessage = "" + workingXhtmlMessage + "";
}
args.message = workingPlainMessage;
args.xhtmlMessage = workingXhtmlMessage;
}
});
};
return self;
}(CandyShop.Emphasis || {}, Candy, jQuery));