scan-adoc-refs: fix title change via attribute
[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 } elsif (!defined($doctype)) {
111 # change title via :title: attribute
112 $fileinfo->{titles}->{$env}->{$filename} = $title;
113 }
114
115 if (defined($doctype) && ($env eq 'manvolnum') && ($doctype == 0)) {
116 if ($title =~ m/.*\(([1-8])\)\s*$/) {
117 $fileinfo->{mansection}->{$env}->{$filename} = $1;
118 }
119 }
120
121 if ($blockid) {
122 die "internal error"
123 if !defined($fileinfo->{blockid}->{$env}->{$blockid});
124 $fileinfo->{reftitle}->{$env}->{$blockid} = $title;
125 }
126 }
127
128 sub scan_adoc_file {
129 my ($filename) = @_;
130
131 reset_environment_stack();
132
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 = {};
139 my $env_last_blockid = {};
140
141 while (defined (my $line = <$fh>)) {
142 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
143 my ($not, $env, $text) = ($1, $2, $3);
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]}) {
159 if ($line =~ m/^=====+$/) {
160 register_title($filename, $e, 0, $env_last_line->{$e},
161 $env_last_blockid->{$e});
162 } elsif ($line =~ m/^-----+$/) {
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});
171 } elsif ($line =~ m/^= +(\S.*?)( +=)?$/) {
172 register_title($filename, $e, 0, $1, $env_last_blockid->{$e});
173 } elsif ($line =~ m/^== +(\S.*?)( +==)?$/) {
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});
179 }
180
181 $env_last_line->{$e} = $line;
182 chomp $env_last_line->{$e};
183 }
184
185 if ($line =~ m/^:(\S+?):\s*(.*\S)?\s*$/) {
186 my ($key, $value) = ($1, $2);
187 if ($key eq 'pve-toplevel') {
188
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 }
195 } elsif ($key eq 'title') {
196 foreach my $e (@{$env_stack->[-1]}) {
197 register_title($filename, $e, undef, $value);
198 }
199 }
200 }
201
202 if ($line =~ m/^\[\[(.*)\]\]\s*$/) {
203 my $blockid = $1;
204 die "implement me" if $blockid =~m/,/;
205 my $reftext = '';
206 register_blockid($filename, $blockid, $reftext, $env_stack->[-1]);
207 foreach my $e (@{$env_stack->[-1]}) {
208 $env_last_blockid->{$e} = $blockid;
209 }
210 }
211
212 if ($line =~ m/^\s*$/) {
213 foreach my $e (@{$env_stack->[-1]}) {
214 delete $env_last_blockid->{$e};
215 }
216 }
217
218 # fixme: "anchor:"
219 # bibliography anchors
220 if ($line =~ m/\[\[\[([^\]]*)\]\]\]/) {
221 my $blockid = $1;
222 die "implement me" if $blockid =~m/,/;
223 register_blockid($filename, $blockid, "&#91;$blockid&#93;", $env_stack->[-1]);
224 }
225 }
226 }
227
228 my $scanned_files = {};
229 while (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
237 sub 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) {
247 if ($include_hash->{$fn}->{$filename}) {
248 next if ($fn eq 'pve-admin-guide.adoc') &&
249 $fileinfo->{outfile}->{$env}->{$filename};
250 $filename = $fn;
251 $repeat = 1;
252 last;
253 }
254 }
255 }
256
257 return $filename;
258 }
259
260
261 # try to generate output file mapping
262 foreach 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 }
272 $realfn = "$realfn-plain.html";
273 $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
274 } elsif ($e eq 'manvolnum') {
275 my $realfn = $fn;
276 $realfn =~ s/\.adoc$//;
277 die "toplevel file '$fn' is not marked as manual page!" if !$mansection;
278 $realfn .= ".$mansection";
279 $fileinfo->{outfile}->{$e}->{$fn} = $realfn;
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;
295 }
296 }
297 }
298
299 # now resolve blockids
300 foreach 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};
306 $title =~ s/\s/_/g;
307 die "found not title for '$fn' in env '$e'" if !$title;
308 $fileinfo->{blockid_target}->{$e}->{$blockid} = "link:/wiki/$title#$blockid";
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
314 } elsif ($e eq 'default') {
315 my $realfn = $fileinfo->{outfile}->{$e}->{$fn} ||
316 die "no output file mapping for '$fn'\n";
317 $fileinfo->{blockid_target}->{$e}->{$blockid} = "link:/pve-docs/$realfn#$blockid";
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";
322 $fileinfo->{blockid_target}->{$e}->{$blockid} = $fn;
323 }
324 }
325 }
326
327
328 print to_json($fileinfo, { pretty => 1, canonical => 1 } );