]>
git.proxmox.com Git - pmg-docs.git/blob - asciidoc-pmg.in
16 my $release = '@RELEASE@';
19 die "no command specified\n";
22 while (<main
::DATA
>) { $data_str .= $_; }
24 my $fileinfo = decode_json
($data_str);
26 my $tmpprefix = '.asciidoc-pmg-tmp'.$$.'_';
28 my $adoc_source_dir = "/usr/share/pmg-doc-generator";
30 # inside pmg-docs source dir?
31 if (-f
"asciidoc-pmg.in" && -f
"pmg-admin-guide.adoc") {
32 $adoc_source_dir = getcwd
();
35 my $prepared_files = {};
37 my $man_target = 'man';
41 my $online_help_links = {
42 'pmg_service_daemons' => {
43 link => '/pmg-docs/index.html#_service_daemons',
44 title
=> 'Service Daemons',
46 'pmg_documentation_index' => {
47 link => '/pmg-docs/index.html',
48 title
=> 'Proxmox Mail Gateway Documentation Index',
50 'pmg_admin_guide' => {
51 link => '/pmg-docs/pmg-admin-guide.html',
52 title
=> 'Proxmox Mail Gateway Administration Guide',
61 print STDERR
"asciidoc-pmg: $msg\n";
64 sub push_environment
{
65 my ($env, $skip) = @_;
67 $skip = 1 if $env_skip;
68 $skip = 0 if !defined($skip);
70 push @$env_stack, [$env, $skip];
78 my $last_stack_entry = pop @$env_stack;
79 die "unable to pop env '$env'" if !defined($last_stack_entry);
81 my ($last_env, $skip) = @$last_stack_entry;
82 die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
84 if (!scalar(@$env_stack)) {
87 my (undef, $skip) = @{$env_stack->[-1]};
92 my $files_for_cleanup = [];
96 return if $keep_artifacts;
98 foreach my $file (@$files_for_cleanup) {
103 sub replace_wiki_xref
{
104 my ($blockid, $text) = @_;
106 my $link = $fileinfo->{blockid_target
}->{wiki
}->{$blockid};
107 my $reftext = $fileinfo->{reftext
}->{wiki
}->{$blockid};
109 die "unable to resolve wiki link (xref:$blockid)\n"
112 $text = $reftext if !length($text);
114 die "xref: no text for wiki link '$blockid'\n" if !$text;
116 return "$link\[$text\]";
119 sub replace_default_xref
{
120 my ($blockid, $text) = @_;
122 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
123 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
125 die "unable to resolve chapter link (xref:$blockid)\n"
128 $text = $reftext if !length($text);
130 die "xref: no text for chapter link '$blockid'\n" if !$text;
132 return "$link\[$text\]";
135 sub replace_man_xref
{
136 my ($blockid, $text) = @_;
138 my $link = $fileinfo->{blockid_target
}->{manvolnum
}->{$blockid};
139 my $reftext = $fileinfo->{reftext
}->{manvolnum
}->{$blockid};
141 die "unable to resolve man page link (xref:$blockid)\n"
144 $text = $reftext if !length($text);
146 die "xref: no text for man page link '$blockid'\n" if !$text;
148 my $section = $fileinfo->{mansection
}->{manvolnum
}->{$link};
149 # die "link target is not a manual page" if !defined($section);
150 if (!defined($section)) {
151 warn "link '$blockid' target '$link' is not a manual page, ignoring\n";
156 if ($man_target eq 'html') {
158 $target =~ s/\.adoc//;
159 $target .= ".$section";
160 return "link:${target}.html#${blockid}\[$text\]";
161 } elsif ($man_target eq 'man') {
163 $command =~ s/\.adoc//;
164 return "\*${text}\* (man \*${command}\*($section))";
171 my ($env, $blockid, $text) = @_;
173 if ($env eq 'wiki') {
174 return replace_wiki_xref
($blockid, $text);
175 } elsif ($env eq 'manvolnum') {
176 if (($man_target eq 'man') || ($man_target eq 'html')) {
177 return replace_man_xref
($blockid, $text);
178 } elsif ($man_target eq 'wiki') {
179 return replace_wiki_xref
($blockid, $text);
183 } elsif ($env eq 'default') {
184 return replace_default_xref
($blockid, $text);
186 die "internal error";
190 sub prepare_adoc_file
{
191 my ($target_env, $filename, $attributes) = @_;
193 return $prepared_files->{$filename} if defined($prepared_files->{$filename});
195 debug
("prepare $filename");
197 my $dirname = dirname
($filename);
198 my $basename = basename
($filename);
200 my $outfilename = "$dirname/${tmpprefix}$basename";
202 $prepared_files->{$filename} = $outfilename;
204 my $fh = IO
::File-
>new("$filename", "r") or
205 die "unable to open file '$filename' - $!\n";
207 my $outfh = IO
::File-
>new("$outfilename", "w") or
208 die "unable to open temporary file '$outfilename'\n";
210 push @$files_for_cleanup, $outfilename;
212 while (defined (my $line = <$fh>)) {
214 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
215 my ($not, $env, $text) = ($1, $2, $3);
216 die "unsuported ifdef usage - implement me" if $text;
218 my $skip = !exists($attributes->{$env}) ?
1 : 0;
219 $skip = ($skip ?
0 : 1 ) if $not;
221 push_environment
($env, $skip);
223 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
224 my ($env, $text) = ($1, $2);
225 die "unsuported ifdef usage - implement me" if $text;
226 pop_environment
($env);
232 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
233 my ($fn, $rest) = ($1, $2);
234 debug
("include $fn");
235 my $new_fn = prepare_adoc_file
($target_env, $fn, $attributes);
237 print $outfh "include::${new_fn}$rest\n";
241 if ($line =~ m/xref:\S+?\[[^\]]*$/) {
242 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
244 if ($line =~ m/<<((?!\>\>).)*$/) {
245 die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
248 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
250 $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
252 print $outfh $line . "\n";
258 sub compile_asciidoc
{
263 GetOptions
("outfile=s" => \
$outfile,
264 "keep-artifacts" => \
$keep_artifacts,
265 "verbose" => \
$verbose) or
266 die("Error in command line arguments\n");
268 my $infile = shift(@ARGV) or
269 die "no input file specified\n";
271 scalar(@ARGV) == 0 or
272 die "too many arguments...\n";
274 my $outfilemap = $fileinfo->{outfile
}->{$env}->{$infile} ||
275 die "no output file mapping for '$infile' ($env)";
277 if ($man_target eq 'html') {
278 $outfilemap .= '.html';
279 } elsif ($man_target eq 'wiki') {
280 $outfilemap .= '-plain.html';
283 if (defined($outfile)) {
284 die "wrong output file name '$outfile != $outfilemap' ($env)"
285 if $outfile ne $outfilemap;
287 $outfile = $outfilemap;
290 defined($fileinfo->{titles
}->{$env}) ||
291 die "unknown environment '$env'";
293 my $title = $fileinfo->{titles
}->{$env}->{$infile} or
294 die "unable to get title for '$infile'$env\n";
296 debug
("compile $title");
300 my $doctype = $fileinfo->{doctype
}->{$env}->{$infile};
302 die "unable to get document type for '$infile'\n"
303 if !defined($doctype);
305 $leveloffset = - $doctype;
312 leveloffset
=> $leveloffset,
313 revnumber
=> $release,
317 my $mansection = $fileinfo->{mansection
}->{$env}->{$infile};
319 if ($env eq 'wiki') {
320 } elsif ($env eq 'manvolnum') {
321 die "undefined man section" if !defined($mansection);
322 $attributes->{manvolnum
} = $mansection;
323 } elsif ($env eq 'default') {
324 die "$infile: wrong doctype\n" if $doctype != 0;
325 $attributes->{toc
} = undef;
326 $attributes->{toc2
} = undef;
329 if (!defined($outfile)) {
331 $outfile =~ s/\.adoc$//;
332 if ($env eq 'manvolnum') {
333 if (($man_target eq 'html') || ($man_target eq 'wiki')) {
334 $outfile .= ".$mansection.html";
336 $outfile .= ".$mansection";
343 if (($env eq 'manvolnum') && ($man_target eq 'man')) {
345 # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
346 # section like footnotes, so we cannot use a2x.
347 # We use xmlto instead.
349 my $cmd = ['asciidoc', '-dmanpage', '-bdocbook',
350 '-f', "$adoc_source_dir/asciidoc/asciidoc-pmg.conf",
353 foreach my $key (keys %$attributes) {
354 my $value = $attributes->{$key};
355 if (defined($value)) {
356 push @$cmd, '-a', "$key=$value";
358 push @$cmd, '-a', $key;
362 push @$cmd, '--verbose' if $verbose;
364 my $tmpxmlfile = "${outfile}.xml.tmp";
366 push @$cmd, '--out-file', $tmpxmlfile;
368 push @$files_for_cleanup, $tmpxmlfile;
370 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
372 push @$cmd, $new_infile;
374 debug
("run " . join(' ', @$cmd));
376 system(@$cmd) == 0 or
379 $cmd = ['xmlto', 'man', $tmpxmlfile];
381 push @$cmd, '-v' if $verbose;
383 debug
("run " . join(' ', @$cmd));
385 system(@$cmd) == 0 or
390 $attributes->{icons
} = undef;
391 $attributes->{'data-uri'} = undef;
393 my $cmd = ['asciidoc',
394 '-f', "$adoc_source_dir/asciidoc/asciidoc-pmg.conf",
397 if (($env eq 'wiki') ||
398 (($env eq 'manvolnum') && ($man_target eq 'wiki'))) {
400 push @$cmd, '-b', "$adoc_source_dir/asciidoc/mediawiki";
402 push @$cmd, '-b', "$adoc_source_dir/asciidoc/pmg-html";
405 foreach my $key (keys %$attributes) {
406 my $value = $attributes->{$key};
407 if (defined($value)) {
408 push @$cmd, '-a', "$key=$value";
410 push @$cmd, '-a', $key;
414 push @$cmd, '--verbose' if $verbose;
416 push @$cmd, '--out-file', $outfile;
418 my $new_infile = prepare_adoc_file
($env, $infile, $attributes);
420 push @$cmd, $new_infile;
422 debug
("run " . join(' ', @$cmd));
424 system(@$cmd) == 0 or
433 foreach my $blockid (sort keys %{$fileinfo->{blockid_target
}->{default}}) {
434 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
435 my $reftitle = $fileinfo->{reftitle
}->{default}->{$blockid};
436 my $reftext = $fileinfo->{reftext
}->{default}->{$blockid};
437 die "internal error" if $link !~ m/^link:/;
440 my $file = $fileinfo->{blockid
}->{default}->{$blockid};
441 die "internal error - no filename" if ! defined($file);
442 my $title = $fileinfo->{titles
}->{default}->{$file} ||
443 die "internal error - no title";
445 $data->{$blockid}->{title
} = $title;
446 $data->{$blockid}->{link} = $link;
447 my $subtitle = $reftitle || $reftext;
448 $data->{$blockid}->{subtitle
} = $subtitle
449 if $subtitle && ($title ne $subtitle);
455 sub scan_extjs_file
{
456 my ($filename, $res_data) = @_;
458 my $fh = IO
::File-
>new($filename, "r") ||
459 die "unable to open '$filename' - $!\n";
461 debug
("scan-extjs $filename");
463 while(defined(my $line = <$fh>)) {
464 if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) {
466 my $link = $fileinfo->{blockid_target
}->{default}->{$blockid};
467 die "undefined blockid '$blockid' ($filename, line $.)\n"
468 if !(defined($link) || defined($online_help_links->{$blockid}));
470 $res_data->{$blockid} = 1;
475 if ($clicmd eq 'compile-wiki') {
477 eval { compile_asciidoc
('wiki'); };
484 } elsif ($clicmd eq 'compile-chapter') {
486 eval { compile_asciidoc
('default'); };
493 } elsif ($clicmd eq 'compile-man-html') {
495 $man_target = 'html';
497 eval { compile_asciidoc
('manvolnum'); };
504 } elsif ($clicmd eq 'compile-man-wiki') {
506 $man_target = 'wiki';
508 eval { compile_asciidoc
('manvolnum'); };
515 } elsif ($clicmd eq 'compile-man') {
517 eval { compile_asciidoc
('manvolnum'); };
524 } elsif ($clicmd eq 'print-links') {
528 GetOptions
("outfile=s" => \
$outfile,
529 "verbose" => \
$verbose) or
530 die("Error in command line arguments\n");
532 scalar(@ARGV) == 0 or
533 die "too many arguments...\n";
535 my $data = get_links
();
537 my $res = to_json
($data, { pretty
=> 1, canonical
=> 1 } );
539 if (defined($outfile)) {
540 my $outfh = IO
::File-
>new("$outfile", "w") or
541 die "unable to open temporary file '$outfile'\n";
550 } elsif ($clicmd eq 'scan-extjs') {
552 GetOptions
("verbose" => \
$verbose) or
553 die("Error in command line arguments\n");
556 my $scanned_files = {};
557 while (my $filename = shift) {
558 die "got strange file name '$filename'\n"
559 if $filename !~ m/\.js$/;
560 next if $scanned_files->{$filename};
562 scan_extjs_file
($filename, $link_hash);
563 $scanned_files->{$filename} = 1;
566 my $data = get_links
();
570 foreach my $blockid (keys %$link_hash) {
571 $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
572 die "internal error - no data for '$blockid'";
575 my $data_str = to_json
($res_data, { pretty
=> 1, canonical
=> 1 });
578 print "var proxmoxOnlineHelpInfo = ${data_str};\n";
580 } elsif ($clicmd eq 'chapter-table') {
582 print '[width="100%",options="header"]' . "\n";
584 print "|Title|Link\n";
586 my $filelist = $fileinfo->{outfile
}->{default};
587 foreach my $sourcefile (sort keys %$filelist) {
588 my $target = $filelist->{$sourcefile};
589 next if $target eq 'pmg-admin-guide.html';
590 my $title = $fileinfo->{titles
}->{default}->{$sourcefile} ||
591 die "not title for '$sourcefile'";
592 print "|$title|link:$target\[\]\n";
597 } elsif ($clicmd =~ m/^man([158])page-table$/) {
600 print '[width="100%",cols="5*d",options="header"]' . "\n";
602 print "|Name 3+|Title|Link\n";
604 my $filelist = $fileinfo->{outfile
}->{manvolnum
};
605 foreach my $manpage (sort keys %$filelist) {
606 next if $section ne $fileinfo->{mansection
}->{manvolnum
}->{$manpage};
607 my $mantitle = $fileinfo->{titles
}->{manvolnum
}->{$manpage} ||
608 die "not manual title for '$manpage'";
609 my $title = $fileinfo->{titles
}->{default}->{$manpage} ||
610 die "not title for '$manpage'";
612 # hack - remove command name prefix from titles
613 $title =~ s/^[a-z_\-]+\s+-\s*//;
615 my $target = $filelist->{$manpage};
616 print "|$mantitle 3+|$title|link:$target.html\[$target\]\n";
623 die "unknown command '$clicmd'\n";