# structuredText.pm
#
# A small module for parsing of "structured Text". 
#
# author:  Reyk Floeter <reyk@synack.de>,
#          Max-Gerd Retzlaff <m.retzlaff@gmx.net>
# date:    $Date: 2002/03/07 01:34:52 $
# version: $Revision: 1.3 $
#
# This application is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version. 
#
# This application is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111 USA.  

package structuredText;

use Exporter;
@ISA = ('Exporter');
@EXPORT = ('structuredText2HTML');

sub structuredText2HTML
{
    my $text = $_[0];
    #my $text = join("", @{$_[0]});

    # code
    $text =~ s/\-+snip\-+/<blockquote><pre>/gi;
    $text =~ s/\-+snap\-+/<\/pre><\/blockquote>/gi;

    # header
    # $text =~ s/(^|<p>\n\n)(.*?)\n<p>\n/$1<h3>$2<\/h3>\n\n/g;
    $text =~ s/(^|\n\n+)(.*?)\n+\ ([^\ ])/$1<h3>$2<\/h3>\n$3/g; # zope-style

    # paragraph 
    $text =~ s/\n\n/\n<p>\n/g;

    # <hr>
    $text =~ s/\n\-{20}\-*\n/\n<hr>\n/g;

    # list
    # $text =~ s/\n(\-|\+|\s*?\-|\s*?\+)\s+?(.*)/\n<li>$2<\/li>/g;
    #$text =~ s/\n\s*?(?:\-|\+)\s+?(.*)/\n<li>$1<\/li>/g;
    $text =~ s/\n\s*?\-\s+?(.*)/\n<li>$1<\/li>/g;

    # hyperlink
    # $text =~ s/\"(.*?)\"\s\(((http|https|ftp|news|telnet|mailto):.*?)\)/<a href=\"$2\">$1<\/a>/gi;
    $text =~ s/\"([^\"]*?)\"\s\(((?:http|https|ftp|news|telnet|mailto):.*?)\)/<a href=\"$2\">$1<\/a>/gi;
   
    # image
    # $text =~ s/\"(.*?)\"\s\(image:(.*?)\)/<img src="$2" border="0" alt="$1">/g;
    $text =~ s/\"([^\"]*?)\"\s\(image:(.*?)\)/<img src="$2" border="0" alt="$1">/g;

    # bold
    # $text =~ s/(\s|\W)\*\*(.*?)\*\*(\s|\W)/$1<b>$2<\/b>$3/g;
    $text =~ s/(\s|\W|\_)\*\*(.*?)\*\*(\s|\W|\_)/$1<b>$2<\/b>$3/g;
    
    # emphasis
    $text =~ s/(\s|\W)\*(.*?)\*(\s|\W)/$1<em>$2<\/em>$3/g;

    # underline
    $text =~ s/(\s|\W)\_(.*?)\_(\s|\W)/$1<u>$2<\/u>$3/g;

    ## misc foo
    $text =~ s/(\w)(?:\_|\*\*|\*)(\w)/$1 $2/g;

    # mail reply
    $text =~ s/\n(\>(?:\ |=20$))/\n<br>$1/g;

    return($text);
}

return 1;

