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
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@",
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
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
75 # This contains all the entities and their relative URLs.
77 # This hold the path entries we already scanned
80 # failing link targets we don't warn about even once
96 if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
98 #print "Path prefix: $path_prefix\n";
101 if (!defined $MODULE_DIR) {
102 $MODULE_DIR="$HTML_DIR/$MODULE";
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`;
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);
117 &ScanIndices ($dir, 0);
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);
130 &ScanIndices ($dir, 0);
132 push (@VisitedPaths, $dir);
134 # ubuntu started to compress this as index.sgml.gz :/
135 # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
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) {
151 foreach $vdir (@VisitedPaths) {
152 if ($dir eq $vdir || $dir =~ m%^\Q$vdir\E/%) {
153 #print "Skipping EXTRA_DIR directory: $dir\n";
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);
167 &ScanIndices ($dir, 0);
171 open (INPUT, "$MODULE-sections.txt")
172 || die "Can't open $MODULE-sections.txt: $!";
178 } elsif (m/^<SECTION>/) {
180 } elsif (m/^<SUBSECTION\s*(.*)>/i) {
182 } elsif (m/^<SUBSECTION>/) {
184 } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
186 } elsif (m/^<FILE>(.*)<\/FILE>/) {
188 } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
190 } elsif (m/^<\/SECTION>/) {
192 } elsif (m/^(\S+)/) {
193 my $symbol=CreateValidSGMLID($1);
195 if ($subsection eq "Standard" || $subsection eq "Private") {
196 $NoLinks{$symbol} = 1;
202 &FixCrossReferences ($MODULE_DIR);
205 my ($scan_dir, $use_absolute_links) = @_;
207 #print "Scanning source directory: $scan_dir absolute: $use_absolute_links\n";
209 # This array holds any subdirectories found.
212 opendir (HTMLDIR, $scan_dir) || return;
214 foreach $file (readdir (HTMLDIR)) {
215 if ($file eq '.' || $file eq '..') {
217 } elsif (-d "$scan_dir/$file") {
218 push (@subdirs, $file);
219 } elsif ($file eq "index.sgml") {
220 &ScanIndex ("$scan_dir/$file", $use_absolute_links);
222 # ubuntu started to compress this as index.sgml.gz :/
223 # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
227 # Now recursively scan the subdirectories.
229 foreach $dir (@subdirs) {
230 &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
235 my ($file, $use_absolute_links) = @_;
236 #print "Scanning index file: $file absolute: $use_absolute_links\n";
238 # Determine the absolute directory, to be added to links in index.sgml
239 # if we need to use an absolute link.
240 # $file will be something like /opt/gnome/share/gtk-doc/html/gtk/index.sgml
241 # We want the part up to 'html' since the links in index.sgml include
244 if ($use_absolute_links) {
245 $file =~ /(.*\/)(.*?)\/index\.sgml/;
249 open (INDEXFILE, $file)
250 || die "Can't open $file: $!";
251 while (<INDEXFILE>) {
252 if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) {
253 #print "Found id: $1 href: $2\n";
254 $Links{$1} = "$dir$2";
261 sub FixCrossReferences {
264 opendir (HTMLDIR, $scan_dir)
265 || die "Can't open HTML directory $scan_dir: $!";
267 foreach $file (readdir (HTMLDIR)) {
268 if ($file eq '.' || $file eq '..') {
270 } elsif ($file =~ m/.html?$/) {
271 &FixHTMLFile ("$scan_dir/$file");
280 #print "Fixing file: $file\n";
282 open (HTMLFILE, $file)
283 || die "Can't open $file: $!";
285 my $entire_file = <HTMLFILE>;
288 if ("@HIGHLIGHT@" ne "") {
289 if ("@HIGHLIGHT@" =~ m%/vim$%) {
290 $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSourceVim($1,$2);%gse;
293 $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
295 # this just broke existing GTKDOCLINK tags
296 # <GTKDOCLINK HREF="GST-PAD-SINK:CAPS">GST_PAD_SINK</GTKDOCLINK>
297 $entire_file =~ s%\<GTKDOCLINK\s+HREF=\"(.*?)\"\>(.*?)\</GTKDOCLINK\>%\<GTKDOCLINK\ HREF=\"$1\"\>$2\</GTKDOCLINK\>%gs;
299 # from the highlighter we get all the functions marked up
300 # now we could turn them into GTKDOCLINK items
301 $entire_file =~ s%(<span class=\"function\">)(.*?)(</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
302 # we could also try the first item in stuff marked up as 'normal'
303 $entire_file =~ s%(<span class=\"normal\">\s*)(.+?)((\s+.+?)?\s*</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
306 my @lines = split(/\n/, $entire_file);
307 for (my $i=0; $i<$#lines; $i++) {
308 $lines[$i] =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($file,$i+1,$1,$2); %ge;
309 #if ($lines[$i] =~ m/GTKDOCLINK/) {
310 # print "make xref failed for line: ",$lines[$i], "\n";
313 $entire_file = join("\n",@lines);
315 open (NEWFILE, ">$file.new")
316 || die "Can't open $file: $!";
317 print NEWFILE $entire_file;
321 || die "Can't delete $file: $!";
322 rename ("$file.new", $file)
323 || die "Can't rename $file.new: $!";
327 my ($file, $line, $id, $text) = @_;
329 my $href = $Links{$id};
331 # this is a workaround for some inconsitency we have with CreateValidSGMLID
332 if (!$href && $id =~ m/:/) {
335 $href = $Links{$tid};
337 # poor mans plural support
338 if (!$href && $id =~ m/s$/) {
341 $href = $Links{$tid};
345 # if it is a link to same module, remove path to make it work
347 if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
350 #print " Fixing link: $id, $href, $text\n";
351 return "<a href=\"$href\">$text</a>";
354 #print " no link for: $id, $text\n";
356 # don't warn multiple times and also skip blacklisted (ctypes)
357 $warn = 0 if exists $NoLinks{$id};
358 # if it's a function, don't warn if it does not contain a "_"
359 # (transformed to "-")
360 # - gnome coding style would use '_'
361 # - will avoid wrong warnings for ansi c functions
362 $warn = 0 if ($text =~ m/ class=\"function\"/ && $id !~ m/-/);
363 # if it's a 'return value', don't warn (implicitly created link)
364 $warn = 0 if ($text =~ m/ class=\"returnvalue\"/);
365 # if it's a 'type', don't warn if it starts with lowercase
366 # - gnome coding style would use CamelCase
367 $warn = 0 if ($text =~ m/ class=\"type\"/ && ($id =~ m/^[a-z]/));
370 &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
379 my ($pre,$symbol,$post) = @_;
381 my $id=CreateValidSGMLID($symbol);
383 # these are implicitely created links in highlighed sources
384 # we don't want warnings for those if the links cannot be resolved.
387 #return "<span class=\"$type\"><GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK></span>";
388 return "$pre<GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK>$post";
392 sub HighlightSource {
393 my ($type, $source) = @_;
395 # chop of leading and trailing empty lines
396 $source =~ s/^\s*\n+//gs;
397 $source =~ s/[\s\n]+$//gs;
399 $source =~ m/^(\s*)/;
400 $source =~ s/^$1//gms;
401 # avoid double entity replacement
402 $source =~ s/</</g;
403 $source =~ s/>/>/g;
404 $source =~ s/&/&/g;
406 # write source to a temp file
407 # FIXME: use .c for now to hint the language to the highlighter
408 my $temp_source_file="$MODULE_DIR/_temp_src.$$.c";
409 open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
410 print NEWFILE $source;
413 #print" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file \n";
416 my $highlighted_source=`@HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file`;
417 if ("@HIGHLIGHT@" =~ m%/source-highlight$%) {
418 $highlighted_source =~ s%^<\!-- .*? -->%%gs;
419 $highlighted_source =~ s%<pre><tt>(.*?)</tt></pre>%$1%gs;
421 elsif ("@HIGHLIGHT@" =~ m%/highlight$%) {
422 # need to rewrite the stylesheet classes
423 $highlighted_source =~ s%<span class="gtkdoc com">%<span class="comment">%gs;
424 $highlighted_source =~ s%<span class="gtkdoc dir">%<span class="preproc">%gs;
425 $highlighted_source =~ s%<span class="gtkdoc kwd">%<span class="function">%gs;
426 $highlighted_source =~ s%<span class="gtkdoc kwa">%<span class="keyword">%gs;
427 $highlighted_source =~ s%<span class="gtkdoc line">%<span class="linenum">%gs;
428 $highlighted_source =~ s%<span class="gtkdoc num">%<span class="number">%gs;
429 $highlighted_source =~ s%<span class="gtkdoc str">%<span class="string">%gs;
430 $highlighted_source =~ s%<span class="gtkdoc sym">%<span class="symbol">%gs;
432 # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
435 unlink ($temp_source_file)
436 || die "Can't delete $temp_source_file: $!";
438 return &HighlightSourcePostprocess($type, $highlighted_source);
441 sub HighlightSourceVim {
442 my ($type, $source) = @_;
444 # chop of leading and trailing empty lines
445 $source =~ s/^[\s\n]+//gs;
446 $source =~ s/[\s\n]+$//gs;
447 # avoid double entity replacement
448 $source =~ s/</</g;
449 $source =~ s/>/>/g;
450 $source =~ s/&/&/g;
452 # write source to a temp file
453 my $temp_source_file="$MODULE_DIR/_temp_src.$$.h";
454 open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
455 print NEWFILE $source;
459 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";
461 my $highlighted_source;
464 open (NEWFILE, "<$temp_source_file.html");
465 $highlighted_source = <NEWFILE>;
468 $highlighted_source =~ s#.*<pre>\n##s;
469 $highlighted_source =~ s#</pre>.*##s;
471 # need to rewrite the stylesheet classes
472 # FIXME: Vim has somewhat different syntax groups
473 $highlighted_source =~ s%<span class="Comment">%<span class="comment">%gs;
474 $highlighted_source =~ s%<span class="PreProc">%<span class="preproc">%gs;
475 $highlighted_source =~ s%<span class="Statement">%<span class="keyword">%gs;
476 $highlighted_source =~ s%<span class="Identifier">%<span class="function">%gs;
477 $highlighted_source =~ s%<span class="Constant">%<span class="number">%gs;
478 $highlighted_source =~ s%<span class="Special">%<span class="symbol">%gs;
479 $highlighted_source =~ s%<span class="Type">%<span class="type">%gs;
482 unlink ($temp_source_file)
483 || die "Can't delete $temp_source_file: $!";
484 unlink ("$temp_source_file.html")
485 || die "Can't delete $temp_source_file.html: $!";
487 return &HighlightSourcePostprocess($type, $highlighted_source);
490 sub HighlightSourcePostprocess {
491 my ($type, $highlighted_source) = @_;
493 # chop of leading and trailing empty lines
494 $highlighted_source =~ s/^[\s\n]+//gs;
495 $highlighted_source =~ s/[\s\n]+$//gs;
497 # turn common urls in comments into links
498 $highlighted_source =~ s%<span class="url">(.*?)</span>%<span class="url"><a href="$1">$1</a></span>%gs;
500 # we do own line-numbering
502 my $line_count = () = $highlighted_source =~ /\n/gs;
503 for (my $i=1; $i < ($line_count+2); $i++) {
504 $source_lines.="$i\n";
506 $source_lines =~ s/\n\Z//;
510 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
513 <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
514 <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>