add helper to correctly resolve links
[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 $resolve_skip_files = {
18 default => {},
19 wiki => { 'pve-admin-guide.adoc' => 1 },
20 manvolnum => {},
21 };
22
23 my $fileinfo = {};
24
25 my $start_env = [];
26 foreach my $e (keys %$environments) {
27 push @$start_env, $e if $environments->{$e};
28 }
29
30 my $env_stack = [$start_env];
31 my $env_name_stack = [];
32
33 sub reset_environment_stack {
34 $env_stack = [$start_env];
35 $env_name_stack = [];
36 }
37
38 sub push_environment {
39 my ($env, $not) = @_;
40
41 die "undefined environment '$env'\n" if !defined($environments->{$env});
42
43 return if !$environments->{$env}; # do not track
44
45 if ($not) {
46 my $new_env = [];
47 foreach my $e (@{$env_stack->[-1]}) {
48 if ($e ne $env) {
49 push @$new_env, $e;
50 }
51 }
52 die "empty environment" if !scalar($new_env);
53 push @$env_stack, $new_env;
54 } else {
55 push @$env_stack, [$env];
56 }
57
58 push @$env_name_stack, $env;
59 }
60
61 sub pop_environment {
62 my ($env) = @_;
63
64 die "undefined environment '$env'\n" if !defined($environments->{$env});
65
66 return if !$environments->{$env}; # do not track
67
68 pop @$env_stack;
69 my $res = pop @$env_name_stack;
70
71 die "environment missmatch ($res != $env)\n" if $res ne $env;
72 }
73
74 sub register_include {
75 my ($filename, $include_filename, $env_list) = @_;
76
77 return if $include_filename !~ m/\.adoc$/; # skip attributes.txt
78
79 foreach my $e (@$env_list) {
80 $fileinfo->{include}->{$e}->{$filename}->{$include_filename} = 1;
81 }
82 }
83
84 sub register_blockid {
85 my ($filename, $blockid, $env_list) = @_;
86
87 foreach my $e (@$env_list) {
88 my $fn = $fileinfo->{blockid}->{$e}->{$blockid};
89 die "blockid '$blockid' already defined in $fn"
90 if defined($fn);
91 $fileinfo->{blockid}->{$e}->{$blockid} = $filename;
92 }
93 }
94
95 sub scan_adoc_file {
96 my ($filename) = @_;
97
98 reset_environment_stack();
99
100 # print "SCAN $filename\n";
101
102 my $fh = IO::File->new("$filename", "r") or
103 die "unable to open file '$filename' - $!\n";
104
105 my $env_last_line = {};
106
107 while (defined (my $line = <$fh>)) {
108 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
109 my ($not, $env, $text) = ($1, $2);
110 die "unsuported ifdef usage - implement me" if $text;
111 push_environment($env, $not);
112 next;
113 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
114 my ($env, $text) = ($1, $2);
115 die "unsuported ifdef usage - implement me" if $text;
116 pop_environment($env);
117 next;
118 } elsif ($line =~ m/^include::(\S+)\[.*\]\s*$/) {
119 register_include($filename, $1, $env_stack->[-1]);
120 next;
121 }
122
123 # try to detect titles
124 foreach my $e (@{$env_stack->[-1]}) {
125 my $title = $fileinfo->{titles}->{$e}->{$filename};
126 next if defined($title);
127
128 if (($line =~ m/^=====+/) || ($line =~ m/^-----+/)) {
129 $fileinfo->{titles}->{$e}->{$filename} = $env_last_line->{$e};
130 }
131 $env_last_line->{$e} = $line;
132 chomp $env_last_line->{$e};
133 }
134
135 # fixme: also scan <<>>
136
137 while ($line =~ m/xref:([^\s\[\]]+)\[([^\]]*)\]/g) {
138 # print "$filename xref:$1 [$2]\n";
139 }
140
141 if ($line =~ m/^\[\[(.*)\]\]\s*$/) {
142 my $blockid = $1;
143 die "implement me" if $blockid =~m/,/;
144 register_blockid($filename, $blockid, $env_stack->[-1]);
145 }
146 }
147 }
148
149 my $scanned_files = {};
150 while (my $filename = shift) {
151 next if $filename !~ m/\.adoc$/; # skip attributes.txt
152 next if $scanned_files->{$filename};
153
154 scan_adoc_file($filename);
155 $scanned_files->{$filename} = 1;
156 }
157
158 sub resolve_link_target {
159 my ($env, $filename) = @_;
160
161 my $include_hash = $fileinfo->{include}->{$env};
162
163 my $repeat = 1;
164
165 while ($repeat) {
166 $repeat = 0;
167 foreach my $fn (keys %$include_hash) {
168 next if $resolve_skip_files->{$env}->{$fn};
169 if ($include_hash->{$fn}->{$filename}) {
170 $filename = $fn;
171 $repeat = 1;
172 last;
173 }
174 }
175 }
176
177 return $filename;
178 }
179
180 # now resolve blockids
181 foreach my $e (@$start_env) {
182 my $blockid_hash = $fileinfo->{blockid}->{$e};
183 foreach my $blockid (keys %$blockid_hash) {
184 my $fn = resolve_link_target($e, $blockid_hash->{$blockid});
185 if ($e eq 'wiki') {
186 my $title = $fileinfo->{titles}->{$e}->{$fn};
187 $title =~ s/\s/_/g;
188 $title =~ s/\{pve\}/Proxmox VE/g;
189 die "found not title for '$fn' in env '$e'" if !$title;
190 $fileinfo->{blockid_target}->{$e}->{$blockid} = "link:/wiki/$title#$blockid";
191 } else {
192 $fileinfo->{blockid_target}->{$e}->{$blockid} = $fn;
193 }
194 }
195 }
196
197
198 print to_json($fileinfo, { pretty => 1 } );
199