1 | #!@PERL@ -w |
---|
2 | # -*- cperl -*- |
---|
3 | # |
---|
4 | # gtk-doc - GTK DocBook documentation generator. |
---|
5 | # Copyright (C) 1998 Damon Chaplin |
---|
6 | # |
---|
7 | # This program is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | # |
---|
21 | |
---|
22 | ############################################################################# |
---|
23 | # Script : gtkdoc-fixxref |
---|
24 | # Description : This fixes cross-references in the HTML documentation. |
---|
25 | ############################################################################# |
---|
26 | |
---|
27 | use strict; |
---|
28 | use bytes; |
---|
29 | use Getopt::Long; |
---|
30 | |
---|
31 | # Options |
---|
32 | |
---|
33 | # name of documentation module |
---|
34 | my $MODULE; |
---|
35 | my $MODULE_DIR; |
---|
36 | my $HTML_DIR = ""; |
---|
37 | my @EXTRA_DIRS; |
---|
38 | my $PRINT_VERSION; |
---|
39 | my $PRINT_HELP; |
---|
40 | |
---|
41 | my %optctl = (module => \$MODULE, |
---|
42 | 'module-dir' => \$MODULE_DIR, |
---|
43 | 'html-dir' => \$HTML_DIR, |
---|
44 | 'extra-dir' => \@EXTRA_DIRS, |
---|
45 | 'version' => \$PRINT_VERSION, |
---|
46 | 'help' => \$PRINT_HELP); |
---|
47 | GetOptions(\%optctl, "module=s", "module-dir=s", "html-dir=s", "extra-dir=s@", |
---|
48 | "version", "help"); |
---|
49 | |
---|
50 | if ($PRINT_VERSION) { |
---|
51 | print "@VERSION@\n"; |
---|
52 | exit 0; |
---|
53 | } |
---|
54 | |
---|
55 | if ($PRINT_HELP) { |
---|
56 | print "gtkdoc-fixxref version @VERSION@\n"; |
---|
57 | print "\n--module=MODULE_NAME Name of the doc module being parsed"; |
---|
58 | print "\n--module-dir=MODULE_DIR The directory which contains the generated HTML"; |
---|
59 | print "\n--html-dir=HTML_DIR The directory where gtk-doc generated documentation is installed"; |
---|
60 | print "\n--extra-dir=EXTRA_DIR Directories to scan for indices in addition to HTML_DIR"; |
---|
61 | print "\n May be used more than once for multiple directories"; |
---|
62 | print "\n--version Print the version of this program"; |
---|
63 | print "\n--help Print this help\n"; |
---|
64 | exit 0; |
---|
65 | } |
---|
66 | |
---|
67 | # This contains all the entities and their relative URLs. |
---|
68 | my %Links; |
---|
69 | |
---|
70 | &ScanIndices ($HTML_DIR); |
---|
71 | foreach my $dir (@EXTRA_DIRS) { |
---|
72 | &ScanIndices ($dir); |
---|
73 | } |
---|
74 | |
---|
75 | &FixCrossReferences (defined $MODULE_DIR ? $MODULE_DIR : "$HTML_DIR/$MODULE"); |
---|
76 | |
---|
77 | sub ScanIndices { |
---|
78 | my ($scan_dir) = @_; |
---|
79 | # print "Scanning source directory: $scan_dir\n"; |
---|
80 | |
---|
81 | # This array holds any subdirectories found. |
---|
82 | my (@subdirs) = (); |
---|
83 | |
---|
84 | opendir (HTMLDIR, $scan_dir) || return; |
---|
85 | my $file; |
---|
86 | foreach $file (readdir (HTMLDIR)) { |
---|
87 | if ($file eq '.' || $file eq '..') { |
---|
88 | next; |
---|
89 | } elsif (-d "$scan_dir/$file") { |
---|
90 | push (@subdirs, $file); |
---|
91 | } elsif ($file eq "index.sgml") { |
---|
92 | &ScanIndex ("$scan_dir/$file"); |
---|
93 | } |
---|
94 | } |
---|
95 | closedir (HTMLDIR); |
---|
96 | |
---|
97 | # Now recursively scan the subdirectories. |
---|
98 | my $dir; |
---|
99 | foreach $dir (@subdirs) { |
---|
100 | &ScanIndices ("$scan_dir/$dir"); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | sub ScanIndex { |
---|
105 | my ($file) = @_; |
---|
106 | # print "Scanning index file: $file\n"; |
---|
107 | |
---|
108 | open (INDEXFILE, $file) |
---|
109 | || die "Can't open $file: $!"; |
---|
110 | while (<INDEXFILE>) { |
---|
111 | if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) { |
---|
112 | # print "Found id: $1 href: $2\n"; |
---|
113 | $Links{$1} = $2; |
---|
114 | } |
---|
115 | } |
---|
116 | close (INDEXFILE); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | sub FixCrossReferences { |
---|
121 | my ($scan_dir) = @_; |
---|
122 | |
---|
123 | opendir (HTMLDIR, $scan_dir) |
---|
124 | || die "Can't open HTML directory $scan_dir: $!"; |
---|
125 | my $file; |
---|
126 | foreach $file (readdir (HTMLDIR)) { |
---|
127 | if ($file eq '.' || $file eq '..') { |
---|
128 | next; |
---|
129 | } elsif ($file =~ m/.html?$/) { |
---|
130 | &FixHTMLFile ("$scan_dir/$file"); |
---|
131 | } |
---|
132 | } |
---|
133 | closedir (HTMLDIR); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | sub FixHTMLFile { |
---|
138 | my ($file) = @_; |
---|
139 | # print "Fixing file: $file\n"; |
---|
140 | |
---|
141 | open (HTMLFILE, $file) |
---|
142 | || die "Can't open $file: $!"; |
---|
143 | undef $/; |
---|
144 | my $entire_file = <HTMLFILE>; |
---|
145 | close (HTMLFILE); |
---|
146 | |
---|
147 | $entire_file =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($1, $2); %ge; |
---|
148 | |
---|
149 | open (NEWFILE, ">$file.new") |
---|
150 | || die "Can't open $file: $!"; |
---|
151 | print NEWFILE $entire_file; |
---|
152 | close (NEWFILE); |
---|
153 | |
---|
154 | unlink ($file) |
---|
155 | || die "Can't delete $file: $!"; |
---|
156 | rename ("$file.new", $file) |
---|
157 | || die "Can't rename $file.new: $!"; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | sub MakeXRef { |
---|
162 | my ($id, $text) = @_; |
---|
163 | |
---|
164 | my $href = $Links{$id}; |
---|
165 | if ($href) { |
---|
166 | return "<a\nhref=\"../$href\"\n>$text</a>"; |
---|
167 | } else { |
---|
168 | return $text; |
---|
169 | } |
---|
170 | } |
---|