Skip to content

Commit

Permalink
Add the option to skip docbook for single page (#1391)
Browse files Browse the repository at this point in the history
This adds the option to generate html directly from asciidoctor for
single page books with `--direct_html`. Right now no books use this but
we'd like to migrate all of them too it once we're sure the output looks
good for them!

Relates to #743
  • Loading branch information
nik9000 authored Nov 8, 2019
1 parent ce0954a commit 4439415
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 42 deletions.
5 changes: 5 additions & 0 deletions build_docs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ sub build_local {
);
}
else {
die '--direct_html not yet supported' if $Opts->{direct_html};
build_chunked( $index, $raw_dir, $dir, %$Opts,
latest => $latest,
alternatives => \@alternatives,
Expand Down Expand Up @@ -906,6 +907,7 @@ sub command_line_opts {
# Options only compatible with --doc
'doc=s',
'alternatives=s@',
'direct_html',
'chunk=i',
'lang=s',
'lenient',
Expand Down Expand Up @@ -956,6 +958,8 @@ sub usage {
--chunk 1 Also chunk sections into separate files
--alternatives <source_lang>:<alternative_lang>:<dir>
Examples in alternative languages.
--direct_html Generate html directly from Asciidoctor without
using docbook.
--lang Defaults to 'en'
--lenient Ignore linking errors
--out dest/dir/ Defaults to ./html_docs.
Expand Down Expand Up @@ -1033,6 +1037,7 @@ sub check_opts {
die('--alternatives only compatible with --doc') if $Opts->{alternatives};
die('--chunk only compatible with --doc') if $Opts->{chunk};
# Lang will be 'en' even if it isn't specified so we don't check it.
die('--direct_html only compatible with --doc') if $Opts->{direct_html};
die('--lenient only compatible with --doc') if $Opts->{lenient};
die('--out only compatible with --doc') if $Opts->{out};
die('--pdf only compatible with --doc') if $Opts->{pdf};
Expand Down
1 change: 0 additions & 1 deletion lib/ES/Toc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ sub write {
build_single( $adoc_file, $raw_dir, $dir,
type => 'article',
lang => $self->lang,
direct_html => 1,
is_toc => 1,
latest => 1, # Run all of our warnings
private => 1, # Don't generate edit me urls
Expand Down
87 changes: 49 additions & 38 deletions lib/ES/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ sub build_chunked {
my $branch = $opts{branch};
my $roots = $opts{roots};
my $relativize = $opts{relativize};
my $direct_html = $opts{direct_html};

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

Expand Down Expand Up @@ -191,6 +192,7 @@ sub build_single {
my $branch = $opts{branch};
my $roots = $opts{roots};
my $relativize = $opts{relativize};
my $direct_html = $opts{direct_html} || 0;

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

Expand All @@ -206,27 +208,7 @@ sub build_single {
$raw_dest->mkpath;
}

my %xsltopts = (
"generate.toc" => $toc,
"toc.section.depth" => 0,
"local.book.version" => $version,
"local.book.multi_version" => $multi,
"local.page.header" => $page_header,
"local.book.section.title" => "Learn/Docs/$section",
"local.book.subject" => $subject,
"local.noindex" => $noindex,
'navig.graphics' => 1,
'admon.textlabel' => 0,
'admon.graphics' => 1,
);
if ( $type eq 'book' ) {
$xsltopts{'chunk.section.depth'} = 0;
}

my ( $output, $died );
my $dest_xml = $index->basename;
$dest_xml =~ s/\.a(scii)?doc$/\.xml/;
$dest_xml = $raw_dest->file($dest_xml);

# Emulate asciidoc_dir because we use it to find shared asciidoc files
# but asciidoctor doesn't support it.
Expand All @@ -238,7 +220,7 @@ sub build_single {
$output = run(
'asciidoctor', '-v', '--trace',
'-r' => dir('resources/asciidoctor/lib/extensions.rb')->absolute,
'-b' => 'docbook45',
'-b' => $direct_html ? 'html5' : 'docbook45',
'-d' => $type,
'-a' => 'showcomments=1',
'-a' => "lang=$lang",
Expand All @@ -259,6 +241,11 @@ sub build_single {
# '-a' => 'attribute-missing=warn',
$relativize ? ('-a' => 'relativize-link=https://www.elastic.co/') : (),
roots_opts( $roots ),
$direct_html ? (
# Turn off style options because we'll provide our own
'-a' => 'stylesheet!',
'-a' => 'icons!',
) : (),
'--destination-dir=' . $raw_dest,
docinfo($index),
$index
Expand All @@ -267,25 +254,47 @@ sub build_single {
} or do { $output = $@; $died = 1; };
_check_build_error( $output, $died, $lenient );

if ( !$lenient ) {
unless ( $direct_html ) {
my $dest_xml = $index->basename;
$dest_xml =~ s/\.a(scii)?doc$/.xml/;
$dest_xml = $raw_dest->file($dest_xml);

if ( !$lenient ) {
eval {
$output = _xml_lint($dest_xml);
1;
} or do { $output = $@; $died = 1; };
_check_build_error( $output, $died, $lenient );
}
my %xsltopts = (
"generate.toc" => $toc,
"toc.section.depth" => 0,
"local.book.version" => $version,
"local.book.multi_version" => $multi,
"local.page.header" => $page_header,
"local.book.section.title" => "Learn/Docs/$section",
"local.book.subject" => $subject,
"local.noindex" => $noindex,
'navig.graphics' => 1,
'admon.textlabel' => 0,
'admon.graphics' => 1,
);
if ( $type eq 'book' ) {
$xsltopts{'chunk.section.depth'} = 0;
}
eval {
$output = _xml_lint($dest_xml);
$output = run(
'xsltproc',
rawxsltopts(%xsltopts),
'--output' => "$raw_dest/index.html",
file('resources/website.xsl')->absolute,
$dest_xml
);
1;
} or do { $output = $@; $died = 1; };
_check_build_error( $output, $died, $lenient );
unlink $dest_xml;
}
eval {
$output = run(
'xsltproc',
rawxsltopts(%xsltopts),
'--output' => "$raw_dest/index.html",
file('resources/website.xsl')->absolute,
$dest_xml
);
1;
} or do { $output = $@; $died = 1; };
_check_build_error( $output, $died, $lenient );
unlink $dest_xml;

my $base_name = $index->basename;
$base_name =~ s/\.[^.]+$/.html/;
Expand All @@ -297,9 +306,11 @@ sub build_single {
or die "Couldn't rename <$src> to <index.html>: $!";
}

my $contents = $html_file->slurp( iomode => '<:encoding(UTF-8)' );
$contents = _html5ify( $contents );
$html_file->spew( iomode => '>:utf8', $contents );
unless ( $direct_html ) {
my $contents = $html_file->slurp( iomode => '<:encoding(UTF-8)' );
$contents = _html5ify( $contents );
$html_file->spew( iomode => '>:utf8', $contents );
}

finish_build( $index->parent, $raw_dest, $dest, $lang, $opts{is_toc} );
}
Expand Down
11 changes: 8 additions & 3 deletions template/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ module.exports = templateSource => {
* at the end of the chunk in case the marker is on the edge. */
const slice = Math.max(0, chunk.length - preserve);
chunk = chunk.slice(slice) + result.value;
// TODO check if slice + keeps a copy of internal memory. This implementation assumes it *doesn't*
} else {
chunk = result.value;
}
Expand Down Expand Up @@ -108,7 +107,12 @@ module.exports = templateSource => {
yield* template.gather("<!-- DOCS LANG -->");
yield `lang="${lang}"`;
yield* template.gather("<!-- DOCS BODY -->");
await raw.dump("<body>");
/*
* Docbook spits out <body> and asciidoctor spits out <body class=....>
* Either way, we just want what comes after the body tag.
*/
await raw.dump("<body");
await raw.dump(">");
yield* raw.gather("</body>");
yield* template.gather("<!-- DOCS FINAL -->");
yield `<script type="text/javascript">
Expand Down Expand Up @@ -189,7 +193,8 @@ module.exports = templateSource => {
const out = apply(raw[Symbol.asyncIterator](), lang, initialJsState);
write.on("close", resolve);
write.on("error", reject);
out.on("error", write.destroy);
// out.on("error", write.destroy) doesn't properly forward the error!
out.on("error", err => write.destroy(err));
out.pipe(write);
}).finally(() => raw.close());
};
Expand Down

0 comments on commit 4439415

Please sign in to comment.