'/nocheck/',
'callback' => 'freelinking_nocheck_callback',
);
function freelinking_nocheck_settings() {
$form['freelinking_nocheck_base'] = array(
'#title' => t('Target base path'),
'#type' => 'textfield',
'#default_value' => variable_get('freelinking_nocheck_base', 'freelinking'),
'#description' => t('The drupal path to prepend (e.g. mywiki to use paths like mywiki/page. Do not include leading or trailing slashes.'),
'#size' => 30,
'#maxlength' => 30,
);
$form['freelinking_separator'] = array(
'#title' => t('Separator'),
'#type' => 'textfield',
'#default_value' => variable_get('freelinking_separator', '-'),
'#description' => t('Character used to separate words in addresses. This will replace spaces and some punctuation characters. Using a space or + character can cause unexpected results.'),
'#size' => 1,
'#maxlength' => 1,
);
$form['freelinking_nocheck_transliterate'] = array(
'#title' => t('Treatment of special characters'),
'#type' => 'select',
'#options' => array(
0 => t('Permissive: Keep most special characters, only turn `"*^<>+?%#@|{}\\ into separators'),
1 => t('Moderate: Replace according to a table and turn `"*^<>+?%#@|{}\\ into separators'),
2 => t('Restrictive: Replace according to a table, then limit to a-z, A-Z, digits and -_~=/.;,\'()[]'),
),
'#default_value' => variable_get('freelinking_nocheck_transliterate', 0),
'#description' => t('The replacement table is taken from the Pathauto module\'s i18n-ascii.txt if available, otherwise a default table is used.
It seems page requests are transliterated or fuzzy so that a link with non-ASCII characters works even when the path alias is set with ASCII-only.'),
);
return $form;
}
function freelinking_nocheck_callback($match) {
$target = $match[1];
$link_text = $match[2];
$target = freelinking_nocheck_cleanstring($target, false); // false: allow '/' in addresses, is that OK?
$target = strtolower($target);
return l($link_text, variable_get('freelinking_nocheck_base', 'freelinking') .'/'. $target);
}
/**
* Make a string suitable for use in the URL. Remove or replace some characters,
* e.g. spaces are turned into a separator character
* and non-English characters are transliterated.
* @param string $string The string to process
* @param bool $clean_slash (Default: TRUE) Should '/' be replaced?
*/
function freelinking_nocheck_cleanstring($string, $clean_slash = TRUE) {
$separator = variable_get('freelinking_separator', '-');
if ($clean_slash)
$string = str_replace('/', $separator, $string);
// Optionally remove accents and transliterate
$transliterate = variable_get('freelinking_nocheck_transliterate', 0);
if ($transliterate >= 1) {
static $i18n_loaded = false;
static $translations = array();
static $only_lowercase = false;
if (!$i18n_loaded) {
// Use the Pathauto module if enabled
if (module_exists('pathauto') && $path = drupal_get_path('module', 'pathauto')
&& is_file($path .'/i18n-ascii.txt')) {
$translations = parse_ini_file($path .'/i18n-ascii.txt');
} else { // Use a fixed set of replacements
$translations = array('à'=>'a','å'=>'a','ä'=>'a','æ'=>'ae','ç'=>'c','é'=>'e','è'=>'e','ê'=>'e','ë'=>'e',
'ï'=>'i','ň'=>'n','ñ'=>'n','œ'=>'oe','ö'=>'o','ô'=>'o','š'=>'s','ß'=>'ss',
'ü'=>'ue','û'=>'u','ž'=>'z','—'=>'-','–'=>'-');
$only_lowercase = true; // for simplicity we only support lower-case letters
}
$i18n_loaded = true;
}
if ($only_lowercase)
$string = strtolower($string);
$string = strtr($string, $translations);
}
if ($transliterate >= 2)
// As an extra safeguard, reduce to a small set of characters.
$string = preg_replace('/[^-_a-zA-Z0-9\/()~=\\]\\[;,.\']+/', $separator, $string);
else // $transliterate is 0 or 1:
// Only replace a few non-suitable characters (which are part of ASCII). Leave non-ASCII as it is.
$string = preg_replace('/["`|{}+*%#?<>\\\\^@\\s]+/', $separator, $string);
// Also replace repeated $separator with a single $separator (unless already done).
if ($separator !== '-' && $separator !== '_')
$string = preg_replace('/('. preg_quote($separator) .')+/', $separator, $string);
// Avoid separators at start and end of string
$trimmed = trim($string, $separator);
if ($trimmed !== '')
return $trimmed;
else
return $string;
}