SignpostMarv Martin. Author: Joel Bennett Author URI: http://www.HuddledMasses.org Copyright (c) 2003 Released under the GPL license http://www.gnu.org/licenses/gpl.txt This file is part of WordPress. WordPress is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // different types of ubernyms: define("uttReplace", "uttReplace"); define("uttJustLink", "uttJustLink" ); define("uttAbbreviation", "uttAbbreviation" ); define("uttAcronym", "uttAcronym" ); define("uttInitialism", "uttInitialism" ); function ubernyms_load_settings() { global $ubernyms_settings; if( empty($ubernyms_settings) ) { $ubernyms_settings = get_option("ubernyms_settings"); if( empty($ubernyms_settings) ) { update_option("ubernyms_settings", array( true, true, true, true, true) ); } } } function ubernyms_load() { global $ubernyms_collection; ubernyms_load_settings(); if( empty($ubernyms_collection) ) { $ubernyms_collection = get_option("ubernyms_collection"); // First, we define all the things we're going to replace, without using parenthesis or pipes (|) // ESPECIALLY note that they all end with commas EXCEPT the last one if( empty($ubernyms_collection) ) { // $type, $abbr, $text, $description = NULL, $english = FALSE, $makeLink = NULL, $appendLink = NULL $ubernyms_collection = array( new ubernym( uttInitialism, "AOL", "America Online", "Still the largest internet service provider in the world", false, NULL, "http://www.aol.com" ), new ubernym( uttAcronym, "AIM", "AOL Instant Messenger", "The most popular instant messaging service was originally for AOL members only.", false, NULL, "http://www.aim.com" ), new ubernym( uttAcronym, "AJAX", "Asynchronous Javascript And XML", "How we make web-pages truly dynamic", true, NULL, NULL ), new ubernym( uttInitialism, "CSS", "Cascading Style Sheets", "", true, NULL, "http://www.w3.org/Style/CSS/"), new ubernym( uttInitialism, "HTML", "HyperText Markup Language","Link to the spec:",false, NULL,"http://www.w3.org/MarkUp/"), new ubernym( uttInitialism, "HTTP", "HyperText Transfer Protocol","",false, NULL, NULL), new ubernym( uttInitialism, "PDF", "Portable Document Format","",false, NULL, NULL), new ubernym( uttInitialism, "PHP", "PHP: Hypertext Preprocessor","",false ,"http://www.php.net"), new ubernym( uttInitialism, "PNG", "Portable Network Graphics","", false, "http://www.w3.org/Graphics/PNG/", NULL), new ubernym( uttInitialism, "RDF", "Resource Description Framework", "", true ,"http://www.w3.org/RDF/", NULL), new ubernym( uttInitialism, "RSS", "Really Simple Syndication","",false, NULL, NULL), new ubernym( uttJustLink, "ubernyms", "A WordPress plugin", "The original acronym and autolinker plugins all rolled into one slick plugin with fancy tooltips and more.", false, NULL, "http://www.huddledmasses.org/category/development/wordpress/ubernyms-plugin/" ), new ubernym( uttInitialism, "URL", "Uniform Resource Locator","",false, NULL, NULL), new ubernym( uttInitialism, "USB", "Universal Serial Bus","",false, NULL, NULL), new ubernym( uttInitialism, "VBS", "Visual Basic Script", "", true, NULL, NULL), new ubernym( uttAcronym, "VOIP", "Voice Over Internet Protocol", "Allows you to make voice phone calls over the internet, which makes it much cheaper, and in many cases, free.", false, NULL, NULL), new ubernym( uttInitialism, "W3C", "World Wide Web Consortium","",false ,"http://www.w3.org/", NULL), new ubernym( uttAbbreviation,"WCAG", "Web Content Accessibility Guidelines","",false ,"http://www.w3.org/TR/WAI-WEBCONTENT", NULL), new ubernym( uttAcronym, "WYSIWYG", "what you see is what you get","",false, NULL, NULL), new ubernym( uttInitialism, "XHTML", "eXtensible HyperText Markup Language - HTML reformulated as XML","",false ,"http://www.w3.org/MarkUp/", NULL), new ubernym( uttInitialism, "XML", "eXtensible Markup Language","",false ,"http://www.w3.org/XML/", NULL), new ubernym( uttAbbreviation,"XRX", "Xerox (stock ticker symbol)","",false ,"http://www.google.com/finance?XRX", NULL), new ubernym( uttInitialism, "XSL", "eXtensible Stylesheet Language","",false ,"http://www.w3.org/Style/XSL/", NULL), new ubernym( uttInitialism, "XSLT", "eXtensible Stylesheet Language Transformation", "", true ,"http://www.w3.org/Style/XSL/", NULL) ); update_option("ubernyms_collection", $ubernyms_collection ); } } } function ubernyms_make_config_page() { if ( function_exists('add_submenu_page') ) add_submenu_page('plugins.php', 'Ubernyms Configuration', 'Ubernyms Configuration', 1, basename(__FILE__), 'ubernyms_config_page'); } function ubernyms_config_page() { global $ubernyms_collection, $ubernyms_settings; ubernyms_load(); if (isset($_POST['ubernyms_update'])) { ?>

Settings updated!

"" && $_POST['uber_text'][$utt] > "" ) { $uttstored++; $ubernyms_collection[$utt] = new ubernym( $_POST['ubertt_type'][$utt], // One of: uttReplace, uttAbbreviation, uttAcronym, uttInitialism wp_specialchars(stripslashes($_POST['uber_abbr'][$utt])), // the search text wp_specialchars(stripslashes($_POST['uber_text'][$utt])), // the replacement text (or tooltip text) wp_specialchars(stripslashes($_POST['uber_description'][$utt])), // additional text (in parenthesis, or in DomTT) ( $_POST['uber_english'][$utt] == "true" ), // TRUE to show the full description the first time the text appears wp_specialchars(stripslashes($_POST['uber_makeLink'][$utt])), // URL to convert text into a link (leave NULL to not link) wp_specialchars(stripslashes($_POST['uber_appendLink'][$utt]))); // URL to append a (link) to the tooltip (or text if not using DomTT) } } update_option("ubernyms_collection", $ubernyms_collection ); print( "

$uttstored ubernyms stored." ); ?>

Ubernyms Configuration

The purpose of Ubernyms is to allow you configure a set of frequently used abbreviations that will be automatically coded nicely whenever you use them. The goal is to make them visible in a way that is literate for both humans and computers. In the future, I'll probably put the list of acronyms here on this page and store them in the database, but for now you have to actually edit the plugin to add, remove or change ubernyms.

There is a setting for each ubernym that lets you define if it is an acronym or an initialism, or just a plain old abbreviation. You can also specify special text that you want replaced completely, and even add links. In fact, you can add HTML to the replacements or even to the tooltips if you use the DomTT method.

The default is for all settings to be checked

Acronyms

The semantically correct thing to do is use the <abbr> tag for all abbreviations, unless they are actually pronounceable words like NATO and RADAR, in which case you should use the <acronym> tag. The problem with this is that IE6 doesn't correctly render the <abbr> tag at all. Of course, if you use the DomTT javascript and add style rules to your CSS for the span that this plugin adds, you can render it however you like!

/> Use semantically correct tags

Javascript

The DomTT fancy tooltips require a javascript, and depend on CSS for their styling. Because of that, you may want to use disable the DOM tooltips. If you don't, remember that Firefox (at least) will show the native tooltips over the DOM Tooltips, so you probably don't want to set the title attribute on the abbr tags, even though that makes them less useful to screen readers. And in addition, you need to have rules in your CSS for the tooltips to display correctly. In other words, these three ought to be all or nothing, unless you have your own custom CSS, or want my styles on your acronyms even without the DOM tooltips.

/> Use the DomTT script for tooltips

/> Don't use the title attribute

/> Add DomTT default CSS

Case Sensitivity

By default Ubernyms operates without regard for case. This isn't usually a problem because the replacement function prevents replacing letters in the middle of words. Personally, I find case-insensitive to be more useful since it allows me to autolink certain normal words, and put abbreviations for words like Canada as CA or Ca. At any rate, if you want to make it case-sensitive, feel free to uncheck this setting.

/> Ignore case.
Ubernyms

The ubernyms that are configured are listed below. To remove one, simly delete it's Text and Definition.

The meaning of each setting is as follows:

Type
The type of ubernym to create.
Replace Text
Replace the Text with the Definition. No tooltip, but links work.
Just Link
Just create a link with a tooltip.
Abbreviation
An abbr tag (surrounded with a span tag if you're using DomTT tooltips)
Acronym
An acronym tag (surrounded with a span tag if you're using DomTT tooltips)
Initials
An abbr tag with the class set to initialism (surrounded with a span tag if you're using DomTT tooltips)
Text
The text to replace
Definition
The definition for the abbreviation, or the replacement text
Description
Optional. A longer description of the term (used in DomTT tooltips, if present)
English
Optional. In normal english, the first time we use an acronym our audience wouldn't recognize, we spell out the full definition in the text. If you enable this option, ubernyms will do the same.
Make Link
Optional. A URL to use to create a link on the page.
Append Link
Optional. A URL to use to create a link in the tooltip, or to append on the end, if not using DomTT (You probably don't want to use this if you're not using DomTT)
Type Text Definition Description English Make Link Append Link
Type Text Definition Description English Make Link Append Link

Add new acronyms:

Type Text Definition Description English Make Link Append Link

type = $type; // One of: uttReplace, uttJustLink, uttAbbreviation, uttAcronym, uttInitialism $this->abbr = $abbr; // the search text $this->text = $text; // the replacement text (or tooltip text) $this->description = $description; // additional text (in parenthesis, or in DomTT) $this->english = $english; // TRUE to show the full description the first time the text appears $this->makeLink = $makeLink; // URL to convert text into a link (leave NULL to not link) $this->appendLink= $appendLink; // URL to append a (link) to the tooltip (or text if not using DomTT) } } function ubernyms_replacer($text) { global $ubernyms_collection, $ubernyms_settings; ubernyms_load(); $useSemanticalTags = $ubernyms_settings[0]; $useDomTT = $ubernyms_settings[1]; $hideTitle = $ubernyms_settings[2]; $caseInsensitive = $ubernyms_settings[3]; // put spaces on either end so we don't need special cases in our regex $text = " $text "; foreach($ubernyms_collection as $tooltip) { if( $tooltip->abbr > "" && $tooltip->text > "" ) { // need to improve this so links don't end up inside links, but PHP regex doesn't support (?abbr // debug ."\n StripSlashes:" . stripslashes($tooltip->abbr) // debug ."\n HTML Decode:" . html_entity_decode(stripslashes($tooltip->abbr),ENT_QUOTES) // debug ."\n R3 HTML Encode:" . wp_specialchars(html_entity_decode(stripslashes($tooltip->abbr),ENT_QUOTES), 'double') // debug ."\n add slashes:" . addslashes(wp_specialchars(html_entity_decode(stripslashes($tooltip->abbr),ENT_QUOTES), 'double')) // debug ."\n-->"); $regex = "¤(?!<[^<>]*?)(?abbr),ENT_QUOTES), 'double')).")\b(?!:)(?![^<>]*?>)¤imsU"; } else { // $regex = "¤(?!<[^<>]*?)(?abbr),ENT_QUOTES), 'double')).")\b(?!:)(?![^<>]*?>)¤msU"; } if( $tooltip->english ) { // pre-search with a single replacement and append the text. $text = preg_replace( $regex, $tooltip->abbr. " (".$tooltip->text.")", $text, 1 ); } $class = $tooltip->type;// == uttAbbreviation ? "abbreviation" : ($tooltip->type == uttAcronym ? "acronym" : ($tooltip->type == uttInitialism ? "initialism" : "abbr" ))); $tag = ((($tooltip->type == uttAcronym) || !$useSemanticalTags )? '': ' title="'.$tooltip->text.'">')."$1"; $tag .= (($tooltip->type == uttAcronym) || !$useSemanticalTags )? '' : ''; if( $tooltip->type == uttReplace ) { // if it's a type that calls for replace text, we replace the text. $tag = $tooltip->text; } if( $tooltip->type == uttJustLink ) { // if it's a type that just wants a link, we use the same text. $tag = "$1"; } // we're NOT using the javascript library if( !$useDomTT ) { // if they want to wrap it in a link if( NULL != $tooltip->makeLink ) { // we'll wrap the $tag in link $text = preg_replace( $regex, '' .$tag .'', $text); } else if( NULL != $tooltip->appendLink ){ // we'll just tag the link on the end in the plain text. $text = preg_replace( $regex, $tag. '(link)' , $text); } else { // simple replacement. No fancy stuff. $text = preg_replace( $regex, $tag, $text); } } else { // we're using the javascript library if( "" != $tooltip->description ) { $content = "'content', '". addslashes(wp_specialchars(html_entity_decode($tooltip->description,ENT_QUOTES), 'double')) .( ( null != $tooltip->appendLink ) ? ' (<a href="'. $tooltip->appendLink.'">link</a>)' : '' ). "','caption', '". addslashes(htmlentities(html_entity_decode($tooltip->text,ENT_QUOTES), ENT_COMPAT)) ."'"; } else { $content = "'content', '". addslashes(wp_specialchars(html_entity_decode($tooltip->text,ENT_QUOTES), 'double')) .( ( null != $tooltip->appendLink ) ? ' (<a href="'. $tooltip->appendLink.'">link</a>)' : '' )."'"; } // if they want to wrap it in a link if( NULL != $tooltip->makeLink ) { // we'll use the link tag to host the DomTT $text = preg_replace( $regex, '". $tag.'', $text); } else { // we'll use the tag to host the DomTT // they MAY want to append a link in the DomTT description // debug echo ""; // debug echo " "; $text = preg_replace( $regex, '". $tag .'', $text); } } } } return trim( $text ); } // add the javascript add_action('wp_head', 'ubertt_tooltip_script'); // add the config menu add_action('admin_menu', 'ubernyms_make_config_page'); // add the actual plugin to all the content we want to ubernym-ize add_filter('the_content', 'ubernyms_replacer', 18); add_filter('comment_text', 'ubernyms_replacer', 18); add_filter('sidebar_content', 'ubernyms_replacer', 18); ?>