#!/usr/bin/env perl

use warnings;
use strict;

use RENA::LocalConfiguration qw($BASE_DIR);
use RENA::Support qw(&analyse_article &xml_to_xhtml &yaml_to_xhtml);
use RENA::Support::Classes;
use RENA::JSpellFilter;
use RENA::JSpellFilters qw(&default_pipeline);

my $q = new CGI;

my $text = $q->param("text");

# show form for text input
if (not defined $text) {
    print $q->header,
    $q->start_html({-style => {-src => "/~epa/article.css", -type => "text/css"},
		    -title => "Text submission"}),
    $q->start_form(-method=>"POST",
                   -enctype => "multipart/form-data",
                   -accept_charset=>"ISO-8859-15"),
    "Text to submit:<br>",
    $q->textarea(-name => "text",
		 -rows => 24,
		 -columns => 80),
    #'<br><input type="checkbox" name="blank"> Paragraphs include non-consecutive linebreaks',
    '<br><button type="submit">Submit</button>',
    $q->end_form(),
    $q->end_html;
    exit 0;
}

# process submitted text and display the results

my $cl = RENA::Support::Classes::load_from_file
    ("$BASE_DIR/tree.yaml");

die if not $cl;

my @parts = split /(?:\r?\n){1,}/s, $text;

my @pipeline = @{&default_pipeline($BASE_DIR)};
push @pipeline, { name => "unified_yaml", options => {} },
                { name => "toxml", options => {} };

my $filter = new RENA::JSpellFilter
    (pipeline => \@pipeline, global => {cl => $cl});

my $analysis = &analyse_article($filter, \@parts);
my $xml_xhtml = &xml_to_xhtml($analysis->{xml});
my $yaml_xhtml = &yaml_to_xhtml($analysis->{yaml}, $cl);

print $q->header,
    $q->start_html({-style => {-src => "/~epa/article.css", -type => "text/css"},
		    -script => {-src => "/~epa/pick_entity.js"},
		    -title => "Preview for your text"}),
    qq(<div id="analysis">),
    $yaml_xhtml,
    $xml_xhtml,
    "</div>",
    $q->end_html;
