ab6edef67e21afe580fa097bccfd9f610ac1f687
[pve-docs.git] / asciidoc-pve.in
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 return "$link\[$text\]";
76 }
77
78 sub prepare_adoc_file {
79 my ($filename, $attributes) = @_;
80
81 return if $prepared_files->{$filename};
82
83 print "PREPARE $filename\n";
84
85 $prepared_files->{$filename} = 1;
86
87 my $dirname = dirname($filename);
88 my $basename = basename($filename);
89
90 my $outfilename = "$dirname/${tmpprefix}$basename";
91
92 my $fh = IO::File->new("$filename", "r") or
93 die "unable to open file '$filename' - $!\n";
94
95 my $outfh = IO::File->new("$outfilename", "w") or
96 die "unable to open temporary file '$outfilename'\n";
97
98 while (defined (my $line = <$fh>)) {
99 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
100 my ($not, $env, $text) = ($1, $2, $3);
101 die "unsuported ifdef usage - implement me" if $text;
102
103 my $skip = !exists($attributes->{$env}) ? 1 : 0;
104 $skip = ($skip ? 0 : 1 ) if $not;
105
106 push_environment($env, $skip);
107 next;
108 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
109 my ($env, $text) = ($1, $2);
110 die "unsuported ifdef usage - implement me" if $text;
111 pop_environment($env);
112 next;
113 }
114
115 next if $env_skip;
116
117 if ($line =~ m/^include::(\S+)(\[.*\]\s*)$/) {
118 my ($fn, $rest) = ($1, $2);
119 print "INCLUDE: $fn\n";
120 my $new_fn = prepare_adoc_file($fn, $attributes);
121 $line = "include::${new_fn}$rest\n";
122 }
123
124 # fix xrefs
125 $line =~ s/xref:([^\s\[\]]+)\[([^\]]*)\]/replace_wiki_xref($1,$2)/ge;
126
127
128 print $outfh $line;
129 }
130
131 return $outfilename;
132 }
133
134 sub compile_wiki {
135
136 my $verbose;
137 my $outfile;
138 my $target_env;
139
140 GetOptions ("outfile=s" => \$outfile,
141 "verbose" => \$verbose) or
142 die("Error in command line arguments\n");
143
144 my $infile = shift(@ARGV) or
145 die "no input file specified\n";
146
147 scalar(@ARGV) == 0 or
148 die "too many arguments...\n";
149
150 my $env = 'wiki';
151
152
153 my $title = $fileinfo->{titles}->{$env}->{$infile} or
154 die "unable to get title for '$infile'\n";
155
156
157 print "compile: $title\n";
158
159 my $leveloffset = 0;
160
161 if ($clicmd eq 'compile-wiki-chapter') {
162 $leveloffset = 0;
163 } elsif ($clicmd eq 'compile-wiki-section') {
164 $leveloffset = -1;
165 }
166
167 my $date = `date`;
168 chomp $date;
169
170 my $attributes = {
171 $env => undef,
172 icons => undef,
173 'data-uri' => undef,
174 date => $date,
175 leveloffset => $leveloffset,
176 revnumber => $release,
177 };
178
179 my $cmd = ['asciidoc', '-s'];
180
181 foreach my $key (keys %$attributes) {
182 my $value = $attributes->{$key};
183 if (defined($value)) {
184 push @$cmd, '-a', "$key=$value";
185 } else {
186 push @$cmd, '-a', $key;
187 }
188 }
189
190 push @$cmd, '--verbose' if $verbose;
191
192 if (!defined($outfile)) {
193 $outfile = $infile;
194 $outfile =~ s/\.adoc$//;
195 $outfile .= ".html";
196 }
197
198 push @$cmd, '--out-file', $outfile;
199
200 my $new_infile = prepare_adoc_file($infile, $attributes);
201
202 push @$cmd, $new_infile;
203
204 print "RUN " . join(' ', @$cmd) . "\n";
205
206 system(@$cmd) == 0 or
207 die "aciidoc error";
208 }
209
210 if ($clicmd eq 'compile-wiki-chapter' ||
211 $clicmd eq 'compile-wiki-section') {
212
213 eval { compile_wiki(); };
214 my $err = $@;
215
216 # cleanup
217
218 die $err if $err;
219
220 } else {
221
222 die "unknown command '$clicmd'\n";
223
224 }
225
226
227
228
229
230
231 exit 0;
232
233 __END__