docs: FAQ and Controlling the results
[gtk-doc.git] / gtkdoc-fixxref.in
blob5a703ad30bfb7f97c98bdbd34095bc61030760b5
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 push @INC, '@PACKAGE_DATA_DIR@';
32 require "gtkdoc-common.pl";
34 # Options
36 # name of documentation module
37 my $MODULE;
38 my $MODULE_DIR;
39 my $HTML_DIR = "";
40 my @EXTRA_DIRS;
41 my $PRINT_VERSION;
42 my $PRINT_HELP;
44 my %optctl = ('module' => \$MODULE,
45               'module-dir' => \$MODULE_DIR,
46               'html-dir' => \$HTML_DIR,
47               'extra-dir' => \@EXTRA_DIRS,
48               'version' => \$PRINT_VERSION,
49               'help' => \$PRINT_HELP);
50 GetOptions(\%optctl, "module=s", "module-dir=s", "html-dir:s", "extra-dir=s@",
51         "version", "help");
53 if ($PRINT_VERSION) {
54     print "@VERSION@\n";
55     exit 0;
58 if ($PRINT_HELP) {
59         print <<EOF;
60 gtkdoc-fixxref version @VERSION@ - fix cross references in html files
62 --module=MODULE_NAME    Name of the doc module being parsed
63 --module-dir=MODULE_DIR The directory which contains the generated HTML
64 --html-dir=HTML_DIR     The directory where gtk-doc generated documentation is
65                         installed
66 --extra-dir=EXTRA_DIR   Directories to recursively scan for indices (index.sgml)
67                         in addition to HTML_DIR
68                         May be used more than once for multiple directories
69 --version               Print the version of this program
70 --help                  Print this help
71 EOF
72     exit 0;
75 # This contains all the entities and their relative URLs.
76 my %Links;
77 # This hold the path entries we already scanned
78 my @VisitedPaths;
80 # failing link targets we don't warn about even once
81 my %NoLinks = (
82     'char'  => 1,
83     'double'  => 1,
84     'float'  => 1,
85     'int'  => 1,
86     'long'  => 1,
87     'main'  => 1,
88     'signed'  => 1,
89     'unsigned'  => 1,
90     'va-list' => 1,
91     'void'  => 1,
92     'GInterface' => 1
93     );
95 my $path_prefix="";
96 if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
97     $path_prefix=$1;
98     #print "Path prefix: $path_prefix\n";
101 if (!defined $MODULE_DIR) {
102   $MODULE_DIR="$HTML_DIR/$MODULE";
105 my $dir;
107 # We scan the directory containing GLib and any directories in GNOME2_PATH
108 # first, but these will be overriden by any later scans.
109 $dir = `pkg-config --variable=prefix glib-2.0`;
110 $dir =~ s/\s+$//;
111 $dir = $dir . "/share/gtk-doc/html";
112 if (-d $dir && $dir ne $HTML_DIR) {
113     #print "Scanning GLib directory: $dir\n";
114     if ($dir !~ m%^\Q$path_prefix\E/%) {
115         &ScanIndices ($dir, 1);
116     } else {
117         &ScanIndices ($dir, 0);
118     }
119     push (@VisitedPaths, $dir);
122 if (defined ($ENV{"GNOME2_PATH"})) {
123     foreach $dir (split (/:/, $ENV{"GNOME2_PATH"})) {
124         $dir = $dir . "/share/gtk-doc/html";
125         if (-d $dir && $dir ne $HTML_DIR) {
126             #print "Scanning GNOME2_PATH directory: $dir\n";
127             if ($dir !~ m%^\Q$path_prefix\E/%) {
128                 &ScanIndices ($dir, 1);
129             } else {
130                 &ScanIndices ($dir, 0);
131             }
132             push (@VisitedPaths, $dir);
133         }
134         # ubuntu started to compress this as index.sgml.gz :/
135         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
136     }
139 #print "Scanning HTML_DIR directory: $HTML_DIR\n";
140 &ScanIndices ($HTML_DIR, 0);
141 push (@VisitedPaths, $HTML_DIR);
142 #print "Scanning HTML_DIR directory: $MODULE_DIR\n";
143 &ScanIndices ($MODULE_DIR, 0);
144 push (@VisitedPaths, $MODULE_DIR);
146 # check all extra dirs, but skip already scanned dirs or subdirs of those
147 foreach my $dir (@EXTRA_DIRS) {
148     my $vdir;
149     my $skip = 0;
151     foreach $vdir (@VisitedPaths) {
152         if ($dir eq $vdir || $dir =~ m%^\Q$vdir\E/%) {
153             #print "Skipping EXTRA_DIR directory: $dir\n";
154             $skip=1;
155         }
156     }
157     next if $skip;
158     #print "Scanning EXTRA_DIR directory: $dir\n";
159     push (@VisitedPaths, $dir);
161     # If the --extra-dir option is not relative and is not sharing the same
162     # prefix as the target directory of the docs, we need to use absolute
163     # directories for the links
164     if ($dir !~m/^\.\./ &&  $dir !~ m%\Q$path_prefix\E/%) {
165         &ScanIndices ($dir, 1);
166     } else {
167         &ScanIndices ($dir, 0);
168     }
171 if (defined($MODULE)) {
172     open (INPUT, "$MODULE-sections.txt")
173             || die "Can't open $MODULE-sections.txt: $!";
174     my $subsection = "";
175     while (<INPUT>) {
176         if (m/^#/) {
177             next;
179         } elsif (m/^<SECTION>/) {
180             $subsection = "";
181         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
182             $subsection = $1;
183         } elsif (m/^<SUBSECTION>/) {
184             next;
185         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
186             next;
187         } elsif (m/^<FILE>(.*)<\/FILE>/) {
188             next;
189         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
190             next;
191         } elsif (m/^<\/SECTION>/) {
192             next;
193         } elsif (m/^(\S+)/) {
194             my $symbol=CreateValidSGMLID($1);
196             if ($subsection eq "Standard" || $subsection eq "Private") {
197                 $NoLinks{$symbol} = 1;
198             }
199         }
200     }
201     close (INPUT);
204 &FixCrossReferences ($MODULE_DIR);
206 sub ScanIndices {
207     my ($scan_dir, $use_absolute_links) = @_;
209     #print "Scanning source directory: $scan_dir absolute: $use_absolute_links\n";
211     # This array holds any subdirectories found.
212     my (@subdirs) = ();
214     opendir (HTMLDIR, $scan_dir) || return;
215     my $file;
216     foreach $file (readdir (HTMLDIR)) {
217         if ($file eq '.' || $file eq '..') {
218             next;
219         } elsif (-d "$scan_dir/$file") {
220             push (@subdirs, $file);
221         } elsif ($file eq "index.sgml") {
222             &ScanIndex ("$scan_dir/$file", $use_absolute_links);
223         }
224         # ubuntu started to compress this as index.sgml.gz :/
225         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
226     }
227     closedir (HTMLDIR);
229     # Now recursively scan the subdirectories.
230     my $dir;
231     foreach $dir (@subdirs) {
232         &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
233     }
236 sub ScanIndex {
237     my ($file, $use_absolute_links) = @_;
238     #print "Scanning index file: $file absolute: $use_absolute_links\n";
240     # Determine the absolute directory, to be added to links in index.sgml
241     # if we need to use an absolute link.
242     # $file will be something like /opt/gnome/share/gtk-doc/html/gtk/index.sgml
243     # We want the part up to 'html' since the links in index.sgml include
244     # the rest.
245     my $dir = "../";
246     if ($use_absolute_links) {
247         $file =~ /(.*\/)(.*?)\/index\.sgml/;
248         $dir = $1;
249     }
251     open (INDEXFILE, $file)
252         || die "Can't open $file: $!";
253     while (<INDEXFILE>) {
254         if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) {
255             #print "Found id: $1 href: $2\n";
256             $Links{$1} = "$dir$2";
257         }
258     }
259     close (INDEXFILE);
263 sub FixCrossReferences {
264     my ($scan_dir) = @_;
266     opendir (HTMLDIR, $scan_dir)
267         || die "Can't open HTML directory $scan_dir: $!";
268     my $file;
269     foreach $file (readdir (HTMLDIR)) {
270         if ($file eq '.' || $file eq '..') {
271             next;
272         } elsif ($file =~ m/.html?$/) {
273             &FixHTMLFile ("$scan_dir/$file");
274         }
275     }
276     closedir (HTMLDIR);
280 sub FixHTMLFile {
281     my ($file) = @_;
282     #print "Fixing file: $file\n";
284     open (HTMLFILE, $file)
285         || die "Can't open $file: $!";
286     undef $/;
287     my $entire_file = <HTMLFILE>;
288     close (HTMLFILE);
289     
290     if ("@HIGHLIGHT@" ne "") {
291         if ("@HIGHLIGHT@" =~ m%/vim$%) {
292             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSourceVim($1,$2);%gse;
293         }
294         else {
295             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
296         }
297         # this just broke existing GTKDOCLINK tags
298         # &lt;GTKDOCLINK HREF=&quot;GST-PAD-SINK:CAPS&quot;&gt;GST_PAD_SINK&lt;/GTKDOCLINK&gt;
299         $entire_file =~ s%\&lt;GTKDOCLINK\s+HREF=\&quot;(.*?)\&quot;\&gt;(.*?)\&lt;/GTKDOCLINK\&gt;%\<GTKDOCLINK\ HREF=\"$1\"\>$2\</GTKDOCLINK\>%gs;
300         
301         # from the highlighter we get all the functions marked up
302         # now we could turn them into GTKDOCLINK items
303         $entire_file =~ s%(<span class=\"function\">)(.*?)(</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
304         # we could also try the first item in stuff marked up as 'normal'
305         $entire_file =~ s%(<span class=\"normal\">\s*)(.+?)((\s+.+?)?\s*</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
306     }
308     my @lines = split(/\n/, $entire_file);
309     for (my $i=0; $i<$#lines; $i++) {
310         $lines[$i] =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($file,$i+1,$1,$2); %ge;
311         #if ($lines[$i] =~ m/GTKDOCLINK/) {
312         #    print "make xref failed for line: ",$lines[$i], "\n";
313         #}
314     }
315     $entire_file = join("\n",@lines);
317     open (NEWFILE, ">$file.new")
318         || die "Can't open $file: $!";
319     print NEWFILE $entire_file;
320     close (NEWFILE);
322     unlink ($file)
323         || die "Can't delete $file: $!";
324     rename ("$file.new", $file)
325         || die "Can't rename $file.new: $!";
328 sub MakeXRef {
329     my ($file, $line, $id, $text) = @_;
331     my $href = $Links{$id};
332     
333     # this is a workaround for some inconsitency we have with CreateValidSGMLID
334     if (!$href && $id =~ m/:/) {
335         my $tid = $id;
336         $tid =~ s/:/--/g;
337         $href = $Links{$tid};
338     }
339     # poor mans plural support
340     if (!$href && $id =~ m/s$/) {
341         my $tid = $id;
342         $tid =~ s/s$//g;
343         $href = $Links{$tid};
344     }
346     if ($href) {
347         # if it is a link to same module, remove path to make it work
348         # uninstalled
349         if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
350             $href=$1;
351         }
352         #print "  Fixing link: $id, $href, $text\n";
353         return "<a href=\"$href\">$text</a>";
354     } else {
355         my $warn = 1;
356         #print "  no link for: $id, $text\n";
358         # don't warn multiple times and also skip blacklisted (ctypes)
359         $warn = 0 if exists $NoLinks{$id};
360         # if it's a function, don't warn if it does not contain a "_"
361         # (transformed to "-")
362         # - gnome coding style would use '_'
363         # - will avoid wrong warnings for ansi c functions
364         $warn = 0 if ($text =~ m/ class=\"function\"/ && $id !~ m/-/);
365         # if it's a 'return value', don't warn (implicitly created link)
366         $warn = 0 if ($text =~ m/ class=\"returnvalue\"/);
367         # if it's a 'type', don't warn if it starts with lowercase
368         # - gnome coding style would use CamelCase
369         $warn = 0 if ($text =~ m/ class=\"type\"/ && ($id =~ m/^[a-z]/));
371         if ($warn == 1) {
372           &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
373           $NoLinks{$id} = 1;
374         }
375         return $text;
376     }
380 sub MakeGtkDocLink {
381     my ($pre,$symbol,$post) = @_;
383     my $id=CreateValidSGMLID($symbol);
385     # these are implicitely created links in highlighed sources
386     # we don't want warnings for those if the links cannot be resolved.
387     $NoLinks{$id} = 1;
389     #return "<span class=\"$type\"><GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK></span>";
390     return "$pre<GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK>$post";
394 sub HighlightSource {
395     my ($type, $source) = @_;
397     # chop of leading and trailing empty lines
398     $source =~ s/^\s*\n+//gs;
399     $source =~ s/[\s\n]+$//gs;
400     # cut common indent
401     $source =~ m/^(\s*)/;
402     $source =~ s/^$1//gms;
403     # avoid double entity replacement
404     $source =~ s/&lt;/</g;
405     $source =~ s/&gt;/>/g;
406     $source =~ s/&amp;/&/g;
408     # write source to a temp file
409     # FIXME: use .c for now to hint the language to the highlighter
410     my $temp_source_file="$MODULE_DIR/_temp_src.$$.c";
411     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
412     print NEWFILE $source;
413     close (NEWFILE);
414     
415     #print" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file \n";
416     
417     # format source
418     my $highlighted_source=`@HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file`;
419     if ("@HIGHLIGHT@" =~ m%/source-highlight$%) {
420         $highlighted_source =~ s%^<\!-- .*? -->%%gs;
421         $highlighted_source =~ s%<pre><tt>(.*?)</tt></pre>%$1%gs;
422     }
423     elsif ("@HIGHLIGHT@" =~ m%/highlight$%) {
424         # need to rewrite the stylesheet classes
425         $highlighted_source =~ s%<span class="gtkdoc com">%<span class="comment">%gs;
426         $highlighted_source =~ s%<span class="gtkdoc dir">%<span class="preproc">%gs;
427         $highlighted_source =~ s%<span class="gtkdoc kwd">%<span class="function">%gs;
428         $highlighted_source =~ s%<span class="gtkdoc kwa">%<span class="keyword">%gs;
429         $highlighted_source =~ s%<span class="gtkdoc line">%<span class="linenum">%gs;
430         $highlighted_source =~ s%<span class="gtkdoc num">%<span class="number">%gs;
431         $highlighted_source =~ s%<span class="gtkdoc str">%<span class="string">%gs;
432         $highlighted_source =~ s%<span class="gtkdoc sym">%<span class="symbol">%gs;
433         # maybe also do
434         # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
435     }
436     # remove temp file
437     unlink ($temp_source_file)
438         || die "Can't delete $temp_source_file: $!";
440     return &HighlightSourcePostprocess($type, $highlighted_source);
443 sub HighlightSourceVim {
444     my ($type, $source) = @_;
446     # chop of leading and trailing empty lines
447     $source =~ s/^[\s\n]+//gs;
448     $source =~ s/[\s\n]+$//gs;
449     # avoid double entity replacement
450     $source =~ s/&lt;/</g;
451     $source =~ s/&gt;/>/g;
452     $source =~ s/&amp;/&/g;
454     # write source to a temp file
455     my $temp_source_file="$MODULE_DIR/_temp_src.$$.h";
456     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
457     print NEWFILE $source;
458     close (NEWFILE);
460     # format source
461     system "echo 'let html_number_lines=0|let html_use_css=1|let use_xhtml=1|syn on|e $temp_source_file|run! syntax/2html.vim|wa!|qa!' | @HIGHLIGHT@ -n -e -u /dev/null -T xterm >/dev/null";
463     my $highlighted_source;
464     {
465         local $/;
466         open (NEWFILE, "<$temp_source_file.html");
467         $highlighted_source = <NEWFILE>;
468         close (NEWFILE);
469     }
470     $highlighted_source =~ s#.*<pre>\n##s;
471     $highlighted_source =~ s#</pre>.*##s;
473     # need to rewrite the stylesheet classes
474     # FIXME: Vim has somewhat different syntax groups
475     $highlighted_source =~ s%<span class="Comment">%<span class="comment">%gs;
476     $highlighted_source =~ s%<span class="PreProc">%<span class="preproc">%gs;
477     $highlighted_source =~ s%<span class="Statement">%<span class="keyword">%gs;
478     $highlighted_source =~ s%<span class="Identifier">%<span class="function">%gs;
479     $highlighted_source =~ s%<span class="Constant">%<span class="number">%gs;
480     $highlighted_source =~ s%<span class="Special">%<span class="symbol">%gs;
481     $highlighted_source =~ s%<span class="Type">%<span class="type">%gs;
483     # remove temp files
484     unlink ($temp_source_file)
485         || die "Can't delete $temp_source_file: $!";
486     unlink ("$temp_source_file.html")
487         || die "Can't delete $temp_source_file.html: $!";
489     return &HighlightSourcePostprocess($type, $highlighted_source);
492 sub HighlightSourcePostprocess {
493     my ($type, $highlighted_source) = @_;
495     # chop of leading and trailing empty lines
496     $highlighted_source =~ s/^[\s\n]+//gs;
497     $highlighted_source =~ s/[\s\n]+$//gs;
499     # turn common urls in comments into links
500     $highlighted_source =~ s%<span class="url">(.*?)</span>%<span class="url"><a href="$1">$1</a></span>%gs;
502     # we do own line-numbering
503     my $source_lines="";
504     my $line_count = () = $highlighted_source =~ /\n/gs;
505     for (my $i=1; $i < ($line_count+2); $i++) {
506         $source_lines.="$i\n";
507     }
508     $source_lines =~ s/\n\Z//;
510     return <<END_OF_HTML
511 <div class="$type">
512   <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
513     <tbody>
514       <tr>
515         <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
516         <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>
517       </tr>
518     </tbody>
519   </table>
520 </div>
521 END_OF_HTML