Acronym (or Abbreviation) Formatter


This is the development page for the Acronym (or Abbreviation) Formatter.
 

This modification allows Wikka to automatically parse known acronyms and render them as <acronym> elements with titles, for example:
CSS - FAQ - HTML

The list of acronyms can be set by the WikiAdmin in a configuration file: each time an acronym is found in the page source matching one of the entries of this file, it is automatically rendered with the appropriate markup and expanded description.

Features

Current version: 0.3 (improved regex pattern)

To do


The code

Here's the list of files that you will have to create or modify (backup the original files before making any modification)

1. Modify ./formatters/wakka.php

original:
  1. // we're cutting the last <br />
  2. $text = preg_replace("/<br \/>$/","", $text);
  3.  
  4. echo ($text);
  5. wakka2callback('closetags');


modified:
  1. // we're cutting the last <br />
  2. $text = preg_replace("/<br \/>$/","", $text);
  3.  
  4. //render acronyms
  5. $text = $this->RenderAcronyms($text);
  6.  
  7. echo ($text);
  8. wakka2callback('closetags');


2. Modify wikka.php

Add the following function in the engine, for instance immediately before the VARIABLES section:

original:
  1.     // VARIABLES


modified:
  1.     /**
  2.      * Look up and return acronym definition from a configuration file.
  3.      *
  4.      * @author      {@link http://wikka.jsnx.com/DarTar DarioTaraborelli}
  5.      * @version     0.3
  6.      *
  7.      * @access      public
  8.      * @uses        GetConfigValue()
  9.      *
  10.      * @param       string  $text  source sent from the formatter
  11.      * @return      string $text source with known acronyms formatted as HTML elements
  12.      */
  13.  
  14.     function RenderAcronyms($text){
  15.         if (($this->GetConfigValue('enable_acronyms') == 1) && file_exists($this->GetConfigValue('acronym_table'))) {
  16.             // define constants
  17.             define('ACRONYM_PATTERN', '/\b([A-Z]{2,})\b/'); #matches sequences of 2 or more capital letters within word boundaries
  18.             define('FORMATTED_ACRONYM','<acronym title="%s">%s</acronym>'); # acronym can be replaced by abbrv         
  19.             // get acronym definitions
  20.             global $wikka_acronyms;
  21.             include($this->GetConfigValue('acronym_table'));
  22.             // replace known acronyms with HTML elements
  23.             $text = preg_replace_callback(
  24.                 ACRONYM_PATTERN,
  25.                 create_function(
  26.                     '$matches',
  27.                     'global $wikka_acronyms; return (is_array($wikka_acronyms) && array_key_exists($matches[0], $wikka_acronyms))? sprintf(FORMATTED_ACRONYM, $wikka_acronyms[$matches[0]], $matches[0]) : $matches[0];'
  28.                 ),
  29.                 $text);
  30.         }
  31.         return $text;
  32.     }
  33.  
  34.     // VARIABLES


3. Modify wikka.config.php

Add the following values to the configuration file:

    "enable_acronyms" => "1",
    "acronym_table" => "acronyms.php",


4. Create the acronym configuration file (acronyms.php)

Save the following code as acronyms.php in the root folder of your Wikka installation. You can obviously add as many acronym definitions as you like:

<?php

$wikka_acronyms = array(
    "ACL"   => "Access Control List",
    "API"   => "Application Program(ming) Interface",
    "CSS"   => "Cascading Style Sheets",
    "CVS"   => "Concurrent Version System",
    "DHTML" => "Dynamic HyperText Markup Language",
    "DOM"   => "Document Object Model",
    "DTD"   => "Document Type Definition",
    "FAQ"   => "Frequently Asked Questions",
    "FF"    => "Firefox",
    "GIF"   => "Graphics Interchange Format",
    "GPL"   => "GNU General Public License",
    "GUI"   => "Graphical User Interface",
    "HTML" => "HyperText Markup Language",
    "HTTP"  => "HyperText Transfer Protocol",
    "IE"    => "Internet Explorer",
    "PHP"    => "PHP hypertext processor",
    "RSS"   => "Rich Site Summary",         # or Really Simple Syndication or RDF Site Summary...
    "SQL"   => "Structured Query Language",
    "TOC"   => "Table of Contents",
);

?> 


5. Add some style

Some browsers (Mozilla/FF) automatically highlight acronym elements in the page. To make acronyms visible also in other browsers, paste the following in your stylesheet (default: ./css/wikka.css):

acronym {
    border-bottom: 1px dotted #333;
    cursor: help /*modifies the mouse pointer as a question mark*/
}



CategoryDevelopmentFormatters, CategoryUserContributions
There are 9 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki