use strict;

# ==============================================================================
sub ET1
{
    my ($node, $et) = @_;
    $_ = $node->{'path'};
    my $class = lc( $node->{'attr'}{'class'} || '' );

# for navigation
    m<^/div/div/h2/a/$> && defined( $node->{'root'}{'nav'} ) &&
        push @{$node->{'root'}{'nav'}}, $node->{'parent'};


# sections and titles ----------------------------------------------------------
    # make div_sect0 with h1
    m<^/h1/$> &&
        return( ML::Tree->new( 'div', { 'class' => 'sect0',
                                        'type' => $node->{'attr'}{'class'} } ),
                'skipto', 'proc' );

    # propagate sect0 type (preface/abstract/...)
    m</div/div/$> && do {
        $node->{'attr'}{'type'} = $node->{'parent'}{'attr'}{'type'} || '';
        return $node->Proc( $et );
    };
    
    # erase section numbers from titles
    m</div/h[1234]/$> && do {
        $node->{'grab_title'} = $node->{'parent'}{'attr'}{'type'} &&
            $node->{'parent'}{'attr'}{'type'} =~ /chapter|appendix/ &&
                $node->{'parent'}{'attr'}{'class'} !~ /sect[23]/;      
                                                  # if title need to be prepared
        return $node->Proc( $et );
    };

    m</div/h1/a/$> && $class eq 'title' and
        $node->{'attr'}{'name'} =  $node->{'root'}{'attr'}{'ch'};

    m</div/h[1234]/a/$> && $class eq 'title' && do {
        $node->{'parent'}{'attr'}{'name'} = $node->{'attr'}{'name'}; 
        $node->{'parent'}{'parent'}{'attr'}{'name'} = $node->{'attr'}{'name'}; 
        return $node->Proc( $et );
    };

    m</div/table/caption/$> && do {
        $node->{'tag'} = 'h4';
        $node->{'parent'}{'parent'}->AddFirst( $node, 1 );
        $node->Proc( $et, 'grab_title' => 1 );
        return 0;
    };


    m</div/h[1234]/a/TEXT/$> && do {
        if( $node->{'parent'}{'parent'}{'grab_title'} )
        {
            my $type = $node->{'parent'}{'parent'}{'parent'}{'attr'}{'type'}
            || '';
            my $cl = $node->{'parent'}{'parent'}{'parent'}{'attr'}{'class'};
            $node->{'parent'}{'parent'}{'grab_title'} = 0;
            $_ = $node->{'content'};
            if( $cl eq 'sect0'   and
                ( $type eq 'appendix' && /^\s*(\w\.)\s*/
                  || /^\s*(\d+\.)\s*/ ) or
                $cl eq 'sect1'   and 
                ( $type eq 'appendix' && /^\s*(\w\.\d+)\s*/
                  || /^\s*(\d+\.\d+)\s*/ ) or
                $cl eq 'figure'  and /^\s*Figure\s*(\d+\.\d+):\s*/ or
                $cl eq 'table'   and
                ( $type eq 'appendix' && /^\s*Table\s*(\w\.\d+):\s*/ 
                  || /^\s*Table\s*(\d+\.\d+):\s*/ ) or


                $cl eq 'example' and /^\s*Example\s*(\d+\.\d+):\s*/ )
            {
                $node->{'content'} = $';
                $node->{'parent'}{'parent'}{'attr'}{'orignum'} = $1;
                return( new ML::Tree( 'span', 
                                      { 'class'=> 'secnum',
                                        'style' => 'background-color:darkgray'},
                                      [ newTEXT ML::Tree( $& ) ] ),
                        $node );
            }
            die $node->Tx( "%f syntax error in caption %p\n ");
        }
        return $node;
    };

# text and footnotes -------------------------------------------------------
    # footnote - only p allowed
    m</blockquote/$> && $class eq 'footnote' && do {
        $node->Proc( $et, 'path' => 'F/' );
        return $node->{'content'};
    };


    m<^F/TEXT_S/$> && return $node;

    # resolve content of footnote
    m<^F/p/$> && do {
        die $node->Tx( "%f syntax error in %p\n " ) 
            if( not $node->{'content'}[0] or
                ref( $node->{'content'}[0]{'content'} )
                or $node->{'content'}[0]{'content'} !~ /^\s*\[(\d+)\]\s*/ );
        $node->{'content'}[0]{'content'} = $';
        $node->AddFirst( new ML::Tree( 'a', { 'class' => 'footnote',
                               'style' => 'background-color:darkgray',
                                              'name' => $1 }, 
                                     [ newTEXT ML::Tree( $& ) ] ) );
        $node->{'tag'} = 'blockquote';
        $node->{'attr'}{'class'} = 'footnote';
        $node->{'attr'}{'name'} = $1;
        return $node;
    };

    # resolve TEXT for [] (only in p )
    m</div/p/TEXT/$> && $node->{'content'} =~ /\s*\[(\d+)\]/ && do {
        $node->{'content'} = $';
        return( newTEXT ML::Tree( $` ), 
                new ML::Tree( 'a', { 'class' => 'footnote', 'href' => "#$1",
                                 'style' => 'background-color:darkgray' },
                              [ newTEXT ML::Tree( $& ) ] ),
                'add', 'redo' );
    };

    return $node->Proc( $et );
    die $node->Tx( "%f unhandled %p" );
}

1;
