function _freelinking_do_filtering in Freelinking 6.2
Same name and namespace in other branches
- 5 freelinking.module \_freelinking_do_filtering()
- 6 freelinking.module \_freelinking_do_filtering()
2 calls to _freelinking_do_filtering()
File
- ./
freelinking.module, line 451
Code
function _freelinking_do_filtering($text, $store = FALSE) {
$allowcamelcase = variable_get("freelinking_camelcase", TRUE);
$freelinkingregexp = '/[^!](\\[\\[.+]])/Uu';
// this finds [[links like this]], un-greedily and utf-8
preg_match_all($freelinkingregexp, $text, $flmatches, PREG_PATTERN_ORDER);
if ($allowcamelcase) {
$camelcaseregexp = '/\\b([[:upper:]][[:lower:]]+){2,}\\b/';
// this gets us close, but is not perfect. Example: ThisIsACamelCaseWord won't match (two caps in a row)
preg_match_all($camelcaseregexp, $text, $ccmatches);
$wikiwords = array_merge($ccmatches[0], $flmatches[1]);
}
else {
$wikiwords = $flmatches[1];
}
foreach (array_unique($wikiwords) as $wikiword) {
if (substr($wikiword, 0, 2) == '[[') {
// if it's a freelink, the expressions are different
$phrase = substr($wikiword, 2, -2);
$freelink = $phrase;
$barpos = strpos($phrase, '|');
$pattern = '/\\[\\[' . preg_quote($phrase, '/') . ']]/';
if ($barpos) {
$freelink = substr($freelink, 0, $barpos);
$phrase = substr($phrase, $barpos + 1);
}
if (preg_match('/^(http(s)?|mailto|ftp):/', $freelink)) {
$replacement = '<a class="freelinking external" href="' . $freelink . '">' . $phrase . '</a>';
$store = FALSE;
}
else {
$flnkexists = _freelinking_exists($freelink);
if ($flnkexists) {
$tooltip = html_entity_decode($freelink);
$lnkoptions = array(
'attributes' => array(
'title' => $tooltip,
),
);
$replacement = l(html_entity_decode($phrase), drupal_get_path_alias('node/' . $flnkexists), $lnkoptions);
}
else {
//link doesn't exist - apply nofollow
$lnkoptions = array(
'attributes' => array(
'class' => 'freelinking noexist',
'rel' => 'nofollow',
),
);
$replacement = l(html_entity_decode($phrase), 'freelinking/' . rawurlencode($freelink), $lnkoptions);
}
}
}
else {
if ($allowcamelcase) {
// it's a CamelCase, expressions are a bit simpler
$pattern = '/(?<!\\/)\\b' . $wikiword . '\\b(?![^<]*>)/';
$phrase = $wikiword;
// consistency for the db
$freelink = $wikiword;
// also for the db
$replacement = l($wikiword, 'freelinking/' . urlencode($wikiword), array(
'attributes' => array(
'class' => 'freelinking',
),
));
}
}
$text = preg_replace($pattern, $replacement, $text, variable_get("freelinking_onceonly", 0) ? 1 : -1);
if ($store) {
_freelinking_store($freelink, $replacement);
}
}
// foreach wikiword
return $text;
}