]>
git.proxmox.com Git - pve-docs.git/blob - asciidoc-pve.in
15 my $release = '@RELEASE@';
18 die "no command specified\n";
21 while (<main
::DATA
>) { $data_str .= $_; }
23 my $fileinfo = decode_json
($data_str);
25 my $tmpprefix = ".asciidoc-pve-tmp_";
27 my $adoc_source_dir = "/usr/share/pve-doc-generator";
29 # inside pve-docs source dir?
30 if (-f
"attributes.txt" && -f
"pve-admin-guide.adoc") {
31 $adoc_source_dir = "."
34 my $prepared_files = {};
36 my $man_target_html = 0;
45 print STDERR
"asciidoc-pve: $msg\n";
48 sub push_environment
{
49 my ($env, $skip) = @_;
51 $skip = 1 if $env_skip;
52 $skip = 0 if !defined($skip);
54 push @$env_stack, [$env, $skip];
62 my $last_stack_entry = pop @$env_stack;
63 die "unable to pop env '$env'" if !defined($last_stack_entry);
65 my ($last_env, $skip) = @$last_stack_entry;
66 die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
68 if (!scalar(@$env_stack)) {
71 my (undef, $skip) = @{$env_stack->[-1]};
76 my $files_for_cleanup = [];
80 return if $keep_artifacts;
82 foreach my $file (@$files_for_cleanup) {
87 sub replace_wiki_xref
{
88 my ($blockid, $text) = @_;
90 my $link = $fileinfo->{blockid_target
}->{wiki
}->{$blockid};
91 my $reftext = $fileinfo->{reftext
}->{wiki
}->{$blockid};
93 die "unable to resolve wiki link (xref:$blockid)\n"
96 $text = $reftext if !length($text);
98 die "xref: no text for wiki link '$blockid'\n" if !$text;
100 return "$link\[$text\]";
103 sub replace_default_xref
{
104 my ($blockid, $text) = @_;
106 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
107 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
109 die "unable to resolve chapter link (xref:$blockid)\n"
112 $text = $reftext if !length($text);
114 die "xref: no text for chapter link '$blockid'\n" if !$text;
116 return "$link\[$text\]";
119 sub replace_man_xref
{
120 my ($blockid, $text) = @_;
122 my $link = $fileinfo->{blockid_target
}->{manvolnum
}->{$blockid};
123 my $reftext = $fileinfo->{reftext
}->{manvolnum
}->{$blockid};
125 die "unable to resolve man page link (xref:$blockid)\n"
128 $text = $reftext if !length($text);
130 die "xref: no text for man page link '$blockid'\n" if !$text;
132 my $section = $fileinfo->{mansection
}->{manvolnum
}->{$link};
133 die "link target is not a manual page" if !defined($section);
136 if (0 && $man_target_html) {
138 $target =~ s/\.adoc//;
139 $target .= ".$section";
140 return "link:${target}.html#${blockid}\[$text\]";
143 $command =~ s/\.adoc//;
144 return "\*${text}\* (man \*${command}\*($section))";
149 my ($env, $blockid, $text) = @_;
151 if ($env eq 'wiki') {
152 return replace_wiki_xref
($blockid, $text);
153 } elsif ($env eq 'manvolnum') {
154 return replace_man_xref
($blockid, $text);
155 } elsif ($env eq 'default') {
156 return replace_default_xref
($blockid, $text);
158 die "internal error";
162 sub prepare_adoc_file
{
163 my ($target_env, $filename, $attributes) = @_;
165 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
167 debug
("prepare $filename");
169 my $dirname = dirname
($filename);
170 my $basename = basename
($filename);
172 my $outfilename = "$dirname/${tmpprefix}$basename";
174 $prepared_files->{$filename} = $outfilename;
176 my $fh = IO
::File-
>new("$filename", "r") or
177 die "unable to open file '$filename' - $!\n";
179 my $outfh = IO
::File-
>new("$outfilename", "w") or
180 die "unable to open temporary file '$outfilename'\n";
182 push @$files_for_cleanup, $outfilename;
184 while (defined (my $line = <$fh>)) {
186 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
187 my ($not, $env, $text) = ($1, $2, $3);
188 die "unsuported ifdef usage - implement me" if $text;
190 my $skip = !exists($attributes->{$env}) ?
1 : 0;
191 $skip = ($skip ?
0 : 1 ) if $not;
193 push_environment
($env, $skip);
195 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
196 my ($env, $text) = ($1, $2);
197 die "unsuported ifdef usage - implement me" if $text;
198 pop_environment
($env);
204 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
205 my ($fn, $rest) = ($1, $2);
206 debug
("include $fn");
207 my $new_fn = prepare_adoc_file
($target_env, $fn, $attributes);
209 print $outfh "include::${new_fn}$rest\n";
214 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
216 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
218 print $outfh $line . "\n";
224 sub compile_asciidoc
{
229 GetOptions
("outfile=s" => \
$outfile,
230 "keep-artifacts" => \
$keep_artifacts,
231 "verbose" => \
$verbose) or
232 die("Error in command line arguments\n");
234 my $infile = shift(@ARGV) or
235 die "no input file specified\n";
237 scalar(@ARGV) == 0 or
238 die "too many arguments...\n";
240 defined($fileinfo->{titles
}->{$env}) ||
241 die "unknown environment '$env'";
243 my $title = $fileinfo->{titles
}->{$env}->{$infile} or
244 die "unable to get title for '$infile'$env\n";
246 debug
("compile $title");
250 my $doctype = $fileinfo->{doctype
}->{$env}->{$infile};
252 die "unable to get document type for '$infile'\n"
253 if !defined($doctype);
255 $leveloffset = - $doctype;
262 leveloffset
=> $leveloffset,
265 my $mansection = $fileinfo->{mansection
}->{$env}->{$infile};
267 if ($env eq 'wiki') {
268 } elsif ($env eq 'manvolnum') {
269 die "undefined man section" if !defined($mansection);
270 $attributes->{manvolnum
} = $mansection;
271 } elsif ($env eq 'default') {
272 die "$infile: wrong doctype\n" if $doctype != 0;
273 $attributes->{toc
} = undef;
276 if (!defined($outfile)) {
278 $outfile =~ s/\.adoc$//;
279 if ($env eq 'manvolnum') {
280 if ($man_target_html) {
281 $outfile .= ".$mansection.html";
283 $outfile .= ".$mansection";
290 if (($env eq 'manvolnum') && !$man_target_html) {
292 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
293 # section like footnotes, so we cannot use a2x.
294 # We use xmlto instead.
296 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook', '-a', 'docinfo1'];
298 foreach my $key (keys %$attributes) {
299 my $value = $attributes->{$key};
300 if (defined($value)) {
301 push @$cmd, '-a', "$key=$value";
303 push @$cmd, '-a', $key;
307 push @$cmd, '--verbose' if $verbose;
309 my $tmpxmlfile = "${outfile}.xml.tmp";
311 push @$cmd, '--out-file', $tmpxmlfile;
313 push @$files_for_cleanup, $tmpxmlfile;
315 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
317 push @$cmd, $new_infile;
319 debug
("run " . join(' ', @$cmd));
321 system(@$cmd) == 0 or
324 $cmd = ['xmlto', 'man', $tmpxmlfile];
326 push @$cmd, '-v' if $verbose;
328 debug
("run " . join(' ', @$cmd));
330 system(@$cmd) == 0 or
335 $attributes->{icons
} = undef;
336 $attributes->{'data-uri'} = undef;
337 $attributes->{revnumber
} = $release;
339 my $cmd = ['asciidoc'];
341 push @$cmd, '-s' if $env eq 'wiki';
343 foreach my $key (keys %$attributes) {
344 my $value = $attributes->{$key};
345 if (defined($value)) {
346 push @$cmd, '-a', "$key=$value";
348 push @$cmd, '-a', $key;
352 push @$cmd, '--verbose' if $verbose;
354 push @$cmd, '--out-file', $outfile;
356 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
358 push @$cmd, $new_infile;
360 debug
("run " . join(' ', @$cmd));
362 system(@$cmd) == 0 or
367 if ($clicmd eq 'compile-wiki') {
369 eval { compile_asciidoc
('wiki'); };
376 } elsif ($clicmd eq 'compile-chapter') {
378 eval { compile_asciidoc
('default'); };
385 } elsif ($clicmd eq 'compile-man-html') {
387 $man_target_html = 1;
389 eval { compile_asciidoc
('manvolnum'); };
396 } elsif ($clicmd eq 'compile-man') {
398 eval { compile_asciidoc
('manvolnum'); };
407 die "unknown command '$clicmd'\n";