Updated Spanish translation
[gtk-doc.git] / gtkdoc-fixxref.in
blobb033a7990e4de4cbe277a38f407f8ee31ee30647
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;
78 # failing link targets we don't warn about even once
79 my %NoLinks = (
80     'char'  => 1,
81     'double'  => 1,
82     'float'  => 1,
83     'int'  => 1,
84     'long'  => 1,
85     'main'  => 1,
86     'signed'  => 1,
87     'unsigned'  => 1,
88     'va-list' => 1,
89     'void'  => 1,
90     'GBoxed' => 1,
91     'GEnum' => 1,
92     'GFlags' => 1,
93     'GInterface' => 1
94     );
96 my $path_prefix="";
97 if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
98     $path_prefix=$1;
99     @TRACE@("Path prefix: $path_prefix");
102 if (!defined $MODULE_DIR) {
103   $MODULE_DIR="$HTML_DIR/$MODULE";
106 my $dir;
108 # We scan the directory containing GLib and any directories in GNOME2_PATH
109 # first, but these will be overriden by any later scans.
110 $dir = `pkg-config --variable=prefix glib-2.0`;
111 $dir =~ s/\s+$//;
112 $dir = $dir . "/share/gtk-doc/html";
113 if (-d $dir && $dir ne $HTML_DIR) {
114     @TRACE@("Scanning GLib directory: $dir");
116     # Some predefined link targets to get links into type hierarchies as these
117     # have no targets. These are always absolute for now.
118     $Links{'GBoxed'} = "$dir/gobject/gobject-Boxed-Types.html";
119     $Links{'GEnum'} = "$dir/gobject/gobject-Enumeration-and-Flag-Types.html";
120     $Links{'GFlags'} = "$dir/gobject/gobject-Enumeration-and-Flag-Types.html";
121     $Links{'GInterface'} = "$dir/gobject/GTypeModule.html";
123     if ($dir !~ m%^\Q$path_prefix\E/%) {
124         &ScanIndices ($dir, 1);
125     } else {
126         &ScanIndices ($dir, 0);
127     }
130 if (defined ($ENV{"GNOME2_PATH"})) {
131     foreach $dir (split (/:/, $ENV{"GNOME2_PATH"})) {
132         $dir = $dir . "/share/gtk-doc/html";
133         if (-d $dir && $dir ne $HTML_DIR) {
134             @TRACE@("Scanning GNOME2_PATH directory: $dir");
135             if ($dir !~ m%^\Q$path_prefix\E/%) {
136                 &ScanIndices ($dir, 1);
137             } else {
138                 &ScanIndices ($dir, 0);
139             }
140         }
141         # ubuntu started to compress this as index.sgml.gz :/
142         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
143     }
146 @TRACE@("Scanning HTML_DIR directory: $HTML_DIR");
147 &ScanIndices ($HTML_DIR, 0);
148 @TRACE@("Scanning HTML_DIR directory: $MODULE_DIR");
149 &ScanIndices ($MODULE_DIR, 0);
151 # check all extra dirs, but skip already scanned dirs or subdirs of those
152 foreach my $dir (@EXTRA_DIRS) {
153     my $vdir;
154     @TRACE@("Scanning EXTRA_DIR directory: $dir");
156     # If the --extra-dir option is not relative and is not sharing the same
157     # prefix as the target directory of the docs, we need to use absolute
158     # directories for the links
159     if ($dir !~m/^\.\./ &&  $dir !~ m%\Q$path_prefix\E/%) {
160         &ScanIndices ($dir, 1);
161     } else {
162         &ScanIndices ($dir, 0);
163     }
166 if (defined($MODULE)) {
167     open (INPUT, "$MODULE-sections.txt")
168             || die "Can't open $MODULE-sections.txt: $!";
169     my $subsection = "";
170     while (<INPUT>) {
171         if (m/^#/) {
172             next;
174         } elsif (m/^<SECTION>/) {
175             $subsection = "";
176         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
177             $subsection = $1;
178         } elsif (m/^<SUBSECTION>/) {
179             next;
180         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
181             next;
182         } elsif (m/^<FILE>(.*)<\/FILE>/) {
183             next;
184         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
185             next;
186         } elsif (m/^<\/SECTION>/) {
187             next;
188         } elsif (m/^(\S+)/) {
189             my $symbol=CreateValidSGMLID($1);
191             if ($subsection eq "Standard" || $subsection eq "Private") {
192                 $NoLinks{$symbol} = 1;
193             }
194         }
195     }
196     close (INPUT);
199 &FixCrossReferences ($MODULE_DIR);
201 sub ScanIndices {
202     my ($scan_dir, $use_absolute_links) = @_;
203     
204     @TRACE@("Scanning source directory: $scan_dir absolute: $use_absolute_links");
206     # This array holds any subdirectories found.
207     my (@subdirs) = ();
209     opendir (HTMLDIR, $scan_dir) || return;
210     my $file;
211     foreach $file (readdir (HTMLDIR)) {
212         if ($file eq '.' || $file eq '..') {
213             next;
214         } elsif (-d "$scan_dir/$file") {
215             push (@subdirs, $file);
216         } elsif ($file eq "index.sgml") {
217             &ScanIndex ("$scan_dir/$file", $use_absolute_links);
218         }
219         # ubuntu started to compress this as index.sgml.gz :/
220         # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
221     }
222     closedir (HTMLDIR);
224     # Now recursively scan the subdirectories.
225     my $dir;
226     foreach $dir (sort(@subdirs)) {
227         &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
228     }
231 sub ScanIndex {
232     my ($file, $use_absolute_links) = @_;
234     # Determine the absolute directory, to be added to links in index.sgml
235     # if we need to use an absolute link.
236     # $file will be something like /opt/gnome/share/gtk-doc/html/gtk/index.sgml
237     # We want the part up to 'html' since the links in index.sgml include
238     # the rest.
239     my $dir = "../";
240     if ($use_absolute_links) {
241         # For uninstalled index.sgml files we'd need to map the path to where it
242         # will be installed to
243         if ($file !~ /\.\//) {
244           $file =~ /(.*\/)(.*?)\/index\.sgml/;
245           $dir = $1;
246         }
247     }
248     @TRACE@("Scanning index file=$file, absolute=$use_absolute_links, dir=$dir");
250     open (INDEXFILE, $file)
251         || die "Can't open $file: $!";
252     while (<INDEXFILE>) {
253         if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) {
254             @TRACE@("Found id: $1 href: $2");
255             $Links{$1} = "$dir$2";
256         }
257     }
258     close (INDEXFILE);
262 sub FixCrossReferences {
263     my ($scan_dir) = @_;
265     opendir (HTMLDIR, $scan_dir)
266         || die "Can't open HTML directory $scan_dir: $!";
267     my $file;
268     foreach $file (readdir (HTMLDIR)) {
269         if ($file eq '.' || $file eq '..') {
270             next;
271         } elsif ($file =~ m/.html?$/) {
272             &FixHTMLFile ("$scan_dir/$file");
273         }
274     }
275     closedir (HTMLDIR);
279 sub FixHTMLFile {
280     my ($file) = @_;
281     @TRACE@("Fixing file: $file");
283     open (HTMLFILE, $file)
284         || die "Can't open $file: $!";
285     undef $/;
286     my $entire_file = <HTMLFILE>;
287     close (HTMLFILE);
289     if ("@HIGHLIGHT@" ne "") {
290         # FIXME: ideally we'd pass a clue about the example language to the highligher
291         # unfortunately the "language" attribute is not appearing in the html output
292         # we could patch the customization to have <code class="xxx"> inside of <pre>
293         if ("@HIGHLIGHT@" =~ m%/vim$%) {
294             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSourceVim($1,$2);%gse;
295         }
296         else {
297             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
298         }
299         # this just broke existing GTKDOCLINK tags
300         # &lt;GTKDOCLINK HREF=&quot;GST-PAD-SINK:CAPS&quot;&gt;GST_PAD_SINK&lt;/GTKDOCLINK&gt;
301         $entire_file =~ s%\&lt;GTKDOCLINK\s+HREF=\&quot;(.*?)\&quot;\&gt;(.*?)\&lt;/GTKDOCLINK\&gt;%\<GTKDOCLINK\ HREF=\"$1\"\>$2\</GTKDOCLINK\>%gs;
303         # from the highlighter we get all the functions marked up
304         # now we could turn them into GTKDOCLINK items
305         $entire_file =~ s%(<span class=\"function\">)(.*?)(</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
306         # we could also try the first item in stuff marked up as 'normal'
307         $entire_file =~ s%(<span class=\"normal\">\s*)(.+?)((\s+.+?)?\s*</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
308     }
310     my @lines = split(/\n/, $entire_file);
311     for (my $i=0; $i<$#lines; $i++) {
312         $lines[$i] =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($file,$i+1,$1,$2); %ge;
313         #if ($lines[$i] =~ m/GTKDOCLINK/) {
314         #    print "make xref failed for line: ",$lines[$i], "\n";
315         #}
316     }
317     $entire_file = join("\n",@lines);
319     open (NEWFILE, ">$file.new")
320         || die "Can't open $file: $!";
321     print NEWFILE $entire_file;
322     close (NEWFILE);
324     unlink ($file)
325         || die "Can't delete $file: $!";
326     rename ("$file.new", $file)
327         || die "Can't rename $file.new: $!";
330 sub MakeXRef {
331     my ($file, $line, $id, $text) = @_;
333     my $href = $Links{$id};
335     # this is a workaround for some inconsistency we have with CreateValidSGMLID
336     if (!$href && $id =~ m/:/) {
337         my $tid = $id;
338         $tid =~ s/:/--/g;
339         $href = $Links{$tid};
340     }
341     # poor mans plural support
342     if (!$href && $id =~ m/s$/) {
343         my $tid = $id;
344         $tid =~ s/s$//g;
345         $href = $Links{$tid};
346     }
348     if ($href) {
349         # if it is a link to same module, remove path to make it work
350         # uninstalled
351         if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
352             $href=$1;
353             @TRACE@("  Fixing link to uninstalled doc: $id, $href, $text");
354         } else {
355             @TRACE@("  Fixing link: $id, $href, $text");
356         }
357         return "<a href=\"$href\">$text</a>";
358     } else {
359         my $warn = 1;
360         @TRACE@("  no link for: $id, $text");
362         # don't warn multiple times and also skip blacklisted (ctypes)
363         $warn = 0 if exists $NoLinks{$id};
364         # if it's a function, don't warn if it does not contain a "_"
365         # (transformed to "-")
366         # - gnome coding style would use '_'
367         # - will avoid wrong warnings for ansi c functions
368         $warn = 0 if ($text =~ m/ class=\"function\"/ && $id !~ m/-/);
369         # if it's a 'return value', don't warn (implicitly created link)
370         $warn = 0 if ($text =~ m/ class=\"returnvalue\"/);
371         # if it's a 'type', don't warn if it starts with lowercase
372         # - gnome coding style would use CamelCase
373         $warn = 0 if ($text =~ m/ class=\"type\"/ && ($id =~ m/^[a-z]/));
374         # don't warn for self links
375         $warn = 0 if ($text eq $id);
377         if ($warn == 1) {
378           &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
379           $NoLinks{$id} = 1;
380         }
381         return $text;
382     }
386 sub MakeGtkDocLink {
387     my ($pre,$symbol,$post) = @_;
389     my $id=CreateValidSGMLID($symbol);
391     # these are implicitely created links in highlighed sources
392     # we don't want warnings for those if the links cannot be resolved.
393     $NoLinks{$id} = 1;
395     #return "<span class=\"$type\"><GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK></span>";
396     return "$pre<GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK>$post";
400 sub HighlightSource {
401     my ($type, $source) = @_;
403     # chop of leading and trailing empty lines
404     $source =~ s/^\s*\n+//gs;
405     $source =~ s/[\s\n]+$//gs;
406     # cut common indent
407     $source =~ m/^(\s*)/;
408     $source =~ s/^$1//gms;
409     # avoid double entity replacement
410     $source =~ s/&lt;/</g;
411     $source =~ s/&gt;/>/g;
412     $source =~ s/&amp;/&/g;
414     # write source to a temp file
415     # FIXME: use .c for now to hint the language to the highlighter
416     my $temp_source_file="$MODULE_DIR/_temp_src.$$.c";
417     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
418     print NEWFILE $source;
419     close (NEWFILE);
421     @TRACE@(" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file ");
423     # format source
424     my $highlighted_source=`@HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file`;
425     if ("@HIGHLIGHT@" =~ m%/source-highlight$%) {
426         $highlighted_source =~ s%^<\!-- .*? -->%%gs;
427         $highlighted_source =~ s%<pre><tt>(.*?)</tt></pre>%$1%gs;
428     }
429     elsif ("@HIGHLIGHT@" =~ m%/highlight$%) {
430         # need to rewrite the stylesheet classes
431         $highlighted_source =~ s%<span class="gtkdoc com">%<span class="comment">%gs;
432         $highlighted_source =~ s%<span class="gtkdoc dir">%<span class="preproc">%gs;
433         $highlighted_source =~ s%<span class="gtkdoc kwd">%<span class="function">%gs;
434         $highlighted_source =~ s%<span class="gtkdoc kwa">%<span class="keyword">%gs;
435         $highlighted_source =~ s%<span class="gtkdoc line">%<span class="linenum">%gs;
436         $highlighted_source =~ s%<span class="gtkdoc num">%<span class="number">%gs;
437         $highlighted_source =~ s%<span class="gtkdoc str">%<span class="string">%gs;
438         $highlighted_source =~ s%<span class="gtkdoc sym">%<span class="symbol">%gs;
439         # maybe also do
440         # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
441     }
442     # remove temp file
443     unlink ($temp_source_file)
444         || die "Can't delete $temp_source_file: $!";
446     return &HighlightSourcePostprocess($type, $highlighted_source);
449 sub HighlightSourceVim {
450     my ($type, $source) = @_;
452     # chop of leading and trailing empty lines
453     $source =~ s/^[\s\n]+//gs;
454     $source =~ s/[\s\n]+$//gs;
455     # avoid double entity replacement
456     $source =~ s/&lt;/</g;
457     $source =~ s/&gt;/>/g;
458     $source =~ s/&amp;/&/g;
460     # write source to a temp file
461     my $temp_source_file="$MODULE_DIR/_temp_src.$$.h";
462     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
463     print NEWFILE $source;
464     close (NEWFILE);
466     # format source
467     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";
469     my $highlighted_source;
470     {
471         local $/;
472         open (NEWFILE, "<$temp_source_file.html");
473         $highlighted_source = <NEWFILE>;
474         close (NEWFILE);
475     }
476     $highlighted_source =~ s#.*<pre\b[^>]*>\n##s;
477     $highlighted_source =~ s#</pre>.*##s;
479     # need to rewrite the stylesheet classes
480     # FIXME: Vim has somewhat different syntax groups
481     $highlighted_source =~ s%<span class="Comment">%<span class="comment">%gs;
482     $highlighted_source =~ s%<span class="PreProc">%<span class="preproc">%gs;
483     $highlighted_source =~ s%<span class="Statement">%<span class="keyword">%gs;
484     $highlighted_source =~ s%<span class="Identifier">%<span class="function">%gs;
485     $highlighted_source =~ s%<span class="Constant">%<span class="number">%gs;
486     $highlighted_source =~ s%<span class="Special">%<span class="symbol">%gs;
487     $highlighted_source =~ s%<span class="Type">%<span class="type">%gs;
489     # remove temp files
490     unlink ($temp_source_file)
491         || die "Can't delete $temp_source_file: $!";
492     unlink ("$temp_source_file.html")
493         || die "Can't delete $temp_source_file.html: $!";
495     return &HighlightSourcePostprocess($type, $highlighted_source);
498 sub HighlightSourcePostprocess {
499     my ($type, $highlighted_source) = @_;
501     # chop of leading and trailing empty lines
502     $highlighted_source =~ s/^[\s\n]+//gs;
503     $highlighted_source =~ s/[\s\n]+$//gs;
505     # turn common urls in comments into links
506     $highlighted_source =~ s%<span class="url">(.*?)</span>%<span class="url"><a href="$1">$1</a></span>%gs;
508     # we do own line-numbering
509     my $source_lines="";
510     my $line_count = () = $highlighted_source =~ /\n/gs;
511     for (my $i=1; $i < ($line_count+2); $i++) {
512         $source_lines.="$i\n";
513     }
514     $source_lines =~ s/\n\Z//;
516     return <<END_OF_HTML
517 <div class="$type">
518   <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
519     <tbody>
520       <tr>
521         <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
522         <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>
523       </tr>
524     </tbody>
525   </table>
526 </div>
527 END_OF_HTML