Page MenuHomeDevCentral
Paste P219

Servers log decorators
ActivePublic

Authored by dereckson on Sep 9 2016, 21:47.
Tags
None
Referenced Files
F53346: Servers log decorators
Sep 9 2016, 21:47
Subscribers
None
///
/// Format messages helper functions
///
/**
* @var array
*/
messageDecorators: [{
// SHA-1 Git commit hashes
re: /\b([0-9a-f]{7,40})\b/g,
/**
* Callback method to linkify when needed a SHA-1 hash.
*
* @param match The expression matched by the regexp
* @param p1 The SHA-1 hash candidate
* @param offset The position p1 has been found
* @param string The full string p1 has been found
* @returns {string}
*/
replaceBy: function replaceBy(match, p1, offset, string) {
if (!serversLog.isHash(p1)) {
return p1;
}
return '<a href="https://devcentral.nasqueron.org/search/?query=%1">%1</a>'.replace(/%1/g, p1);
}
}, {
// Tasks, reviews and pastes
re: /\b([TDP][0-9]{1,6}(\#[0-9]{1,10})?)\b/g,
replaceBy: '<a href="https://devcentral.nasqueron.org/$1">$1</a>'
}, {
// Repositories callsigns
re: /\br([A-Z]{3,32})\b/g,
replaceBy: '<a rel="repository" href="https://devcentral.nasqueron.org/diffusion/$1/">r$1</a>'
}, {
// Commits with callsigns
re: /\br([A-Z]{3,32}[0-9a-f]{7,40})\b/g,
replaceBy: '<a rel="commit" href="https://devcentral.nasqueron.org/r$1">r$1</a>'
}, {
// Code (or SQL query parameter)
re: /`(.*?)`/g,
replaceBy: '<code>$1</code>'
}],
/**
* Whitelist of known hexadecimal words.
*
* @var array
*/
hexadecimalKnownWord: ["added", "ed25519"],
/**
* Determines if an expression matches a whitelisted hexadecimal word.
*
* @param word The word to check
* @returns {boolean}
*/
isHexadecimalKnownWord: function isHexadecimalKnownWord(word) {
return this.hexadecimalKnownWord.indexOf(word) > -1;
},
/**
* Determines if the specified expression is probably an hash.
*
* An hash is anything hexadecimal with at least one digit < 10
* and one digit > 9 (A-F), not matching known vocabulary.
*
* @param hash
* @returns {boolean}
*/
isHash: function isHash(hash) {
if (this.isHexadecimalKnownWord(hash)) {
return false;
}
if (/^\d+$/.test(hash) || /^[a-z]+$/i.test(hash)) {
// Contains only letter or digits,
// so not a good hash candidate.
return false;
}
return true;
},