From b60e554fe64b70cc1eb99e304e43785ce0465ee9 Mon Sep 17 00:00:00 2001 From: Nigel Date: Wed, 8 Nov 2023 19:15:18 +0000 Subject: [PATCH] Sort pagemarks correctly when adding HTML pagenums Although `markNext` iterates through the marks in order of their location, it does not guarantee an order when two marks have the same location. This could lead to some page numbers that directly followed blank pages not being output to the HTML file. To resolve it, just sort a list of page markers and use that, rather than iterating through the markers in the file. Fixes #1272 --- src/lib/Guiguts/HTMLConvert.pm | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/lib/Guiguts/HTMLConvert.pm b/src/lib/Guiguts/HTMLConvert.pm index 901e3d27..0b0f41cb 100644 --- a/src/lib/Guiguts/HTMLConvert.pm +++ b/src/lib/Guiguts/HTMLConvert.pm @@ -1245,20 +1245,19 @@ sub html_convert_sidenotes { sub html_convert_pageanchors { my $textwindow = $::textwindow; ::working("Inserting Page Number Markup"); - $|++; my @pagerefs; # keep track of first/last page markers at the same position - my $tempcounter; - my $mark = '1.0'; - while ( $textwindow->markPrevious($mark) ) { - $mark = $textwindow->markPrevious($mark); - } my $closure = voidclosure(); - # Work through all the text markers - while ( $mark = $textwindow->markNext($mark) ) { - next unless $mark =~ m{Pg(\S+)}; # Only look at page markers - my $markindex = $textwindow->index($mark); # Get page marker's index + # Get a correctly sorted list of page markers - if two markers are at the + # same location, the order returned by markNext is not defined, but we need + # them ordered by png. + my @pagemarklist = sort grep ( /^Pg\S+$/, $textwindow->markNames ); + + for my $idx ( 0 .. $#pagemarklist ) { + my $mark = $pagemarklist[$idx]; + my $markindex = $textwindow->index($mark); # Get page marker's index + my $marknext = $idx < $#pagemarklist ? $pagemarklist[ $idx + 1 ] : ""; # This is the custom page label my $num = $::pagenumbers{$mark}{label}; @@ -1278,12 +1277,6 @@ sub html_convert_pageanchors { push @pagerefs, $num; } - # Find next page marker & its index - my $marknext = $mark; - while ( $marknext = $textwindow->markNext($marknext) ) { - last if $marknext =~ m{Pg(\S+)}; - } - # If no more marks (reached end of file) or there are word characters between the # current mark and the next, then convert batch of accumulated page markers to a string my $pagereference = '';