Created
June 1, 2012 22:07
-
-
Save bshaffer/2855427 to your computer and use it in GitHub Desktop.
Colloquy Smart Linking Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Smart Linking Colloquy Plugin | |
* By: Brent Shaffer | |
* | |
* Add this plugin to one of the following directories and restart Colloquy: | |
* - ~/Library/Application Support/Colloquy/PlugIns | |
* - /Library/Application Support/Colloquy/PlugIns | |
* - /Network/Library/Application Support/Colloquy/PlugIns | |
*/ | |
/* sample configuration. Change below to match your desired linking pattern */ | |
var linkConfig = [ | |
{ | |
regex: /issue #?(\d+)/i, | |
regexAll: /issue #?(\d+)/ig, | |
url: 'http://issues.mycorp.com/*/show', | |
}, | |
{ | |
regex: /build #?(\d+)/i, | |
regexAll: /build #?(\d+)/ig, | |
url: 'https://builds.mycorp.com/info?build_id=*', | |
}, | |
]; | |
var enabled = true; | |
function processUserCommand(command, arguments, connection, view) { | |
if( command == 'smartlinks' ) { | |
if( arguments == 'enable' || arguments == 'on' || arguments == 'yes' ) { | |
view.addEventMessageToDisplay('Smart Links enabled.', 'smartLinksEnabled', null ); | |
enabled = true; | |
} else if( arguments == 'disable' || arguments == 'off' || arguments == 'no' ) { | |
view.addEventMessageToDisplay('Smart Links disabled.', 'smartLinksDisabled', null ); | |
enabled = false; | |
} else if( arguments == 'reset' || arguments == 'clear' || arguments == 'forget' ) { | |
view.addEventMessageToDisplay('Smart Links cache cleared.', 'smartLinksReset', null ); | |
titleCache = {}; | |
} | |
return true; | |
} | |
return false; | |
} | |
function processIncomingMessage(message, view) { | |
if( ! enabled ) return; | |
html = message.bodyAsPlainText(); | |
for(var i=0; i<linkConfig.length; i++) { | |
html = processLinkHtml(linkConfig[i], html); | |
} | |
if (html != message.bodyAsPlainText()) { | |
message.setBodyAsHTML(html); | |
} | |
} | |
function processLinkHtml(config, html) { | |
if (links = html.match(config.regexAll)) { | |
links.unique(); | |
links.sort( sortByStringLengthDescending ); | |
for(var i=0; i<links.length; i++) { | |
if( matches = links[i].match(config.regex) ) { | |
linkHtml = '<a href="'+config.url.replace('*', matches[1])+'">'+matches[0]+'</a>'; | |
html = html.replace(matches[0], linkHtml); | |
} | |
} | |
} | |
return html; | |
} | |
function sortByStringLengthDescending ( a, b ) | |
{ | |
if ( a.length > b.length ) | |
return -1; | |
if ( a.length < b.length ) | |
return 1; | |
return 0; // a and b are the same length | |
} | |
Array.prototype.unique = function() { | |
var o = {}, i, l = this.length, r = []; | |
for(i=0; i<l;i+=1) o[this[i]] = this[i]; | |
for(i in o) r.push(o[i]); | |
return r; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment