2016-10-06 18:29:03 +03:00
#!/usr/bin/perl
use strict;
use warnings;
2016-10-07 13:56:55 +03:00
use Getopt::Long;
use File::Path;
use File::Basename;
use IO::File;
2016-10-06 18:29:03 +03:00
use JSON;
2016-10-11 17:48:31 +03:00
my $verbose;
my $keep_artifacts;
2016-10-07 13:56:55 +03:00
my $release = '@RELEASE@';
my $clicmd = shift or
die "no command specified\n";
2016-10-06 18:29:03 +03:00
my $data_str = "";
while (<main::DATA>) { $data_str .= $_; }
my $fileinfo = decode_json($data_str);
2016-10-07 13:56:55 +03:00
my $tmpprefix = ".asciidoc-pve-tmp_";
my $adoc_source_dir = "/usr/share/pve-doc-generator";
# inside pve-docs source dir?
if (-f "attributes.txt" && -f "pve-admin-guide.adoc") {
$adoc_source_dir = "."
}
my $prepared_files = {};
2016-10-11 11:28:30 +03:00
my $man_target_html = 0;
2016-10-07 13:56:55 +03:00
my $env_stack = [];
my $env_skip = 0;
sub push_environment {
my ($env, $skip) = @_;
$skip = 1 if $env_skip;
$skip = 0 if !defined($skip);
push @$env_stack, [$env, $skip];
$env_skip = $skip;
}
sub pop_environment {
my ($env) = @_;
my $last_stack_entry = pop @$env_stack;
die "unable to pop env '$env'" if !defined($last_stack_entry);
my ($last_env, $skip) = @$last_stack_entry;
die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
if (!scalar(@$env_stack)) {
$env_skip = 0;
} else {
my (undef, $skip) = @{$env_stack->[-1]};
$env_skip = $skip;
}
}
2016-10-11 17:48:31 +03:00
my $files_for_cleanup = [];
2016-10-10 11:18:56 +03:00
sub cleanup {
2016-10-11 17:48:31 +03:00
return if $keep_artifacts;
foreach my $file (@$files_for_cleanup) {
unlink $file;
}
2016-10-10 11:18:56 +03:00
}
2016-10-07 13:56:55 +03:00
sub replace_wiki_xref {
my ($blockid, $text) = @_;
my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid};
2016-10-08 14:07:08 +03:00
my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid};
2016-10-07 13:56:55 +03:00
die "unable to resolve wiki link (xref:$blockid)\n"
if !defined($link);
2016-10-08 14:07:08 +03:00
$text = $reftext if !length($text);
2016-10-11 11:28:30 +03:00
die "xref: no text for wiki link '$blockid'\n" if !$text;
2016-10-09 09:01:49 +03:00
2016-10-07 13:56:55 +03:00
return "$link\[$text\]";
}
2016-10-11 11:28:30 +03:00
sub replace_default_xref {
my ($blockid, $text) = @_;
my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
die "unable to resolve chapter link (xref:$blockid)\n"
if !defined($link);
$text = $reftext if !length($text);
die "xref: no text for chapter link '$blockid'\n" if !$text;
return "$link\[$text\]";
}
sub replace_man_xref {
my ($blockid, $text) = @_;
my $link = $fileinfo->{blockid_target}->{manvolnum}->{$blockid};
my $reftext = $fileinfo->{reftext}->{manvolnum}->{$blockid};
die "unable to resolve man page link (xref:$blockid)\n"
if !defined($link);
$text = $reftext if !length($text);
die "xref: no text for man page link '$blockid'\n" if !$text;
my $section = $fileinfo->{mansection}->{manvolnum}->{$link};
die "link target is not a manual page" if !defined($section);
if (0 && $man_target_html) {
my $target = $link;
$target =~ s/\.adoc//;
$target .= ".$section";
return "link:${target}.html#${blockid}\[$text\]";
} else {
my $command = $link;
$command =~ s/\.adoc//;
return "\*${text}\* (man \*${command}\*($section))";
}
}
2016-10-10 11:41:27 +03:00
sub replace_xref {
my ($env, $blockid, $text) = @_;
2016-10-07 13:56:55 +03:00
2016-10-10 11:41:27 +03:00
if ($env eq 'wiki') {
return replace_wiki_xref($blockid, $text);
2016-10-11 11:28:30 +03:00
} elsif ($env eq 'manvolnum') {
return replace_man_xref($blockid, $text);
} elsif ($env eq 'default') {
return replace_default_xref($blockid, $text);
2016-10-10 11:41:27 +03:00
} else {
2016-10-11 11:28:30 +03:00
die "internal error";
2016-10-10 11:41:27 +03:00
}
}
sub prepare_adoc_file {
my ($target_env, $filename, $attributes) = @_;
2016-10-09 09:01:49 +03:00
2016-10-11 11:28:30 +03:00
return $prepared_files->{$filename} if defined($prepared_files->{$filename});
2016-10-07 13:56:55 +03:00
print "PREPARE $filename\n";
my $dirname = dirname($filename);
my $basename = basename($filename);
my $outfilename = "$dirname/${tmpprefix}$basename";
2016-10-11 11:28:30 +03:00
$prepared_files->{$filename} = $outfilename;
2016-10-07 13:56:55 +03:00
my $fh = IO::File->new("$filename", "r") or
die "unable to open file '$filename' - $!\n";
my $outfh = IO::File->new("$outfilename", "w") or
die "unable to open temporary file '$outfilename'\n";
2016-10-11 17:48:31 +03:00
push @$files_for_cleanup, $outfilename;
2016-10-07 13:56:55 +03:00
while (defined (my $line = <$fh>)) {
2016-10-11 11:28:30 +03:00
chomp $line;
2016-10-07 13:56:55 +03:00
if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
my ($not, $env, $text) = ($1, $2, $3);
die "unsuported ifdef usage - implement me" if $text;
my $skip = !exists($attributes->{$env}) ? 1 : 0;
$skip = ($skip ? 0 : 1 ) if $not;
push_environment($env, $skip);
next;
} elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
my ($env, $text) = ($1, $2);
die "unsuported ifdef usage - implement me" if $text;
pop_environment($env);
next;
}
next if $env_skip;
if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
my ($fn, $rest) = ($1, $2);
print "INCLUDE: $fn\n";
2016-10-10 11:41:27 +03:00
my $new_fn = prepare_adoc_file($target_env, $fn, $attributes);
2016-10-11 11:28:30 +03:00
print $outfh "include::${new_fn}$rest\n";
next;
2016-10-07 13:56:55 +03:00
}
# fix xrefs
2016-10-10 11:41:27 +03:00
$line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
2016-10-07 13:56:55 +03:00
2016-10-10 11:41:27 +03:00
$line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
print $outfh $line . "\n";
2016-10-07 13:56:55 +03:00
}
return $outfilename;
}
2016-10-10 11:41:27 +03:00
sub compile_asciidoc {
my ($env) = @_;
2016-10-07 13:56:55 +03:00
my $outfile;
GetOptions ("outfile=s" => \$outfile,
2016-10-11 17:48:31 +03:00
"keep-artifacts" => \$keep_artifacts,
2016-10-07 13:56:55 +03:00
"verbose" => \$verbose) or
die("Error in command line arguments\n");
my $infile = shift(@ARGV) or
die "no input file specified\n";
scalar(@ARGV) == 0 or
die "too many arguments...\n";
2016-10-11 11:28:30 +03:00
defined($fileinfo->{titles}->{$env}) ||
2016-10-10 11:41:27 +03:00
die "unknown environment '$env'";
2016-10-07 13:56:55 +03:00
my $title = $fileinfo->{titles}->{$env}->{$infile} or
2016-10-11 11:28:30 +03:00
die "unable to get title for '$infile'$env\n";
2016-10-07 13:56:55 +03:00
print "compile: $title\n";
my $leveloffset = 0;
2016-10-11 11:28:30 +03:00
my $doctype = $fileinfo->{doctype}->{$env}->{$infile};
die "unable to get document type for '$infile'\n"
if !defined($doctype);
2016-10-10 10:14:25 +03:00
$leveloffset = - $doctype;
2016-10-07 13:56:55 +03:00
my $date = `date`;
chomp $date;
my $attributes = {
$env => undef,
leveloffset => $leveloffset,
};
2016-10-11 11:28:30 +03:00
my $mansection = $fileinfo->{mansection}->{$env}->{$infile};
if ($env eq 'wiki') {
} elsif ($env eq 'manvolnum') {
die "undefined man section" if !defined($mansection);
$attributes->{manvolnum} = $mansection;
} elsif ($env eq 'default') {
die "$infile: wrong doctype\n" if $doctype != 0;
2016-10-10 11:41:27 +03:00
$attributes->{toc} = undef;
}
2016-10-11 11:28:30 +03:00
if (!defined($outfile)) {
$outfile = $infile;
$outfile =~ s/\.adoc$//;
if ($env eq 'manvolnum') {
if ($man_target_html) {
$outfile .= ".$mansection.html";
} else {
$outfile .= ".$mansection";
}
2016-10-07 13:56:55 +03:00
} else {
2016-10-11 11:28:30 +03:00
$outfile .= ".html";
2016-10-07 13:56:55 +03:00
}
}
2016-10-11 11:28:30 +03:00
if (($env eq 'manvolnum') && !$man_target_html) {
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
# asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
# section like footnotes, so we cannot use a2x.
# We use xmlto instead.
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
foreach my $key (keys %$attributes) {
my $value = $attributes->{$key};
if (defined($value)) {
push @$cmd, '-a', "$key=$value";
} else {
push @$cmd, '-a', $key;
}
}
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
push @$cmd, '--verbose' if $verbose;
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
my $tmpxmlfile = "${outfile}.xml.tmp";
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
push @$cmd, '--out-file', $tmpxmlfile;
2016-10-11 17:48:31 +03:00
push @$files_for_cleanup, $tmpxmlfile;
2016-10-11 11:28:30 +03:00
my $new_infile = prepare_adoc_file($env, $infile, $attributes);
push @$cmd, $new_infile;
print "RUN " . join(' ', @$cmd) . "\n";
system(@$cmd) == 0 or
die "aciidoc error";
$cmd = ['xmlto', 'man', $tmpxmlfile];
push @$cmd, '-v' if $verbose;
print "RUN " . join(' ', @$cmd) . "\n";
system(@$cmd) == 0 or
die "xmlto error";
} else {
2016-10-07 13:56:55 +03:00
2016-10-11 11:28:30 +03:00
$attributes->{icons} = undef;
$attributes->{'data-uri'} = undef;
$attributes->{revnumber} = $release;
my $cmd = ['asciidoc'];
push @$cmd, '-s' if $env eq 'wiki';
foreach my $key (keys %$attributes) {
my $value = $attributes->{$key};
if (defined($value)) {
push @$cmd, '-a', "$key=$value";
} else {
push @$cmd, '-a', $key;
}
}
push @$cmd, '--verbose' if $verbose;
push @$cmd, '--out-file', $outfile;
my $new_infile = prepare_adoc_file($env, $infile, $attributes);
push @$cmd, $new_infile;
print "RUN " . join(' ', @$cmd) . "\n";
system(@$cmd) == 0 or
die "aciidoc error";
}
}
2016-10-10 11:41:27 +03:00
2016-10-10 10:14:25 +03:00
if ($clicmd eq 'compile-wiki') {
2016-10-07 13:56:55 +03:00
2016-10-10 11:41:27 +03:00
eval { compile_asciidoc('wiki'); };
2016-10-07 13:56:55 +03:00
my $err = $@;
2016-10-10 11:18:56 +03:00
cleanup();
2016-10-07 13:56:55 +03:00
die $err if $err;
2016-10-11 11:28:30 +03:00
} elsif ($clicmd eq 'compile-chapter') {
eval { compile_asciidoc('default'); };
my $err = $@;
cleanup();
die $err if $err;
} elsif ($clicmd eq 'compile-man-html') {
$man_target_html = 1;
eval { compile_asciidoc('manvolnum'); };
my $err = $@;
cleanup();
die $err if $err;
} elsif ($clicmd eq 'compile-man') {
eval { compile_asciidoc('manvolnum'); };
my $err = $@;
cleanup();
die $err if $err;
2016-10-07 13:56:55 +03:00
} else {
die "unknown command '$clicmd'\n";
}
exit 0;
2016-10-06 18:29:03 +03:00
__END__