Updated Spanish translation
[gtk-doc.git] / gtkdoc-fixxref.in
blob2fb8d8fac0413f3a7660e73dd7f4aef203810cd1
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;
43 my $SRC_LANG;
45 # This contains all the entities and their relative URLs.
46 my %Links;
48 # failing link targets we don't warn about even once
49 my %NoLinks = (
50     'char' => 1,
51     'double' => 1,
52     'float' => 1,
53     'int' => 1,
54     'long' => 1,
55     'main' => 1,
56     'signed' => 1,
57     'unsigned' => 1,
58     'va-list' => 1,
59     'void' => 1,
60     'GBoxed' => 1,
61     'GEnum' => 1,
62     'GFlags' => 1,
63     'GInterface' => 1
67 run() unless caller; # Run program unless loaded as a module
70 sub run {
71     my %optctl = ('module' => \$MODULE,
72                   'module-dir' => \$MODULE_DIR,
73                   'html-dir' => \$HTML_DIR,
74                   'extra-dir' => \@EXTRA_DIRS,
75                   'version' => \$PRINT_VERSION,
76                   'help' => \$PRINT_HELP,
77                   'src-lang' => \$SRC_LANG);
78     
79     GetOptions(\%optctl, "module=s", "module-dir=s", "html-dir:s", "extra-dir=s@",
80               "src-lang=s", "version", "help");
81     
82     if ($PRINT_VERSION) {
83         print "@VERSION@\n";
84         exit 0;
85     }
86     
87     if ($PRINT_HELP) {
88             print <<EOF;
89 gtkdoc-fixxref version @VERSION@ - fix cross references in html files
91 --module=MODULE_NAME    Name of the doc module being parsed
92 --module-dir=MODULE_DIR The directory which contains the generated HTML
93 --html-dir=HTML_DIR     The directory where gtk-doc generated documentation is
94                         installed
95 --extra-dir=EXTRA_DIR   Directories to recursively scan for indices (index.sgml)
96                         in addition to HTML_DIR
97                         May be used more than once for multiple directories
98 --src-lang=SRC_LANG     Programing language used for syntax highlighting. The
99                         available languages depend on the source source
100                         highlighter you use.
101 --version               Print the version of this program
102 --help                  Print this help
104         exit 0;
105     }
106     
107     if (!$SRC_LANG) {
108         $SRC_LANG="c"
109     }
110       
111     my $path_prefix="";
112     if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
113         $path_prefix=$1;
114         @TRACE@("Path prefix: $path_prefix");
115     }
116     
117     if (!defined $MODULE_DIR) {
118         $MODULE_DIR="$HTML_DIR/$MODULE";
119     }
120     
121     my $dir;
122     
123     # We scan the directory containing GLib and any directories in GNOME2_PATH
124     # first, but these will be overriden by any later scans.
125     $dir = `pkg-config --variable=prefix glib-2.0`;
126     $dir =~ s/\s+$//;
127     $dir = $dir . "/share/gtk-doc/html";
128     if (-d $dir) {
129         # Some predefined link targets to get links into type hierarchies as these
130         # have no targets. These are always absolute for now.
131         $Links{'GBoxed'} = "$dir/gobject/gobject-Boxed-Types.html";
132         $Links{'GEnum'} = "$dir/gobject/gobject-Enumeration-and-Flag-Types.html";
133         $Links{'GFlags'} = "$dir/gobject/gobject-Enumeration-and-Flag-Types.html";
134         $Links{'GInterface'} = "$dir/gobject/GTypeModule.html";
135     
136         if ($dir ne $HTML_DIR) {
137             @TRACE@("Scanning GLib directory: $dir");
138             if ($dir !~ m%^\Q$path_prefix\E/%) {
139                 &ScanIndices ($dir, 1);
140             } else {
141                 &ScanIndices ($dir, 0);
142             }
143         }
144     }
145     
146     if (defined ($ENV{"GNOME2_PATH"})) {
147         foreach $dir (split (/:/, $ENV{"GNOME2_PATH"})) {
148             $dir = $dir . "/share/gtk-doc/html";
149             if (-d $dir && $dir ne $HTML_DIR) {
150                 @TRACE@("Scanning GNOME2_PATH directory: $dir");
151                 if ($dir !~ m%^\Q$path_prefix\E/%) {
152                     &ScanIndices ($dir, 1);
153                 } else {
154                     &ScanIndices ($dir, 0);
155                 }
156             }
157             # ubuntu started to compress this as index.sgml.gz :/
158             # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
159         }
160     }
161     
162     @TRACE@("Scanning HTML_DIR directory: $HTML_DIR");
163     &ScanIndices ($HTML_DIR, 0);
164     @TRACE@("Scanning HTML_DIR directory: $MODULE_DIR");
165     &ScanIndices ($MODULE_DIR, 0);
166     
167     # check all extra dirs, but skip already scanned dirs or subdirs of those
168     foreach my $dir (@EXTRA_DIRS) {
169         my $vdir;
170         @TRACE@("Scanning EXTRA_DIR directory: $dir");
171     
172         # If the --extra-dir option is not relative and is not sharing the same
173         # prefix as the target directory of the docs, we need to use absolute
174         # directories for the links
175         if ($dir !~m/^\.\./ &&  $dir !~ m%\Q$path_prefix\E/%) {
176             &ScanIndices ($dir, 1);
177         } else {
178             &ScanIndices ($dir, 0);
179         }
180     }
181     
182     &ReadSections ();
183     
184     &FixCrossReferences ($MODULE_DIR);
188 sub ScanIndices {
189     my ($scan_dir, $use_absolute_links) = @_;
190     
191     @TRACE@("Scanning source directory: $scan_dir absolute: $use_absolute_links");
193     # This array holds any subdirectories found.
194     my (@subdirs) = ();
196     opendir (HTMLDIR, $scan_dir) || return;
197     my $file;
198     foreach $file (readdir (HTMLDIR)) {
199         if ($file eq '.' || $file eq '..') {
200             next;
201         } elsif (-d "$scan_dir/$file") {
202             push (@subdirs, $file);
203             next;
204         }
205         if ($file =~ m/\.devhelp2$/) {
206             # if devhelp-file is good don't read index.sgml
207             &ReadDevhelp ("$scan_dir/$file", $use_absolute_links);
208         }
209         elsif ($file eq "index.sgml.gz") {
210             # debian/ubuntu started to compress this as index.sgml.gz :/
211             print <<EOF;
212 Please fix https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138 . For now run:
213 gunzip $scan_dir/$file
215         }
216         elsif ($file =~ m/\.devhelp2.gz$/) {
217             # debian/ubuntu started to compress this as *devhelp2.gz :/
218             print <<EOF;
219 Please fix https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/1466210 . For now run:
220 gunzip $scan_dir/$file
222         }
223         # we could consider supporting: use IO::Zlib;
224     }
225     closedir (HTMLDIR);
227     # Now recursively scan the subdirectories.
228     my $dir;
229     foreach $dir (sort(@subdirs)) {
230         &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
231     }
235 sub ReadDevhelp {
236     my ($file, $use_absolute_links) = @_;
238     # Determine the absolute directory, to be added to links in $file
239     # if we need to use an absolute link.
240     # $file will be something like /prefix/gnome/share/gtk-doc/html/gtk/$file
241     # We want the part up to 'html/.*' since the links in $file include
242     # the rest.
243     my $dir = "../";
244     if ($use_absolute_links) {
245         # For uninstalled index.sgml files we'd need to map the path to where it
246         # will be installed to
247         if ($file !~ /\.\//) {
248             $file =~ /(.*\/)(.*?)\/.*?\.devhelp2/;
249             $dir = "$1$2";
250         }
251     } else {
252         if ($file =~ /(.*\/)(.*?)\/.*?\.devhelp2/) {
253             $dir .= "$2/";
254         } else {
255             $dir = "";
256         }
257     }
259     @TRACE@("Scanning index file=$file, absolute=$use_absolute_links, dir=$dir");
261     open (INDEXFILE, $file)
262         || die "Can't open $file: $!";
263     while (<INDEXFILE>) {
264         if (m/ link="([^#]*)#([^"]*)"/) {
265             @TRACE@("Found id: $2 href: $1#$2");
266             $Links{$2} = "$dir$1#$2";
267         }
268     }
269     close (INDEXFILE);
273 sub ReadSections {
274     if (!defined($MODULE)) {
275         return;
276     }
278     open (INPUT, "$MODULE-sections.txt")
279             || die "Can't open $MODULE-sections.txt: $!";
280     my $subsection = "";
281     while (<INPUT>) {
282         if (m/^#/) {
283             next;
285         } elsif (m/^<SECTION>/) {
286             $subsection = "";
287         } elsif (m/^<SUBSECTION\s*(.*)>/i) {
288             $subsection = $1;
289         } elsif (m/^<SUBSECTION>/) {
290             next;
291         } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
292             next;
293         } elsif (m/^<FILE>(.*)<\/FILE>/) {
294             next;
295         } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
296             next;
297         } elsif (m/^<\/SECTION>/) {
298             next;
299         } elsif (m/^(\S+)/) {
300             my $symbol=$1;
302             if ($subsection eq "Standard" || $subsection eq "Private") {
303                 $NoLinks{CreateValidSGMLID($symbol)} = 1;
304             }
305         }
306     }
307     close (INPUT);
311 sub FixCrossReferences {
312     my ($scan_dir) = @_;
314     opendir (HTMLDIR, $scan_dir)
315         || die "Can't open HTML directory $scan_dir: $!";
316     my $file;
317     foreach $file (readdir (HTMLDIR)) {
318         if ($file eq '.' || $file eq '..') {
319             next;
320         } elsif ($file =~ m/.html?$/) {
321             &FixHTMLFile ("$scan_dir/$file");
322         }
323     }
324     closedir (HTMLDIR);
328 sub FixHTMLFile {
329     my ($file) = @_;
330     @TRACE@("Fixing file: $file");
332     open (HTMLFILE, $file)
333         || die "Can't open $file: $!";
334     undef $/;
335     my $entire_file = <HTMLFILE>;
336     close (HTMLFILE);
338     if ("@HIGHLIGHT@" ne "") {
339         # FIXME: ideally we'd pass a clue about the example language to the highligher
340         # unfortunately the "language" attribute is not appearing in the html output
341         # we could patch the customization to have <code class="xxx"> inside of <pre>
342         if ("@HIGHLIGHT@" =~ m%/vim$%) {
343             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSourceVim($1,$2);%gse;
344         }
345         else {
346             $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
347         }
348         # this just broke existing GTKDOCLINK tags
349         # &lt;GTKDOCLINK HREF=&quot;GST-PAD-SINK:CAPS&quot;&gt;GST_PAD_SINK&lt;/GTKDOCLINK&gt;
350         $entire_file =~ s%\&lt;GTKDOCLINK\s+HREF=\&quot;(.*?)\&quot;\&gt;(.*?)\&lt;/GTKDOCLINK\&gt;%\<GTKDOCLINK\ HREF=\"$1\"\>$2\</GTKDOCLINK\>%gs;
352         # from the highlighter we get all the functions marked up
353         # now we could turn them into GTKDOCLINK items
354         $entire_file =~ s%(<span class=\"function\">)(.*?)(</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
355         # we could also try the first item in stuff marked up as 'normal'
356         $entire_file =~ s%(<span class=\"normal\">\s*)(.+?)((\s+.+?)?\s*</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
357     }
359     my @lines = split(/\n/, $entire_file);
360     for (my $i=0; $i<$#lines; $i++) {
361         $lines[$i] =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($file,$i+1,$1,$2); %ge;
362         #if ($lines[$i] =~ m/GTKDOCLINK/) {
363         #    print "make xref failed for line: ",$lines[$i], "\n";
364         #}
365     }
366     $entire_file = join("\n",@lines);
368     open (NEWFILE, ">$file.new")
369         || die "Can't open $file: $!";
370     print NEWFILE $entire_file;
371     close (NEWFILE);
373     unlink ($file)
374         || die "Can't delete $file: $!";
375     rename ("$file.new", $file)
376         || die "Can't rename $file.new: $!";
379 sub MakeXRef {
380     my ($file, $line, $id, $text) = @_;
382     my $href = $Links{$id};
384     # this is a workaround for some inconsistency we have with CreateValidSGMLID
385     if (!$href && $id =~ m/:/) {
386         my $tid = $id;
387         $tid =~ s/:/--/g;
388         $href = $Links{$tid};
389     }
390     # poor mans plural support
391     if (!$href && $id =~ m/s$/) {
392         my $tid = $id;
393         $tid =~ s/s$//g;
394         $href = $Links{$tid};
395         if (!$href && defined $Links{"$tid-struct"}) {
396             $href = $Links{"$tid-struct"};
397         }
398     }
399     if (!$href && defined $Links{"$id-struct"}) {
400         $href = $Links{"$id-struct"};
401     }
403     if ($href) {
404         # if it is a link to same module, remove path to make it work
405         # uninstalled
406         if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
407             $href=$1;
408             @TRACE@("  Fixing link to uninstalled doc: $id, $href, $text");
409         } else {
410             @TRACE@("  Fixing link: $id, $href, $text");
411         }
412         return "<a href=\"$href\">$text</a>";
413     } else {
414         my $warn = 1;
415         @TRACE@("  no link for: $id, $text");
417         # don't warn multiple times and also skip blacklisted (ctypes)
418         $warn = 0 if exists $NoLinks{$id};
419         # if it's a function, don't warn if it does not contain a "_"
420         # (transformed to "-")
421         # - gnome coding style would use '_'
422         # - will avoid wrong warnings for ansi c functions
423         $warn = 0 if ($text =~ m/ class=\"function\"/ && $id !~ m/-/);
424         # if it's a 'return value', don't warn (implicitly created link)
425         $warn = 0 if ($text =~ m/ class=\"returnvalue\"/);
426         # if it's a 'type', don't warn if it starts with lowercase
427         # - gnome coding style would use CamelCase
428         $warn = 0 if ($text =~ m/ class=\"type\"/ && ($id =~ m/^[a-z]/));
429         # don't warn for self links
430         $warn = 0 if ($text eq $id);
432         if ($warn == 1) {
433           &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
434           $NoLinks{$id} = 1;
435         }
436         return $text;
437     }
441 sub MakeGtkDocLink {
442     my ($pre,$symbol,$post) = @_;
444     my $id=CreateValidSGMLID($symbol);
446     # these are implicitely created links in highlighed sources
447     # we don't want warnings for those if the links cannot be resolved.
448     $NoLinks{$id} = 1;
450     #return "<span class=\"$type\"><GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK></span>";
451     return "$pre<GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK>$post";
455 sub HighlightSource {
456     my ($type, $source) = @_;
458     # chop of leading and trailing empty lines
459     $source =~ s/^\s*\n+//gs;
460     $source =~ s/[\s\n]+$//gs;
461     # cut common indent
462     $source =~ m/^(\s*)/;
463     $source =~ s/^$1//gms;
464     # avoid double entity replacement
465     $source =~ s/&lt;/</g;
466     $source =~ s/&gt;/>/g;
467     $source =~ s/&amp;/&/g;
469     # write source to a temp file
470     # FIXME: use .c for now to hint the language to the highlighter
471     my $temp_source_file="$MODULE_DIR/_temp_src.$$.c";
472     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
473     print NEWFILE $source;
474     close (NEWFILE);
476     @TRACE@(" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file ");
478     # format source
479     my $highlighted_source=`@HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file`;
480     if ("@HIGHLIGHT@" =~ m%/source-highlight$%) {
481         $highlighted_source =~ s%^<\!-- .*? -->%%gs;
482         $highlighted_source =~ s%<pre><tt>(.*?)</tt></pre>%$1%gs;
483     }
484     elsif ("@HIGHLIGHT@" =~ m%/highlight$%) {
485         # need to rewrite the stylesheet classes
486         $highlighted_source =~ s%<span class="gtkdoc com">%<span class="comment">%gs;
487         $highlighted_source =~ s%<span class="gtkdoc dir">%<span class="preproc">%gs;
488         $highlighted_source =~ s%<span class="gtkdoc kwd">%<span class="function">%gs;
489         $highlighted_source =~ s%<span class="gtkdoc kwa">%<span class="keyword">%gs;
490         $highlighted_source =~ s%<span class="gtkdoc line">%<span class="linenum">%gs;
491         $highlighted_source =~ s%<span class="gtkdoc num">%<span class="number">%gs;
492         $highlighted_source =~ s%<span class="gtkdoc str">%<span class="string">%gs;
493         $highlighted_source =~ s%<span class="gtkdoc sym">%<span class="symbol">%gs;
494         # maybe also do
495         # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
496     }
497     # remove temp file
498     unlink ($temp_source_file)
499         || die "Can't delete $temp_source_file: $!";
501     return &HighlightSourcePostprocess($type, $highlighted_source);
505 sub HighlightSourceVim {
506     my ($type, $source) = @_;
508     # chop of leading and trailing empty lines
509     $source =~ s/^\s*\n+//gs;
510     $source =~ s/[\s\n]+$//gs;
511     # cut common indent
512     $source =~ m/^(\s*)/;
513     $source =~ s/^$1//gms;
514     # avoid double entity replacement
515     $source =~ s/&lt;/</g;
516     $source =~ s/&gt;/>/g;
517     $source =~ s/&amp;/&/g;
519     # write source to a temp file
520     my $temp_source_file="$MODULE_DIR/_temp_src.$$.h";
521     open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
522     print NEWFILE $source;
523     close (NEWFILE);
525     # format source
526     system "echo 'let html_number_lines=0|let html_use_css=1|let html_use_xhtml=1|e $temp_source_file|syn on|set syntax=$SRC_LANG|run! syntax/2html.vim|w! $temp_source_file.html|qa!' | @HIGHLIGHT@ -n -e -u NONE -T xterm >/dev/null";
528     my $highlighted_source;
529     {
530         local $/;
531         open (NEWFILE, "<$temp_source_file.html");
532         $highlighted_source = <NEWFILE>;
533         close (NEWFILE);
534     }
535     $highlighted_source =~ s#.*<pre\b[^>]*>\n##s;
536     $highlighted_source =~ s#</pre>.*##s;
538     # need to rewrite the stylesheet classes
539     # FIXME: Vim has somewhat different syntax groups
540     $highlighted_source =~ s%<span class="Comment">%<span class="comment">%gs;
541     $highlighted_source =~ s%<span class="PreProc">%<span class="preproc">%gs;
542     $highlighted_source =~ s%<span class="Statement">%<span class="keyword">%gs;
543     $highlighted_source =~ s%<span class="Identifier">%<span class="function">%gs;
544     $highlighted_source =~ s%<span class="Constant">%<span class="number">%gs;
545     $highlighted_source =~ s%<span class="Special">%<span class="symbol">%gs;
546     $highlighted_source =~ s%<span class="Type">%<span class="type">%gs;
548     # remove temp files
549     unlink ($temp_source_file)
550         || die "Can't delete $temp_source_file: $!";
551     unlink ("$temp_source_file.html")
552         || die "Can't delete $temp_source_file.html: $!";
554     return &HighlightSourcePostprocess($type, $highlighted_source);
558 sub HighlightSourcePostprocess {
559     my ($type, $highlighted_source) = @_;
561     # chop of leading and trailing empty lines
562     $highlighted_source =~ s/^[\s\n]+//gs;
563     $highlighted_source =~ s/[\s\n]+$//gs;
565     # turn common urls in comments into links
566     $highlighted_source =~ s%<span class="url">(.*?)</span>%<span class="url"><a href="$1">$1</a></span>%gs;
568     # we do own line-numbering
569     my $source_lines="";
570     my $line_count = () = $highlighted_source =~ /\n/gs;
571     for (my $i=1; $i < ($line_count+2); $i++) {
572         $source_lines.="$i\n";
573     }
574     $source_lines =~ s/\n\Z//;
576     return <<END_OF_HTML
577 <div class="$type">
578   <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
579     <tbody>
580       <tr>
581         <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
582         <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>
583       </tr>
584     </tbody>
585   </table>
586 </div>
587 END_OF_HTML