You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

635 lines
15KB

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use File::Path;
  6. use File::Basename;
  7. use IO::File;
  8. use Cwd;
  9. use JSON;
  10. my $verbose;
  11. my $keep_artifacts;
  12. my $release = '@RELEASE@';
  13. my $clicmd = shift or
  14. die "no command specified\n";
  15. my $data_str = "";
  16. while (<main::DATA>) { $data_str .= $_; }
  17. my $fileinfo = decode_json($data_str);
  18. my $tmpprefix = '.asciidoc-pve-tmp'.$$.'_';
  19. my $adoc_source_dir = "/usr/share/pve-doc-generator";
  20. # inside pve-docs source dir?
  21. if (-f "asciidoc-pve.in" && -f "pve-admin-guide.adoc") {
  22. $adoc_source_dir = getcwd();
  23. }
  24. my $prepared_files = {};
  25. my $man_target = 'man';
  26. my $env_stack = [];
  27. my $env_skip = 0;
  28. my $online_help_links = {
  29. 'pve_service_daemons' => {
  30. link => '/pve-docs/index.html#_service_daemons',
  31. title => 'Service Daemons',
  32. },
  33. 'pve_documentation_index' => {
  34. link => '/pve-docs/index.html',
  35. title => 'Proxmox VE Documentation Index',
  36. },
  37. 'pve_admin_guide' => {
  38. link => '/pve-docs/pve-admin-guide.html',
  39. title => 'Proxmox VE Administration Guide',
  40. },
  41. };
  42. sub debug {
  43. my $msg = shift;
  44. return if !$verbose;
  45. print STDERR "asciidoc-pve: $msg\n";
  46. }
  47. sub push_environment {
  48. my ($env, $skip) = @_;
  49. $skip = 1 if $env_skip;
  50. $skip = 0 if !defined($skip);
  51. push @$env_stack, [$env, $skip];
  52. $env_skip = $skip;
  53. }
  54. sub pop_environment {
  55. my ($env) = @_;
  56. my $last_stack_entry = pop @$env_stack;
  57. die "unable to pop env '$env'" if !defined($last_stack_entry);
  58. my ($last_env, $skip) = @$last_stack_entry;
  59. die "environment missmatch (${last_env} != $env)\n" if $last_env ne $env;
  60. if (!scalar(@$env_stack)) {
  61. $env_skip = 0;
  62. } else {
  63. my (undef, $skip) = @{$env_stack->[-1]};
  64. $env_skip = $skip;
  65. }
  66. }
  67. my $files_for_cleanup = [];
  68. sub cleanup {
  69. return if $keep_artifacts;
  70. foreach my $file (@$files_for_cleanup) {
  71. unlink $file;
  72. }
  73. }
  74. sub replace_wiki_xref {
  75. my ($blockid, $text) = @_;
  76. my $link = $fileinfo->{blockid_target}->{wiki}->{$blockid};
  77. my $reftext = $fileinfo->{reftext}->{wiki}->{$blockid};
  78. die "unable to resolve wiki link (xref:$blockid)\n"
  79. if !defined($link);
  80. $text = $reftext if !length($text);
  81. die "xref: no text for wiki link '$blockid'\n" if !$text;
  82. return "$link\[$text\]";
  83. }
  84. sub replace_default_xref {
  85. my ($blockid, $text) = @_;
  86. my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
  87. my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
  88. die "unable to resolve chapter link (xref:$blockid)\n"
  89. if !defined($link);
  90. $text = $reftext if !length($text);
  91. die "xref: no text for chapter link '$blockid'\n" if !$text;
  92. return "$link\[$text\]";
  93. }
  94. sub replace_man_xref {
  95. my ($blockid, $text) = @_;
  96. my $link = $fileinfo->{blockid_target}->{manvolnum}->{$blockid};
  97. my $reftext = $fileinfo->{reftext}->{manvolnum}->{$blockid};
  98. die "unable to resolve man page link (xref:$blockid)\n"
  99. if !defined($link);
  100. $text = $reftext if !length($text);
  101. die "xref: no text for man page link '$blockid'\n" if !$text;
  102. my $section = $fileinfo->{mansection}->{manvolnum}->{$link};
  103. if (!defined($section)) {
  104. warn "link '$blockid' target '$link' is not a manual page, ignoring\n";
  105. return "$text";
  106. }
  107. if ($man_target eq 'html') {
  108. my $target = $link;
  109. $target =~ s/\.adoc//;
  110. $target .= ".$section";
  111. return "link:${target}.html#${blockid}\[$text\]";
  112. } elsif ($man_target eq 'man') {
  113. my $command = $link;
  114. $command =~ s/\.adoc//;
  115. return "\*${text}\* (man \*${command}\*($section))";
  116. } else {
  117. die "internal error"
  118. }
  119. }
  120. sub replace_xref {
  121. my ($env, $blockid, $text) = @_;
  122. if ($env eq 'wiki') {
  123. return replace_wiki_xref($blockid, $text);
  124. } elsif ($env eq 'manvolnum') {
  125. if (($man_target eq 'man') || ($man_target eq 'html')) {
  126. return replace_man_xref($blockid, $text);
  127. } elsif ($man_target eq 'wiki') {
  128. return replace_wiki_xref($blockid, $text);
  129. } else {
  130. die "internal error"
  131. }
  132. } elsif ($env eq 'default') {
  133. return replace_default_xref($blockid, $text);
  134. } else {
  135. die "internal error";
  136. }
  137. }
  138. sub prepare_adoc_file {
  139. my ($target_env, $filename, $attributes) = @_;
  140. return $prepared_files->{$filename} if defined($prepared_files->{$filename});
  141. debug("prepare $filename");
  142. my $dirname = dirname($filename);
  143. my $basename = basename($filename);
  144. my $outfilename = "$dirname/${tmpprefix}$basename";
  145. $prepared_files->{$filename} = $outfilename;
  146. my $fh = IO::File->new("$filename", "r") or
  147. die "unable to open file '$filename' - $!\n";
  148. my $outfh = IO::File->new("$outfilename", "w") or
  149. die "unable to open temporary file '$outfilename'\n";
  150. push @$files_for_cleanup, $outfilename;
  151. while (defined (my $line = <$fh>)) {
  152. chomp $line;
  153. if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
  154. my ($not, $env, $text) = ($1, $2, $3);
  155. die "unsuported ifdef usage - implement me" if $text;
  156. my $skip = !exists($attributes->{$env}) ? 1 : 0;
  157. $skip = ($skip ? 0 : 1 ) if $not;
  158. push_environment($env, $skip);
  159. next;
  160. } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
  161. my ($env, $text) = ($1, $2);
  162. die "unsuported ifdef usage - implement me" if $text;
  163. pop_environment($env);
  164. next;
  165. }
  166. next if $env_skip;
  167. if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
  168. my ($fn, $rest) = ($1, $2);
  169. debug("include $fn");
  170. my $new_fn = prepare_adoc_file($target_env, $fn, $attributes);
  171. print $outfh "include::${new_fn}$rest\n";
  172. next;
  173. }
  174. if ($line =~ m/xref:\S+?\[[^\]]*$/) {
  175. die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
  176. }
  177. if ($line =~ m/<<((?!\>\>).)*$/) {
  178. die "possible xref spanning multiple lines in '$filename':\n(line $.): $line\n";
  179. }
  180. # fix xrefs
  181. $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_xref(${target_env},$1,$2)/ge;
  182. $line =~ s/<<([^\s,\[\]]+)(?:,(.*?))?>>/replace_xref(${target_env},$1,$2)/ge;
  183. print $outfh $line . "\n";
  184. }
  185. return $outfilename;
  186. }
  187. sub compile_asciidoc {
  188. my ($env) = @_;
  189. my $outfile;
  190. GetOptions ("outfile=s" => \$outfile,
  191. "keep-artifacts" => \$keep_artifacts,
  192. "verbose" => \$verbose) or
  193. die("Error in command line arguments\n");
  194. my $infile = shift(@ARGV) or
  195. die "no input file specified\n";
  196. scalar(@ARGV) == 0 or
  197. die "too many arguments...\n";
  198. my $outfilemap = $fileinfo->{outfile}->{$env}->{$infile} ||
  199. die "no output file mapping for '$infile' ($env)";
  200. if ($man_target eq 'html') {
  201. $outfilemap .= '.html';
  202. } elsif ($man_target eq 'wiki') {
  203. $outfilemap .= '-plain.html';
  204. }
  205. if (defined($outfile)) {
  206. die "wrong output file name '$outfile != $outfilemap' ($env)"
  207. if $outfile ne $outfilemap;
  208. } else {
  209. $outfile = $outfilemap;
  210. }
  211. defined($fileinfo->{titles}->{$env}) ||
  212. die "unknown environment '$env'";
  213. my $title = $fileinfo->{titles}->{$env}->{$infile} or
  214. die "unable to get title for '$infile'$env\n";
  215. debug("compile $title");
  216. my $leveloffset = 0;
  217. my $doctype = $fileinfo->{doctype}->{$env}->{$infile};
  218. die "unable to get document type for '$infile'\n"
  219. if !defined($doctype);
  220. $leveloffset = - $doctype;
  221. my $date;
  222. if (defined($ENV{SOURCE_DATE_EPOCH})) {
  223. $date = `date -d "\@$ENV{SOURCE_DATE_EPOCH}"`;
  224. } else {
  225. $date = `date`;
  226. }
  227. chomp $date;
  228. my $attributes = {
  229. $env => undef,
  230. leveloffset => $leveloffset,
  231. revnumber => $release,
  232. revdate => $date,
  233. 'footer-style' => 'revdate',
  234. };
  235. my $mansection = $fileinfo->{mansection}->{$env}->{$infile};
  236. if ($env eq 'wiki') {
  237. } elsif ($env eq 'manvolnum') {
  238. die "undefined man section" if !defined($mansection);
  239. $attributes->{manvolnum} = $mansection;
  240. } elsif ($env eq 'default') {
  241. die "$infile: wrong doctype\n" if $doctype != 0;
  242. $attributes->{toc} = undef;
  243. }
  244. if (!defined($outfile)) {
  245. $outfile = $infile;
  246. $outfile =~ s/\.adoc$//;
  247. if ($env eq 'manvolnum') {
  248. if (($man_target eq 'html') || ($man_target eq 'wiki')) {
  249. $outfile .= ".$mansection.html";
  250. } else {
  251. $outfile .= ".$mansection";
  252. }
  253. } else {
  254. $outfile .= ".html";
  255. }
  256. }
  257. if (($env eq 'manvolnum') && ($man_target eq 'man')) {
  258. # asciidoc /etc/asciidoc/docbook-xsl/manpage.xsl skip REFERENCES
  259. # section like footnotes, so we cannot use a2x.
  260. # We use xmlto instead.
  261. my $cmd = ['asciidoc', '-dmanpage', '-bdocbook',
  262. '-f', "$adoc_source_dir/asciidoc/asciidoc-pve.conf",
  263. '-a', 'docinfo1'];
  264. foreach my $key (keys %$attributes) {
  265. my $value = $attributes->{$key};
  266. if (defined($value)) {
  267. push @$cmd, '-a', "$key=$value";
  268. } else {
  269. push @$cmd, '-a', $key;
  270. }
  271. }
  272. push @$cmd, '--verbose' if $verbose;
  273. my $tmpxmlfile = "${outfile}.xml.tmp";
  274. push @$cmd, '--out-file', $tmpxmlfile;
  275. push @$files_for_cleanup, $tmpxmlfile;
  276. my $new_infile = prepare_adoc_file($env, $infile, $attributes);
  277. push @$cmd, $new_infile;
  278. debug("run " . join(' ', @$cmd));
  279. system(@$cmd) == 0 or
  280. die "aciidoc error";
  281. $cmd = ['xmlto', 'man', $tmpxmlfile];
  282. push @$cmd, '-v' if $verbose;
  283. debug("run " . join(' ', @$cmd));
  284. system(@$cmd) == 0 or
  285. die "xmlto error";
  286. } else {
  287. $attributes->{icons} = undef;
  288. $attributes->{'data-uri'} = undef;
  289. my $cmd = ['asciidoc',
  290. '-f', "$adoc_source_dir/asciidoc/asciidoc-pve.conf",
  291. ];
  292. if (($env eq 'wiki') ||
  293. (($env eq 'manvolnum') && ($man_target eq 'wiki'))) {
  294. push @$cmd, '-b', "$adoc_source_dir/asciidoc/mediawiki";
  295. } else {
  296. push @$cmd, '-b', "$adoc_source_dir/asciidoc/pve-html";
  297. }
  298. foreach my $key (keys %$attributes) {
  299. my $value = $attributes->{$key};
  300. if (defined($value)) {
  301. push @$cmd, '-a', "$key=$value";
  302. } else {
  303. push @$cmd, '-a', $key;
  304. }
  305. }
  306. push @$cmd, '--verbose' if $verbose;
  307. push @$cmd, '--out-file', $outfile;
  308. my $new_infile = prepare_adoc_file($env, $infile, $attributes);
  309. push @$cmd, $new_infile;
  310. debug("run " . join(' ', @$cmd));
  311. system(@$cmd) == 0 or
  312. die "aciidoc error";
  313. }
  314. }
  315. sub get_links {
  316. my $data = {};
  317. foreach my $blockid (sort keys %{$fileinfo->{blockid_target}->{default}}) {
  318. my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
  319. my $reftitle = $fileinfo->{reftitle}->{default}->{$blockid};
  320. my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
  321. die "internal error" if $link !~ m/^link:/;
  322. $link =~ s/^link://;
  323. my $file = $fileinfo->{blockid}->{default}->{$blockid};
  324. die "internal error - no filename" if ! defined($file);
  325. my $title = $fileinfo->{titles}->{default}->{$file} ||
  326. die "internal error - no title";
  327. $data->{$blockid}->{title} = $title;
  328. $data->{$blockid}->{link} = $link;
  329. my $subtitle = $reftitle || $reftext;
  330. $data->{$blockid}->{subtitle} = $subtitle
  331. if $subtitle && ($title ne $subtitle);
  332. }
  333. return $data;
  334. }
  335. sub scan_extjs_file {
  336. my ($filename, $res_data) = @_;
  337. my $fh = IO::File->new($filename, "r") ||
  338. die "unable to open '$filename' - $!\n";
  339. debug("scan-extjs $filename");
  340. while(defined(my $line = <$fh>)) {
  341. if ($line =~ m/\s+onlineHelp:\s*[\'\"](.*?)[\'\"]/) {
  342. my $blockid = $1;
  343. my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
  344. die "undefined blockid '$blockid' ($filename, line $.)\n"
  345. if !(defined($link) || defined($online_help_links->{$blockid}));
  346. $res_data->{$blockid} = 1;
  347. }
  348. }
  349. }
  350. if ($clicmd eq 'compile-wiki') {
  351. eval { compile_asciidoc('wiki'); };
  352. my $err = $@;
  353. cleanup();
  354. die $err if $err;
  355. } elsif ($clicmd eq 'compile-chapter') {
  356. eval { compile_asciidoc('default'); };
  357. my $err = $@;
  358. cleanup();
  359. die $err if $err;
  360. } elsif ($clicmd eq 'compile-man-html') {
  361. $man_target = 'html';
  362. eval { compile_asciidoc('manvolnum'); };
  363. my $err = $@;
  364. cleanup();
  365. die $err if $err;
  366. } elsif ($clicmd eq 'compile-man-wiki') {
  367. $man_target = 'wiki';
  368. eval { compile_asciidoc('manvolnum'); };
  369. my $err = $@;
  370. cleanup();
  371. die $err if $err;
  372. } elsif ($clicmd eq 'compile-man') {
  373. eval { compile_asciidoc('manvolnum'); };
  374. my $err = $@;
  375. cleanup();
  376. die $err if $err;
  377. } elsif ($clicmd eq 'print-links') {
  378. my $outfile;
  379. GetOptions("outfile=s" => \$outfile,
  380. "verbose" => \$verbose) or
  381. die("Error in command line arguments\n");
  382. scalar(@ARGV) == 0 or
  383. die "too many arguments...\n";
  384. my $data = get_links();
  385. my $res = to_json($data, { pretty => 1, canonical => 1 } );
  386. if (defined($outfile)) {
  387. my $outfh = IO::File->new("$outfile", "w") or
  388. die "unable to open temporary file '$outfile'\n";
  389. print $outfh $res;
  390. } else {
  391. print $res;
  392. }
  393. } elsif ($clicmd eq 'scan-extjs') {
  394. GetOptions("verbose" => \$verbose) or
  395. die("Error in command line arguments\n");
  396. my $link_hash = {};
  397. my $scanned_files = {};
  398. while (my $filename = shift) {
  399. die "got strange file name '$filename'\n"
  400. if $filename !~ m/\.js$/;
  401. next if $scanned_files->{$filename};
  402. scan_extjs_file($filename, $link_hash);
  403. $scanned_files->{$filename} = 1;
  404. }
  405. my $data = get_links();
  406. my $res_data = {};
  407. foreach my $blockid (keys %$link_hash) {
  408. $res_data->{$blockid} = $data->{$blockid} || $online_help_links->{$blockid} ||
  409. die "internal error - no data for '$blockid'";
  410. }
  411. my $data_str = to_json($res_data, { pretty => 1, canonical => 1 });
  412. chomp $data_str;
  413. print "var pveOnlineHelpInfo = ${data_str};\n";
  414. } elsif ($clicmd eq 'chapter-table') {
  415. print '[width="100%",options="header"]' . "\n";
  416. print "|====\n";
  417. print "|Title|Link\n";
  418. my $filelist = $fileinfo->{outfile}->{default};
  419. foreach my $sourcefile (sort keys %$filelist) {
  420. my $target = $filelist->{$sourcefile};
  421. next if $target eq 'pve-admin-guide.html';
  422. my $title = $fileinfo->{titles}->{default}->{$sourcefile} ||
  423. die "not title for '$sourcefile'";
  424. print "|$title|link:$target\[\]\n";
  425. }
  426. print "|====\n";
  427. } elsif ($clicmd =~ m/^man([158])page-table$/) {
  428. my $section = $1;
  429. print '[width="100%",cols="5*d",options="header"]' . "\n";
  430. print "|====\n";
  431. print "|Name 3+|Title|Link\n";
  432. my $filelist = $fileinfo->{outfile}->{manvolnum};
  433. foreach my $manpage (sort keys %$filelist) {
  434. next if $section ne $fileinfo->{mansection}->{manvolnum}->{$manpage};
  435. my $mantitle = $fileinfo->{titles}->{manvolnum}->{$manpage} ||
  436. die "not manual title for '$manpage'";
  437. my $title = $fileinfo->{titles}->{default}->{$manpage} ||
  438. die "not title for '$manpage'";
  439. # hack - remove command name prefix from titles
  440. $title =~ s/^[a-z]+\s*-\s*//;
  441. my $target = $filelist->{$manpage};
  442. print "|$mantitle 3+|$title|link:$target.html\[$target\]\n";
  443. }
  444. print "|====\n";
  445. } else {
  446. die "unknown command '$clicmd'\n";
  447. }
  448. exit 0;
  449. __END__