/*global HANDLEBARS_TEMPLATES:true*/
/**
Support for BBCode rendering
@class BBCode
@namespace Discourse
@module Discourse
**/
Discourse.BBCode = {
QUOTE_REGEXP: /\[quote=([^\]]*)\]((?:[\s\S](?!\[quote=[^\]]*\]))*?)\[\/quote\]/im,
// Define our replacers
replacers: {
base: {
withoutArgs: {
"ol": function(_, content) { return "
" + content + "
"; },
"li": function(_, content) { return "" + content + ""; },
"ul": function(_, content) { return ""; },
"code": function(_, content) { return "" + content + "
"; },
"url": function(_, url) { return "" + url + ""; },
"email": function(_, address) { return "" + address + ""; },
"img": function(_, src) { return "
"; }
},
withArgs: {
"url": function(_, href, title) { return "" + title + ""; },
"email": function(_, address, title) { return "" + title + ""; },
"color": function(_, color, content) {
if (!/^(\#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?)|(aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|purple|red|silver|teal|white|yellow)$/.test(color)) {
return content;
}
return "" + content + "";
}
}
},
// For HTML emails
email: {
withoutArgs: {
"b": function(_, content) { return "" + content + ""; },
"i": function(_, content) { return "" + content + ""; },
"u": function(_, content) { return "" + content + ""; },
"s": function(_, content) { return "" + content + ""; },
"spoiler": function(_, content) { return "" + content + ""; }
},
withArgs: {
"size": function(_, size, content) { return "" + content + ""; }
}
},
// For sane environments that support CSS
"default": {
withoutArgs: {
"b": function(_, content) { return "" + content + ""; },
"i": function(_, content) { return "" + content + ""; },
"u": function(_, content) { return "" + content + ""; },
"s": function(_, content) { return "" + content + ""; },
"spoiler": function(_, content) { return "" + content + "";
}
},
withArgs: {
"size": function(_, size, content) { return "" + content + ""; }
}
}
},
/**
Apply a particular set of replacers
@method apply
@param {String} text The text we want to format
@param {String} environment The environment in which this
**/
apply: function(text, environment) {
var replacer = Discourse.BBCode.parsedReplacers()[environment];
// apply all available replacers
replacer.forEach(function(r) {
text = text.replace(r.regexp, r.fn);
});
return text;
},
/**
Lazy parse replacers
@property parsedReplacers
**/
parsedReplacers: function() {
if (this.parsed) return this.parsed;
var result = {};
Object.keys(Discourse.BBCode.replacers, function(name, rules) {
var parsed = result[name] = [];
Object.keys(Object.merge(Discourse.BBCode.replacers.base.withoutArgs, rules.withoutArgs), function(tag, val) {
parsed.push({ regexp: new RegExp("\\[" + tag + "\\]([\\s\\S]*?)\\[\\/" + tag + "\\]", "igm"), fn: val });
});
Object.keys(Object.merge(Discourse.BBCode.replacers.base.withArgs, rules.withArgs), function(tag, val) {
parsed.push({ regexp: new RegExp("\\[" + tag + "=?(.+?)\\]([\\s\\S]*?)\\[\\/" + tag + "\\]", "igm"), fn: val });
});
});
this.parsed = result;
return this.parsed;
},
/**
Build the BBCode quote around the selected text
@method buildQuoteBBCode
@param {Discourse.Post} post The post we are quoting
@param {String} contents The text selected
**/
buildQuoteBBCode: function(post, contents) {
var contents_hashed, result, sansQuotes, stripped, stripped_hashed, tmp;
if (!contents) contents = "";
sansQuotes = contents.replace(this.QUOTE_REGEXP, '').trim();
if (sansQuotes.length === 0) return "";
result = "[quote=\"" + (post.get('username')) + ", post:" + (post.get('post_number')) + ", topic:" + (post.get('topic_id'));
/* Strip the HTML from cooked */
tmp = document.createElement('div');
tmp.innerHTML = post.get('cooked');
stripped = tmp.textContent || tmp.innerText;
/*
Let's remove any non alphanumeric characters as a kind of hash. Yes it's
not accurate but it should work almost every time we need it to. It would be unlikely
that the user would quote another post that matches in exactly this way.
*/
stripped_hashed = stripped.replace(/[^a-zA-Z0-9]/g, '');
contents_hashed = contents.replace(/[^a-zA-Z0-9]/g, '');
/* If the quote is the full message, attribute it as such */
if (stripped_hashed === contents_hashed) result += ", full:true";
result += "\"]\n" + sansQuotes + "\n[/quote]\n\n";
return result;
},
/**
Replace quotes with appropriate markup
@method formatQuote
@param {String} text The text inside which we want to replace quotes
@param {Object} opts Rendering options
**/
formatQuote: function(text, opts) {
var args, matches, params, paramsSplit, paramsString, templateName, username;
while (matches = this.QUOTE_REGEXP.exec(text)) {
paramsString = matches[1].replace(/\"/g, '');
paramsSplit = paramsString.split(/\, */);
params = [];
paramsSplit.each(function(p, i) {
if (i > 0) {
var assignment = p.split(':');
if (assignment[0] && assignment[1]) {
return params.push({
key: assignment[0],
value: assignment[1].trim()
});
}
}
});
username = paramsSplit[0];
// Arguments for formatting
args = {
username: username,
params: params,
quote: matches[2].trim(),
avatarImg: opts.lookupAvatar ? opts.lookupAvatar(username) : void 0
};
// Name of the template
templateName = 'quote';
if (opts && opts.environment) templateName = "quote_" + opts.environment;
// Apply the template
text = text.replace(matches[0], "" + HANDLEBARS_TEMPLATES[templateName](args) + "");
}
return text;
},
/**
Format a text string using BBCode
@method format
@param {String} text The text we want to format
@param {Object} opts Rendering options
**/
format: function(text, opts) {
var environment = opts && opts.environment ? opts.environment : 'default';
// Apply replacers for basic tags
text = Discourse.BBCode.apply(text, environment);
// Format
text = Discourse.BBCode.formatQuote(text, opts);
return text;
}
};