use strict;

# ==============================================================================
# skip head, some texts, flatten file and body, references
sub ET0
{
    my ($node, $et) = @_;
    $_ = $node->{'path'};
    my $class = lc( $node->{'attr'}{'class'} || '' );

    m<^FILE/(TEXT_S|html(/head|/TEXT_S))/$> && return( 0 );
    m<^FILE/html/body/script/$> && return 0;
    m<^FILE/html/body/div/$> && $class =~ /^html(nav|toc)$/ && return 0;
    m<^FILE/html/body/TEXT_S/$> && return 0;
    m</p/$> && $node->IsSEmpty() && return 0;

# flatten - process children in original context and return them -----------
    m<^FILE/html(/body)?/$> && do {
        $node->Proc( $et );
        $node->Proc( \&ET0t );
        return $node->{'content'};
    };

# resolve references -------------------------------------------------------
    m<^FILE/html/body(/\w+)*/a/$> && ! $class && do {
        return 0 if( !defined( $_ = $node->{'attr'}{'href'} ) );

        m<\#(JSCRIPT-CH-\w+-)(EX|FIG|TAB)((-\w+)+)$> && do {
            $node->{'attr'}{'href'} = $&;
            $#{$node->{'content'}} == 0 and
            ! ref( $node->{'content'}[0]{'content'} ) 
               or die $node->Tx( "wrong content of %p in %f" );
            $node->{'attr'}{'label'} = $1.$2.$3;

            $_ = $node->{'content'}[0]{'content'};
            $node->{'attr'}{'class'} =
                $2 eq 'EX' && /^\s*Example\s*\d+\.\d+$/ && 'ex' ||
                $2 eq 'FIG' && /^\s*Figure\s*\d+\.\d+$/ && 'fig' ||
                $2 eq 'TAB' && /^\s*Table\s*\d+\.\d+$/ && 'tab' ||
                    die $node->Tx( "wrong content of %p in %f" );
            $node->{'attr'}{'style'} = 'background-color:darkgray';
            return $node;

        };


        m<\#(JSCRIPT-APP-\w+-)(TAB)((-\w+)+)$> && do {
            $node->{'attr'}{'href'} = $&;
            $#{$node->{'content'}} == 0 and
            ! ref( $node->{'content'}[0]{'content'} ) 
               or die $node->Tx( "wrong content of %p in %f" );
            $node->{'attr'}{'label'} = $1.$2.$3;

            $_ = $node->{'content'}[0]{'content'};
            $node->{'attr'}{'class'} =
                $2 eq 'TAB' && /^\s*Table\s*\w\.\d+$/ && 'tab' ||
                    die $node->Tx( "wrong content of %p in %f" );
            $node->{'attr'}{'style'} = 'background-color:darkgray';
            return $node;
        };


        m<^(ch)(\d\d)_01.htm$> || m<^(app)([a-h])_01.htm$> and do {
            $node->{'attr'}{'href'} =( $1 eq 'ch' ? "CH" : "APP") . "$2.htm";
            $node->{'attr'}{'label'} = $1.$2;
            $node->{'attr'}{'style'} = 'background-color:darkgray';

            $#{$node->{'content'}} == 1 and
            ! ref( $node->{'content'}[0]{'content'} ) and
            $node->{'content'}[1]{'tag'} eq 'i'
                or Diag( $node->Tx( "warn: %f wrong content (I) of %p" ), 'w' );

            $_ = $node->{'content'}[0]{'content'};
            $node->{'attr'}{'class'} = $1;
            $1 eq 'ch' && /^Chapter\s+\d+,/ && 'ch' ||
            $1 eq 'app' && /^Appendix\s+[A-H],/ && 'app' ||
                Diag( $node->Tx( "warn: %f wrong content (II) of %p" ), 'w' );
            return $node;
        };

        m<^(refp_)(\d+).htm$> and do {
            $node->{'attr'}{'href'} = $&;
            $node->{'attr'}{'label'} = "ref$2";
            $#{$node->{'content'}} == 0 and
            ! ref( $node->{'content'}[0]{'content'} ) 
                or die $node->Tx( "%f wrong content of %p " );
            $_ = $node->{'content'}[0]{'content'};
            $node->{'attr'}{'class'} = 'ref';
            $node->{'attr'}{'style'} = 'background-color:darkgray';
            return $node;
        };

        Diag( $node->Tx( "warn:%f a_ flattened " .
                         "\"$node->{'attr'}{'href'}\"" ), 'w' );
        return $node->Proc( $et )->{'content'};
    };

    # other body elements
    m<^FILE/html/body(/\w+)+/$> && 
        return $node->Proc( $et );

    die $node->Tx( "%f unhandled %p in ET0" );
}

# reduce more TEXTS and \n\n\n to \n -- more \n does not!!! serve as par
sub ET0t
{
    my ($node, $et) = @_;
    $_ = $node->{'path'};

    # resolve TEXT not in pre
    m</pre/$> && return $node;

    m</TEXT(_S)?/$> && $node->{'parent'}{'precontent'}[0] && 
    $node->{'parent'}{'precontent'}[0]{'tag'} =~ /^TEXT(_S)?$/ && do {
        $node->{'parent'}{'precontent'}[0]{'content'} = 
            $node->{'content'} . $node->{'parent'}{'precontent'}[0]{'content'};
        return 0;
    };

    m</TEXT(_S)?/$> && do {
        $node->{'content'} =~ s/\s*\n\s*\n\s*/\n/g;  # // * is greedy
        $node->{'tag'} = $node->{'content'} =~ /^\s*$/ ? 'TEXT_S' : 'TEXT';
    };

    return $node->Proc( $et );
}

1;
