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