sub build_single()

in lib/ES/Util.pm [143:259]


sub build_single {
#===================================
    my ( $index, $raw_dest, $dest, %opts ) = @_;

    my $single = 1;
    my $type = $opts{type} || 'book';
    my $toc = $opts{toc} || '';
    my $lenient   = $opts{lenient}       || '';
    my $version   = $opts{version}       || '';
    my $multi     = $opts{multi}         || 0;
    my $lang      = $opts{lang}          || 'en';
    my $edit_urls = $opts{edit_urls};
    my $section   = $opts{section_title} || '';
    my $subject   = $opts{subject}       || '';
    my $private   = $opts{private}       || '';
    my $noindex   = $opts{noindex}       || '';
    my $resources = $opts{resource}      || [];
    my $page_header = custom_header($index) || $opts{page_header} || '';
    my $latest    = $opts{latest};
    my $respect_edit_url_overrides = $opts{respect_edit_url_overrides} || '';
    my $alternatives = $opts{alternatives} || [];
    my $alternatives_summary = $raw_dest->file('alternatives_summary.json');
    my $branch = $opts{branch};
    my $roots = $opts{roots};
    my $relativize = $opts{relativize};
    my $extra = $opts{extra} || 0;

    die "Can't find index [$index]" unless -f $index;

    unless ( $opts{is_toc} ) {
        # Usually books live in their own directory so we can just `rm -rf`
        # those directories and start over. But the Table of Contents for all
        # vrsions of a book is written to the directory that contains all of
        # the versions of that book. `rm -rf`ed there we'd lose all of the
        # versions of the book. So we just don't.
        $dest->rmtree;
        $dest->mkpath;
        $raw_dest->rmtree;
        $raw_dest->mkpath;
    }

    my ( $output, $died );

    # Emulate asciidoc_dir because we use it to find shared asciidoc files
    # but asciidoctor doesn't support it.
    my $asciidoc_dir = dir('resources/asciidoc-8.6.8/')->absolute;
    # We use the admonishment images from asciidoc so add it as a resource
    # so we can find them
    push @$resources, $asciidoc_dir;
    eval {
        $output = run(
            'asciidoctor', '-v', '--trace',
            '-r' => dir('resources/asciidoctor/lib/extensions.rb')->absolute,
            '-b' => 'html5',
            '-d' => $type,
            '-a' => 'showcomments=1',
            '-a' => "lang=$lang",
            '-a' => "source_branch=$branch",
            $private || !$edit_urls ? () : ( '-a' => "edit_urls=" .
                edit_urls_for_asciidoctor($edit_urls) ),
            '-a' => 'asciidoc-dir=' . $asciidoc_dir,
            '-a' => 'resources=' . join(',', @$resources),
            $latest ? () : ('-a' => "migration-warnings=false"),
            $respect_edit_url_overrides ? ('-a' => "respect_edit_url_overrides=true") : (),
            @{ $alternatives } ? (
                '-a' => _format_alternatives($alternatives),
                '-a' => "alternative_language_report=$raw_dest/alternatives_report.json",
                '-a' => "alternative_language_summary=$alternatives_summary",
            ) : (),
            # Disable warning on missing attributes because we have
            # missing attributes!
            # '-a' => 'attribute-missing=warn',
            $relativize ? ('-a' => 'relativize-link=https://www.elastic.co/') : (),
            roots_opts( $roots ),
            # Turn off style options because we'll provide our own
            '-a' => 'stylesheet!',
            '-a' => 'icons!',
            # Turn off asciidoctor's default footer because we make our own
            '-a' => 'nofooter',
            # Add some metadata
            '-a' => 'dc.type=Learn/Docs/' . $section,
            '-a' => 'dc.subject=' . $subject,
            '-a' => 'dc.identifier=' . $version,
            $multi ? ( '-a' => "title-extra= [$version]" ) : (),
            $noindex ? ('-a' => 'noindex') : (),
            $page_header ? ('-a' => "page-header=$page_header") : (),
            # Turn on asciidoctor's table of contents generation if we want a TOC
            $toc ? ('-a' => 'toc') : (),
            '--destination-dir=' . $raw_dest,
            docinfo($index),
            $index
        );
        1;
    } or do { $output = $@; $died = 1; };
    _check_build_error( $output, $died, $lenient );

    my $base_name = $index->basename;
    $base_name =~ s/\.[^.]+$/.html/;

    my $html_file = $raw_dest->file('index.html');
    if ( $base_name ne 'index.html' ) {
        my $src = $raw_dest->file($base_name);
        rename $src, $html_file
            or die "Couldn't rename <$src> to <index.html>: $!";
    }

    _customize_title_page( $index, $html_file, $single );

    if ( $extra ) {
        my $contents = $html_file->slurp( iomode => '<:encoding(UTF-8)' );
        $contents =~ s{<!--EXTRA-->}{<!--EXTRA-->\n<div id="extra">\n$extra\n</div>} or
            die "Couldn't add toc_extra to $contents";
        $html_file->spew( iomode => '>:utf8', $contents );
    }

    finish_build( $index->parent, $raw_dest, $dest, $lang, $opts{is_toc} );
}