diff --git delicious.inc delicious.inc index 4c5f996..041ba4b 100644 --- delicious.inc +++ delicious.inc @@ -185,14 +185,24 @@ function _delicious_term_link(&$node) { } } -// -// smart-tagging support -// -function _delicious_tag_string($text, $tags, $username) { - - if (!$tags) { - return $text; - } +/** + * Core smart-tagging functionality. + * + * Scans given text for delicious tags and places links to delicious next to them. + * It can be optionally provided with a set of html tags to skip. e.g. . + * + * @param $text string + * Input text. + * @param $delicous_tags + * Delicious tags to scan for. + * @param $username + * Username to point the tags links to. e.g. delicious.com//tag + * @param $skip_tags + * Array of html tags to exclude from scanning. + * @return + * Text with links to given username's delicous tags. + */ +function _delicious_tag_string($text, $delicious_tags, $username, $skip_tags = array()) { // Match absolute URLs. $url_regex = '((http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))'; // Match e-mail addresses. @@ -200,29 +210,32 @@ function _delicious_tag_string($text, $tags, $username) { // Match www domains/addresses. $www_regex = '(www\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\&=/;-])'; - // Replace all URLs, Domains, and e-mail address with tokens. + // Skip given tags + if (!empty($skip_tags)) { + $skip_tags_regex = '(<('. implode('|', $skip_tags) .')[^>]*>.*?)'; + } + + // First preg_match: Replace all URLs, Domains, and e-mail address with tokens. + // Second preg_match: Replace all chunks of tags to skip with tokens. $replacements = array(); - while (preg_match("`($url_regex|$mail_regex|$www_regex)`i", $text, $matches)) { + while ((!empty($skip_tags) && preg_match("`$skip_tags_regex`is", $text, $matches)) || preg_match("`($url_regex|$mail_regex|$www_regex)`i", $text, $matches)) { $token = '__delicious_processing_'. md5($matches[1]); $replacements[$token] = $matches[1]; $text = str_replace($matches[1], $token, $text); } // error, it must start with a tag... :-( - foreach ($tags as $tag) { - if (!empty($tag) && $tag != ' ') { - $path = DELICIOUS_BASE_URL . urlencode($username) .'/'. urlencode($tag); - $modulepath = drupal_get_path('module', 'delicious'); - $delicious_icon = theme('image', $modulepath .'/deliciousIcon.gif', '', t('Browse @username\'s delicious \'@tag\' tagged links', array('@username' => $username, '@tag' => $tag), '', array('class' => 'delicious-icon'))); - $delicious_link = l($delicious_icon, $path, array('html' => TRUE)); - - - $tag = preg_quote($tag); - if (!preg_match('/<.+>/', $text)) { - $text = preg_replace('/(\b'. $tag .'\b)/i', '$1'. $delicious_link, $text); - } - else { - $text = preg_replace('/(?<=>)([^<]+)?(\b'. $tag .'\b)/i', '$1$2'. $delicious_link, $text); - } + foreach ($delicious_tags as $delicious_tag) { + $path = DELICIOUS_BASE_URL . urlencode($username) .'/'. urlencode($delicious_tag); + $modulepath = drupal_get_path('module', 'delicious'); + $delicious_icon = theme('image', $modulepath .'/deliciousIcon.gif', '', t('Browse @username\'s delicious \'@tag\' tagged links', array('@username' => $username, '@tag' => $delicious_tag), '', array('class' => 'delicious-icon'))); + $delicious_link = l($delicious_icon, $path, array('html' => TRUE)); + + $tag = preg_quote($tag); + if (!preg_match('/<.+>/', $text)) { + $text = preg_replace('/(\b'. $delicious_tag .'\b)/i', '$1'. $delicious_link, $text); + } + else { + $text = preg_replace('/(?<=>)([^<]+)?(\b'. $delicious_tag .'\b)/i', '$1$2'. $delicious_link, $text); } } // Restore all URLs, Domains, and e-mail addresses. @@ -232,15 +245,31 @@ function _delicious_tag_string($text, $tags, $username) { return $text; } -// -// support function for tagged text -// +/** + * Adds links to delicious tags found in given text. + * + * @param $text + * Input text. + * @param $name + * Delicious username to use for building tag links URLs. + * @return + * Text with links to given username's delicous tags. + */ function _delicious_tag_text($text, $name) { $result = db_query("SELECT distinct(name) FROM {delicious_tag}"); while ($tag = db_fetch_object($result)) { - $tags[] = $tag->name; + if ($tag = trim($tag->name)) { + $tags[] = $tag; + } + } + + if (empty($tags)) { + return $text; } - return _delicious_tag_string($text, $tags, $name); + + $skip_tags = preg_split('/\s+|<|>/', variable_get('delicious_tagging_skip_tags', '