#!/usr/bin/perl
#=======================================================================
# Copyright (c) 2002 Daniele Giacomini daniele@swlibero.org
#
# This program 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 program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#=======================================================================
# prog < ALTOOLS_SOURCE > ALML_SOURCE
#
# Partial translator from ALdoc to Alml.
#=======================================================================

# Record letto.
$riga = "";


# Salta blocco.
sub SaltaBlocco
{
    local ($conclusione) = $_[0];

    # Cerca di saltare il blocco.
    while ($riga !~ m|$conclusione|i)
      {
	# Trasferisce il record letto, e legge il successivo.
	print STDOUT "$riga";
	$riga = <STDIN>;
      }

    # Trasferisce anche l'ultimo record letto.
    print STDOUT "$riga";
}    

# Salta blocco.
sub ElaboraTitolo
{
    local ($prefisso) = $_[0];
    local ($n) = $_[1];
    local ($hn) = "";
    local ($titolo) = "";
    local ($accumulo) = "";

    $hn = $riga;

    $riga = <STDIN>;

    # Carica le righe successive fino a <Hn>    
    while ($riga !~ m/\<\/$prefisso$n\>/i)
      {
	# Trasferisce il record letto, e legge il successivo.
	$accumulo = $accumulo . $riga;
	chomp ($riga);
	$titolo = $titolo . " " . $riga;
	$riga = <STDIN>;
      }

    $titolo =~ s/\<![(.*)]]>//gs;
    $titolo =~ s/\<indexentry\>(.*)<\/indexentry\>//gs;
    $titolo =~ s/\<\/?special[^>]*\>//gs;
    $titolo =~ s/\<\/?samp[^>]*\>//gs;
    $titolo =~ s/\<\/?[a-z]+[^>]*\>//gs;
    $titolo =~ s/^ (.*)$/$1/gs;

    $hn =~ m/^\<$prefisso$n(.*)\>$/i;
    $hn = "<$prefisso" . $n . $1 . " bookmark=\"$titolo\">";

    $accumulo = $hn . "\n" . $accumulo . $riga;

    print STDOUT "$accumulo";
    #print STDERR "$accumulo";

}    

# Inizio del programma.

# Scandisce le righe.
while ($riga = <STDIN>)
  {
    # Se si incontra un commento deve essere saltato.
    if ($riga =~ m|<!--|)
      {
	&SaltaBlocco( "-->" );
      }
    elsif ($riga =~ m|<verbatimpre>|i)
      {
	&SaltaBlocco( "<\/verbatimpre>" );
      }
    elsif ($riga =~ m|<pre>|i)
      {
	&SaltaBlocco( "<\/pre>" );
      }
    elsif ($riga =~ m|<asciiart>|i)
      {
	&SaltaBlocco( "<\/asciiart>" );
      }
    elsif ($riga =~ m|^<h([0-7]).*>$|i)
      {
	&ElaboraTitolo ("h", $1);
      }
    elsif ($riga =~ m|^<faqh([0-7]).*>$|i)
      {
	&ElaboraTitolo ("faqh", $1);
      }
    elsif ($riga =~ m|^<slideh([0-7]).*>$|i)
      {
	&ElaboraTitolo ("slideh", $1);
      }
    elsif ($riga =~ m|^<sheeth([0-7]).*>$|i)
      {
	&ElaboraTitolo ("sheeth", $1);
      }
    elsif ($riga =~ m|^<unnumberedh([0-7]).*>$|i)
      {
	&ElaboraTitolo ("unnumberedh", $1);
      }
    elsif ($riga =~ m|^<tomeheading.*>$|i)
      {
	&ElaboraTitolo ("tomeheading", "");
      }
    else
      {
        print STDOUT ("$riga");
      }
  }

#======================================================================

