#                                                         -*- Perl -*-
# pdic2html.pl: A converter from PDIC to html for EBStudio.
# Copyright (C) 2007 Kazuhiro Ito <kzhr@d1.dion.ne.jp>
#
# This program is modified from pdic2txt, written by Tsutomu Kuroda.
# pdic2txt is included by PDIC Toolkit.
#
# Here is original comments in pdic2txt.
#========================================================================
#
# pdic2txt.pl
#
# Copyright (c) 2002 Tsutomu Kuroda <tkrd@mail.com> All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
#========================================================================

use strict;
use warnings;
use PDIC::Reader;
use Getopt::Long;

use vars qw($VERSION $DEBUG);
$VERSION = '1.13'; # $Date: 2002/02/24 11:35:00 $
$DEBUG = 0;

use vars qw($EXT0 $EXT1);
$EXT0 = 'DIC';
$EXT1 = 'html';

MAIN: {
  my $time = time;
  my $title;

  GetOptions('title:s' => \$title);
  my $filename0 = $ARGV[0];
  
  $filename0 =~ /(\.$EXT0)?$/;
  my $filename = $`;

  $filename0 = "$filename.$EXT0";
  if (not -e $filename0) {
    die("'$filename0' does not exist.");
  }
  
  my $filename1 = $ARGV[1];
  if (not defined $filename1) {
    $filename1 = "$filename.$EXT1";
  } elsif (not $filename1 =~ /\.$EXT1$/) {
    $filename1 = "$filename1.$EXT1";
  }

  if (-e $filename1) {
    my $i = 1;
    while (-e "$filename1.$i~") {
      $i++;
    }
    rename($filename1, "$filename1.$i~")
	or die("'$filename1' is read-only or locked by some other process.");
  }


  if (!(defined($title))) {
    $title = $filename;
  }


  my $obj_reader = new PDIC::Reader($filename0);
  open(OUT, "> $filename1") or die("Can't open $filename1");
  my $indices = $obj_reader->get_indices();
  my $num_entries = 0;
  my $index;

  output_header($title);

  foreach $index (@$indices) {
    my $entries = $obj_reader->get_data_section($index->{data_block_number});
        
    my $entry;
    foreach $entry (@$entries) {
      output_heading($entry->{headword});
      output_content($entry->{definition});
      print OUT "\n";

      $num_entries++;
    }
  }
  output_footer($title);
  close(OUT);

  print "$filename1 was successfully created.\n";
  printf("duration     : %8d sec.\n", time - $time);
  printf("num entries  : %8d\n", $num_entries);
}

sub output_content {
  my $text = $_[0];
  
  if (defined $text and $text ne '') {
    if ($text eq '.') {
      print OUT "<p>no content</p>\n";
    } else {
      $text = htmlspecialchars($text);
      $text =~ s/\r?\n/<\/p>\n<p>/g;
      print OUT '<p>'.$text."</p>\n";
    }
  } else {
    print OUT ".\n";
  }
}

sub output_heading {
  my $text = $_[0];
  print OUT '<dt>'.htmlspecialchars($text)."</dt>\n";
}

sub output_header {
  my $text = $_[0];

  print OUT '<html>
<head>
<title>'
.htmlspecialchars($text).
'</title>
</head>
<body>
';
}

sub output_footer {
  print OUT '
</dl>
</body>
</html>
';
  
}

sub htmlspecialchars {
  my $text = $_[0];

  $text =~ s/&/&amp;/g;
  $text =~ s/</&lt;/g;
  $text =~ s/>/&gt;/g;

  return $text;
}
