/WebSVN/include/utils.inc
24,8 → 24,6
//
// General utility commands
 
#require_once 'php5compat.inc';
 
// {{{ createDirLinks
//
// Create a list of links to the current path that'll be available from the template
153,11 → 151,68
 
function hardspace($s)
{
return '<code>'.$s.'</code>';
return '<code>' . expandTabs($s) . '</code>';
}
 
// }}}
 
// {{{ expandTabs
 
/**
* Expands the tabs in a line that may or may not include HTML.
*
* Enscript generates code with HTML, so we need to take that into account.
*
* @param string $s Line of possibly HTML-encoded text to expand
* @param int $tabwidth Tab width, -1 to use repository's default, 0 to collapse
* all tabs.
* @return string The expanded line.
* @since 2.1
*/
 
function expandTabs($s, $tabwidth = -1)
{
global $rep;
 
if ($tabwidth == -1)
$tabwidth = $rep->getExpandTabsBy();
$pos = 0;
 
// Parse the string into chunks that are either 1 of: HTML tag, tab char, run of any other stuff
$chunks = preg_split("/((?:<.+?>)|(?:&.+?;)|(?:\t))/", $s, -1, PREG_SPLIT_DELIM_CAPTURE);
 
// Count the sizes of the chunks and replace tabs as we go
for ($i = 0; $i < count($chunks); $i++)
{
# make sure we're not dealing with an empty string
if (empty($chunks[$i])) continue;
switch ($chunks[$i]{0})
{
case '<': // HTML tag : ignore its width by doing nothing
break;
 
case '&': // HTML entity: count its width as 1 char
$pos += 1;
break;
 
case "\t": // Tab char: replace it with a run of spaces between length tabwidth and 1
$tabsize = $tabwidth - ($pos % $tabwidth);
$chunks[$i] = str_repeat(' ', $tabsize);
$pos += $tabsize;
break;
 
default: // Anything else: just keep track of its width
$pos += strlen($chunks[$i]);
break;
}
}
 
// Put the chunks back together and we've got the original line, detabbed.
return join('', $chunks);
}
 
// }}}
 
// {{{ datetimeFormatDuration
//
// Formats a duration of seconds for display.
227,6 → 282,8
 
function getParameterisedSelfUrl($params = true)
{
global $config;
 
$url = null;
 
if ($config->multiViews)
256,4 → 313,28
 
// }}}
 
// {{{ getUserLanguage
 
function getUserLanguage() {
$languages = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'en';
if (strpos($languages, ',') === false) return $languages; # only one specified
# split off the languages into an array of languages and q-values
$qvals = array();
$langs = array();
preg_match_all('#(\S+?)\s*(?:;q=([01](?:\.\d{1,3})?))?\s*,\s*#', $languages, $qvals, PREG_SET_ORDER);
foreach ($qvals as $q) {
$langs[] = array (
$q[1],
floatval(!empty($q[2]) ? $q[2] : 1.0)
);
}
# XXX: now, we should loop through our available languages and choose an
# appropriate one for the user
# note that we shouldn't match the region unless we have a specific region
# to use (e.g. zh-CN vs. zh-TW)
# FIXME: see above; otherwise, this function doesn't really do anything
}
 
// }}}
 
?>