In HTML output, it appears that an extra heading is output before
sectioning commands if the sectioning commands do not have a @node before
them, if the AUTO_SECTION_NODE feature is in effect.

I have ascertained that the reason for this is that there is no
"current node" at the time of parsing and so the sectioning command is
not appropriately associated with the node (and vice versa). This leads
an extra heading to be output for the @node command which is later added
to the tree, which is presumably normally suppressed in favour of a
heading for the sectioning command.

I found this problem for a @top command not preceded by "@node Top",
as in the following input:

    \input texinfo
    
    @top Test manual
    
    In top node
    
    @bye

The HTML output (with current git texi2any) contains a heading for "Top":

<h1 class="node"><span>Top<a class="copiable-link" href="#Top"> 
&para;</a></span></h1>
<h1 class="top" id="Test-manual"><span>Test manual<a class="copiable-link" 
href="#Test-manual"> &para;</a></span></h1>

The class "node" on the first <h1> shows this came from a @node command, which
is added in texi2any's processing with the AUTO_SECTION_NODE=1 setting.

However, if there is an explicit "@node Top", as with the following, there
is no "Top" heading output:

    \input texinfo
    
    @node Top
    @top Test manual
    
    In top node
    
    @bye

So it seems that there is some difference that stops the "Top" heading
being output in the later case.

It was the same with TEXINFO_XS=omit.

In the Perl code, the value of the $do_heading variable in
_convert_heading_command in Texinfo/Convert/HTML.pm is different between
the two test cases.  (Incidentally, I still don't understand why we have
both HTML.pm and HTMLNonXS.pm.)

I was able to check that the difference was due to the absence of
$node_relations->{'associated_title_command'} with the implicitly
added @node.

The following shows two runs with tracing print statements added.
The explicit-node-top.texi file is the working test case above, containing
the "@node Top" line.

$ TEXINFO_XS=omit ../tta/perl/texi2any.pl explicit-node-top.texi --html 
--no-split
SET ASSOC TITLE COMMAND
Trace begun at ../tta/perl/../perl/Texinfo/ParserNonXS.pm line 3851
Texinfo::Parser::_associate_title_command_anchor('HASH(0x5b45407393a8)', 
'Texinfo::TreeElement=HASH(0x5b4540739480)', 'HASH(0x5b4540739738)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 4381
Texinfo::Parser::_end_line_misc_line('Texinfo::Parser=HASH(0x5b45406421b0)', 
'Texinfo::TreeElement=HASH(0x5b4540739528)', 'HASH(0x5b45407306b8)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 5011
Texinfo::Parser::_end_line('Texinfo::Parser=HASH(0x5b45406421b0)', 
'Texinfo::TreeElement=HASH(0x5b4540739528)', 'HASH(0x5b45407306b8)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 8005
Texinfo::Parser::_process_remaining_on_line('Texinfo::Parser=HASH(0x5b45406421b0)',
 'Texinfo::TreeElement=HASH(0x5b4540739528)', '^J', 'HASH(0x5b45407306b8)') 
called at ../tta/perl/../perl/Texinfo/ParserNonXS.pm line 8088
Texinfo::Parser::_parse_texi('Texinfo::Parser=HASH(0x5b45406421b0)', 
'Texinfo::TreeElement=HASH(0x5b45404716f8)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 1107
Texinfo::Parser::_parse_texi_document('Texinfo::Parser=HASH(0x5b45406421b0)') 
called at ../tta/perl/../perl/Texinfo/ParserNonXS.pm line 998
Texinfo::Parser::parse_file('Texinfo::Parser=HASH(0x5b45406421b0)', 
'explicit-node-top.texi') called at ../tta/perl/texi2any.pl line 1818
HEADING LEVEL LEFT UNDEF
ASSOC TITLE EXISTS
DO HEADING 
NO HEADING LEVEL
DO HEADING 1
HEADING LEVEL 0
$ TEXINFO_XS=omit ../tta/perl/texi2any.pl implicit-node-top.texi --html 
--no-split
HEADING LEVEL DEF 1
DO HEADING 1
HEADING LEVEL 0
DO HEADING 1
HEADING LEVEL 0
$ 


Diff with print statements added:

diff --git a/tta/perl/Texinfo/Convert/HTML.pm b/tta/perl/Texinfo/Convert/HTML.pm
index ffaf8a7e4c..419d5f6096 100644
--- a/tta/perl/Texinfo/Convert/HTML.pm
+++ b/tta/perl/Texinfo/Convert/HTML.pm
@@ -3445,11 +3445,17 @@ sub _convert_heading_command($$$$$) {
         and !exists($node_relations->{'associated_title_command'})) {
       if ($element->{'extra'}->{'identifier'} eq 'Top') {
         $heading_level = 0;
+        print "HEADING LEVEL DEF 1\n";
       } else {
         # use node
         $heading_level = 3;
+        print "HEADING LEVEL DEF 2\n";
       }
     }
+    else { print "HEADING LEVEL LEFT UNDEF\n"; }
+    if (exists($node_relations->{'associated_title_command'})) {
+      print "ASSOC TITLE EXISTS\n";
+    }
   } elsif (exists($element->{'extra'})
            and exists($element->{'extra'}->{'section_level'})) {
     $heading_level = $element->{'extra'}->{'section_level'};
@@ -3464,6 +3470,9 @@ sub _convert_heading_command($$$$$) {
 
   my $do_heading = (defined($heading) and $heading ne ''
                     and defined($heading_level));
+  print "DO HEADING $do_heading\n";
+  print defined($heading_level) ? "HEADING LEVEL $heading_level\n"
+  : "NO HEADING LEVEL\n";
 
   # if set, the id is associated to the heading text
   my $heading_id;
diff --git a/tta/perl/Texinfo/ParserNonXS.pm b/tta/perl/Texinfo/ParserNonXS.pm
index 46a0c75fd9..f80d1673a7 100644
--- a/tta/perl/Texinfo/ParserNonXS.pm
+++ b/tta/perl/Texinfo/ParserNonXS.pm
@@ -3846,6 +3846,11 @@ sub _associate_title_command_anchor($$$) {
   my ($current_node_relations, $current, $section_relations) = @_;
 
   if (not exists($current_node_relations->{'associated_title_command'})) {
+    print "SET ASSOC TITLE COMMAND\n";
+    use Devel::StackTrace;
+    my $trace = Devel::StackTrace->new;
+    print $trace->as_string;
+
     $current_node_relations->{'associated_title_command'} = $current;
     $section_relations->{'associated_anchor_command'}
                         = $current_node_relations;


So based on this, it appears to be a difference in the parsing phases, rather
than some difference with a later transformation of the parse tree where
"@node" is added.  The full parsing debug output to this point could
be illuminative:


$ TEXINFO_XS=omit ../tta/perl/texi2any.pl explicit-node-top.texi --html 
--no-split -c DEBUG=1
!!!!!!!!!!!!!!!! RESETTING THE PARSER !!!!!!!!!!!!!!!!!!!!!
NEW LINE @node Top\n
COMMAND @node
ABORT EMPTY in (before_node_section)[C2]: empty_line; ||
NEW TEXT (merge):  Top|||
END LINE (line_arg)[C1] <- (arguments_line)
MERGED TEXT: 
||| in [T:  Top] last of (line_arg)[C1]
MISC END node
GET_A_NEW_LINE
NEW LINE @top Test manual\n
COMMAND @top
ABORT EMPTY in @node[C2]: empty_line; ||
NEW TEXT (merge):  Test manual|||
END LINE (line_arg)[C1] <- (arguments_line)
MERGED TEXT: 
||| in [T:  Test manual] last of (line_arg)[C1]
MISC END top
SET ASSOC TITLE COMMAND
Trace begun at ../tta/perl/../perl/Texinfo/ParserNonXS.pm line 3851
Texinfo::Parser::_associate_title_command_anchor('HASH(0x589be0ab56a0)', 
'Texinfo::TreeElement=HASH(0x589be0ab5778)', 'HASH(0x589be0ab5a30)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 4381
Texinfo::Parser::_end_line_misc_line('Texinfo::Parser=HASH(0x589be09be880)', 
'Texinfo::TreeElement=HASH(0x589be0ab5820)', 'HASH(0x589be0aac998)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 5011
Texinfo::Parser::_end_line('Texinfo::Parser=HASH(0x589be09be880)', 
'Texinfo::TreeElement=HASH(0x589be0ab5820)', 'HASH(0x589be0aac998)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 8005
Texinfo::Parser::_process_remaining_on_line('Texinfo::Parser=HASH(0x589be09be880)',
 'Texinfo::TreeElement=HASH(0x589be0ab5820)', '^J', 'HASH(0x589be0aac998)') 
called at ../tta/perl/../perl/Texinfo/ParserNonXS.pm line 8088
Texinfo::Parser::_parse_texi('Texinfo::Parser=HASH(0x589be09be880)', 
'Texinfo::TreeElement=HASH(0x589be06ddd78)') called at 
../tta/perl/../perl/Texinfo/ParserNonXS.pm line 1107
Texinfo::Parser::_parse_texi_document('Texinfo::Parser=HASH(0x589be09be880)') 
called at ../tta/perl/../perl/Texinfo/ParserNonXS.pm line 998
Texinfo::Parser::parse_file('Texinfo::Parser=HASH(0x589be09be880)', 
'implicit-node-top.texi') called at ../tta/perl/texi2any.pl line 1818
GET_A_NEW_LINE
NEW LINE \n
END LINE @top[C2] <- (document_root)
MERGED TEXT: 
||| in (empty_line)[T] last of @top[C2]
END EMPTY LINE in @top[C2]
GET_A_NEW_LINE
...

For comparison:

$ TEXINFO_XS=omit ../tta/perl/texi2any.pl implicit-node-top.texi --html 
--no-split -c DEBUG=1
!!!!!!!!!!!!!!!! RESETTING THE PARSER !!!!!!!!!!!!!!!!!!!!!
NEW LINE @top Test manual\n
COMMAND @top
ABORT EMPTY in (before_node_section)[C2]: empty_line; ||
NEW TEXT (merge):  Test manual|||
END LINE (line_arg)[C1] <- (arguments_line)
MERGED TEXT: 
||| in [T:  Test manual] last of (line_arg)[C1]
MISC END top
GET_A_NEW_LINE
NEW LINE \n
...


So where it seems to go wrong is at the end of the "@top" line in the
input, at these lines in Texinfo/ParserNonXS.pm:

    # associate the section (not part) with the current node.
    if ($command ne 'node' and $command ne 'part') {
      # associate section with the current node as its title.
      if (exists($self->{'current_node'})) {
        my $node_relations = $self->{'current_node'};
        _associate_title_command_anchor($node_relations, $command_element,
                                        $section_relations);

I haven't investigated any more, but I would expect that if @node commands
are later added to the Texinfo tree, this association between @node and
sectioning commands would have to be re-done.  It also seems possible that
sectioning commands are associated to the wrong node, as well as no node
at all.

Reply via email to