                                                                                                    


<?php
//-->-->-->-->-->-->-->-->
//-- Description:
//--   The purpose of this function is to transform a string of text
//--   into some kind of meaningful HTML. The plain text is expected to
//--   have as little 'mark-up' imbedded within it as is possible for 
//--   the given application. This attempt is an evolution on previous
//--   efforts which I have written which were written in the Unix Bash
//--   shell scripting language and which relied heavily programs such
//--   as 'sed' and 'grep'. 
//--
//--   These 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. 
//--
//--  Here are some plain textformats which should be hyper-linked by 
//--  the current function:
//-- 
//--     http://somehost.dom/some/resouce.ext
//--     link://some/local/resource.ext
//--     www.somehost.dom/some/resouce.ext
//--     "Hyper-link display text" http://somehost.dom/some/resouce.ext
//--     'Hyper-link display text' http://somehost.dom/some/resouce.ext
//--     "Hyper-link display text" www.somehost.dom/some/resouce.ext
//--     'Hyper-link display text' www.somehost.dom/some/resouce.ext
//--     'Hyper-link display text' link://local/resource.ext
//--       In this case the resource specified is assumed to be on the 
//--       local web-server relative to the current web-path
//--       Please note, that if you want the referenced resource to be
//--       relative to the web-server document root then you need to use
//--       3 forward slashes and not 2. For example:
//--     'Hyper-link display text' link:///local/resource.ext    
//--     "Hyper-link display text" link://local/resource.ext
//--       In this case the resource specified is assumed to be on the 
//--       local web-server relative to the current web-path
//--     The structures --&gt;&gt; and --&lt;&lt; are supposed to 
//--     stop the formatting of the source text.

function fnTransformPlainTextToHtml($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);
  //-- this temporarily disabled because of the 'display-product.php'
  //-- page.
  
  //-- 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);

  //-- I am using this strange tecnique in order to stop things
  //-- which are between certain tags from being formatted. The technique
  //-- it to duplicated every letter between the <pre> tags and then
  //-- unduplicate every letter at the very end of the transformation.
  //-- Surprisingly, this is not too slow.
  $sReturn = 
    preg_replace(
         "#<pre>((.|\n)*?)</pre>#ime", 
         "'<pre>'.fnReduplicateCharacters('$1').'</pre>'", $sReturn);


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

  //-- Hyperlink email addresses with a 'mailto:' link
  $sReturn = 
    preg_replace(
     '#(\S+@\S+)#im', '<a href="mailto:$1">$1</a>', $sReturn);


  //-- Hyperlink this format:
  //--   "Hyper-link display text" http://somehost.dom/some/resouce.ext
  //-- This assumes that the double quote character " may have already been
  //-- 'entitized' by some other process, quite probably the current 
  //-- php function itself.
  $sReturn = 
    preg_replace(
     '#\s(&quot;|")(.+?)(&quot;|")\s{0,20}(http://\S{2,})#im', 
     ' <a href="$4">$2</a>', $sReturn);

  //-- Hyperlink this format:
  //--   'Hyper-link display text' http://somehost.dom/some/resouce.ext
  $sReturn = 
    preg_replace(
     "#\s'([^']+)'\s{0,20}(http://\S{2,})#im", 
     ' <a href="$2">$1</a>', $sReturn);

  //-- Hyperlink this format:
  //--   "Hyper-link display text" www.somehost.dom/some/resouce.ext
  $sReturn = 
    preg_replace(
     '#\s(&quot;|")(.+?)(&quot;|")\s{0,20}(www\.\S{2,})#im', 
     ' <a href="http://$4">$2</a>', $sReturn);

  //-- Hyperlink this format:
  //--   'Hyper-link display text' www.somehost.dom/some/resouce.ext
  $sReturn = 
    preg_replace(
     "#\s'([^']+)'\s{0,20}(www\.\S{2,})#im", 
     ' <a href="http://$2">$1</a>', $sReturn);

  //-- Hyperlink this format:
  //--   'Hyper-link display text' link://local/resource.ext
  //-- In this case the resource specified is assumed to be on the 
  //-- local web-server relative to the web-servers 'document root'
  $sReturn = 
    preg_replace(
     "#\s'([^']+)'\s{0,20}link://(\S{2,})#im", 
     ' <a href="$2">$1</a>', $sReturn);

  //-- Hyperlink this format:
  //--   "Hyper-link display text" link://local/resource.ext
  //-- In this case the resource specified is assumed to be on the 
  //-- local web-server relative to the web-servers 'document root'
  $sReturn = 
    preg_replace(
     '#\s(&quot;|")(.+?)(&quot;|")\s{0,20}link://(\S{2,})#im', 
     ' <a href="$4">$2</a>', $sReturn);

  //-- Hyperlink URLs beginning with 'www.' This doesn't work if the 
  //-- URL begins at the first character of the file.
  $sReturn = 
    preg_replace(
     '#\s(www\.\S{2,})#im', ' <a href="http://$1">$1</a>', $sReturn);

  //-- Hyperlink URLs beginning with http:// 
  $sReturn = 
    preg_replace(
     '#\s(http://\S{2,})#im', ' <a href="$1">$1</a>', $sReturn);

  //-- Hyperlink URLs beginning with link://
  //-- The resource referenced is assumed to be on the local web-server 
  $sReturn = 
    preg_replace(
     '#\slink://(\S{2,})#im', ' <a href="$1">$1</a>', $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 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);

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

  return $sReturn;
} //-- function fnTransformPlainTextToHtml 
     
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

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