| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | use Getopt::Long; |
| 6 | use File::Path; |
| 7 | use File::Basename; |
| 8 | use IO::File; |
| 9 | |
| 10 | use JSON; |
| 11 | |
| 12 | my $release = '@RELEASE@'; |
| 13 | |
| 14 | my $clicmd = shift or |
| 15 | die "no command specified\n"; |
| 16 | |
| 17 | my $data_str = ""; |
| 18 | while (<main::DATA>) { $data_str .= $_; } |
| 19 | |
| 20 | my $fileinfo = decode_json($data_str); |
| 21 | |
| 22 | my $tmpprefix = ".asciidoc-pve-tmp_"; |
| 23 | |
| 24 | my $adoc_source_dir = "/usr/share/pve-doc-generator"; |
| 25 | |
| 26 | # inside pve-docs source dir? |
| 27 | if (-f "attributes.txt" && -f "pve-admin-guide.adoc") { |
| 28 | $adoc_source_dir = "." |
| 29 | } |
| 30 | |
| 31 | my $prepared_files = {}; |
| 32 | |
| 33 | my $env_stack = []; |
| 34 | my $env_skip = 0; |
| 35 | |
| 36 | sub push_environment { |
| 37 | my ($env, $skip) = @_; |
| 38 | |
| 39 | $skip = 1 if $env_skip; |
| 40 | $skip = 0 if !defined($skip); |
| 41 | |
| 42 | push @$env_stack, [$env, $skip]; |
| 43 | |
| 44 | $env_skip = $skip; |
| 45 | } |
| 46 | |
| 47 | sub pop_environment { |
| 48 | my ($env) = @_; |
| 49 | |
| 50 | my $last_stack_entry = pop @$env_stack; |
| 51 | die "unable to pop env '$env'" if !defined($last_stack_entry); |
| 52 | |
| 53 | my ($last_env, $skip) = @$last_stack_entry; |
| 54 | die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env; |
| 55 | |
| 56 | if (!scalar(@$env_stack)) { |
| 57 | $env_skip = 0; |
| 58 | } else { |
| 59 | my (undef, $skip) = @{$env_stack->[-1]}; |
| 60 | $env_skip = $skip; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | sub replace_wiki_xref { |
| 65 | my ($blockid, $text) = @_; |
| 66 | |
| 67 | my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid}; |
| 68 | my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid}; |
| 69 | |
| 70 | die "unable to resolve wiki link (xref:$blockid)\n" |
| 71 | if !defined($link); |
| 72 | |
| 73 | $text = $reftext if !length($text); |
| 74 | |
| 75 | die "xref: no text for '$blockid'\n" if !$text; |
| 76 | |
| 77 | return "$link\[$text\]"; |
| 78 | } |
| 79 | |
| 80 | sub prepare_adoc_file { |
| 81 | my ($filename, $attributes) = @_; |
| 82 | |
| 83 | |
| 84 | return if $prepared_files->{$filename}; |
| 85 | |
| 86 | print "PREPARE $filename\n"; |
| 87 | |
| 88 | $prepared_files->{$filename} = 1; |
| 89 | |
| 90 | my $dirname = dirname($filename); |
| 91 | my $basename = basename($filename); |
| 92 | |
| 93 | my $outfilename = "$dirname/${tmpprefix}$basename"; |
| 94 | |
| 95 | my $fh = IO::File->new("$filename", "r") or |
| 96 | die "unable to open file '$filename' - $!\n"; |
| 97 | |
| 98 | my $outfh = IO::File->new("$outfilename", "w") or |
| 99 | die "unable to open temporary file '$outfilename'\n"; |
| 100 | |
| 101 | while (defined (my $line = <$fh>)) { |
| 102 | if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) { |
| 103 | my ($not, $env, $text) = ($1, $2, $3); |
| 104 | die "unsuported ifdef usage - implement me" if $text; |
| 105 | |
| 106 | my $skip = !exists($attributes->{$env}) ? 1 : 0; |
| 107 | $skip = ($skip ? 0 : 1 ) if $not; |
| 108 | |
| 109 | push_environment($env, $skip); |
| 110 | next; |
| 111 | } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) { |
| 112 | my ($env, $text) = ($1, $2); |
| 113 | die "unsuported ifdef usage - implement me" if $text; |
| 114 | pop_environment($env); |
| 115 | next; |
| 116 | } |
| 117 | |
| 118 | next if $env_skip; |
| 119 | |
| 120 | if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) { |
| 121 | my ($fn, $rest) = ($1, $2); |
| 122 | print "INCLUDE: $fn\n"; |
| 123 | my $new_fn = prepare_adoc_file($fn, $attributes); |
| 124 | $line = "include::${new_fn}$rest\n"; |
| 125 | } |
| 126 | |
| 127 | # fix xrefs |
| 128 | $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_wiki_xref($1,$2)/ge; |
| 129 | |
| 130 | $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_wiki_xref($1,$2)/ge; |
| 131 | |
| 132 | print $outfh $line; |
| 133 | } |
| 134 | |
| 135 | return $outfilename; |
| 136 | } |
| 137 | |
| 138 | sub compile_wiki { |
| 139 | |
| 140 | my $verbose; |
| 141 | my $outfile; |
| 142 | my $target_env; |
| 143 | |
| 144 | GetOptions ("outfile=s" => \$outfile, |
| 145 | "verbose" => \$verbose) or |
| 146 | die("Error in command line arguments\n"); |
| 147 | |
| 148 | my $infile = shift(@ARGV) or |
| 149 | die "no input file specified\n"; |
| 150 | |
| 151 | scalar(@ARGV) == 0 or |
| 152 | die "too many arguments...\n"; |
| 153 | |
| 154 | my $env = 'wiki'; |
| 155 | |
| 156 | |
| 157 | my $title = $fileinfo->{titles}->{$env}->{$infile} or |
| 158 | die "unable to get title for '$infile'\n"; |
| 159 | |
| 160 | |
| 161 | print "compile: $title\n"; |
| 162 | |
| 163 | my $leveloffset = 0; |
| 164 | |
| 165 | if ($clicmd eq 'compile-wiki-chapter') { |
| 166 | $leveloffset = 0; |
| 167 | } elsif ($clicmd eq 'compile-wiki-section') { |
| 168 | $leveloffset = -1; |
| 169 | } |
| 170 | |
| 171 | my $date = `date`; |
| 172 | chomp $date; |
| 173 | |
| 174 | my $attributes = { |
| 175 | $env => undef, |
| 176 | icons => undef, |
| 177 | 'data-uri' => undef, |
| 178 | date => $date, |
| 179 | leveloffset => $leveloffset, |
| 180 | revnumber => $release, |
| 181 | }; |
| 182 | |
| 183 | my $cmd = ['asciidoc', '-s']; |
| 184 | |
| 185 | foreach my $key (keys %$attributes) { |
| 186 | my $value = $attributes->{$key}; |
| 187 | if (defined($value)) { |
| 188 | push @$cmd, '-a', "$key=$value"; |
| 189 | } else { |
| 190 | push @$cmd, '-a', $key; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | push @$cmd, '--verbose' if $verbose; |
| 195 | |
| 196 | if (!defined($outfile)) { |
| 197 | $outfile = $infile; |
| 198 | $outfile =~ s/\.adoc$//; |
| 199 | $outfile .= ".html"; |
| 200 | } |
| 201 | |
| 202 | push @$cmd, '--out-file', $outfile; |
| 203 | |
| 204 | my $new_infile = prepare_adoc_file($infile, $attributes); |
| 205 | |
| 206 | push @$cmd, $new_infile; |
| 207 | |
| 208 | print "RUN " . join(' ', @$cmd) . "\n"; |
| 209 | |
| 210 | system(@$cmd) == 0 or |
| 211 | die "aciidoc error"; |
| 212 | } |
| 213 | |
| 214 | if ($clicmd eq 'compile-wiki-chapter' || |
| 215 | $clicmd eq 'compile-wiki-section') { |
| 216 | |
| 217 | eval { compile_wiki(); }; |
| 218 | my $err = $@; |
| 219 | |
| 220 | # cleanup |
| 221 | |
| 222 | die $err if $err; |
| 223 | |
| 224 | } else { |
| 225 | |
| 226 | die "unknown command '$clicmd'\n"; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | exit 0; |
| 236 | |
| 237 | __END__ |