<?php
/*
CZECH TYPO

This function replaces some widely used symbol combinations
with corresponding typographic entities for czech language:
... => &hellip;
--  => &ndash;
--- => &mdash;
""  => &bdquo;&ldquo;
>>  => &raquo;
<<  => &laquo;
(C) => &copy;
(R) => &reg;
(TM)=> &trade;
v $ => v&nbsp;$ (for A, I, i, K, k, S, s, V, v, O, o, U, u, Z, z)
It will not do any replaces between '<' and '>' characters
and inside PRE and CODE containers.

Usage:
$result = czechtypo($text);

Copyright (c) 2007 Radek Brich (radek at brich.org)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

$czechtypo_version = '2.0';

// checks for white-space characters
function czechtypo_isspace($c)
{
        return ($c == ' ' || $c == '\n' || $c == '\r' || $c == '\t');
}

// checks for alphanumeric characters
function czechtypo_isalnum($c)
{
	return (($c >= 'A' && $c <= 'Z') || ($c >= 'a' && $c <= 'z')
		|| ($c >= '0' && $c <= '9') || $c == '_' || ord($c) >= 128);
}

// read a token -- a HTML tag, continuous dashes or dots,
// or just one char
function czechtypo_readtoken($text, &$i)
{
	$state = 0;
	$output = '';
	while ($state < 20 && $i < strlen($text))
	{
		$c = $text[$i++];
		switch ($state)
		{
			// start
			case 0:
			if ($c == '<')
				$state = 1;
			else
			if ($c == '>')
				$state = 3;
			else
			if ($c == '-')
				$state = 4;
			else
			if ($c == '.')
				$state = 5;
			else
			if ($c == '(')
				$state = 6;
			else
			if (czechtypo_isspace($c))
				$state = 9;
			else
			if (czechtypo_isalnum($c))
				$state = 10;
			else
				$state = 20;
			break;

			// <
			case 1:
			if ($c == '<' || $c == '>')
				$state = 20;
			else
				$state = 2;
			break;

			// tag
			case 2:
			if ($c == '>')
				$state = 20;
			break;

			// >
			case 3:
			if ($c == '>')
				$state = 20;
			else
				$state = 21;
			break;

			// dashes
			case 4:
			if ($c != '-')
				$state = 21;
			break;

			// dots
			case 5:
			if ($c != '.')
				$state = 21;
			break;

			// left parentesis
			case 6:
			if ($c == 'T')
				$state = 7;
			else
			if ($c == 'C' || $c == 'R')
				$state = 8;
			else
				$state = 21;
			break;

			// (T
			case 7:
			if ($c == 'M')
				$state = 8;
			else
				$state = 21;
			break;

			// (C, (R, (TM
			case 8:
			if ($c == ')')
				$state = 20;
			else
				$state = 21;
			break;

			// spaces
			case 9:
			if (!czechtypo_isspace($c))
				$state = 21;
			break;

			// a word
			case 10:
			if (!czechtypo_isalnum($c))
				$state = 21;
			break;
		}
		if ($state == 21)
			$i--;
		else
			$output .= $c;
	}
	return $output;
}

// main function
function czechtypo($text)
{
	$state = 0;
	$quot = 0;
	$i = 0;
	$output = '';
	while (($token = czechtypo_readtoken($text, $i)) != '')
	{
		switch ($state)
		{
			// start
			case 0:
			// after one letter long preposition
			case 1:
			if (!strcmp($token, '--'))
				$output .= '&ndash;';
			else
			if (!strcmp($token, '---'))
				$output .= '&mdash;';
			else
			if (!strcmp($token, '...'))
				$output .= '&hellip;';
			else
			if (!strcmp($token, '(C)'))
				$output .= '&copy;';
			else
			if (!strcmp($token, '(R)'))
				$output .= '&reg;';
			else
			if (!strcmp($token, '(TM)'))
				$output .= '&trade;';
			else
			if (!strcmp($token, '>>'))
				$output .= '&raquo;';
			else
			if (!strcmp($token, '<<'))
				$output .= '&laquo;';
			else
			if ($token == '"')
			{
				if ($quot)
					$output .= '&ldquo;';
				else
					$output .= '&bdquo;';
				$quot = !$quot;
			}
			else
			if ($state == 1)
			{
				if ($token == ' ')
					$output .= '&nbsp;';
				else
					$output .= $token;
				$state = 0;
			}
			else
				$output .= $token;

			if (strlen($token) == 1
			&& strstr('AIiKkSsVvOoUuZz', $token))
				$state = 1;
			else
			if (stristr($token, '<pre')
			|| stristr($token, '<code'))
				$state = 2;
			break;

			// inside PRE or CODE
			case 2:
			if (stristr($token, '/pre')
			|| stristr($token, '/code'))
				$state = 0;
			$output .= $token;
			break;
		}
	}
	return $output;
}

?>