#!/usr/bin/perl
#=======================================================================
# Copyright (c) 2000 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.
#=======================================================================
# ALdoc2Alml < 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";
}    


#======================================================================
# Elabora bloccotitolo.
#----------------------------------------------------------------------
sub ElaboraBloccoTitolo {

    local $head = "";
    local $accumulo = "";
    local $id = "";

    $riga !~ m|<(h.)>|i;
    $head = $1;
    $riga = <STDIN>;
    
    while ( $riga !~ m|</bloccotitolo>|i ) {

	if ( $riga =~ m|^id="(.*)"|i ) {
	    $id = $1;
	} else {
	    $accumulo = $accumulo . $riga;
	}
        $riga = <STDIN>;
    }
    if ($id eq "")
      {
        print STDOUT "<$head>\n";
      }
    else
      {
        print STDOUT "<$head id=\"$id\">\n";
      }
    print STDOUT "$accumulo";
    print STDOUT "</$head>\n";
}    

sub ElaboraBloccoTitoloTomo {

    local $head = "";
    local $accumulo = "";
    local $id = "";

    $riga !~ m|<(tomeheading)>|i;
    $head = $1;
    $riga = <STDIN>;
    
    while ( $riga !~ m|</bloccotitolo>|i ) {

	if ( $riga =~ m|^id="(.*)"|i ) {
	    $id = $1;
	} else {
	    $accumulo = $accumulo . $riga;
	}
        $riga = <STDIN>;
    }
    if ($id eq "")
      {
        print STDOUT "<$head>\n";
      }
    else
      {
        print STDOUT "<$head id=\"$id\">\n";
      }
    print STDOUT "$accumulo";
    print STDOUT "</$head>\n";
}    


# Inizio del programma.

# Scandisce le righe.
while ($riga = <STDIN>)
  {
    # Se si incontra un commento deve essere saltato.
    if ($riga =~ m|<!--|)
      {
	&SaltaBlocco( "-->" );
      }
    elsif ($riga =~ m|<testopreformattato>|i)
      {
	&SaltaBlocco( "</testopreformattato>" );
      }
    elsif ($riga =~ m|<figuratestopreformattato>|i)
      {
	&SaltaBlocco( "</figuratestopreformattato>" );
      }
    elsif ($riga =~ m|<sintassitestopreformattato>|i)
      {
	&SaltaBlocco( "</sintassitestopreformattato>" );
      }
    elsif ($riga =~ m/^<h[0-4]>$/)
      {
	    &ElaboraBloccoTitolo ();
      }
    elsif ($riga =~ m/^<tomeheading>$/)
      {
	    &ElaboraBloccoTitoloTomo ();
      }
    else
      {
        print STDOUT ("$riga");
      }
  }

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

