                                                                                                                                    


<?php
//-->-->-->-->-->-->-->-->
//--Description: This was added with vim
//--   The purpose of this function is to transform a string of text
//--   into a functional and well formatted web (HTML) page
//--   The soruce text is expected to
//--   have as little 'mark-up' imbedded within it as is possible for 
//--   the given application. 
//--
//--History:
//--   Previous efforts should be viewable at URL's such as 
//--   http://www.geocities.com/matth3wbishop/ and (if it still exists)
//--   http://www.ella-associates.org/alexis-docs/
//--
//--   The overall purpose of this effort is to attempt to allow an
//--   author to write texts in a plain text editor such as 
//--   Microsoft Notepad or Vi or Vim or Emacs or Nano etc and for those
//--   written texts to be transformed into readable and function HTML
//--   and even possibly DocBook XML. 
//--
//-- Please see the file 
//--   fnHyperlinkPlainText.php for explanations as to what sort of 
//--   plain text structures will be hyperlinked.
//--
//-- Notes:
//--   I will rename this 'fnDocumentToHtml.php'
//--
//-- Dependencies:
//--   fnHyperlinkPlainText
//--  
//--   fnCodeBlockToHtml
//--     This formats text which is between HTML pre tags
//--
//--   fnReduplicateCharacters
//--   fnUnduplicateCharacters
//--
//--

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

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

  //-- Trailing space serves no purpose.
  $sReturn = 
    preg_replace(
     "#\s*\n#im", "\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);
  
  //-- 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);


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

  $sHeadings = 
     preg_replace('#^.*[a-z].*$#m', '', $sReturn);

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

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

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

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

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

  //echo "<pre>[$sHeadings]</pre>";
  //echo $sHeadings;
  $sReturn = 
    preg_replace(
      "#^\s*([^a-z]{4,})*\s*$#m", 
      "<h3><a name = '$1'>$1</a><a href = '#toc'>[toc]</a></h3>", 
      $sReturn);

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

  $sReturn = fnHyperlinkPlainText($sReturn);

  //-- Turn line breaks into <br> tags. This isn't really a good idea 
  //-- since you dont know the width of the target screen
  $sReturn = preg_replace(
     "#\n#im", "\n<br>", $sReturn);

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

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

  //-- transform spaces into entitized HTML
  $sReturn = 
    preg_replace(
     "#\s\s#im", "&nbsp;&nbsp;", $sReturn);


  //-- Delete pre tags since line breaks and spaces are being put
  //-- in anyway
  $sReturn = 
    preg_replace(
     "#</?pre>#im", "", $sReturn);

  return $sReturn;
} //-- function fnTransformPlainTextDocumentToHtml 
     
function fnReduplicateCharacters($sString)
{
  $sReturn = 
    preg_replace(
     "#(.)#im", "$1$1", $sString);

  return $sReturn;       
} //-- function fnReduplicateCharacters

function fnUnduplicateCharacters($sString)
{
  $sReturn = 
    preg_replace(
     "#(.)\\1#im", "$1", $sString);

  return $sReturn;       
} //-- function fnReduplicateCharacters

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