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 #############################################################################
33 # name of documentation module
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@",
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";
67 # This contains all the entities and their relative URLs.
69 # This hold the path entries we already scanned
74 if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
76 # print "Path prefix: $path_prefix\n";
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`;
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);
91 &ScanIndices ($dir, 0);
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);
104 &ScanIndices ($dir, 0);
106 push (@VisitedPaths, $dir);
108 # ubuntu started to compress this as index.sgml.gz :/
109 # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
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 or subdirs of those
118 foreach my $dir (@EXTRA_DIRS) {
122 foreach $vdir (@VisitedPaths) {
123 if ($dir eq $vdir || $dir =~ m%^\Q$vdir\E/%) {
124 # print "Skipping EXTRA_DIR directory: $dir\n";
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);
138 &ScanIndices ($dir, 0);
142 &FixCrossReferences (defined $MODULE_DIR ? $MODULE_DIR : "$HTML_DIR/$MODULE");
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.
152 opendir (HTMLDIR, $scan_dir) || return;
154 foreach $file (readdir (HTMLDIR)) {
155 if ($file eq '.' || $file eq '..') {
157 } elsif (-d "$scan_dir/$file") {
158 push (@subdirs, $file);
159 } elsif ($file eq "index.sgml") {
160 &ScanIndex ("$scan_dir/$file", $use_absolute_links);
162 # ubuntu started to compress this as index.sgml.gz :/
163 # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
167 # Now recursively scan the subdirectories.
169 foreach $dir (@subdirs) {
170 &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
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
184 if ($use_absolute_links) {
185 $file =~ /(.*\/)(.*?)\/index\.sgml/;
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";
201 sub FixCrossReferences {
204 opendir (HTMLDIR, $scan_dir)
205 || die "Can't open HTML directory $scan_dir: $!";
207 foreach $file (readdir (HTMLDIR)) {
208 if ($file eq '.' || $file eq '..') {
210 } elsif ($file =~ m/.html?$/) {
211 &FixHTMLFile ("$scan_dir/$file");
220 #print "Fixing file: $file\n";
222 open (HTMLFILE, $file)
223 || die "Can't open $file: $!";
225 my $entire_file = <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;
236 || die "Can't delete $file: $!";
237 rename ("$file.new", $file)
238 || die "Can't rename $file.new: $!";
243 my ($id, $text) = @_;
245 my $href = $Links{$id};
248 # print " Fixing link: $id, $href, $text\n";
249 return "<a\nhref=\"$href\"\n>$text</a>";
251 # print " no link for: $id, $text\n";