do not add system-timesync-plain.html to WIKI_IMPORTS
[pve-docs.git] / scan-adoc-refs
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use JSON;
7
8 use Data::Dumper;
9
10 my $environments = {
11 default => 1,
12 wiki => 1,
13 manvolnum => 1,
14 pvelogo => 0, # ignore
15 };
16
17 my $fileinfo = {};
18
19 my $start_env = [];
20 foreach my $e (keys %$environments) {
21 push @$start_env, $e if $environments->{$e};
22 }
23
24 my $env_stack = [$start_env];
25 my $env_name_stack = [];
26
27 sub reset_environment_stack {
28 $env_stack = [$start_env];
29 $env_name_stack = [];
30 }
31
32 sub push_environment {
33 my ($env, $not) = @_;
34
35 die "undefined environment '$env'\n" if !defined($environments->{$env});
36
37 # FIXME: this seems wrong (nested env?)?
38 return if !$environments->{$env}; # do not track
39
40 if ($not) {
41 my $new_env = [];
42 foreach my $e (@{$env_stack->[-1]}) {
43 if ($e ne $env) {
44 push @$new_env, $e;
45 }
46 }
47 die "empty environment" if !scalar($new_env);
48 push @$env_stack, $new_env;
49 } else {
50 push @$env_stack, [$env];
51 }
52
53 push @$env_name_stack, $env;
54 }
55
56 sub pop_environment {
57 my ($env) = @_;
58
59 die "undefined environment '$env'\n" if !defined($environments->{$env});
60
61 return if !$environments->{$env}; # do not track
62
63 pop @$env_stack;
64 my $res = pop @$env_name_stack;
65
66 die "environment missmatch ($res != $env)\n" if $res ne $env;
67 }
68
69 sub register_include {
70 my ($filename, $include_filename, $env_list) = @_;
71
72 return if $include_filename !~ m/\.adoc$/; # skip attributes.txt
73
74 foreach my $e (@$env_list) {
75 $fileinfo->{include}->{$e}->{$filename}->{$include_filename} = 1;
76 }
77 }
78
79 sub register_blockid {
80 my ($filename, $blockid, $reftext, $env_list) = @_;
81
82 foreach my $e (@$env_list) {
83 my $fn = $fileinfo->{blockid}->{$e}->{$blockid};
84 die "blockid '$blockid' already defined in $fn"
85 if defined($fn);
86 $fileinfo->{blockid}->{$e}->{$blockid} = $filename;
87 $fileinfo->{reftext}->{$e}->{$blockid} = $reftext
88 if defined($reftext);
89 }
90 }
91
92 sub register_title {
93 my ($filename, $env, $doctype, $title, $blockid) = @_;
94
95 # fixme: what about other macros?
96 $title =~ s/\{pve\}/Proxmox VE/g;
97 $title =~ s!http://\S+\[(.*?)\]!$1!g;
98
99 # register document title (onyl once)
100 if (!defined($fileinfo->{titles}->{$env}->{$filename})) {
101
102 $fileinfo->{titles}->{$env}->{$filename} = $title;
103
104 if (defined($doctype)) {
105 $fileinfo->{doctype}->{$env}->{$filename} = $doctype;
106 } else {
107 die "unable to change title (no doctype)"
108 if !defined($fileinfo->{doctype}->{$env}->{$filename});
109 }
110
111 if (defined($doctype) && ($env eq 'manvolnum') && ($doctype == 0)) {
112 if ($title =~ m/.*\(([1-8])\)\s*$/) {
113 $fileinfo->{mansection}->{$env}->{$filename} = $1;
114 }
115 }
116 }
117
118 if ($blockid) {
119 die "internal error"
120 if !defined($fileinfo->{blockid}->{$env}->{$blockid});
121 $fileinfo->{reftitle}->{$env}->{$blockid} = $title;
122 }
123 }
124
125 sub scan_adoc_file {
126 my ($filename) = @_;
127
128 reset_environment_stack();
129
130 # print "SCAN $filename\n";
131
132 my $fh = IO::File->new("$filename", "r") or
133 die "unable to open file '$filename' - $!\n";
134
135 my $env_last_line = {};
136 my $env_last_blockid = {};
137
138 while (defined (my $line = <$fh>)) {
139 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
140 my ($not, $env, $text) = ($1, $2, $3);
141 die "unsuported ifdef usage - implement me" if $text;
142 push_environment($env, $not);
143 next;
144 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
145 my ($env, $text) = ($1, $2);
146 die "unsuported ifdef usage - implement me" if $text;
147 pop_environment($env);
148 next;
149 } elsif ($line =~ m/^include::(\S+)\[.*\]\s*$/) {
150 register_include($filename, $1, $env_stack->[-1]);
151 next;
152 }
153
154 # try to detect titles
155 foreach my $e (@{$env_stack->[-1]}) {
156 if ($line =~ m/^=====+$/) {
157 register_title($filename, $e, 0, $env_last_line->{$e},
158 $env_last_blockid->{$e});
159 } elsif ($line =~ m/^-----+$/) {
160 register_title($filename, $e, 1, $env_last_line->{$e},
161 $env_last_blockid->{$e});
162 } elsif ($line =~ m/^~~~~~+$/) {
163 register_title($filename, $e, 2, $env_last_line->{$e},
164 $env_last_blockid->{$e});
165 } elsif ($line =~ m/^\^\^\^\^\^+$/) {
166 register_title($filename, $e, 3, $env_last_line->{$e},
167 $env_last_blockid->{$e});
168 } elsif ($line =~ m/^= +(\S.*?)( +=)?$/) {
169 register_title($filename, $e, 0, $1, $env_last_blockid->{$e});
170 } elsif ($line =~ m/^== +(\S.*?)( +==)?$/) {
171 register_title($filename, $e, 1, $1, $env_last_blockid->{$e});
172 } elsif ($line =~ m/^=== +(\S.*?)( +===)?$/) {
173 register_title($filename, $e, 2, $1, $env_last_blockid->{$e});
174 } elsif ($line =~ m/^==== +(\S.*?)( +====)?$/) {
175 register_title($filename, $e, 3, $1, $env_last_blockid->{$e});
176 }
177
178 $env_last_line->{$e} = $line;
179 chomp $env_last_line->{$e};
180 }
181
182 if ($line =~ m/^:(\S+?):\s*(.*\S)?\s*$/) {
183 my ($key, $value) = ($1, $2);
184 if ($key eq 'pve-toplevel') {
185
186 foreach my $e (@{$env_stack->[-1]}) {
187 my $title = $fileinfo->{titles}->{$e}->{$filename};
188 die "not title for toplevel file '$filename' (env=$e)\n"
189 if !defined($title);
190 $fileinfo->{toplevel}->{$e}->{$filename} = 1;
191 }
192 } elsif ($key eq 'title') {
193 foreach my $e (@{$env_stack->[-1]}) {
194 register_title($filename, $e, undef, $value);
195 }
196 }
197 }
198
199 if ($line =~ m/^\[\[(.*)\]\]\s*$/) {
200 my $blockid = $1;
201 die "implement me" if $blockid =~m/,/;
202 my $reftext = '';
203 register_blockid($filename, $blockid, $reftext, $env_stack->[-1]);
204 foreach my $e (@{$env_stack->[-1]}) {
205 $env_last_blockid->{$e} = $blockid;
206 }
207 }
208
209 if ($line =~ m/^\s*$/) {
210 foreach my $e (@{$env_stack->[-1]}) {
211 delete $env_last_blockid->{$e};
212 }
213 }
214
215 # fixme: "anchor:"
216 # bibliography anchors
217 if ($line =~ m/\[\[\[([^\]]*)\]\]\]/) {
218 my $blockid = $1;
219 die "implement me" if $blockid =~m/,/;
220 register_blockid($filename, $blockid, "&#91;$blockid&#93;", $env_stack->[-1]);
221 }
222 }
223 }
224
225 my $scanned_files = {};
226 while (my $filename = shift) {
227 next if $filename !~ m/\.adoc$/; # skip attributes.txt
228 next if $scanned_files->{$filename};
229
230 scan_adoc_file($filename);
231 $scanned_files->{$filename} = 1;
232 }
233
234 sub resolve_link_target {
235 my ($env, $filename) = @_;
236
237 my $include_hash = $fileinfo->{include}->{$env};
238
239 my $repeat = 1;
240
241 while ($repeat) {
242 $repeat = 0;
243 foreach my $fn (keys %$include_hash) {
244 if ($include_hash->{$fn}->{$filename}) {
245 next if ($fn eq 'pve-admin-guide.adoc') &&
246 $fileinfo->{outfile}->{$env}->{$filename};
247 $filename = $fn;
248 $repeat = 1;
249 last;
250 }
251 }
252 }
253
254 return $filename;
255 }
256
257
258 # try to generate output file mapping
259 foreach my $e (@$start_env) {
260 my $toplevel_hash = $fileinfo->{toplevel}->{$e};
261 foreach my $fn (sort keys %$toplevel_hash) {
262 my $mansection = $fileinfo->{mansection}->{manvolnum}->{$fn};
263 if ($e eq 'wiki') {
264 my $realfn = $fn;
265 $realfn =~ s/\.adoc$//;
266 if (defined($mansection) && ($mansection == 5)) {
267 $realfn .= ".$mansection";
268 }
269 $realfn = "$realfn-plain.html";
270 $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
271 } elsif ($e eq 'manvolnum') {
272 my $realfn = $fn;
273 $realfn =~ s/\.adoc$//;
274 die "toplevel file '$fn' is not marked as manual page!" if !$mansection;
275 $realfn .= ".$mansection";
276 $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
277 } elsif ($e eq 'default') {
278 my $realfn = $fn;
279 $realfn =~ s/\.adoc$//;
280 if (defined($mansection) && ($mansection == 5)) {
281 $realfn .= ".$mansection";
282 $realfn = "$realfn.html";
283 } else {
284 if (($fn ne 'pve-admin-guide.adoc') &&
285 $fileinfo->{doctype}->{$e}->{$fn} == 0) {
286 $realfn = "chapter-$realfn.html";
287 } else {
288 $realfn = "$realfn.html";
289 }
290 }
291 $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
292 }
293 }
294 }
295
296 # now resolve blockids
297 foreach my $e (@$start_env) {
298 my $blockid_hash = $fileinfo->{blockid}->{$e};
299 foreach my $blockid (keys %$blockid_hash) {
300 my $fn = resolve_link_target($e, $blockid_hash->{$blockid});
301 if ($e eq 'wiki') {
302 my $title = $fileinfo->{titles}->{$e}->{$fn};
303 $title =~ s/\s/_/g;
304 die "found not title for '$fn' in env '$e'" if !$title;
305 $fileinfo->{blockid_target}->{$e}->{$blockid} = "link:/wiki/$title#$blockid";
306
307 # we do not produce wiki pages for all content
308 #my $realfn = $fileinfo->{outfile}->{$e}->{$fn};
309 #warn "no output file mapping for '$fn' ($e)\n" if !$realfn;
310
311 } elsif ($e eq 'default') {
312 my $realfn = $fileinfo->{outfile}->{$e}->{$fn} ||
313 die "no output file mapping for '$fn'\n";
314 $fileinfo->{blockid_target}->{$e}->{$blockid} = "link:/pve-docs/$realfn#$blockid";
315 } elsif ($e eq 'manvolnum') {
316 # we do not produce manpages for all content
317 # my $realfn = $fileinfo->{outfile}->{$e}->{$fn} ||
318 # warn "no output file mapping for '$fn'\n";
319 $fileinfo->{blockid_target}->{$e}->{$blockid} = $fn;
320 }
321 }
322 }
323
324
325 print to_json($fileinfo, { pretty => 1, canonical => 1 } );