11# !/usr/local/bin/perl
22# Add index anchors to source file _and_ generate index database
33# Arnaud Le Hors - lehors@w3.org
4- # $Id: addianch,v 1.4 1997-08-01 13:29:09 ijacobs Exp $
4+ # $Id: addianch,v 1.5 1997-08-01 17:03:03 ijacobs Exp $
55
66if (($_ = $ARGV [0], /^-r /) && $ARGV [0]) {
77 shift ;
@@ -45,20 +45,33 @@ sub readfile {
4545 close (INPUT);
4646}
4747
48- # store index and return unique anchor
48+ # store index and return an anchor.
49+ # Since each index markup can define several
50+ # links to the same anchor, create a unique
51+ # anchor the first time this function is
52+ # called (per markup) and reuse it thereafter
53+
4954sub storeindex {
50- ($prefix , $index , $cmt ) = @_ ;
55+ ($prefix , $index , $cmt , $anchor ) = @_ ;
5156 if ($indexes {$index }) {
52- $n = $# {$indexes {$index }} + 1;
53- $anchor = " $prefix -$index -$n " ;
54- # change possible spaces to underscores
55- $anchor =~ s / / _/ g ;
56- push (@{$indexes {$index }}, " $file #$anchor ;$cmt " );
57+ if (!$anchor ) {
58+ $n = $# {$indexes {$index }} + 1;
59+ $anchor = " $prefix -$index -$n " ;
60+ # change possible spaces to underscores
61+ $anchor =~ s / / _/ g ;
62+ # leave only alphanumeric characters
63+ $anchor =~ s / [^a-zA-Z0-9_-]// g ;
64+ }
65+ push (@{$indexes {$index }}, " $file #$anchor |$cmt " );
5766 } else {
58- $anchor = " $prefix -$index " ;
59- # change possible spaces to underscores
60- $anchor =~ s / / _/ g ;
61- @{$indexes {$index }} = (" $file #$anchor ;$cmt " );
67+ if (!$anchor ) {
68+ $anchor = " $prefix -$index " ;
69+ # change possible spaces to underscores
70+ $anchor =~ s / / _/ g ;
71+ # leave only alphanumeric characters
72+ $anchor =~ s / [^a-zA-Z0-9_-]// g ;
73+ }
74+ @{$indexes {$index }} = (" $file #$anchor |$cmt " );
6275 }
6376
6477 return $anchor ;
@@ -74,28 +87,38 @@ sub addanchor {
7487 # see if a title is specified
7588 $_ = $st ;
7689 if (/ ${sep} title=(?:$qwd |($wd ))/sgio ) {
77- # only $1 or $2 will actually be non null
78- ($index , $cmt ) = split (/ , */ , " $1$2 " , 2);
90+ $anchor = ();
91+ # only $1 or $2 will actually be non null
92+ foreach $idxentry (split (/ [\n \t ]*\| [\n \t ]*/ , " $1$2 " , 2)) {
93+ ($index , $cmt ) = split (/ , */ , " $idxentry " , 2);
94+ # New lines -> spaces in keys (title OR content)
95+ # And compress extra space
96+ $index =~ s / [\n \t ]+/ / g ;
97+ $cmt =~ s / [\n \t ]+/ / g ;
98+ # Remove initial white space
99+ $index =~ s / ^[\n \t ]+// ;
100+ $cmt =~ s / ^[\n \t ]+// ;
101+ $anchor = storeindex($prefix , $index , $cmt , $anchor );
102+ }
79103 } else {
80- $index = $content ;
81- $cmt = ();
104+ $index = $content ;
105+ $cmt = ();
106+ # New lines -> spaces in keys (title OR content)
107+ # And compress extra space
108+ $index =~ s / [\n \t ]+/ / g ;
109+ $cmt =~ s / [\n \t ]+/ / g ;
110+ # Remove initial white space
111+ $index =~ s / ^[\n \t ]+// ;
112+ $cmt =~ s / ^[\n \t ]+// ;
113+ $anchor = storeindex($prefix , $index , $cmt , ());
82114 }
83- # New lines -> spaces in keys (title OR content)
84- # And compress extra space
85- $index =~ s / [\n \t ]+/ / g ;
86- $cmt =~ s / [\n \t ]+/ / g ;
87- # Remove initial white space
88- $index =~ s / ^[\n \t ]+// ;
89- $cmt =~ s / ^[\n \t ]+// ;
90115
91116 # HACK!!!
92117 # add a non breakable space in the anchor to workaround
93118 # broken browsers which don't support correctly empty anchors
94- return " $st <a name=\" " .
95- storeindex($prefix , $index , $cmt ) . " \" > </a>$content$et " ;
119+ return " $st <a name=\" " . $anchor . " \" > </a>$content$et " ;
96120# if the span tags are to be removed use the following two lines instead
97- # return "<a name=\"" .
98- # storeindex($prefix, $index, $cmt) . "\"> </a>$content";
121+ # return "<a name=\"" . $anchor . "\"> </a>$content";
99122}
100123
101124# ## main
@@ -118,12 +141,15 @@ print OUTPUT $buf;
118141close (OUTPUT);
119142
120143# print out index database
144+ # Don't separate fields with ";" because keys may contain markup
145+ # With entities.
146+
121147if (!open (DBASE, " > $dbasef " )) {
122148 print STDERR " $PROGNAME Error: Cannot open dbfile: $dbasef \n " ;
123149} else {
124150 foreach $index (sort (keys %indexes )) {
125151 foreach $item (@{$indexes {$index }}) {
126- print DBASE " $index ; $item \n " ;
152+ print DBASE " $index | $item \n " ;
127153 }
128154 }
129155 close (DBASE);
0 commit comments