/** ConvertToPreHTML
  * 
  * @access public
  * @param string Text
  * @return string to HTML convertet Code
  */
 function ConvertToPreHTML($Text)
 {
     $Text = stripslashes($Text) . " ";
     // make all EOL-sings equal
     $Text = preg_replace("!(\r\n)|(\r)!", "\n", $Text);
     // convert htmlentities to
     $Text = str_replace('ä', 'ä', $Text);
     $Text = str_replace('Ä', 'Ä', $Text);
     $Text = str_replace('ü', 'ü', $Text);
     $Text = str_replace('Ü', 'Ü', $Text);
     $Text = str_replace('ö', 'ö', $Text);
     $Text = str_replace('Ö', 'Ö', $Text);
     $Text = str_replace('ß', 'ß', $Text);
     $Text = str_replace('>', '>', $Text);
     $Text = str_replace('&lt;', '<', $Text);
     // remove comments
     $Text = preg_replace("/<!--(.+?)-->/s", '', $Text);
     // extract all text we won't convert <plain>...TEXT...</plain>
     preg_match_all("/<plain>(.+?)<\\/plain>/s", $Text, $matches);
     $plains = array();
     foreach ($matches[1] as $key => $match) {
         $plains[$key] = $matches[1][$key];
         $Text = str_replace($matches[0][$key], '[plain]%' . $key . '%[/plain]', $Text);
     }
     // catch all parts, which should used as blocks <block>...TEXT...</block>
     preg_match_all("/<block>(.+?)<\\/block>/s", $Text, $matches);
     $blocks = array();
     foreach ($matches[1] as $key => $match) {
         $blocks[$key] = $matches[1][$key];
         $Text = str_replace($matches[0][$key], '[block]%' . $key . '%[/block]', $Text);
     }
     // 'repair' all urls (with no http:// but a www or ftp)
     $Text = preg_replace("/(\\ |\\r|\\n|\\[)(www|ftp)\\.(.+?)\\.([a-zA-Z.]{2,6}(|\\/.+?))/s", '$1' . "http://\$2.\$3.\$4", $Text);
     // remove all html characters
     $Text = htmlspecialchars($Text);
     // fixes for some security bugs
     $Text = str_replace("\\r", "\r", $Text);
     $Text = str_replace("\\n", "\n", $Text);
     $Text = preg_replace("!(\r\n)|(\r)!", "\n", $Text);
     $Text = preg_replace("#\\\\(\\ |\\r|\\n)#s", "\n<br />\n", $Text);
     // catch all email-adresses which should be convertet to links (<*****@*****.**>)
     preg_match_all("#\\&lt\\;([a-z0-9\\._-]+?)\\@([\\w\\-]+\\.[a-z0-9\\-\\.]+\\.*[\\w]+)\\&gt\\;#s", $Text, $emails);
     // catch all images
     preg_match_all("#\\{\\{(.+?)\\}\\}#s", $Text, $images);
     $imagesData = array();
     foreach ($images[1] as $key => $image) {
         $imagesData[$key] = TextActions::MakeImage($image);
         $Text = str_replace('{{' . $image . '}}', '[img]%' . $key . '%[/img]', $Text);
     }
     // allowed auto-link protocols
     $protos = "http|ftp|https";
     // convert urls to links http://www.domain.com to [[http://www.domain.com|www.domain.com]]
     $Text = preg_replace("#(?<!\\[\\[)({$protos}):\\/\\/(.+?)(\\ |\\n)#s", '[[$1://$2|$2]]$3', $Text);
     $Text = preg_replace("#\\[\\[({$protos}):\\/\\/([a-z0-9\\-\\.]+)\\]\\]#s", '[[$1://$2|$2]]', $Text);
     // FIXME: make configurable
     $antibot = EMAIL_ANTISPAM_TEXT;
     // convert catched emails into the link format [[email@example.com]]
     foreach ($emails[0] as $key => $email) {
         $Text = str_replace('&lt;' . $emails[1][$key] . '@' . $emails[2][$key] . '&gt;', '[[' . $emails[1][$key] . '@' . $emails[2][$key] . '|' . TextActions::EmailAntispam($emails[1][$key] . '@' . $emails[2][$key], $antibot) . ']]', $Text);
     }
     // catch all links
     preg_match_all("#\\[\\[(.+?)\\]\\]#s", $Text, $links);
     $link_list = array();
     $linkNr = 1;
     // replace all links with a short uniqe id to replace them later back
     foreach ($links[1] as $link) {
         $link_list[$linkNr] = $link;
         $Text = str_replace("[[{$link}]]", "[[%{$linkNr}%]]", $Text);
         $linkNr++;
     }
     // convert all **text** to <strong>text</strong> => Bold
     $Text = preg_replace("/\\*\\*(.+?)\\*\\*/s", "<strong>\$1</strong>", $Text);
     // convert all //text// to <em>text</em> => Italic
     $Text = preg_replace("/\\/\\/(.+?)\\/\\//s", "<em>\$1</em>", $Text);
     // convert all __text__ to <u>text</u> => Underline
     $Text = preg_replace("/__(.+?)__/s", "<u>\$1</u>", $Text);
     // convert ====== text ====== to a header <h5>
     $Text = preg_replace("#======\\ (.+?)\\ ======#s", "\n\n<h6>\$1</h6>\n", $Text);
     // convert ===== text ===== to a header <h5>
     $Text = preg_replace("#=====\\ (.+?)\\ =====#s", "\n\n<h5>\$1</h5>\n", $Text);
     // convert ==== text ==== to a header <h4>
     $Text = preg_replace("#====\\ (.+?)\\ ====#s", "\n\n<h4>\$1</h4>\n", $Text);
     // convert === text === to a header <h3>
     $Text = preg_replace("#===\\ (.+?)\\ ===#s", "\n\n<h3>\$1</h3>\n", $Text);
     // convert == text == to a header <h2>
     $Text = preg_replace("#==\\ (.+?)\\ ==#s", "\n\n<h2>\$1</h2>\n", $Text);
     // convert <center>text</center> to <div class="center">text</div>
     $Text = preg_replace("#&lt;center&gt;(.+?)&lt;/center&gt;#s", "</p><div class=\"center\">\$1</div><p>", $Text);
     // convert ({text}{text}) to two colums
     $Text = preg_replace("#\\(\\{([^\\}\\{]*?)[\r\n ]*\\}\\{([^\\}\\{]*?)[\r\n ]*\\}\\)#mu", "</p>\n<div class=\"column ctwo\">\n<p>\n\$1\n</p>\n</div>\n<div class=\"column ctwo\">\n<p>\n\$2\n</p>\n</div>\n<p class=\"after_column\"/>\n<p>\n", $Text);
     // convert ({text}{text}{text}) to tree colums
     $Text = preg_replace("#\\(\\{([^\\}\\{]*?)[\r\n ]*\\}\\{([^\\}\\{]*?)[\r\n ]*\\}\\{([^\\}\\{]*?)[\r\n ]*\\}\\)#mu", "</p>\n<div class=\"column ctree\">\n<p>\n\$1\n</p>\n</div>\n<div class=\"column ctree\">\n<p>\n\$2\n</p>\n</div><div class=\"column ctree\">\n<p>\n\$3\n</p>\n</div>\n<p class=\"after_column\">\n<p>\n", $Text);
     // paste links into the text
     foreach ($link_list as $linkNr => $link) {
         if (preg_match("#^(.+?)\\|(.+?)\$#i", $link, $link2)) {
             $Text = str_replace("[[%{$linkNr}%]]", "<a href=\"" . TextActions::MakeLink($link2[1]) . "\">" . $link2[2] . "</a>", $Text);
         } else {
             $Text = str_replace("[[%{$linkNr}%]]", "<a href=\"" . TextActions::MakeLink($link) . "\">" . $link . "</a>", $Text);
         }
     }
     $lines = explode("\n", $Text);
     $lines[] = "\n";
     $tempText = '';
     $outputText = '';
     $state = LINE_STATE_NONE;
     $lastState = LINE_STATE_NONE;
     foreach ($lines as $line) {
         $lastState = $state;
         // Unsorted lists: *
         if (TextActions::StartsWith('* ', $line)) {
             $state = LINE_STATE_ULIST;
         } else {
             if (TextActions::StartsWith('# ', $line)) {
                 $state = LINE_STATE_OLIST;
             } else {
                 if (TextActions::StartsWith('^', $line) || TextActions::StartsWith('|', $line)) {
                     $state = LINE_STATE_TABLE;
                 } else {
                     if (TextActions::StartsWith('<h', $line)) {
                         $state = LINE_STATE_HEADER;
                     } else {
                         if (TextActions::StartsWith('&gt;', $line) || TextActions::StartsWith('=&gt;', $line)) {
                             $state = LINE_STATE_DEFINITON;
                         } else {
                             if ($line == "\n" || $line == "") {
                                 $state = LINE_STATE_NONE;
                             } else {
                                 $state = LINE_STATE_TEXT;
                             }
                         }
                     }
                 }
             }
         }
         if ($lastState == $state) {
             $tempText .= "\t" . $line . "\n";
         } else {
             // convert the specific parts
             switch ($lastState) {
                 case LINE_STATE_TEXT:
                     $outputText .= TextActions::ConvertText($tempText);
                     break;
                 case LINE_STATE_ULIST:
                     $outputText .= TextActions::ConvertUList($tempText);
                     break;
                 case LINE_STATE_OLIST:
                     $outputText .= TextActions::ConvertOList($tempText);
                     break;
                 case LINE_STATE_TABLE:
                     $outputText .= TextActions::ConvertTable($tempText);
                     break;
                 case LINE_STATE_HEADER:
                     $outputText .= $tempText;
                     break;
                 case LINE_STATE_DEFINITON:
                     $outputText .= TextActions::ConvertDefinition($tempText);
                     break;
             }
             $tempText = "\t" . $line . "\n";
         }
     }
     foreach ($blocks as $key => $match) {
         $outputText = str_replace('[block]%' . $key . '%[/block]', TextActions::ConvertToPreHTML($match), $outputText);
     }
     // paste plain-parts back
     foreach ($plains as $key => $match) {
         $outputText = str_replace('[plain]%' . $key . '%[/plain]', $match, $outputText);
     }
     foreach ($imagesData as $key => $imgHtml) {
         $outputText = str_replace('[img]%' . $key . '%[/img]', $imgHtml, $outputText);
     }
     // remove the spaces which are not necessary
     $outputText = preg_replace('/\\ \\ +/', ' ', $outputText);
     $outputText = str_replace(' -- ', ' &ndash; ', $outputText);
     $outputText = str_replace(' --- ', ' &mdash; ', $outputText);
     $outputText = str_replace('(c)', '&copy;', $outputText);
     $outputText = str_replace('(r)', '&reg;', $outputText);
     $outputText = str_replace('ä', '&auml;', $outputText);
     $outputText = str_replace('Ä', '&Auml;', $outputText);
     $outputText = str_replace('ü', '&uuml;', $outputText);
     $outputText = str_replace('Ü', '&Uuml;', $outputText);
     $outputText = str_replace('ö', '&ouml;', $outputText);
     $outputText = str_replace('Ö', '&Ouml;', $outputText);
     $outputText = str_replace('ß', '&szlig;', $outputText);
     return $outputText;
 }