Skip to content

Commit

Permalink
Homepage|Build Repository: Shorten URLs to bug tracker items in the b…
Browse files Browse the repository at this point in the history
…uild log

Shorten bug tracker URLs in the commit log (cleaner, human-friendly).
  • Loading branch information
danij-deng committed Sep 4, 2012
1 parent 2d46bed commit a80bc04
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
30 changes: 23 additions & 7 deletions web/includes/utilities.inc.php
Expand Up @@ -232,22 +232,28 @@ function json_encode_clean(&$array, $flags=0, $indent_level=0)

function generateHyperlinkHTML($uri, $attributes = array(), $maxLength = 40)
{
$shortUri = NULL;

if($uri instanceof Url)
{
if(!strcmp($uri->host(), 'sourceforge.net') &&
!substr_compare($uri->path(), '/p/deng/bugs/', 0, 13))
{
$bugStr = substr($uri->path(), 13);
$bugNum = intval(substr($bugStr, 0, strpos($bugStr, '/')));

$shortUri = "Bug#$bugNum";
if(is_null($attributes))
$attributes = array();
$attributes['title'] = "View bug report #$bugNum in the tracker";
}
$uri = $uri->toString();
}
else
{
$uri = strval($uri);
}

$maxLength = (integer)$maxLength;
if($maxLength < 0) $maxLength = 0;
if($maxLength > 0 && strlen($uri) > $maxLength)
$shortUri = substr($uri, 0, $maxLength).'...';
else
$shortUri = $uri;

$attribs = '';
if(is_array($attributes))
{
Expand All @@ -257,6 +263,16 @@ function generateHyperlinkHTML($uri, $attributes = array(), $maxLength = 40)
}
}

if(!isset($shortUri))
{
$maxLength = (integer)$maxLength;
if($maxLength < 0) $maxLength = 0;
if($maxLength > 0 && strlen($uri) > $maxLength)
$shortUri = substr($uri, 0, $maxLength).'...';
else
$shortUri = $uri;
}

$html = "<a href=\"{$uri}\"{$attribs}>". htmlspecialchars($shortUri) .'</a>';
return $html;
}
22 changes: 11 additions & 11 deletions web/plugins/buildrepository/commitutils.php
Expand Up @@ -90,19 +90,19 @@ function formatCommitHTML($msg)
htmlspecialchars($msg);

// Rules derived from Søren Løvborg's UrlLinker (see: http://www.kwi.dk/projects/php/UrlLinker/)
$rexProtocol = '((http|https|ftp)\:\/\/)';
$rexDomain = '(?:[-a-zA-Z0-9]{1,63}\.)+[a-zA-Z][-a-zA-Z0-9]{1,62}';
$rexIp = '(?:[1-9][0-9]{0,2}\.|0\.){3}(?:[1-9][0-9]{0,2}|0)';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexUsername = '[^]\\\\\x00-\x20\"(),:-<>[\x7f-\xff]{1,64}';
$rexPassword = $rexUsername; // allow the same characters as in the username
$rexUrl = "$rexProtocol(?:($rexUsername)(:$rexPassword)?@)?($rexDomain|$rexIp)($rexPort$rexPath$rexQuery$rexFragment)";
$rexProtocol = '(https?:\/\/|ftp:\/\/)';
$rexDomain = '(?:[-a-zA-Z0-9]{1,63}\.)+[a-zA-Z][-a-zA-Z0-9]{1,62}';
$rexIp = '(?:[1-9][0-9]{0,2}\.|0\.){3}(?:[1-9][0-9]{0,2}|0)';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexUsername = '[^]\\\\\x00-\x20\"(),:-<>[\x7f-\xff]{1,64}';
$rexPassword = $rexUsername; // allow the same characters as in the username
$rexUrl = "$rexProtocol(?:($rexUsername)(:$rexPassword)?@)?($rexDomain|$rexIp)$rexPort$rexPath$rexQuery$rexFragment";

$msg = preg_replace_callback("&\\b$rexUrl(?=[?.!,;:\"]?(\s|$))&",
"make_pretty_hyperlink", $msg);
"make_pretty_hyperlink", $msg);

return $msg;
}
Expand Down

0 comments on commit a80bc04

Please sign in to comment.