]> git.proxmox.com Git - pve-docs.git/commitdiff
asciidoc-pve.in: implement new command print-links-json
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 12 Oct 2016 09:45:06 +0000 (11:45 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 12 Oct 2016 09:52:13 +0000 (11:52 +0200)
prints/stores information about defined blockids with titles/subtitles.
We can use this for the GUI.

asciidoc-pve.in

index 582267db3bbc9c8afb8b4b950add1312ac2b9ee5..ab23564385f1daf9167d896388350485082887c0 100644 (file)
@@ -422,6 +422,51 @@ if ($clicmd eq 'compile-wiki') {
 
     die $err if $err;
 
 
     die $err if $err;
 
+} elsif ($clicmd eq 'print-links-json') {
+
+    my $outfile;
+
+    GetOptions("outfile=s" => \$outfile,
+              "verbose"   => \$verbose) or
+                  die("Error in command line arguments\n");
+
+    scalar(@ARGV) == 0 or
+       die "too many arguments...\n";
+
+    my $data = {};
+
+    foreach my $blockid (sort keys %{$fileinfo->{blockid_target}->{default}}) {
+       my $link = $fileinfo->{blockid_target}->{default}->{$blockid};
+       my $reftitle = $fileinfo->{reftitle}->{default}->{$blockid};
+       my $reftext = $fileinfo->{reftext}->{default}->{$blockid};
+       die "internal error" if $link !~ m/^link:/;
+       $link =~ s/^link://;
+
+       my $file = $fileinfo->{blockid}->{default}->{$blockid};
+       die "internal error - no filename" if ! defined($file);
+       my $title =  $fileinfo->{titles}->{default}->{$file} ||
+           die "internal error - no title";
+
+       $data->{$blockid}->{title} = $title;
+       $data->{$blockid}->{link} = $link;
+       my $subtitle = $reftitle || $reftext;
+       $data->{$blockid}->{subtitle} = $subtitle
+           if $subtitle && ($title ne $subtitle);
+    }
+
+    my $res = to_json($data, { pretty => 1,  canonical => 1 } );
+
+    if (defined($outfile)) {
+       my $outfh = IO::File->new("$outfile", "w") or
+           die "unable to open temporary file '$outfile'\n";
+
+       print $outfh $res;
+
+    } else {
+
+       print $res;
+    }
+
 } else {
 
     die "unknown command '$clicmd'\n";
 } else {
 
     die "unknown command '$clicmd'\n";