syntax_plugin_lang.php - A PHP4 class that implements * a DokuWiki plugin to specify an area using a different * language than the remaining document. * *

* Markup a section of text to be using a different language, * lang 2-letter-lang-code *

 *	Copyright (C) 2005, 2007 DFG/M.Watermann, D-10247 Berlin, FRG
 *			All rights reserved
 *		EMail : <support@mwat.de>
 * 
*
* This program 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 3 of the * License, or (at your option) any later version.
* This software 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. *
* @author Matthias Watermann * @version $Id: syntax_plugin_lang.php,v 1.4 2007/08/15 12:36:19 matthias Exp $ * @since created 1-Sep-2005 */ class syntax_plugin_lang extends DokuWiki_Syntax_Plugin { /** * @publicsection */ //@{ /** * Tell the parser whether the plugin accepts syntax mode * $aMode within its own markup. * * @param $aMode String The requested syntaxmode. * @return Boolean TRUE unless $aMode is * plugin_lang (which would result in a * FALSE method result). * @public * @see getAllowedTypes() * @static */ function accepts($aMode) { return ('plugin_lang' != $aMode); } // accepts() /** * Connect lookup pattern to lexer. * * @param $aMode String The desired rendermode. * @public * @see render() */ function connectTo($aMode) { // See http://www.w3.org/TR/html401/struct/dirlang.html#h-8.1.1; // better (specialized) REs are used in 'handle()' method. $this->Lexer->addEntryPattern( '\x3Clang\s+[a-z\-A-Z0-9]{2,})?\s*\x3E\s*(?=(?s).*?\x3C\x2Flang\x3E)', $aMode, 'plugin_lang'); } // connectTo() /** * Get an associative array with plugin info. * *

* The returned array holds the following fields: *

*
author
Author of the plugin
*
email
Email address to contact the author
*
date
Last modified date of the plugin in * YYYY-MM-DD format
*
name
Name of the plugin
*
desc
Short description of the plugin (Text only)
*
url
Website with more information on the plugin * (eg. syntax description)
*
* @return Array Information about this plugin class. * @public * @static */ function getInfo() { return array( 'author' => 'Matthias Watermann', 'email' => 'support@mwat.de', 'date' => '2007-08-15', 'name' => 'LANGuage Syntax Plugin', 'desc' => 'Markup a text area using another language', 'url' => 'http://wiki.splitbrain.org/plugin:lang'); } // getInfo() /** * Where to sort in? * * @return Integer 498 (doesn't really matter). * @public * @static */ function getSort() { return 498; } // getSort() /** * Get the type of syntax this plugin defines. * * @return String 'formatting'. * @public * @static */ function getType() { return 'formatting'; } // getType() /** * Handler to prepare matched data for the rendering process. * *

* The $aState parameter gives the type of pattern * which triggered the call to this method: *

*
*
DOKU_LEXER_ENTER
*
a pattern set by addEntryPattern()
*
DOKU_LEXER_MATCHED
*
a pattern set by addPattern()
*
DOKU_LEXER_EXIT
*
a pattern set by addExitPattern()
*
DOKU_LEXER_SPECIAL
*
a pattern set by addSpecialPattern()
*
DOKU_LEXER_UNMATCHED
*
ordinary text encountered within the plugin's syntax mode * which doesn't match any pattern.
*
* @param $aMatch String The text matched by the patterns. * @param $aState Integer The lexer state for the match. * @param $aPos Integer The character position of the matched text. * @param $aHandler Object Reference to the Doku_Handler object. * @return Array Index [0] holds the current * $aState, index [1] the match prepared for * the render() method. * @public * @see render() * @static */ function handle($aMatch, $aState, $aPos, &$aHandler) { if (DOKU_LEXER_ENTER == $aState) { $hits = array(); // RFC 3066, "2. The Language tag", p. 2f. // Language-Tag = Primary-subtag *( "-" Subtag ) if (preg_match('|\s+([a-z]{2,3})\s*>|i', $aMatch, $hits)) { // primary _only_ (most likely to be used) return array($aState, $hits[1]); } // if if (preg_match('|\s+([a-z]{2,3}\-[a-z0-9]{2,})\s*>|i', $aMatch, $hits)) { // primary _and_ subtag return array($aState, $hits[1]); } // if if (preg_match('|\s+([ix]\-[a-z0-9]{2,})\s*>|i', $aMatch, $hits)) { // 1-letter primary _and_ subtag return array($aState, $hits[1]); } // if if (preg_match('|\s+([a-z]{2,3})\-.*\s*>|i', $aMatch, $hits)) { // convenience: accept primary with empty subtag return array($aState, $hits[1]); } // if // invalid language specification return array($aState, FALSE); } // if return array($aState, $aMatch); } // handle() /** * Add exit pattern to lexer. * * @public */ function postConnect() { $this->Lexer->addExitPattern('\x3C\x2Flang\x3E', 'plugin_lang'); } // postConnect() /** * Handle the actual output creation. * *

* The method checks for the given $aFormat and returns * FALSE when a format isn't supported. $aRenderer * contains a reference to the renderer object which is currently * handling the rendering. The contents of $aData is the * return value of the handle() method. *

* @param $aFormat String The output format to generate. * @param $aRenderer Object A reference to the renderer object. * @param $aData Array The data created by the handle() * method. * @return Boolean TRUE if rendered successfully, or * FALSE otherwise. * @public * @see handle() * */ function render($aFormat, &$aRenderer, &$aData) { if ('xhtml' != $aFormat) { return FALSE; } // if static $VALID = TRUE; // flag to notice invalid markup switch ($aData[0]) { case DOKU_LEXER_ENTER: if ($aData[1]) { $aRenderer->doc .= ''; } else { $VALID = FALSE; } // if return TRUE; case DOKU_LEXER_UNMATCHED: $aRenderer->doc .= str_replace(array('&','<', '>'), array('&', '<', '>'), $aData[1]); return TRUE; case DOKU_LEXER_EXIT: if ($VALID) { $aRenderer->doc .= ''; } else { $VALID = TRUE; } // if default: return TRUE; } // switch } // render() //@} } // class syntax_plugin_lang } // if ?>