From 835dd63b03c3ca79ae0a9fa799d50a259fd7dab5 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 7 Oct 2016 12:56:55 +0200 Subject: [PATCH] asciidoc-pve.in: add some real functionality --- Makefile | 19 +++-- asciidoc-pve.in | 220 +++++++++++++++++++++++++++++++++++++++++++++++- scan-adoc-refs | 1 + 3 files changed, 230 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 6cd5684..87926bd 100644 --- a/Makefile +++ b/Makefile @@ -165,9 +165,14 @@ link-refs.json: scan-adoc-refs ${PVE_ADMIN_GUIDE_SOURCES} asciidoc-pve: asciidoc-pve.in link-refs.json cat asciidoc-pve.in link-refs.json >asciidoc-pve.tmp + sed -e s/@RELEASE@/${DOCRELEASE}/ -i asciidoc-pve.tmp chmod +x asciidoc-pve.tmp mv asciidoc-pve.tmp asciidoc-pve +test: asciidoc-pve + ./asciidoc-pve compile-wiki-section pve-package-repos.adoc + #./asciidoc-pve compile-wiki-chapter ha-manager.adoc + WIKI_IMPORTS= \ section-pve-usbstick-plain.html \ section-getting-help-plain.html \ @@ -198,11 +203,11 @@ all: index.html %-nwdiag.svg: %.nwdiag nwdiag -T svg $*.nwdiag -o $@; -sysadmin-%-plain.html: %.adoc - asciidoc -s -a wiki -a 'leveloffset=-1' ${ADOC_STDARG} -o $@ $*.adoc +sysadmin-%-plain.html: asciidoc-pve %.adoc + ./asciidoc-pve compile-wiki-section -o $@ $*.adoc -section-%-plain.html: %.adoc - asciidoc -s -a wiki -a 'leveloffset=-1' ${ADOC_STDARG} -o $@ $*.adoc +section-%-plain.html: asciidoc-pve %.adoc + ./asciidoc-pve compile-wiki-section -o $@ $*.adoc chapter-sysadmin.html chapter-sysadmin-plain.html: ${SYSADMIN_SOURCES} @@ -210,10 +215,10 @@ chapter-%.html: %.adoc ${PVE_COMMON_DOC_SOURCES} asciidoc ${ADOC_STDARG} -a toc -o $@ $*.adoc chapter-%-plain.html: %.adoc ${PVE_COMMON_DOC_SOURCES} - asciidoc -s -a wiki ${ADOC_STDARG} -o $@ $*.adoc + ./asciidoc-pve compile-wiki-chapter -o $@ $*.adoc pve-storage-%-plain.html: pve-storage-%.adoc ${PVE_COMMON_DOC_SOURCES} - asciidoc -s -a wiki -a 'leveloffset=-1' ${ADOC_STDARG} -o $@ pve-storage-$*.adoc + ./asciidoc-pve compile-wiki-section -o $@ pve-storage-$*.adoc %.1.html: %.adoc %.1-synopsis.adoc ${PVE_COMMON_DOC_SOURCES} asciidoc ${ADOC_MAN1_HTML_ARGS} -o $@ $*.adoc @@ -308,5 +313,5 @@ update: clean make all clean: - rm -rf *.tmp.xml *.html *.pdf *.epub *.tmp *.1 *.5 *.8 *.deb *.changes build api-viewer/apidoc.js chapter-*.html chapter-*-plain.html chapter-*.html pve-admin-guide.chunked asciidoc-pve link-refs.json + rm -rf *.tmp.xml *.html *.pdf *.epub *.tmp *.1 *.5 *.8 *.deb *.changes build api-viewer/apidoc.js chapter-*.html chapter-*-plain.html chapter-*.html pve-admin-guide.chunked asciidoc-pve link-refs.json .asciidoc-pve-tmp_* find . -name '*~' -exec rm {} ';' diff --git a/asciidoc-pve.in b/asciidoc-pve.in index 550cd4b..1d0c791 100644 --- a/asciidoc-pve.in +++ b/asciidoc-pve.in @@ -2,15 +2,229 @@ use strict; use warnings; +use Getopt::Long; +use File::Path; +use File::Basename; +use IO::File; + use JSON; +my $release = '@RELEASE@'; + +my $clicmd = shift or + die "no command specified\n"; + my $data_str = ""; while () { $data_str .= $_; } my $fileinfo = decode_json($data_str); - -print to_json($fileinfo, { pretty => 1 }); -die "implement something useful instead"; +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 = {}; + +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; + } +} + +sub replace_wiki_xref { + my ($blockid, $text) = @_; + + my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid}; + + die "unable to resolve wiki link (xref:$blockid)\n" + if !defined($link); + + return "$link\[$text\]"; +} + +sub prepare_adoc_file { + my ($filename, $attributes) = @_; + + return if $prepared_files->{$filename}; + + print "PREPARE $filename\n"; + + $prepared_files->{$filename} = 1; + + my $dirname = dirname($filename); + my $basename = basename($filename); + + my $outfilename = "$dirname/${tmpprefix}$basename"; + + 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"; + + while (defined (my $line = <$fh>)) { + 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"; + my $new_fn = prepare_adoc_file($fn, $attributes); + $line = "include::${new_fn}$rest\n"; + } + + # fix xrefs + $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_wiki_xref($1,$2)/ge; + + + print $outfh $line; + } + + return $outfilename; +} + +sub compile_wiki { + + my $verbose; + my $outfile; + my $target_env; + + GetOptions ("outfile=s" => \$outfile, + "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"; + + my $env = 'wiki'; + + + my $title = $fileinfo->{titles}->{$env}->{$infile} or + die "unable to get title for '$infile'\n"; + + + print "compile: $title\n"; + + my $leveloffset = 0; + + if ($clicmd eq 'compile-wiki-chapter') { + $leveloffset = 0; + } elsif ($clicmd eq 'compile-wiki-section') { + $leveloffset = -1; + } + + my $date = `date`; + chomp $date; + + my $attributes = { + $env => undef, + icons => undef, + 'data-uri' => undef, + date => $date, + leveloffset => $leveloffset, + revnumber => $release, + }; + + my $cmd = ['asciidoc', '-s']; + + 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; + + if (!defined($outfile)) { + $outfile = $infile; + $outfile =~ s/\.adoc$//; + $outfile .= ".html"; + } + + push @$cmd, '--out-file', $outfile; + + my $new_infile = prepare_adoc_file($infile, $attributes); + + push @$cmd, $new_infile; + + print "RUN " . join(' ', @$cmd) . "\n"; + + system(@$cmd) == 0 or + die "aciidoc error"; +} + +if ($clicmd eq 'compile-wiki-chapter' || + $clicmd eq 'compile-wiki-section') { + + eval { compile_wiki(); }; + my $err = $@; + + # cleanup + + die $err if $err; + +} else { + + die "unknown command '$clicmd'\n"; + +} + + + + + + +exit 0; __END__ diff --git a/scan-adoc-refs b/scan-adoc-refs index 2328bed..1ddc935 100755 --- a/scan-adoc-refs +++ b/scan-adoc-refs @@ -40,6 +40,7 @@ sub push_environment { die "undefined environment '$env'\n" if !defined($environments->{$env}); + # FIXME: this seems wrong (nested env?)? return if !$environments->{$env}; # do not track if ($not) { -- 2.39.2