source: tags/ms_r18q1/HELP_SOURCE/generate_index.pl

Last change on this file was 13625, checked in by westram, 9 years ago
  • Property svn:executable set to *
File size: 6.8 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6# ----------------------------------------
7# hardcoded config for help-map
8
9# ignore these pages (they are linked too frequently)
10my %ignore = (
11              'glossary' => 1,
12              'arb' => 1,
13             );
14
15my $start_from = 'pa_optimizer'; # start-page
16my $depth      = 3; # show pages reachable by that many links (both link directions)
17
18# ----------------------------------------
19
20
21sub read_xml($);
22sub read_xml($) {
23  my ($xml_dir) = @_;
24
25  my @xml = ();
26  my @sub = ();
27
28  opendir(DIR,$xml_dir) || die "Failed to read '$xml_dir' (Reason: $!)";
29  foreach (readdir(DIR)) {
30    if ($_ ne '.' and $_ ne '..') {
31      my $full = $xml_dir.'/'.$_;
32      if (-d $full) {
33        push @sub, $_;
34      }
35      elsif (/\.xml$/o) {
36        push @xml, $_;
37      }
38    }
39  }
40  closedir(DIR);
41
42  foreach my $sub (@sub) {
43    my @subxml = read_xml($xml_dir.'/'.$sub);
44    foreach (@subxml) {
45      push @xml, $sub.'/'.$_;
46    }
47  }
48
49  return @xml;
50}
51
52sub print_index(\@) {
53  my ($xml_r) = @_;
54
55  my $header=<<HEADER;
56<?xml version="1.0" encoding="UTF-8" standalone="no"?>
57<!DOCTYPE PAGE SYSTEM 'arb_help.dtd' [
58  <!ENTITY nbsp "&#160;">
59  <!ENTITY acute "&#180;">
60  <!ENTITY eacute "&#233;">
61  <!ENTITY apostr "&#39;">
62  <!ENTITY semi "&#59;">
63]>
64<!-- This file has been generated by ../generate_index.pl -->
65<PAGE name="help_index" edit_warning="devel">
66  <TITLE>ARB help index</TITLE>
67  <SECTION name="List of existing helpfiles">
68    <LIST>
69HEADER
70  my $footer=<<FOOTER;
71    </LIST>
72  </SECTION>
73</PAGE>
74FOOTER
75
76  print $header;
77  foreach my $xml (@$xml_r) {
78    my $hlp  = $xml;
79    $hlp =~ s/\.xml$/\.hlp/o;
80    my $link = '      <T><LINK dest="'.$hlp.'" type="hlp" quoted="0"/></T>';
81    print $link."\n";
82  }
83  print $footer;
84
85}
86
87sub find_indexed_xmls($$) {
88  my ($index_name,$xml_dir) = @_;
89
90  my @xml = read_xml($xml_dir);
91  @xml = sort map {
92    if ($_ eq $index_name) { ; } # dont index the index
93    else { $_; }
94  } @xml;
95  return @xml;
96}
97
98my %link = (); # key='from>to' value=bitvalue(1=uplink,2=sublink)
99
100sub storeLink($$$) {
101  my ($from,$to,$us) = @_;
102  # print STDERR "storeLink from='$from' to='$to' us='$us'\n";
103
104  my $concat = $from.' '.$to;
105  die "invalid char '>' in '$concat'" if $concat =~ />/o;
106
107  my $bit = 0;
108  if ($us eq 'UP') { $bit = 1; }
109  elsif ($us eq 'SUB') { $bit = 2; }
110
111  my $key = $from.'>'.$to;
112  my $val = $link{$key};
113  if (not defined $val) { $val = 0; }
114
115  $val = $val | $bit;
116  $link{$key} = $val;
117}
118
119my %title_line = (); # key=xml-filename, value=lineno of <TITLE>..
120
121sub parse_titles($\@\%) {
122  my ($xml_dir,$xml_r, $title_r) = @_;
123  foreach my $name (@$xml_r) {
124    my $xml = $xml_dir.'/'.$name;
125    open(FILE,'<'.$xml) || die "can't read '$xml' (Reason: $!)";
126
127    my $namePlain = $name;
128    if ($namePlain=~ /\.xml$/o) { $namePlain = $`; }
129
130    my $line;
131  LINE: while (defined($line=<FILE>)) {
132      if ($line =~ /<TITLE>(.*)<\/TITLE>/o) {
133        $$title_r{$name} = $1;
134        $title_line{$name} = $.;
135        last LINE; # TITLE occurs behind UP/SUB links -> done here
136      }
137      if ($line =~ /<(UP|SUB)\s+dest=/o) {
138        my $us = $1;
139        if ($line =~ /"(.*)"\s+type="(.*)"/o) {
140          my ($dest,$type) = ($1,$2);
141          if ($dest =~ /\.hlp$/o) { $dest = $`; }
142          storeLink($namePlain,$dest,$us);
143        }
144      }
145    }
146    close(FILE);
147
148    if (not defined $$title_r{$name}) {
149      die "$xml:1: Failed to parse title\n ";
150    }
151  }
152}
153
154sub warn_duplicate_titles($\%) {
155  my ($xml_dir,$title_r) = @_;
156  my $hlpdir = $xml_dir;
157  my %seen = ();
158  foreach my $file (keys %$title_r) {
159    my $title = $$title_r{$file};
160    if (defined $seen{$title}) {
161      my $firstFile = $seen{$title};
162      my $thisLine  = $title_line{$file};
163      my $firstLine = $title_line{$firstFile};
164
165      print STDERR "${xml_dir}/${file}:${thisLine}: Warning: duplicated title '$title' ..\n";
166      print STDERR "${xml_dir}/${firstFile}:${firstLine}: Warning: .. first seen here.\n";
167    }
168    else {
169      $seen{$title} = $file;
170    }
171  }
172}
173
174sub generate_index($$) {
175  my ($index_name,$xml_dir) = @_;
176
177  my @xml   = find_indexed_xmls($index_name,$xml_dir);
178  my %title = ();
179  parse_titles($xml_dir,@xml,%title);
180
181  warn_duplicate_titles($xml_dir,%title);
182
183  @xml = sort { $title{$a} cmp $title{$b}; } @xml;
184
185  print_index(@xml);
186}
187
188sub dot_label($) {
189  my ($target) = @_;
190  return '"'.$target.'"';
191}
192
193sub generate_map($) {
194  my ($map_name) = @_;
195
196  # my $maxsize = 17; # inch
197  # my $maxsize = 20; # inch
198  # my $maxsize = 40; # inch
199  my $maxsize = 80; # inch
200  open(DOT,'>'.$map_name) || die "can't write '$map_name' (Reason: $!)";
201
202  print DOT "digraph ARBHELPDEP {\n";
203  # print DOT "  rankdir=LR;\n";
204  print DOT "  concentrate=true;\n";
205  print DOT "  searchsize=1000;\n";
206  print DOT "  Damping=2.0;\n";
207  print DOT "  size=\"$maxsize,$maxsize\";\n";
208  # print DOT "  orientation=portrait;\n";
209  print DOT "\n";
210
211  my %use = ( $start_from => 1 );
212
213  my $added = 1;
214  while ($added==1) {
215    $added = 0;
216    foreach (keys %link) {
217      die if (not $_ =~ />/o);
218      my ($from,$to) = ($`,$');
219
220      die "helpfile '$to' links to itself" if ($to eq $from);
221
222      if (exists $use{$from}) {
223        my $next = $use{$from}+1;
224        if (($next<=$depth) and ((not exists $use{$to}) or ($use{$to}>$next)) and (not $ignore{$to})) {
225          $use{$to} = $next;
226          # print STDERR "'$to' set to $next (triggered by '$from' with use=".$use{$from}.")\n";
227          $added = 1;
228        }
229      }
230      if (exists $use{$to}) {
231        my $next = $use{$to}+1;
232        if (($next<=$depth) and ((not exists $use{$from}) or ($use{$from}>$next)) and (not $ignore{$from})) {
233          $use{$from} = $next;
234          # print STDERR "'$from' set to $next (triggered by '$to' with use=".$use{$to}.")\n";
235          $added = 1;
236        }
237      }
238    }
239  }
240
241  foreach (keys %link) {
242    die if (not $_ =~ />/o);
243    my ($from,$to) = ($`,$');
244    if ((not exists $ignore{$from}) and (not exists $ignore{$to})) {
245      if ((exists $use{$from}) and (exists $use{$to})) {
246        ($from,$to) = (dot_label($from.'['.$use{$from}.']'),dot_label($to.'['.$use{$to}.']'));
247        print DOT '    '.$from.' -> '.$to.';'."\n";
248      }
249    }
250  }
251  print DOT "}\n";
252  close(DOT);
253}
254
255sub main() {
256  my $args = scalar(@ARGV);
257  if ($args != 3) {
258    print "Usage: generate_index.pl XMLDIRECTORY NAME_OF_INDEX.xml MAP.dot\n";
259    print "Scans for xml-helpfiles in and below XMLDIRECTORY.\n";
260    print "Generates\n";
261    print "- list of all found helpfiles to STDOUT (assuming it is piped to NAME_OF_INDEX.xml)\n";
262    print "- a (partial) help-map in file MAP.dot\n";
263    die "Error: missing arguments";
264  }
265
266  my $xml_dir    = $ARGV[0];
267  my $index_name = $ARGV[1];
268  my $map_name   = $ARGV[2];
269
270  if (not -d $xml_dir) { die "No such directory '$xml_dir'"; }
271
272  generate_index($index_name,$xml_dir);
273  generate_map($map_name);
274}
275main();
Note: See TracBrowser for help on using the repository browser.