sub _update_title_and_version_drop_downs()

in lib/ES/Book.pm [350:402]


sub _update_title_and_version_drop_downs {
#===================================
    my ( $self, $version_dir, $branch ) = @_;

    my $title = '<li id="book_title"><span>' . $self->title . ': ';
    $title .= '<select id="live_versions">';
    my $removed_any = 0;
    for my $b ( @{ $self->branches } ) {
        my $live = grep( /^$b$/, @{ $self->{live_branches} } );
        unless ( $live || $branch eq $b ) {
            $removed_any = 1;
            next;
        }
        my $version = $self->branch_title($b);

        $title .= '<option value="' . $version . '"';
        $title .= ' selected'  if $branch eq $b;
        $title .= '>' . $version;
        $title .= '</option>';
    }
    $title .= '<option value="other">other versions</option>' if $removed_any;
    $title .= '</select>';
    if ( $removed_any ) {
        $title .= '<span id="other_versions">other versions: <select>';
        for my $b ( @{ $self->branches } ) {
            my $version = $self->branch_title($b);

            $title .= '<option value="' . $version . '"';
            $title .= ' selected'  if $branch eq $b;
            $title .= '>' . $version;
            $title .= '</option>';
        }
        $title .= '</select>';
    }
    $title .= '</span></li>';
    for ( 'toc.html', 'index.html' ) {
        my $file = $version_dir->file($_);
        # Ignore missing files because the books haven't been built yet. This
        # can happen after a new branch is added to the config and then we use
        # --keep_hash to prevent building new books, like for PR tests.
        next unless -e $file;

        my $html = $file->slurp( iomode => "<:encoding(UTF-8)" );

        # If a book uses a custom index page, it may not include the TOC. The
        # substitution below will fail, so we abort early in this case.
        next unless ($_ == 'index.html' && ($html =~ /ul class="toc"/));

        my $success = ($html =~ s/<ul class="toc">(?:<li id="book_title">.+?<\/li>)?\n?<li>/<ul class="toc">${title}<li>/);
        die "couldn't update version" unless $success;
        $file->spew( iomode => '>:utf8', $html );
    }
}