Skip to content

Commit

Permalink
Sort pagemarks correctly when adding HTML pagenums
Browse files Browse the repository at this point in the history
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
  • Loading branch information
windymilla committed Nov 8, 2023
1 parent 81ec662 commit b60e554
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/lib/Guiguts/HTMLConvert.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 = '';
Expand Down

0 comments on commit b60e554

Please sign in to comment.