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 #############################################################################
31 push @INC, '@PACKAGE_DATA_DIR@';
32 require "gtkdoc-common.pl";
36 # name of documentation module
45 # This contains all the entities and their relative URLs.
48 # failing link targets we don't warn about even once
67 run() unless caller; # Run program unless loaded as a module
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);
79 GetOptions(\%optctl, "module=s", "module-dir=s", "html-dir:s", "extra-dir=s@",
80 "src-lang=s", "version", "help");
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
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
101 --version Print the version of this program
102 --help Print this help
112 if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
114 @TRACE@("Path prefix: $path_prefix");
117 if (!defined $MODULE_DIR) {
118 $MODULE_DIR="$HTML_DIR/$MODULE";
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`;
127 $dir = $dir . "/share/gtk-doc/html";
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";
136 if ($dir ne $HTML_DIR) {
137 @TRACE@("Scanning GLib directory: $dir");
138 if ($dir !~ m%^\Q$path_prefix\E/%) {
139 &ScanIndices ($dir, 1);
141 &ScanIndices ($dir, 0);
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);
154 &ScanIndices ($dir, 0);
157 # ubuntu started to compress this as index.sgml.gz :/
158 # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
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);
167 # check all extra dirs, but skip already scanned dirs or subdirs of those
168 foreach my $dir (@EXTRA_DIRS) {
170 @TRACE@("Scanning EXTRA_DIR directory: $dir");
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);
178 &ScanIndices ($dir, 0);
184 &FixCrossReferences ($MODULE_DIR);
189 my ($scan_dir, $use_absolute_links) = @_;
191 @TRACE@("Scanning source directory: $scan_dir absolute: $use_absolute_links");
193 # This array holds any subdirectories found.
196 opendir (HTMLDIR, $scan_dir) || return;
198 foreach $file (readdir (HTMLDIR)) {
199 if ($file eq '.' || $file eq '..') {
201 } elsif (-d "$scan_dir/$file") {
202 push (@subdirs, $file);
205 if ($file =~ m/\.devhelp2$/) {
206 # if devhelp-file is good don't read index.sgml
207 &ReadDevhelp ("$scan_dir/$file", $use_absolute_links);
209 elsif ($file eq "index.sgml.gz") {
210 # debian/ubuntu started to compress this as index.sgml.gz :/
212 Please fix https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138 . For now run:
216 elsif ($file =~ m/\.devhelp2.gz$/) {
217 # debian/ubuntu started to compress this as *devhelp2.gz :/
219 Please fix https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/1466210 . For now run:
223 # we could consider supporting: use IO::Zlib;
227 # Now recursively scan the subdirectories.
229 foreach $dir (sort(@subdirs)) {
230 &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
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
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/;
252 if ($file =~ /(.*\/)(.*?)\/.*?\.devhelp2/) {
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";
274 if (!defined($MODULE)) {
278 open (INPUT, "$MODULE-sections.txt")
279 || die "Can't open $MODULE-sections.txt: $!";
285 } elsif (m/^<SECTION>/) {
287 } elsif (m/^<SUBSECTION\s*(.*)>/i) {
289 } elsif (m/^<SUBSECTION>/) {
291 } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
293 } elsif (m/^<FILE>(.*)<\/FILE>/) {
295 } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
297 } elsif (m/^<\/SECTION>/) {
299 } elsif (m/^(\S+)/) {
302 if ($subsection eq "Standard" || $subsection eq "Private") {
303 $NoLinks{CreateValidSGMLID($symbol)} = 1;
311 sub FixCrossReferences {
314 opendir (HTMLDIR, $scan_dir)
315 || die "Can't open HTML directory $scan_dir: $!";
317 foreach $file (readdir (HTMLDIR)) {
318 if ($file eq '.' || $file eq '..') {
320 } elsif ($file =~ m/.html?$/) {
321 &FixHTMLFile ("$scan_dir/$file");
330 @TRACE@("Fixing file: $file");
332 open (HTMLFILE, $file)
333 || die "Can't open $file: $!";
335 my $entire_file = <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;
346 $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
348 # this just broke existing GTKDOCLINK tags
349 # <GTKDOCLINK HREF="GST-PAD-SINK:CAPS">GST_PAD_SINK</GTKDOCLINK>
350 $entire_file =~ s%\<GTKDOCLINK\s+HREF=\"(.*?)\"\>(.*?)\</GTKDOCLINK\>%\<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;
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";
366 $entire_file = join("\n",@lines);
368 open (NEWFILE, ">$file.new")
369 || die "Can't open $file: $!";
370 print NEWFILE $entire_file;
374 || die "Can't delete $file: $!";
375 rename ("$file.new", $file)
376 || die "Can't rename $file.new: $!";
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/:/) {
388 $href = $Links{$tid};
390 # poor mans plural support
391 if (!$href && $id =~ m/s$/) {
394 $href = $Links{$tid};
395 if (!$href && defined $Links{"$tid-struct"}) {
396 $href = $Links{"$tid-struct"};
399 if (!$href && defined $Links{"$id-struct"}) {
400 $href = $Links{"$id-struct"};
404 # if it is a link to same module, remove path to make it work
406 if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
408 @TRACE@(" Fixing link to uninstalled doc: $id, $href, $text");
410 @TRACE@(" Fixing link: $id, $href, $text");
412 return "<a href=\"$href\">$text</a>";
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);
433 &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
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.
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;
462 $source =~ m/^(\s*)/;
463 $source =~ s/^$1//gms;
464 # avoid double entity replacement
465 $source =~ s/</</g;
466 $source =~ s/>/>/g;
467 $source =~ s/&/&/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;
476 @TRACE@(" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file ");
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;
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;
495 # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
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;
512 $source =~ m/^(\s*)/;
513 $source =~ s/^$1//gms;
514 # avoid double entity replacement
515 $source =~ s/</</g;
516 $source =~ s/>/>/g;
517 $source =~ s/&/&/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;
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;
531 open (NEWFILE, "<$temp_source_file.html");
532 $highlighted_source = <NEWFILE>;
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;
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
570 my $line_count = () = $highlighted_source =~ /\n/gs;
571 for (my $i=1; $i < ($line_count+2); $i++) {
572 $source_lines.="$i\n";
574 $source_lines =~ s/\n\Z//;
578 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
581 <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
582 <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>