# -------------------------------------------------------------------------------------
# flo::editor::Html
# -------------------------------------------------------------------------------------
#
#       Author : Jean-Michel Hiver (jhiver@cisedi.com).
#    Copyright : (c) MKDoc Holdings Ltd, 2001
# 
#      Unauthorized modification, use, reuse, distribution or redistribution
#      of this module is stricly forbidden.
#
#    Description:
#
#      HTML Content component. Lets the user adds chunks of HTML to pages
#
# -------------------------------------------------------------------------------------
package flo::editor::Html;
use flo::XHTMLFilter;
use flo::Standard;
use flo::Editor;
use lib::util::Command;
use lib::Exception;
use strict;


sub new
{
    my $class = shift;
    $class    = ref $class || $class;
    my $self  = bless { @_ }, $class;
    
    my $cgi = $self->{cgi};
    my $param_name = $self->{param_name};
    if (defined $cgi)
    {
	if (defined $cgi->param ($param_name . "_add"))
	{
	    $self->{data} = "";
	}
	else
	{
	    $self->{data} = $cgi->param ($param_name);
	}
    }
    else
    {
	$self->{data} = "";
    }
    $self->{class} = 'html';
    return $self;
}


sub edit
{
    my $self = shift;
    my $cgi  = $self->{cgi};
    
    my $data = $self->{data};
    use lib::util::Text2Html;
    $data = text2html ($data);
    
    return new flo::Template ( file => $self->template_path,
			       hash => {
				   block_name  => $self->{param_name},
				   block_value => $data,
			       } )->parse;
}


##
# Computes the path of the template which has to be used for
# that component given the current document language
##
sub template_path
{
    my $self = shift;
    
    my $document_table = table ('Document');
    my $document = $document_table->get ( Full_Path => path_info );
    my $lang = $document->{Lang};
    
    return template ('editor/html', $lang);
}


sub generate_xml
{
    my $self = shift;
    my $data = $self->{data};
    return flo::XHTMLFilter->purify ($data);
}


sub parse_xml
{
    my $self = shift;
    my $data = shift;
    $self->{data} = $data;
}


sub html
{
    my $self = shift;
    return flo::XHTMLFilter->linkify ($self->{data}, flo::Editor->links);
}


sub to_html { return shift->html }


##
# $self->tidy;
# ------------
#   Tidies the HTML, returns TRUE on tidy success, FALSE otherwise
##
sub tidy
{
    my $self = shift;
    return 1 if ($self->{data} =~ /^(?:\s|\n|\r)*$/sim);
    if (general_config->get ("tidy_command"))
    {
	my $ret = 1;
	try
	{
	    my $tidied_data = $self->html_tidy;
	    $self->{data} = $tidied_data;
	}
	catch
	{
	    $ret = 0;
	};
	return $ret;
    }
    else
    {
	return 1;
    }
}


##
# $obj->html_tidy;
# ----------------
#   Return a clean version of the HTML.
##
sub html_tidy
{
    my $self   = shift;
    my $string = shift || $self->{data};

    my $cfg = general_config();
    my $cmd = $cfg->get ('tidy_command');
    my $tmp_dir = $cfg->get ('temp_dir');
    my ($out, $err) = sysexec ($cmd, $tmp_dir, $string);

    if ($? != 0 && $? != 256)
    {
	throw (new lib::Exception ( code => 'CANNOT_TIDY',
				    info => 'Cannot tidy: ' . $err ) );
    }
    
    # strip everything before <BODY> and after </BODY>
    $out =~ s/^.*<BODY[^>]*>\n?//si;
    $out =~ s\</BODY[^>]*>.*</HTML[^>]*>.*$\\si;

    # hackish fix for \\\n bug
    $out =~ s/\\(?:\n|\r)//gsim;
    return $out;
}


1;




