]>
git.proxmox.com Git - pmg-docs.git/blob - scan-adoc-refs
13 GetOptions
("depends=s" => \
$generate_depends) or
14 die("Error in command line arguments\n");
21 pmglogo
=> 0, # ignore
27 foreach my $e (keys %$environments) {
28 push @$start_env, $e if $environments->{$e};
31 my $env_stack = [$start_env];
32 my $env_name_stack = [];
34 sub reset_environment_stack
{
35 $env_stack = [$start_env];
39 sub push_environment
{
42 die "undefined environment '$env'\n" if !defined($environments->{$env});
44 # FIXME: this seems wrong (nested env?)?
45 return if !$environments->{$env}; # do not track
49 foreach my $e (@{$env_stack->[-1]}) {
54 die "empty environment" if !scalar($new_env);
55 push @$env_stack, $new_env;
57 push @$env_stack, [$env];
60 push @$env_name_stack, $env;
66 die "undefined environment '$env'\n" if !defined($environments->{$env});
68 return if !$environments->{$env}; # do not track
71 my $res = pop @$env_name_stack;
73 die "environment missmatch ($res != $env)\n" if $res ne $env;
76 sub register_include
{
77 my ($filename, $include_filename, $env_list) = @_;
79 foreach my $e (@$env_list) {
80 $fileinfo->{include
}->{$e}->{$filename}->{$include_filename} = 1;
84 sub register_blockid
{
85 my ($filename, $blockid, $reftext, $env_list) = @_;
87 foreach my $e (@$env_list) {
88 my $fn = $fileinfo->{blockid
}->{$e}->{$blockid};
89 die "blockid '$blockid' already defined in $fn"
91 $fileinfo->{blockid
}->{$e}->{$blockid} = $filename;
92 $fileinfo->{reftext
}->{$e}->{$blockid} = $reftext
98 my ($filename, $env, $doctype, $title, $blockid) = @_;
100 # fixme: what about other macros?
101 $title =~ s/\{pmg\}/Proxmox Mail Gateway/g;
102 $title =~ s!http://\S+\[(.*?)\]!$1!g;
104 # register document title (onyl once)
105 if (!defined($fileinfo->{titles
}->{$env}->{$filename})) {
107 $fileinfo->{titles
}->{$env}->{$filename} = $title;
109 if (defined($doctype)) {
110 $fileinfo->{doctype
}->{$env}->{$filename} = $doctype;
112 die "unable to change title (no doctype)"
113 if !defined($fileinfo->{doctype
}->{$env}->{$filename});
115 } elsif (!defined($doctype)) {
116 # change title via :title: attribute
117 $fileinfo->{titles
}->{$env}->{$filename} = $title;
120 if (defined($doctype) && ($env eq 'manvolnum') && ($doctype == 0)) {
121 if ($title =~ m/.*\(([1-8])\)\s*$/) {
122 $fileinfo->{mansection
}->{$env}->{$filename} = $1;
128 if !defined($fileinfo->{blockid
}->{$env}->{$blockid});
129 $fileinfo->{reftitle
}->{$env}->{$blockid} = $title;
136 reset_environment_stack
();
138 # print "SCAN $filename\n";
140 my $fh = IO
::File-
>new("$filename", "r") or
141 die "unable to open file '$filename' - $!\n";
143 my $env_last_line = {};
144 my $env_last_blockid = {};
146 while (defined (my $line = <$fh>)) {
147 if ($line =~ m/^if(n?)def::(\S+)\[(.*)\]\s*$/) {
148 my ($not, $env, $text) = ($1, $2, $3);
149 die "unsuported ifdef usage - implement me" if $text;
150 push_environment
($env, $not);
152 } elsif ($line =~ m/^endif::(\S+)\[(.*)\]\s*$/) {
153 my ($env, $text) = ($1, $2);
154 die "unsuported ifdef usage - implement me" if $text;
155 pop_environment
($env);
157 } elsif ($line =~ m/^include::(\S+)\[.*\]\s*$/) {
158 register_include
($filename, $1, $env_stack->[-1]);
162 # try to detect titles
163 foreach my $e (@{$env_stack->[-1]}) {
164 if ($line =~ m/^===+$/) {
165 register_title
($filename, $e, 0, $env_last_line->{$e},
166 $env_last_blockid->{$e});
167 } elsif ($line =~ m/^---+$/) {
168 register_title
($filename, $e, 1, $env_last_line->{$e},
169 $env_last_blockid->{$e});
170 } elsif ($line =~ m/^~~~+$/) {
171 register_title
($filename, $e, 2, $env_last_line->{$e},
172 $env_last_blockid->{$e});
173 } elsif ($line =~ m/^\^\^\^+$/) {
174 register_title
($filename, $e, 3, $env_last_line->{$e},
175 $env_last_blockid->{$e});
176 } elsif ($line =~ m/^= +(\S.*?)( +=)?$/) {
177 register_title
($filename, $e, 0, $1, $env_last_blockid->{$e});
178 } elsif ($line =~ m/^== +(\S.*?)( +==)?$/) {
179 register_title
($filename, $e, 1, $1, $env_last_blockid->{$e});
180 } elsif ($line =~ m/^=== +(\S.*?)( +===)?$/) {
181 register_title
($filename, $e, 2, $1, $env_last_blockid->{$e});
182 } elsif ($line =~ m/^==== +(\S.*?)( +====)?$/) {
183 register_title
($filename, $e, 3, $1, $env_last_blockid->{$e});
186 $env_last_line->{$e} = $line;
187 chomp $env_last_line->{$e};
190 if ($line =~ m/^:(\S+?):\s*(.*\S)?\s*$/) {
191 my ($key, $value) = ($1, $2);
192 if ($key eq 'pmg-toplevel') {
194 foreach my $e (@{$env_stack->[-1]}) {
195 my $title = $fileinfo->{titles
}->{$e}->{$filename};
196 die "not title for toplevel file '$filename' (env=$e)\n"
198 $fileinfo->{toplevel
}->{$e}->{$filename} = 1;
200 } elsif ($key eq 'title') {
201 foreach my $e (@{$env_stack->[-1]}) {
202 register_title
($filename, $e, undef, $value);
207 if ($line =~ m/^\[\[(.*)\]\]\s*$/) {
209 die "implement me" if $blockid =~m/,/;
211 register_blockid
($filename, $blockid, $reftext, $env_stack->[-1]);
212 foreach my $e (@{$env_stack->[-1]}) {
213 $env_last_blockid->{$e} = $blockid;
217 if ($line =~ m/^\s*$/) {
218 foreach my $e (@{$env_stack->[-1]}) {
219 delete $env_last_blockid->{$e};
224 # bibliography anchors
225 if ($line =~ m/\[\[\[([^\]]*)\]\]\]/) {
227 die "implement me" if $blockid =~m/,/;
228 register_blockid
($filename, $blockid, "[$blockid]", $env_stack->[-1]);
233 my $scanned_files = {};
234 while (my $filename = shift) {
235 next if $filename !~ m/\.adoc$/; # skip attributes.txt
236 next if $filename =~ m/-(opts|synopsis)\.adoc$/;
237 next if $scanned_files->{$filename};
239 scan_adoc_file
($filename);
240 $scanned_files->{$filename} = 1;
243 sub resolve_link_target
{
244 my ($env, $filename) = @_;
246 my $include_hash = $fileinfo->{include
}->{$env};
252 foreach my $fn (keys %$include_hash) {
253 if ($include_hash->{$fn}->{$filename}) {
254 next if ($fn eq 'pmg-admin-guide.adoc') &&
255 $fileinfo->{outfile
}->{$env}->{$filename};
267 # try to generate output file mapping
268 foreach my $e (@$start_env) {
269 my $toplevel_hash = $fileinfo->{toplevel
}->{$e};
270 foreach my $fn (sort keys %$toplevel_hash) {
271 my $mansection = $fileinfo->{mansection
}->{manvolnum
}->{$fn};
274 $realfn =~ s/\.adoc$//;
275 if (defined($mansection) && ($mansection == 5)) {
276 $realfn .= ".$mansection";
278 $realfn = "$realfn-plain.html";
279 $fileinfo->{outfile
}->{$e}->{$fn} = $realfn;
280 } elsif ($e eq 'manvolnum') {
282 $realfn =~ s/\.adoc$//;
283 die "toplevel file '$fn' is not marked as manual page!" if !$mansection;
284 $realfn .= ".$mansection";
285 $fileinfo->{outfile
}->{$e}->{$fn} = $realfn;
286 } elsif ($e eq 'default') {
288 $realfn =~ s/\.adoc$//;
289 if (defined($mansection) && ($mansection == 5)) {
290 $realfn .= ".$mansection";
291 $realfn = "$realfn.html";
293 if (($fn ne 'pmg-admin-guide.adoc') &&
294 $fileinfo->{doctype
}->{$e}->{$fn} == 0) {
295 $realfn = "chapter-$realfn.html";
297 $realfn = "$realfn.html";
300 $fileinfo->{outfile
}->{$e}->{$fn} = $realfn;
305 # now resolve blockids
306 foreach my $e (@$start_env) {
307 my $blockid_hash = $fileinfo->{blockid
}->{$e};
308 foreach my $blockid (keys %$blockid_hash) {
309 my $fn = resolve_link_target
($e, $blockid_hash->{$blockid});
311 my $title = $fileinfo->{titles
}->{$e}->{$fn};
313 die "found not title for '$fn' in env '$e'" if !$title;
314 $fileinfo->{blockid_target
}->{$e}->{$blockid} = "link:/wiki/$title#$blockid";
316 # we do not produce wiki pages for all content
317 #my $realfn = $fileinfo->{outfile}->{$e}->{$fn};
318 #warn "no output file mapping for '$fn' ($e)\n" if !$realfn;
320 } elsif ($e eq 'default') {
321 my $realfn = $fileinfo->{outfile
}->{$e}->{$fn} ||
322 die "no output file mapping for '$fn'\n";
323 $fileinfo->{blockid_target
}->{$e}->{$blockid} = "link:/pmg-docs/$realfn#$blockid";
324 } elsif ($e eq 'manvolnum') {
325 # we do not produce manpages for all content
326 # my $realfn = $fileinfo->{outfile}->{$e}->{$fn} ||
327 # warn "no output file mapping for '$fn'\n";
328 $fileinfo->{blockid_target
}->{$e}->{$blockid} = $fn;
333 my $makevar_hash = {};
338 die "makefile variable '$varname' already defined\n"
339 if exists($makevar_hash->{$varname});
341 $makevar_hash->{$varname} = {};
344 sub makevar_list_add
{
345 my ($varname, $value) = @_;
347 die "makefile variable '$varname' not defined\n"
348 if !exists($makevar_hash->{$varname});
350 $makevar_hash->{$varname}->{$value} = 1;
356 foreach my $varname (sort keys %$makevar_hash) {
357 $txt .= "$varname =";
358 foreach my $value (sort keys %{$makevar_hash->{$varname}}) {
359 $txt .= " \\\n\t$value";
367 if ($generate_depends) {
369 my $tmpfilename = "${generate_depends}.tmp";
370 my $outfh = IO
::File-
>new($tmpfilename, "w") ||
371 die "unable to open temporary file '$tmpfilename'\n";
374 foreach my $e (@$start_env) {
375 my $env_data = $fileinfo->{include
}->{$e};
382 $depends->{$fn}->{$dep} = 1;
383 foreach my $nd (keys %{$env_data->{$dep}}) {
384 &$add_depends($fn, $nd);
388 foreach my $fn (keys %$env_data) {
389 foreach my $dep (keys %{$env_data->{$fn}}) {
390 &$add_depends($fn, $dep);
395 my $depend_varname_hash = {};
397 foreach my $fn (sort keys %$depends) {
398 my $basename = uc($fn);
399 $basename =~s/\.adoc$//i;
400 $basename =~s/[^A-Za-z0-9]/_/g;
402 my $varname1 = "${basename}_ADOCSOURCES";
403 my $varname2 = "${basename}_ADOCDEPENDS";
405 makevar_define
($varname1);
406 makevar_define
($varname2);
408 $depend_varname_hash->{$fn} = $varname2;
410 makevar_list_add
($varname1, $fn);
411 makevar_list_add
($varname2, "\$\{$varname1\}");
413 foreach my $dep (sort keys %{$depends->{$fn}}) {
414 if ($dep =~ m/-(opts|synopsis).adoc$/) {
415 makevar_list_add
($varname2, $dep);
417 makevar_list_add
($varname1, $dep);
422 my $man_sources_hash = {};
423 foreach my $sourcefile (keys %{$fileinfo->{outfile
}->{manvolnum
}}) {
424 $man_sources_hash->{$sourcefile} = 1;
425 my $ihash = $fileinfo->{include
}->{manvolnum
}->{$sourcefile};
426 foreach my $include (keys %$ihash) {
427 if ($include !~ m/-(opts|synopsis).adoc$/) {
428 $man_sources_hash->{$include} = 1;
433 my $varname = "MANUAL_SOURCES";
434 makevar_define
($varname);
435 foreach my $sourcefile (sort keys %$man_sources_hash) {
436 makevar_list_add
($varname, $sourcefile);
439 $varname = "CHAPTER_LIST";
440 makevar_define
($varname);
441 my $filelist = $fileinfo->{outfile
}->{default};
442 foreach my $sourcefile (sort keys %$filelist) {
443 my $target = $filelist->{$sourcefile};
444 makevar_list_add
($varname, $target);
447 $varname = "MANUAL_PAGES";
448 makevar_define
($varname);
449 $filelist = $fileinfo->{outfile
}->{manvolnum
};
450 foreach my $manpage (sort keys %$filelist) {
451 my $target = $filelist->{$manpage};
452 makevar_list_add
($varname, $target);
455 $varname = "WIKI_IMPORTS";
456 makevar_define
($varname);
457 $filelist = $fileinfo->{outfile
}->{wiki
};
458 foreach my $sourcefile (sort keys %$filelist) {
459 my $target = $filelist->{$sourcefile};
460 makevar_list_add
($varname, $target);
463 my $res = makevar_dump
();
465 my $make_targets = {};
466 foreach my $e (@$start_env) {
467 my $filelist = $fileinfo->{outfile
}->{$e};
468 foreach my $sourcefile (sort keys %$filelist) {
469 my $varname = $depend_varname_hash->{$sourcefile};
470 next if !defined($varname);
471 my $target = $filelist->{$sourcefile};
472 my $dep = "\$\{$varname\}";
473 $make_targets->{$target} = $dep;
474 if ($e eq 'manvolnum') {
475 $make_targets->{"$target.html"} = $dep;
476 $make_targets->{"$target-plain.html"} = $dep;
480 foreach my $target (sort keys%$make_targets) {
481 my $dep = $make_targets->{$target};
482 $res .= "$target: $dep\n\n";
488 rename($tmpfilename, $generate_depends) ||
489 die "rename failed - $!";
492 print to_json
($fileinfo, { pretty
=> 1, canonical
=> 1 } );