Current release notes.
[gtk-doc.git] / gtkdoc-fixxref.in
blobf428d487c45b171dd00e1069093fe350588c91bb
1 #!@PERL@ -w
2 # -*- cperl -*-
4 # gtk-doc - GTK DocBook documentation generator.
5 # Copyright (C) 1998  Damon Chaplin
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.
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.
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.
22 #############################################################################
23 # Script      : gtkdoc-fixxref
24 # Description : This fixes cross-references in the HTML documentation.
25 #############################################################################
27 use strict;
28 use bytes;
29 use Getopt::Long;
31 # Options
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;
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");
50 if ($PRINT_VERSION) {
51     print "@VERSION@\n";
52     exit 0;
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 recursively scan for indices (index.sgml) 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;
67 # This contains all the entities and their relative URLs.
68 my %Links;
69 # This hold the path entries we already scanned
70 my @VisitedPaths;
73 my $path_prefix="";
74 if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
75     $path_prefix=$1;
76 #    print "Path prefix: $path_prefix\n";
79 my $dir;
81 # We scan the directory containing GLib and any directories in GNOME2_PATH
82 # first, but these will be overriden by any later scans.
83 $dir = `pkg-config --variable=prefix glib-2.0`;
84 $dir =~ s/\s+$//;
85 $dir = $dir . "/share/gtk-doc/html";
86 if (-d $dir && $dir ne $HTML_DIR) {
87 #    print "Scanning GLib directory: $dir\n";
88     if ($dir !~ m%^\Q$path_prefix\E/%) {
89         &ScanIndices ($dir, 1);
90     } else {
91         &ScanIndices ($dir, 0);
92     }
93     push (@VisitedPaths, $dir);
96 if (defined ($ENV{"GNOME2_PATH"})) {
97     foreach $dir (split (/:/, $ENV{"GNOME2_PATH"})) {
98         $dir = $dir . "/share/gtk-doc/html";
99         if (-d $dir && $dir ne $HTML_DIR) {
100 #           print "Scanning GNOME2_PATH directory: $dir\n";
101             if ($dir !~ m%^\Q$path_prefix\E/%) {
102                 &ScanIndices ($dir, 1);
103             } else {
104                 &ScanIndices ($dir, 0);
105             }
106             push (@VisitedPaths, $dir);
107         }
108         # ubuntu started to compress this as index.sgml.gz :/
109         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
110     }
113 #print "Scanning HTML_DIR directory: $HTML_DIR\n";
114 &ScanIndices ($HTML_DIR, 0);
115 push (@VisitedPaths, $HTML_DIR);
117 # check all extra dirs, but skip already scanned dirs ord subdirs of those
118 foreach my $dir (@EXTRA_DIRS) {
119     my $vdir;
120     my $skip = 0;
122     foreach $vdir (@VisitedPaths) {
123         if ($dir eq $vdir || $dir =~ m%^\Q$vdir\E/%) {
124 #            print "Skipping EXTRA_DIR directory: $dir\n";
125             $skip=1;
126         }
127     }
128     next if $skip;
129 #    print "Scanning EXTRA_DIR directory: $dir\n";
130     push (@VisitedPaths, $dir);
132     # If the --extra-dir option is not relative and is not sharing the same
133     # prefix as the target directory of the docs, we need to use absolute
134     # directories for the links
135     if ($dir !~m/^\.\./ &&  $dir !~ m%\Q$path_prefix\E/%) {
136         &ScanIndices ($dir, 1);
137     } else {
138         &ScanIndices ($dir, 0);
139     }
142 &FixCrossReferences (defined $MODULE_DIR ? $MODULE_DIR : "$HTML_DIR/$MODULE");
144 sub ScanIndices {
145     my ($scan_dir, $use_absolute_links) = @_;
147 #    print "Scanning source directory: $scan_dir absolute: $use_absolute_links\n";
149     # This array holds any subdirectories found.
150     my (@subdirs) = ();
152     opendir (HTMLDIR, $scan_dir) || return;
153     my $file;
154     foreach $file (readdir (HTMLDIR)) {
155         if ($file eq '.' || $file eq '..') {
156             next;
157         } elsif (-d "$scan_dir/$file") {
158             push (@subdirs, $file);
159         } elsif ($file eq "index.sgml") {
160             &ScanIndex ("$scan_dir/$file", $use_absolute_links);
161         }
162         # ubuntu started to compress this as index.sgml.gz :/
163         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
164     }
165     closedir (HTMLDIR);
167     # Now recursively scan the subdirectories.
168     my $dir;
169     foreach $dir (@subdirs) {
170         &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
171     }
174 sub ScanIndex {
175     my ($file, $use_absolute_links) = @_;
176 #    print "Scanning index file: $file absolute: $use_absolute_links\n";
178     # Determine the absolute directory, to be added to links in index.sgml
179     # if we need to use an absolute link.
180     # $file will be something like /opt/gnome/share/gtk-doc/html/gtk/index.sgml
181     # We want the part up to 'html' since the links in index.sgml include
182     # the rest.
183     my $dir = "../";
184     if ($use_absolute_links) {
185         $file =~ /(.*\/)(.*?)\/index\.sgml/;
186         $dir = $1;
187     }
189     open (INDEXFILE, $file)
190         || die "Can't open $file: $!";
191     while (<INDEXFILE>) {
192         if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) {
193 #           print "Found id: $1 href: $2\n";
194             $Links{$1} = "$dir$2";
195         }
196     }
197     close (INDEXFILE);
201 sub FixCrossReferences {
202     my ($scan_dir) = @_;
204     opendir (HTMLDIR, $scan_dir)
205         || die "Can't open HTML directory $scan_dir: $!";
206     my $file;
207     foreach $file (readdir (HTMLDIR)) {
208         if ($file eq '.' || $file eq '..') {
209             next;
210         } elsif ($file =~ m/.html?$/) {
211             &FixHTMLFile ("$scan_dir/$file");
212         }
213     }
214     closedir (HTMLDIR);
218 sub FixHTMLFile {
219     my ($file) = @_;
220 #    print "Fixing file: $file\n";
222     open (HTMLFILE, $file)
223         || die "Can't open $file: $!";
224     undef $/;
225     my $entire_file = <HTMLFILE>;
226     close (HTMLFILE);
228     $entire_file =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($1, $2); %gse;
230     open (NEWFILE, ">$file.new")
231         || die "Can't open $file: $!";
232     print NEWFILE $entire_file;
233     close (NEWFILE);
235     unlink ($file)
236         || die "Can't delete $file: $!";
237     rename ("$file.new", $file)
238         || die "Can't rename $file.new: $!";
242 sub MakeXRef {
243     my ($id, $text) = @_;
245     my $href = $Links{$id};
247     if ($href) {
248 #        print "  Fixing link: $id, $href, $text\n";
249         return "<a\nhref=\"$href\"\n>$text</a>";
250     } else {
251         return $text;
252     }