Here's some sample javascript which can be used to replace text in bug events. I'm currently using this for creating links and replacing long urls with shorter ones within bug events.
In this sample there are two objects which I use as a poor man's replacement for hashes:
- aRegExps contains regular expressions to match in the text of the bug event
- aReplacements contains the replacement texts for the corresponding regexps
What these replacements do:
- svnlink - This replaces a string in the form of SVN#revision with a link to our svn viewVC server
- shortenlink - Replaces a very long url (we have a lot of those here) with a shorter one (note that this doesn't modify the href in the anchor tag, just the text that is displayed)
- replaceurl - Replaces one url with another
To add more replacements just add a matching pair of aRegExps and aReplacements objects.
A couple of notes about this sample:
- The regular expressions in the sample are obviously examples which I replaced before posting here and need to be customized.
- Remember to escape special characters in the regexps
- It's grossly untested and probably
- It has a few nasty bugsbeen mostly tested in Chrome and Firefox. No guarantees on other browsers.
Here's the sample:
Note that you need to edit the below js code to adit/add your keywords and corresponding replacements.
name: Replace links
description: Replaces keywords with links in BugEvents
author: Roman Hernandez
version: 1.0
js:
var aRegExps = new Object();
var aReplacements = new Object();
aRegExps["svnlink"] = RegExp(/SVN#(\d+)/g);
aReplacements["svnlink"] = "<a href=\"http://your.svn.url/viewvc?view=rev&revision=$1\">SVN#$1</a>";
aRegExps["shortenlink"] = RegExp(/>http:\/\/some\.common\.url\/path\?link=something/g);
aReplacements["shortenlink"] = ">SVN#";
aRegExps["replaceurl"] = RegExp(/http:\/\/url\.to\.replace/g);
aReplacements["replaceurl"] = "http://replacement.url.here";
// Find all the bugevent body elements
$("div.body").each(
function (){
var content = $(this).html();
var replace = false;
for (var key in aRegExps){
if (aRegExps[key].test(content)){
replace = true;
content = content.replace(aRegExps[key], aReplacements[key]);
}
}
if (replace)
$(this).html(content);
}
);