4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998 Damon Chaplin
6 # Copyright (C) 2007 David Necas (Yeti)
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #############################################################################
24 # Script : gtkdoc-rebase
25 # Description : Rebases URI references in installed HTML documentation.
26 #############################################################################
30 use Getopt::Long qw(:config gnu_getopt);
46 # These two point to the last seen URI of given type for a package:
47 # OnlineMap: package => on-line URI
48 # LocalMap: package => local URI
49 # This maps all seen URIs of a package to fix broken links in the process:
50 # RevMap: URI => package
51 my (%OnlineMap, %LocalMap, %RevMap);
52 # Remember what mangling we did.
56 run() unless caller; # Run program unless loaded as a module
60 my %optctl = ('html-dir' => \$HTML_DIR,
61 'other-dir' => \@OTHER_DIRS,
62 'dest-dir' => \$DEST_DIR,
64 'relative' => \$RELATIVE,
65 'aggressive' => \$AGGRESSIVE,
66 'verbose' => \$VERBOSE,
67 'version' => \$PRINT_VERSION,
68 'help' => \$PRINT_HELP);
69 GetOptions(\%optctl, 'html-dir=s', 'other-dir=s@', 'dest-dir:s',
70 'online', 'relative', 'aggressive', 'verbose',
80 gtkdoc-rebase version @VERSION@ - rewrite the base url of html files
82 --html-dir=HTML_DIR The directory which contains the installed HTML
83 --other-dir=OTHER_DIR Directories to recursively scan for indices (index.sgml)
84 May be used more than once for multiple directories
85 --online Prefer cross-references to online documents
86 --relative Prefer relative cross-references
87 --aggressive Rebase links to all files that are under a directory
88 matching a package name.
89 --dest-dir=ROOT_DIR Staging area virtual root, this prefix will be removed
90 from HTML_DIR fore relative link calculation.
92 --version Print the version of this program
93 --help Print this help
99 die "No HTML directory (--html-dir) given.\n";
104 # We scan the directory containing GLib and any directories in GNOME2_PATH
105 # first, but these will be overriden by any later scans.
106 if (defined ($ENV{"GNOME2_PATH"})) {
107 foreach $dir (split(/:/, $ENV{"GNOME2_PATH"})) {
108 $dir = $dir . "/share/gtk-doc/html";
109 if ($dir && -d $dir) {
110 print "Prepending GNOME2_PATH directory: $dir\n" if $VERBOSE;
111 unshift @OTHER_DIRS, $dir;
116 $dir = `pkg-config --variable=prefix glib-2.0`;
117 $dir =~ s/^\s*(\S*)\s*$/$1/;
118 $dir = $dir . "/share/gtk-doc/html";
119 print "Prepending GLib directory $dir\n" if $VERBOSE;
120 unshift @OTHER_DIRS, $dir;
122 # Check all other dirs, but skip already scanned dirs ord subdirs of those
124 $DEST_DIR =~ s#/?$#/#;
126 $HTML_DIR =~ s#/?$#/#;
128 foreach $dir (@OTHER_DIRS) {
129 &ScanDirectory($dir, $HTML_DIR);
133 &RelativizeLocalMap($HTML_DIR);
136 &RebaseReferences($HTML_DIR);
137 &PrintWhatWeHaveDone();
142 my ($dir, $self) = @_;
143 # This array holds any subdirectories found.
146 print "Scanning documentation directory $dir\n" if $VERBOSE;
148 if ("$dir/" eq $self) {
149 print "Excluding self\n" if $VERBOSE;
152 if (not opendir(HTMLDIR, $dir)) {
153 print "Cannot open $dir: $!\n";
160 foreach $file (readdir(HTMLDIR)) {
161 if ($file eq '.' or $file eq '..') {
164 elsif (-d "$dir/$file") {
165 push @subdirs, $file;
168 if ($file =~ m/\.devhelp2$/) {
169 print "Reading index from $file\n" if $VERBOSE;
170 my $o = &ReadDevhelp($dir, $file);
171 # Prefer this location over possibly stale index.sgml
177 if (!$onlinedir and ($file eq "index.sgml")) {
178 print "Reading index from index.sgml\n" if $VERBOSE;
179 $onlinedir = &ReadIndex($dir, $file);
182 elsif ($file eq "index.sgml.gz") {
183 # debian/ubuntu started to compress this as index.sgml.gz :/
185 Please fix https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138 . For now run:
189 elsif ($file =~ m/\.devhelp2.gz$/) {
190 # debian/ubuntu started to compress this as *devhelp2.gz :/
192 Please fix https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/1466210 . For now run:
196 # we could consider supporting: use IO::Zlib;
200 &AddMap($dir, $onlinedir);
203 # Now recursively scan the subdirectories.
205 foreach my $subdir (@subdirs) {
206 &ScanDirectory("$dir/$subdir", $self);
212 my ($dir, $file) = @_;
215 open(INDEXFILE, "$dir/$file") || die "Can't open $dir/$file: $!";
216 while (<INDEXFILE>) {
217 # online must come before chapter/functions
218 last if m/<(chapters|functions)/;
219 if (m/ online="([^"]*)"/) {
221 # Remove trailing non-directory component.
222 $onlinedir =~ s#(.*/).*#$1#;
231 my ($dir, $file) = @_;
234 open(INDEXFILE, "$dir/$file") || die "Can't open $dir/$file: $!";
235 while (<INDEXFILE>) {
236 # ONLINE must come before any ANCHORs
238 if (m/^<ONLINE\s+href\s*=\s*"([^"]+)"\s*>/) {
240 # Remove trailing non-directory component.
241 $onlinedir =~ s#(.*/).*#$1#;
250 my ($dir, $onlinedir) = @_;
254 ($package = $dir) =~ s#.*/([^/]+)/#$1#;
255 if ($DEST_DIR and substr($dir, 0, length $DEST_DIR) eq $DEST_DIR) {
256 $dir = substr($dir, -1 + length $DEST_DIR);
259 print "On-line location of $package: $onlinedir\n" if $VERBOSE;
260 $OnlineMap{ $package } = $onlinedir;
261 $RevMap{ $onlinedir } = $package;
263 print "No On-line location for $package found\n" if $VERBOSE;
265 print "Local location of $package: $dir\n" if $VERBOSE;
266 $LocalMap{ $package } = $dir;
267 $RevMap{ $dir } = $package;
271 sub RelativizeLocalMap {
276 $self = realpath $self;
278 ($prefix = $self) =~ s#[^/]+/$##;
279 foreach my $package (keys %LocalMap) {
280 $dir = $LocalMap{ $package };
281 if (substr($dir, 0, length $prefix) eq $prefix) {
282 $dir = "../" . substr($dir, length $prefix);
283 $LocalMap{ $package } = $dir;
284 print "Relativizing local location of $package to $dir\n" if $VERBOSE;
290 sub RebaseReferences {
293 opendir(HTMLDIR, $dir) || die "Can't open HTML directory $dir: $!";
294 foreach my $file (readdir(HTMLDIR)) {
295 if ($file =~ m/\.html?$/) {
296 &RebaseFile("$dir$file");
305 print "Fixing file: $file\n" if $VERBOSE;
307 open(HTMLFILE, $file) || die "Can't open $file: $!";
310 my $text = <HTMLFILE>;
313 $text =~ s#(<a(?:\s+\w+=(?:"[^"]*"|'[^']*'))*\s+href=")([^"]*)(")#$1 . &RebaseLink($2) .$3#gse;
315 open(NEWFILE, ">$file.new") || die "Can't open $file: $!";
319 unlink($file) || die "Can't delete $file: $!";
320 rename("$file.new", $file) || die "Can't rename $file.new: $!";
326 my ($dir, $origdir, $file, $package);
328 if ($href =~ m#^(.*/)([^/]*)$#) {
329 $dir = $origdir = $1;
331 if ($RevMap{ $dir }) {
332 $package = $RevMap{ $dir };
334 elsif ($dir =~ m#^\.\./([^/]+)/#) {
337 elsif ($AGGRESSIVE) {
338 $dir =~ m#([^/]+)/$#;
343 if ($ONLINE && $OnlineMap{ $package }) {
344 $dir = $OnlineMap{ $package };
346 elsif ($LocalMap{ $package }) {
347 $dir = $LocalMap{ $package };
349 $href = $dir . $file;
351 if ($dir ne $origdir) {
352 if ($Mapped{ $origdir }) {
353 $Mapped{ $origdir }->[1]++;
356 $Mapped{ $origdir } = [ $dir, 1 ];
364 sub PrintWhatWeHaveDone {
365 my ($origdir, $info);
366 foreach $origdir (sort keys %Mapped) {
367 $info = $Mapped{$origdir};
368 print "$origdir -> ", $info->[0], " (", $info->[1], ")\n";