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