Fixed another typo in string...
[gtk-doc.git] / gtkdoc-fixxref.in
blob3bb47e2ad8232be5dd43a26aed993f3e3f758e58
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     'GBoxed' => 1,
93     'GEnum' => 1,
94     'GFlags' => 1,
95     'GInterface' => 1
96     );
98 my $path_prefix="";
99 if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
100     $path_prefix=$1;
101     @TRACE@("Path prefix: $path_prefix");
104 if (!defined $MODULE_DIR) {
105   $MODULE_DIR="$HTML_DIR/$MODULE";
108 my $dir;
110 # We scan the directory containing GLib and any directories in GNOME2_PATH
111 # first, but these will be overriden by any later scans.
112 $dir = `pkg-config --variable=prefix glib-2.0`;
113 $dir =~ s/\s+$//;
114 $dir = $dir . "/share/gtk-doc/html";
115 if (-d $dir && $dir ne $HTML_DIR) {
116     @TRACE@("Scanning GLib directory: $dir");
117     if ($dir !~ m%^\Q$path_prefix\E/%) {
118         &ScanIndices ($dir, 1);
119     } else {
120         &ScanIndices ($dir, 0);
121     }
122     push (@VisitedPaths, $dir);
125 if (defined ($ENV{"GNOME2_PATH"})) {
126     foreach $dir (split (/:/, $ENV{"GNOME2_PATH"})) {
127         $dir = $dir . "/share/gtk-doc/html";
128         if (-d $dir && $dir ne $HTML_DIR) {
129             @TRACE@("Scanning GNOME2_PATH directory: $dir");
130             if ($dir !~ m%^\Q$path_prefix\E/%) {
131                 &ScanIndices ($dir, 1);
132             } else {
133                 &ScanIndices ($dir, 0);
134             }
135             push (@VisitedPaths, $dir);
136         }
137         # ubuntu started to compress this as index.sgml.gz :/
138         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
139     }
142 @TRACE@("Scanning HTML_DIR directory: $HTML_DIR");
143 &ScanIndices ($HTML_DIR, 0);
144 push (@VisitedPaths, $HTML_DIR);
145 @TRACE@("Scanning HTML_DIR directory: $MODULE_DIR");
146 &ScanIndices ($MODULE_DIR, 0);
147 push (@VisitedPaths, $MODULE_DIR);
149 # check all extra dirs, but skip already scanned dirs or subdirs of those
150 foreach my $dir (@EXTRA_DIRS) {
151     my $vdir;
152     my $skip = 0;
154     foreach $vdir (@VisitedPaths) {
155         if ($dir eq $vdir || $dir =~ m%^\Q$vdir\E/%) {
156             @TRACE@("Skipping EXTRA_DIR directory: $dir");
157             $skip=1;
158         }
159     }
160     next if $skip;
161     @TRACE@("Scanning EXTRA_DIR directory: $dir");
162     push (@VisitedPaths, $dir);
164     # If the --extra-dir option is not relative and is not sharing the same
165     # prefix as the target directory of the docs, we need to use absolute
166     # directories for the links
167     if ($dir !~m/^\.\./ &&  $dir !~ m%\Q$path_prefix\E/%) {
168         &ScanIndices ($dir, 1);
169     } else {
170         &ScanIndices ($dir, 0);
171     }
174 if (defined($MODULE)) {
175     open (INPUT, "$MODULE-sections.txt")
176             || die "Can't open $MODULE-sections.txt: $!";
177     my $subsection = "";
178     while (<INPUT>) {
179         if (m/^#/) {
180             next;
182         } elsif (m/^<SECTION>/) {
183             $subsection = "";
184         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
185             $subsection = $1;
186         } elsif (m/^<SUBSECTION>/) {
187             next;
188         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
189             next;
190         } elsif (m/^<FILE>(.*)<\/FILE>/) {
191             next;
192         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
193             next;
194         } elsif (m/^<\/SECTION>/) {
195             next;
196         } elsif (m/^(\S+)/) {
197             my $symbol=CreateValidSGMLID($1);
199             if ($subsection eq "Standard" || $subsection eq "Private") {
200                 $NoLinks{$symbol} = 1;
201             }
202         }
203     }
204     close (INPUT);
207 &FixCrossReferences ($MODULE_DIR);
209 sub ScanIndices {
210     my ($scan_dir, $use_absolute_links) = @_;
212     @TRACE@("Scanning source directory: $scan_dir absolute: $use_absolute_links");
214     # This array holds any subdirectories found.
215     my (@subdirs) = ();
217     opendir (HTMLDIR, $scan_dir) || return;
218     my $file;
219     foreach $file (readdir (HTMLDIR)) {
220         if ($file eq '.' || $file eq '..') {
221             next;
222         } elsif (-d "$scan_dir/$file") {
223             push (@subdirs, $file);
224         } elsif ($file eq "index.sgml") {
225             &ScanIndex ("$scan_dir/$file", $use_absolute_links);
226         }
227         # ubuntu started to compress this as index.sgml.gz :/
228         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
229     }
230     closedir (HTMLDIR);
232     # Now recursively scan the subdirectories.
233     my $dir;
234     foreach $dir (@subdirs) {
235         &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
236     }
239 sub ScanIndex {
240     my ($file, $use_absolute_links) = @_;
242     # Determine the absolute directory, to be added to links in index.sgml
243     # if we need to use an absolute link.
244     # $file will be something like /opt/gnome/share/gtk-doc/html/gtk/index.sgml
245     # We want the part up to 'html' since the links in index.sgml include
246     # the rest.
247     my $dir = "../";
248     if ($use_absolute_links) {
249         # For uninstalled index.sgml files we'd need to map the path to where it
250         # will be installed to
251         if ($file !~ /\.\//) {
252           $file =~ /(.*\/)(.*?)\/index\.sgml/;
253           $dir = $1;
254         }
255     }
256     @TRACE@("Scanning index file=$file, absolute=$use_absolute_links, dir=$dir");
258     open (INDEXFILE, $file)
259         || die "Can't open $file: $!";
260     while (<INDEXFILE>) {
261         if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) {
262             @TRACE@("Found id: $1 href: $2");
263             $Links{$1} = "$dir$2";
264         }
265     }
266     close (INDEXFILE);
270 sub FixCrossReferences {
271     my ($scan_dir) = @_;
273     opendir (HTMLDIR, $scan_dir)
274         || die "Can't open HTML directory $scan_dir: $!";
275     my $file;
276     foreach $file (readdir (HTMLDIR)) {
277         if ($file eq '.' || $file eq '..') {
278             next;
279         } elsif ($file =~ m/.html?$/) {
280             &FixHTMLFile ("$scan_dir/$file");
281         }
282     }
283     closedir (HTMLDIR);
287 sub FixHTMLFile {
288     my ($file) = @_;
289     @TRACE@("Fixing file: $file");
291     open (HTMLFILE, $file)
292         || die "Can't open $file: $!";
293     undef $/;
294     my $entire_file = <HTMLFILE>;
295     close (HTMLFILE);
297     if ("@HIGHLIGHT@" ne "") {
298         # FIXME: ideally we'd pass a clue about the example language to the highligher
299         # unfortunately the "language" attribute is not appearing in the html output
300         # we could patch the customization to have <code class="xxx"> inside of <pre>
301         if ("@HIGHLIGHT@" =~ m%/vim$%) {
302             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSourceVim($1,$2);%gse;
303         }
304         else {
305             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
306         }
307         # this just broke existing GTKDOCLINK tags
308         # &lt;GTKDOCLINK HREF=&quot;GST-PAD-SINK:CAPS&quot;&gt;GST_PAD_SINK&lt;/GTKDOCLINK&gt;
309         $entire_file =~ s%\&lt;GTKDOCLINK\s+HREF=\&quot;(.*?)\&quot;\&gt;(.*?)\&lt;/GTKDOCLINK\&gt;%\<GTKDOCLINK\ HREF=\"$1\"\>$2\</GTKDOCLINK\>%gs;
311         # from the highlighter we get all the functions marked up
312         # now we could turn them into GTKDOCLINK items
313         $entire_file =~ s%(<span class=\"function\">)(.*?)(</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
314         # we could also try the first item in stuff marked up as 'normal'
315         $entire_file =~ s%(<span class=\"normal\">\s*)(.+?)((\s+.+?)?\s*</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
316     }
318     my @lines = split(/\n/, $entire_file);
319     for (my $i=0; $i<$#lines; $i++) {
320         $lines[$i] =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($file,$i+1,$1,$2); %ge;
321         #if ($lines[$i] =~ m/GTKDOCLINK/) {
322         #    print "make xref failed for line: ",$lines[$i], "\n";
323         #}
324     }
325     $entire_file = join("\n",@lines);
327     open (NEWFILE, ">$file.new")
328         || die "Can't open $file: $!";
329     print NEWFILE $entire_file;
330     close (NEWFILE);
332     unlink ($file)
333         || die "Can't delete $file: $!";
334     rename ("$file.new", $file)
335         || die "Can't rename $file.new: $!";
338 sub MakeXRef {
339     my ($file, $line, $id, $text) = @_;
341     my $href = $Links{$id};
343     # this is a workaround for some inconsistency we have with CreateValidSGMLID
344     if (!$href && $id =~ m/:/) {
345         my $tid = $id;
346         $tid =~ s/:/--/g;
347         $href = $Links{$tid};
348     }
349     # poor mans plural support
350     if (!$href && $id =~ m/s$/) {
351         my $tid = $id;
352         $tid =~ s/s$//g;
353         $href = $Links{$tid};
354     }
356     if ($href) {
357         # if it is a link to same module, remove path to make it work
358         # uninstalled
359         if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
360             $href=$1;
361             @TRACE@("  Fixing link to uninstalled doc: $id, $href, $text");
362         } else {
363             @TRACE@("  Fixing link: $id, $href, $text");
364         }
365         return "<a href=\"$href\">$text</a>";
366     } else {
367         my $warn = 1;
368         @TRACE@("  no link for: $id, $text");
370         # don't warn multiple times and also skip blacklisted (ctypes)
371         $warn = 0 if exists $NoLinks{$id};
372         # if it's a function, don't warn if it does not contain a "_"
373         # (transformed to "-")
374         # - gnome coding style would use '_'
375         # - will avoid wrong warnings for ansi c functions
376         $warn = 0 if ($text =~ m/ class=\"function\"/ && $id !~ m/-/);
377         # if it's a 'return value', don't warn (implicitly created link)
378         $warn = 0 if ($text =~ m/ class=\"returnvalue\"/);
379         # if it's a 'type', don't warn if it starts with lowercase
380         # - gnome coding style would use CamelCase
381         $warn = 0 if ($text =~ m/ class=\"type\"/ && ($id =~ m/^[a-z]/));
382         # don't warn for self links
383         $warn = 0 if ($text eq $id);
385         if ($warn == 1) {
386           &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
387           $NoLinks{$id} = 1;
388         }
389         return $text;
390     }
394 sub MakeGtkDocLink {
395     my ($pre,$symbol,$post) = @_;
397     my $id=CreateValidSGMLID($symbol);
399     # these are implicitely created links in highlighed sources
400     # we don't want warnings for those if the links cannot be resolved.
401     $NoLinks{$id} = 1;
403     #return "<span class=\"$type\"><GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK></span>";
404     return "$pre<GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK>$post";
408 sub HighlightSource {
409     my ($type, $source) = @_;
411     # chop of leading and trailing empty lines
412     $source =~ s/^\s*\n+//gs;
413     $source =~ s/[\s\n]+$//gs;
414     # cut common indent
415     $source =~ m/^(\s*)/;
416     $source =~ s/^$1//gms;
417     # avoid double entity replacement
418     $source =~ s/&lt;/</g;
419     $source =~ s/&gt;/>/g;
420     $source =~ s/&amp;/&/g;
422     # write source to a temp file
423     # FIXME: use .c for now to hint the language to the highlighter
424     my $temp_source_file="$MODULE_DIR/_temp_src.$$.c";
425     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
426     print NEWFILE $source;
427     close (NEWFILE);
429     @TRACE@(" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file ");
431     # format source
432     my $highlighted_source=`@HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file`;
433     if ("@HIGHLIGHT@" =~ m%/source-highlight$%) {
434         $highlighted_source =~ s%^<\!-- .*? -->%%gs;
435         $highlighted_source =~ s%<pre><tt>(.*?)</tt></pre>%$1%gs;
436     }
437     elsif ("@HIGHLIGHT@" =~ m%/highlight$%) {
438         # need to rewrite the stylesheet classes
439         $highlighted_source =~ s%<span class="gtkdoc com">%<span class="comment">%gs;
440         $highlighted_source =~ s%<span class="gtkdoc dir">%<span class="preproc">%gs;
441         $highlighted_source =~ s%<span class="gtkdoc kwd">%<span class="function">%gs;
442         $highlighted_source =~ s%<span class="gtkdoc kwa">%<span class="keyword">%gs;
443         $highlighted_source =~ s%<span class="gtkdoc line">%<span class="linenum">%gs;
444         $highlighted_source =~ s%<span class="gtkdoc num">%<span class="number">%gs;
445         $highlighted_source =~ s%<span class="gtkdoc str">%<span class="string">%gs;
446         $highlighted_source =~ s%<span class="gtkdoc sym">%<span class="symbol">%gs;
447         # maybe also do
448         # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
449     }
450     # remove temp file
451     unlink ($temp_source_file)
452         || die "Can't delete $temp_source_file: $!";
454     return &HighlightSourcePostprocess($type, $highlighted_source);
457 sub HighlightSourceVim {
458     my ($type, $source) = @_;
460     # chop of leading and trailing empty lines
461     $source =~ s/^[\s\n]+//gs;
462     $source =~ s/[\s\n]+$//gs;
463     # avoid double entity replacement
464     $source =~ s/&lt;/</g;
465     $source =~ s/&gt;/>/g;
466     $source =~ s/&amp;/&/g;
468     # write source to a temp file
469     my $temp_source_file="$MODULE_DIR/_temp_src.$$.h";
470     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
471     print NEWFILE $source;
472     close (NEWFILE);
474     # format source
475     system "echo 'let html_number_lines=0|let html_use_css=1|let html_use_xhtml=1|syn on|e $temp_source_file|run! syntax/2html.vim|w! $temp_source_file.html|qa!' | @HIGHLIGHT@ -n -e -u NONE -T xterm >/dev/null";
477     my $highlighted_source;
478     {
479         local $/;
480         open (NEWFILE, "<$temp_source_file.html");
481         $highlighted_source = <NEWFILE>;
482         close (NEWFILE);
483     }
484     $highlighted_source =~ s#.*<pre\b[^>]*>\n##s;
485     $highlighted_source =~ s#</pre>.*##s;
487     # need to rewrite the stylesheet classes
488     # FIXME: Vim has somewhat different syntax groups
489     $highlighted_source =~ s%<span class="Comment">%<span class="comment">%gs;
490     $highlighted_source =~ s%<span class="PreProc">%<span class="preproc">%gs;
491     $highlighted_source =~ s%<span class="Statement">%<span class="keyword">%gs;
492     $highlighted_source =~ s%<span class="Identifier">%<span class="function">%gs;
493     $highlighted_source =~ s%<span class="Constant">%<span class="number">%gs;
494     $highlighted_source =~ s%<span class="Special">%<span class="symbol">%gs;
495     $highlighted_source =~ s%<span class="Type">%<span class="type">%gs;
497     # remove temp files
498     unlink ($temp_source_file)
499         || die "Can't delete $temp_source_file: $!";
500     unlink ("$temp_source_file.html")
501         || die "Can't delete $temp_source_file.html: $!";
503     return &HighlightSourcePostprocess($type, $highlighted_source);
506 sub HighlightSourcePostprocess {
507     my ($type, $highlighted_source) = @_;
509     # chop of leading and trailing empty lines
510     $highlighted_source =~ s/^[\s\n]+//gs;
511     $highlighted_source =~ s/[\s\n]+$//gs;
513     # turn common urls in comments into links
514     $highlighted_source =~ s%<span class="url">(.*?)</span>%<span class="url"><a href="$1">$1</a></span>%gs;
516     # we do own line-numbering
517     my $source_lines="";
518     my $line_count = () = $highlighted_source =~ /\n/gs;
519     for (my $i=1; $i < ($line_count+2); $i++) {
520         $source_lines.="$i\n";
521     }
522     $source_lines =~ s/\n\Z//;
524     return <<END_OF_HTML
525 <div class="$type">
526   <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
527     <tbody>
528       <tr>
529         <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
530         <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>
531       </tr>
532     </tbody>
533   </table>
534 </div>
535 END_OF_HTML