[Tm-svn] SF.net SVN: tigermouse: [193] lib/translation
Status: Alpha
Brought to you by:
strongier
From: <str...@us...> - 2008-01-20 14:48:39
|
Revision: 193 http://tigermouse.svn.sourceforge.net/tigermouse/?rev=193&view=rev Author: strongier Date: 2008-01-20 06:48:40 -0800 (Sun, 20 Jan 2008) Log Message: ----------- Wiki translator now can be appended with custom translation modes Modified Paths: -------------- lib/translation/ITranslator.interface.php lib/translation/MD5Translator.class.php lib/translation/WikiTranslator.class.php lib/translation/WikiTranslatorHeadingMode.class.php lib/translation/WikiTranslatorListMode.class.php lib/translation/WikiTranslatorMode.class.php lib/translation/WikiTranslatorPreformattedMode.class.php lib/translation/WikiTranslatorTextMode.class.php lib/translation/WikiTranslatorToC.class.php lib/translation/WikiTranslatorToCMode.class.php Modified: lib/translation/ITranslator.interface.php =================================================================== --- lib/translation/ITranslator.interface.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/ITranslator.interface.php 2008-01-20 14:48:40 UTC (rev 193) @@ -1,13 +1,30 @@ <?php +/** + * Translation interface + * + * Defined two way translation: model data to view data and the opposite direction. + * + * @version DR1.7 + */ interface ITranslator { + /** + * Translates from view to model data + * + * Use this method to convert data from form fields or other user input. + */ public function vtom($value); + /** + * Translates from model to view data + * + * Use this method to convert data from database or other source. + */ public function mtov($value); Modified: lib/translation/MD5Translator.class.php =================================================================== --- lib/translation/MD5Translator.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/MD5Translator.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -4,12 +4,18 @@ + /** + * Returns MD5 hash of user provided string + */ public function vtom($value) { return md5($value); } + /** + * Returns null value to prevent exposing hash to user interface + */ public function mtov($value) { return null; } Modified: lib/translation/WikiTranslator.class.php =================================================================== --- lib/translation/WikiTranslator.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslator.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -1,5 +1,15 @@ <?php +/** + * Wikipedia formatting to HTML translator + * + * Parses input and return formatted HTML output based on Wiki formatting. + * Implements State design pattern to switch between editing modes. + * To add custom translator mode use register() method. + * To remove existing mode use unregister() method. + * + * @version DR1.7 + */ class WikiTranslator implements ITranslator { @@ -7,49 +17,103 @@ protected $mode; protected $out = array(); protected $toc; + protected $registeredModes = array( + 'WikiTranslatorHeadingMode', + 'WikiTranslatorListMode', + 'WikiTranslatorPreformattedMode', + 'WikiTranslatorToCMode' + ); + /** + * Constructor + * + * Creates Table of Contents object for use with WikiTranslatorHeadingMode objects. + */ public function __construct() { $this->toc = new WikiTranslatorToC(); } + /** + * Registers translator mode class name for inclusion + * + * Use this method to add custom modes. Mode class must implement + * public static method buidIfMatch(string, WikiTranslatorToC) + * that returns mode instance if passed string matches activation condition. + */ + public function register($className) { + if (!in_array($className, $this->registeredModes)) { + $this->registeredModes[] = $className; + } + } + + + + /** + * Removes registered translator mode class name + * + * Use this method to exclude some Wiki syntax elements, + * e.g. unregister('WikiTanslatorHeadingMode') will ignore all headings. + */ + public function unregister($className) { + if (false !== ($pos = array_search($className, $this->registeredModes))) { + unset($this->registeredModes[$pos]); + } + } + + + + /** + * Does no converion from view to model, saves as Wiki text + */ public function vtom($value) { return $value; } + /** + * Converts from Wiki text to HTML + */ public function mtov($value) { foreach (explode("\n", $value) as $ln) { + // unset mode if empty line given and proceed to next line if ($ln == '') { + $this->mode = null; continue; } - if ($ln[0] == '*' and $ln[1] == ' ') { - $this->setMode(new WikiTranslatorListMode()); - } elseif ($ln[0] == ' ' and $ln[1] == ' ') { - $this->setMode(new WikiTranslatorPreformattedMode()); - } elseif (preg_match('/^==== .* ====$/', $ln)) { - $this->setMode(new WikiTranslatorHeadingMode(3, $this->toc)); - } elseif (preg_match('/^=== .* ===$/', $ln)) { - $this->setMode(new WikiTranslatorHeadingMode(2, $this->toc)); - } elseif (preg_match('/^== .* ==$/', $ln)) { - $this->setMode(new WikiTranslatorHeadingMode(1, $this->toc)); - } elseif ($ln == '*TOC*') { - $this->setMode(new WikiTranslatorToCMode($this->toc)); - } else { + $mode = null; + // perform mode recognition + foreach ($this->registeredModes as $className) { + eval('$mode = ' . $className . '::buildIfMatch($ln, $this->toc);'); + if ($mode) { + $this->setMode($mode); + break; + } + } + // if no mode matched the string, use text mode + if (!$mode) { $this->setMode(new WikiTranslatorTextMode()); } - $this->put($ln); + $this->out[] = $this->mode->parse($ln); } + // close pending modes and return HTML + $this->setMode(new WikiTranslatorTextMode()); return implode("\n", $this->out); } - public function setMode(WikiTranslatorMode $mode) { + + /** + * Switches parser mode to handle given Wiki syntax element + * + * @param WikiTranslatorMode + */ + protected function setMode(WikiTranslatorMode $mode) { if (get_class($mode) == get_class($this->mode)) { return; } @@ -62,12 +126,6 @@ - public function put($ln) { - $this->out[] = $this->mode->parse($ln); - } - - - } // class WikiTranslator ?> \ No newline at end of file Modified: lib/translation/WikiTranslatorHeadingMode.class.php =================================================================== --- lib/translation/WikiTranslatorHeadingMode.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslatorHeadingMode.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -26,6 +26,27 @@ + public static function buildIfMatch($ln, WikiTranslatorToC $toc) { + if (substr($ln, 0, 5) == '==== ' and substr($ln, -5) == ' ====') { + static $m3; + $m3 or ($m3 = new WikiTranslatorHeadingMode(3, $toc)); + return $m3; + } + if (substr($ln, 0, 4) == '=== ' and substr($ln, -4) == ' ===') { + static $m2; + $m2 or ($m2 = new WikiTranslatorHeadingMode(2, $toc)); + return $m2; + } + if (substr($ln, 0, 3) == '== ' and substr($ln, -3) == ' ==') { + static $m1; + $m1 or ($m1 = new WikiTranslatorHeadingMode(1, $toc)); + return $m1; + } + return null; + } + + + } // class WikiTranslatorHeadingMode ?> \ No newline at end of file Modified: lib/translation/WikiTranslatorListMode.class.php =================================================================== --- lib/translation/WikiTranslatorListMode.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslatorListMode.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -4,14 +4,24 @@ + protected $ordered; + + + + public function __construct($ordered) { + $this->ordered = $ordered; + } + + + public function open() { - return '<ul>'; + return $this->ordered ? '<ol>' : '<ul>'; } public function close() { - return '</ul>'; + return $this->ordered ? '</ol>' : '</ul>'; } @@ -22,6 +32,17 @@ + public static function buildIfMatch($ln, WikiTranslatorToC $toc) { + if ($ln[0] == '*' and $ln[1] == ' ') { + return new WikiTranslatorListMode(false); + } elseif ($ln[0] == '#' and $ln[1] == ' ') { + return new WikiTranslatorListMode(true); + } + return null; + } + + + } // class WikiTranslatorListMode ?> \ No newline at end of file Modified: lib/translation/WikiTranslatorMode.class.php =================================================================== --- lib/translation/WikiTranslatorMode.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslatorMode.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -23,6 +23,17 @@ + /** + * Matches given string and returns mode instance + * + * @param string + * @param WikiTranslatorToC + * @return null or WikiTanslatorMode + */ + abstract public static function buildIfMatch($ln, WikiTranslatorToC $toc); + + + } // class WikiTranslatorMode ?> \ No newline at end of file Modified: lib/translation/WikiTranslatorPreformattedMode.class.php =================================================================== --- lib/translation/WikiTranslatorPreformattedMode.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslatorPreformattedMode.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -22,6 +22,17 @@ + public static function buildIfMatch($ln, WikiTranslatorToC $toc) { + if ($ln[0] == ' ' and $ln[1] == ' ') { + static $m; + $m or ($m = new WikiTranslatorPreformattedMode()); + return $m; + } + return null; + } + + + } // class WikiTranslationPreformattedMode ?> \ No newline at end of file Modified: lib/translation/WikiTranslatorTextMode.class.php =================================================================== --- lib/translation/WikiTranslatorTextMode.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslatorTextMode.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -10,6 +10,12 @@ + public static function buildIfMatch($ln, WikiTranslatorToC $toc) { + return false; + } + + + } // class WikiTranslatorTextMode ?> \ No newline at end of file Modified: lib/translation/WikiTranslatorToC.class.php =================================================================== --- lib/translation/WikiTranslatorToC.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslatorToC.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -1,6 +1,6 @@ <?php -class WikiTranslatorToC extends WikiTranslatorMode { +class WikiTranslatorToC { Modified: lib/translation/WikiTranslatorToCMode.class.php =================================================================== --- lib/translation/WikiTranslatorToCMode.class.php 2008-01-20 02:19:02 UTC (rev 192) +++ lib/translation/WikiTranslatorToCMode.class.php 2008-01-20 14:48:40 UTC (rev 193) @@ -32,6 +32,15 @@ + public static function buildIfMatch($ln, WikiTranslatorToC $toc) { + if ($ln == '*TOC*') { + return new WikiTranslatorToCMode($toc); + } + return null; + } + + + } // class WikiTranslationToCMode ?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |