#!/usr/bin/perl -w
###################################################################
###                                                             ###
###    Author: Stefan Evert                                     ###
###   Purpose: CGI script to display simple concordance         ###
###   Created: Mon Apr 21 19:26:39 2003                         ###
###  Modified: Mon Mar  1 11:48:40 2004 (evert)                 ###
###                                                             ###
###################################################################


## This software is provided AS IS and the author makes no warranty as to
## its use and performance. You may use this script, redistribute and
## modify it under the same terms as Perl itself.
## 
## IN NO EVENT WILL THE AUTHOR BE LIABLE TO YOU FOR ANY CONSEQUENTIAL,
## INCIDENTAL OR SPECIAL DAMAGES, INCLUDING ANY LOST PROFITS OR LOST
## SAVINGS, EVEN IF AN IMS REPRESENTATIVE HAS BEEN ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY THIRD PARTY.


use Carp;

use lib '.';
use My::Config;
use My::Param;
use My::KWIC;

use CGI qw/:standard *table *Tr *td/;

$query = My::Param::Get("query");
@matches = split /,/, $query;

# display simple concordance:
#  - start and end of cpos of each match are passed in "query" parameter
#  - standard context and display parameters are used 

print header,
  start_html(-title => "CQPDemo: Simple Concordance",
	     -style => { -src => "$My::Config::CSSDir/standard.css" }),
  "\n";

$novel_title = My::Config::GetAttribute("novel_title");
$book_num = My::Config::GetAttribute("book_num");
$chapter_num = My::Config::GetAttribute("chapter_num");

print start_table, "\n";

$n = 0;
while (@matches) {
  $n++;
  $match = shift @matches;
  last if $match eq "...";			# indicates truncated list
  $match =~ /^([0-9]+):([0-9]+)$/
    or croak "Can't parse interval description '$match'.";
  ($s, $e) = ($1, $2);				# cf. display.pl
  $title = $novel_title->cpos2struc2str($s);
  $book = $book_num->cpos2struc2str($s);
  $chapter = $chapter_num->cpos2struc2str($s);
  $location = $title;
  $location .= ", Book $book"
    if defined $book;
  $location .= ", Chapter $chapter"
    if defined $chapter;
  ($cs, $ce) = My::KWIC::Context($s, $e);
  
  print Tr(td({-align => "right"}, b("$n.")),
	   td({-class => "location"}, escapeHTML($location))), "\n";
  print Tr(td({-valign => "top"},
	      a({-href => "context.pl?start=$s\&end=$e#match", -target => "context"},
		"context")),
	   td({-valign => "top"},
	      My::KWIC::Format($cs, $s-1), 
	      span({-class => "match"}, My::KWIC::Format($s, $e)),
	      My::KWIC::Format($e+1, $ce))), "\n";
}

print end_table, "\n";

if ($match eq "...") {
  print p(b("etc.")),"\n";
}

print end_html, "\n";

exit 0;

