                                                                                                                                                                                                                                                


<?php
//-->-->-->-->-->-->-->-->
//-- Description:
//--   The purpose of this function is to transform a 'string' of text
//--   into some kind of meaningful HTML. The 'string' is considered to
//--   be a 'message' which one of the PHP scripts is echo-ing to the 
//--   requesting client. The text may contain special codes or 'structures'
//--   which indicate to this transformation script the way in which the
//--   text should be formatted in the HTML result. For example if a word
//--   begins with the text 'http://' then it will be made into an HTML
//--   hyperlink, which will transport the clicker to the target URL or 
//--   web-page. Or, if the line begins with an equals sign '=' then that
//--   line will be made into an HTML level 1 heading, which will appear
//--   very large and bold in the resulting web-page.
//--
//--
//-- Notes:
//--   I created this function by basically copying the function
//--   fnDocumentToHtml. fnFaqToHtml, fnJournalToHtml are also very 
//--   similar to the present function.
//--
//-- History:
//--
//-- Dependencies:
//--   This dependency list is not particularly reliable. It should be
//--   generated automatically I suppose. At the moment, it is maintained
//--   by hand.
//--
//--   fnHyperlinkPlainText
//--   fnReduplicateCharacters
//--   fnUnduplicateCharacters
//--   fnCodeBlockToHtml
//--   fnCodeBlockFromHtml
//--   fnListBlockToHtml
//--
//-- See Also:
//--
//--   fnHyperlinkPlainText.php 
//--     has explanations as to what sort of 
//--     plain text structures will be hyperlinked. 
//--
//-- 
//--   retrovox-notepad.php    
//--     A script to allow editing of files located on a server using a web
//--     browser.
//--   webpad.php
//--     A script to allow editing of 'documents' which are located on 
//--     a server running PHP and probably the Apache web-server. While
//--     the 'retrovox-notepad.php' script is designed to edit any file on
//--     the server, the 'webpad.php' script is only able to edit a small
//--     subsection of the files on the server, namely, those files which
//--     are considered to be 'documents' or 'webpages'.
//--
//-- Author:
//--   matth3wbishop@yahoo.com
//--   http://www.geocities.com/matth3wbishop/
//--

function fnMessageToHtml($sPlainText)
{
  $sReturn = $sPlainText;

  //-- On lines that have nothing but spaces, delete the spaces
  //$sReturn = preg_replace("#\n\s*\n#im", "\n\n", $sReturn);

  //-- Entitize special HTML characters such as < and > so that these
  //-- characters will not be considered part of the resulting HTML.
  //-- We have to do this first so that the HTML which we later insert
  //-- (such as hyperlinks) does not also get entitized. I
  //-- might also have to entitize accented characters such as ???
  $sReturn = htmlspecialchars($sReturn);

  //-- Trailing space serves no purpose.
  $sReturn = preg_replace("#[ \t]*([\n\r])#m", "$1", $sReturn);
  
  //-- Try to allow the delimiters -->> and --<< to stop any further 
  //-- transformations on the plain text (by placing the text with HTML
  //-- <pre> tags and then doubling every letter
  $sReturn = str_replace("--&lt;&lt;", "</pre>", $sReturn);

  $sReturn = str_replace("--&gt;&gt;", "<pre>", $sReturn);
  $sReturn = 
    preg_replace(
         "#<pre>((.|\n)*?)</pre>#ime", 
         "'<pre>'.fnCodeBlockToHtml('$1').'</pre>'", $sReturn);
  

  $sReturn = 
    preg_replace(
         "#^\s*([a1Iu]-(.|\n)*?)\n\s*\n#ime", 
         "''.fnListBlockToHtml('$1').'\n\n'", $sReturn);


  //-- Make empty lines delimite HTML paragraphs
  $sReturn = preg_replace("#^\s*$#im", "</p><p>", $sReturn);

  //-- Make the title into a first level heading  
  $sReturn = 
    preg_replace("#^[ ]*=(.*)#m", "<center><h1>$1</h1></center><dl>", $sReturn);

  $sHeadings = $sReturn;

  $sHeadings = 
     preg_replace("#^[ \t]*#m", "", $sHeadings);

  $sHeadings = 
     preg_replace('#^[^\*].*$#im', '', $sHeadings);

  $sHeadings = 
     preg_replace("#\n\s*\n#m", "\n", $sHeadings);

  //-- Get rid of the leading 'star' characters for each of the questions
  $sHeadings = 
     preg_replace("#^\s*\*\s*#m", "", $sHeadings);

  $sHeadings = 
     preg_replace("#\s*$#m", "", $sHeadings);

  $sHeadings = 
     preg_replace("#^(.*)$#m", "<li><a href = '#$1'>$1</a>" , $sHeadings);

  $sHeadings = 
     "<a name = 'toc'></a><br><ol type = '1'>".$sHeadings."</ol>";

  //echo "<pre>[$sHeadings]</pre>";
  //echo $sHeadings;
  $sReturn = preg_replace(
      "#^\s*\*\s*([^\n\r]*)#m", 
      "<dt><h4><a name = '$1'>$1</a>&nbsp;<a href = '#toc'>[toc]</a></h4></dt><dd>", 
      $sReturn);

  $sReturn = $sHeadings."\n".$sReturn;

  $sReturn = fnHyperlinkPlainText($sReturn);


  //-- Make a tab into 2 HTML spaces
  $sReturn = preg_replace("#\t#im", "&nbsp;&nbsp;", $sReturn);


  //-- get rid of the [*faq*] meta-tag which is used to tell the transformation
  //-- system that the current document should be transformed according to the 
  //-- rules for a journal for an faq style document.

  //-- This </dl> rubbish is a terrible hack. It is assuming that 
  //-- the faq flag occurs right at the end of the document and that
  //-- there is some kind of definition list in the doc. Very ugly.
  $sReturn = preg_replace("#^\s*\[\*faq\*\]\s*$#im", "</dl>", $sReturn);

  $sReturn = 
    preg_replace(
         "#<pre>((.|\n)*?)</pre>#ime", 
         "'<pre>'.fnCodeBlockFromHtml('$1').'</pre>'", $sReturn);

  return $sReturn;
} //-- function fnDocumentToHtml 
     

//--<--<--<--<--<--<--<--<
?>
                                                                                                                                                                                                                                                                                