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 if (defined($MODULE)) {
172 open (INPUT, "$MODULE-sections.txt")
173 || die "Can't open $MODULE-sections.txt: $!";
179 } elsif (m/^<SECTION>/) {
181 } elsif (m/^<SUBSECTION\s*(.*)>/i) {
183 } elsif (m/^<SUBSECTION>/) {
185 } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
187 } elsif (m/^<FILE>(.*)<\/FILE>/) {
189 } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
191 } elsif (m/^<\/SECTION>/) {
193 } elsif (m/^(\S+)/) {
194 my $symbol=CreateValidSGMLID($1);
196 if ($subsection eq "Standard" || $subsection eq "Private") {
197 $NoLinks{$symbol} = 1;
204 &FixCrossReferences ($MODULE_DIR);
207 my ($scan_dir, $use_absolute_links) = @_;
209 #print "Scanning source directory: $scan_dir absolute: $use_absolute_links\n";
211 # This array holds any subdirectories found.
214 opendir (HTMLDIR, $scan_dir) || return;
216 foreach $file (readdir (HTMLDIR)) {
217 if ($file eq '.' || $file eq '..') {
219 } elsif (-d "$scan_dir/$file") {
220 push (@subdirs, $file);
221 } elsif ($file eq "index.sgml") {
222 &ScanIndex ("$scan_dir/$file", $use_absolute_links);
224 # ubuntu started to compress this as index.sgml.gz :/
225 # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
229 # Now recursively scan the subdirectories.
231 foreach $dir (@subdirs) {
232 &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
237 my ($file, $use_absolute_links) = @_;
238 #print "Scanning index file: $file absolute: $use_absolute_links\n";
240 # Determine the absolute directory, to be added to links in index.sgml
241 # if we need to use an absolute link.
242 # $file will be something like /opt/gnome/share/gtk-doc/html/gtk/index.sgml
243 # We want the part up to 'html' since the links in index.sgml include
246 if ($use_absolute_links) {
247 $file =~ /(.*\/)(.*?)\/index\.sgml/;
251 open (INDEXFILE, $file)
252 || die "Can't open $file: $!";
253 while (<INDEXFILE>) {
254 if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) {
255 #print "Found id: $1 href: $2\n";
256 $Links{$1} = "$dir$2";
263 sub FixCrossReferences {
266 opendir (HTMLDIR, $scan_dir)
267 || die "Can't open HTML directory $scan_dir: $!";
269 foreach $file (readdir (HTMLDIR)) {
270 if ($file eq '.' || $file eq '..') {
272 } elsif ($file =~ m/.html?$/) {
273 &FixHTMLFile ("$scan_dir/$file");
282 #print "Fixing file: $file\n";
284 open (HTMLFILE, $file)
285 || die "Can't open $file: $!";
287 my $entire_file = <HTMLFILE>;
290 if ("@HIGHLIGHT@" ne "") {
291 if ("@HIGHLIGHT@" =~ m%/vim$%) {
292 $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSourceVim($1,$2);%gse;
295 $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
297 # this just broke existing GTKDOCLINK tags
298 # <GTKDOCLINK HREF="GST-PAD-SINK:CAPS">GST_PAD_SINK</GTKDOCLINK>
299 $entire_file =~ s%\<GTKDOCLINK\s+HREF=\"(.*?)\"\>(.*?)\</GTKDOCLINK\>%\<GTKDOCLINK\ HREF=\"$1\"\>$2\</GTKDOCLINK\>%gs;
301 # from the highlighter we get all the functions marked up
302 # now we could turn them into GTKDOCLINK items
303 $entire_file =~ s%(<span class=\"function\">)(.*?)(</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
304 # we could also try the first item in stuff marked up as 'normal'
305 $entire_file =~ s%(<span class=\"normal\">\s*)(.+?)((\s+.+?)?\s*</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
308 my @lines = split(/\n/, $entire_file);
309 for (my $i=0; $i<$#lines; $i++) {
310 $lines[$i] =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($file,$i+1,$1,$2); %ge;
311 #if ($lines[$i] =~ m/GTKDOCLINK/) {
312 # print "make xref failed for line: ",$lines[$i], "\n";
315 $entire_file = join("\n",@lines);
317 open (NEWFILE, ">$file.new")
318 || die "Can't open $file: $!";
319 print NEWFILE $entire_file;
323 || die "Can't delete $file: $!";
324 rename ("$file.new", $file)
325 || die "Can't rename $file.new: $!";
329 my ($file, $line, $id, $text) = @_;
331 my $href = $Links{$id};
333 # this is a workaround for some inconsitency we have with CreateValidSGMLID
334 if (!$href && $id =~ m/:/) {
337 $href = $Links{$tid};
339 # poor mans plural support
340 if (!$href && $id =~ m/s$/) {
343 $href = $Links{$tid};
347 # if it is a link to same module, remove path to make it work
349 if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
352 #print " Fixing link: $id, $href, $text\n";
353 return "<a href=\"$href\">$text</a>";
356 #print " no link for: $id, $text\n";
358 # don't warn multiple times and also skip blacklisted (ctypes)
359 $warn = 0 if exists $NoLinks{$id};
360 # if it's a function, don't warn if it does not contain a "_"
361 # (transformed to "-")
362 # - gnome coding style would use '_'
363 # - will avoid wrong warnings for ansi c functions
364 $warn = 0 if ($text =~ m/ class=\"function\"/ && $id !~ m/-/);
365 # if it's a 'return value', don't warn (implicitly created link)
366 $warn = 0 if ($text =~ m/ class=\"returnvalue\"/);
367 # if it's a 'type', don't warn if it starts with lowercase
368 # - gnome coding style would use CamelCase
369 $warn = 0 if ($text =~ m/ class=\"type\"/ && ($id =~ m/^[a-z]/));
372 &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
381 my ($pre,$symbol,$post) = @_;
383 my $id=CreateValidSGMLID($symbol);
385 # these are implicitely created links in highlighed sources
386 # we don't want warnings for those if the links cannot be resolved.
389 #return "<span class=\"$type\"><GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK></span>";
390 return "$pre<GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK>$post";
394 sub HighlightSource {
395 my ($type, $source) = @_;
397 # chop of leading and trailing empty lines
398 $source =~ s/^\s*\n+//gs;
399 $source =~ s/[\s\n]+$//gs;
401 $source =~ m/^(\s*)/;
402 $source =~ s/^$1//gms;
403 # avoid double entity replacement
404 $source =~ s/</</g;
405 $source =~ s/>/>/g;
406 $source =~ s/&/&/g;
408 # write source to a temp file
409 # FIXME: use .c for now to hint the language to the highlighter
410 my $temp_source_file="$MODULE_DIR/_temp_src.$$.c";
411 open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
412 print NEWFILE $source;
415 #print" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file \n";
418 my $highlighted_source=`@HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file`;
419 if ("@HIGHLIGHT@" =~ m%/source-highlight$%) {
420 $highlighted_source =~ s%^<\!-- .*? -->%%gs;
421 $highlighted_source =~ s%<pre><tt>(.*?)</tt></pre>%$1%gs;
423 elsif ("@HIGHLIGHT@" =~ m%/highlight$%) {
424 # need to rewrite the stylesheet classes
425 $highlighted_source =~ s%<span class="gtkdoc com">%<span class="comment">%gs;
426 $highlighted_source =~ s%<span class="gtkdoc dir">%<span class="preproc">%gs;
427 $highlighted_source =~ s%<span class="gtkdoc kwd">%<span class="function">%gs;
428 $highlighted_source =~ s%<span class="gtkdoc kwa">%<span class="keyword">%gs;
429 $highlighted_source =~ s%<span class="gtkdoc line">%<span class="linenum">%gs;
430 $highlighted_source =~ s%<span class="gtkdoc num">%<span class="number">%gs;
431 $highlighted_source =~ s%<span class="gtkdoc str">%<span class="string">%gs;
432 $highlighted_source =~ s%<span class="gtkdoc sym">%<span class="symbol">%gs;
434 # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
437 unlink ($temp_source_file)
438 || die "Can't delete $temp_source_file: $!";
440 return &HighlightSourcePostprocess($type, $highlighted_source);
443 sub HighlightSourceVim {
444 my ($type, $source) = @_;
446 # chop of leading and trailing empty lines
447 $source =~ s/^[\s\n]+//gs;
448 $source =~ s/[\s\n]+$//gs;
449 # avoid double entity replacement
450 $source =~ s/</</g;
451 $source =~ s/>/>/g;
452 $source =~ s/&/&/g;
454 # write source to a temp file
455 my $temp_source_file="$MODULE_DIR/_temp_src.$$.h";
456 open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
457 print NEWFILE $source;
461 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";
463 my $highlighted_source;
466 open (NEWFILE, "<$temp_source_file.html");
467 $highlighted_source = <NEWFILE>;
470 $highlighted_source =~ s#.*<pre>\n##s;
471 $highlighted_source =~ s#</pre>.*##s;
473 # need to rewrite the stylesheet classes
474 # FIXME: Vim has somewhat different syntax groups
475 $highlighted_source =~ s%<span class="Comment">%<span class="comment">%gs;
476 $highlighted_source =~ s%<span class="PreProc">%<span class="preproc">%gs;
477 $highlighted_source =~ s%<span class="Statement">%<span class="keyword">%gs;
478 $highlighted_source =~ s%<span class="Identifier">%<span class="function">%gs;
479 $highlighted_source =~ s%<span class="Constant">%<span class="number">%gs;
480 $highlighted_source =~ s%<span class="Special">%<span class="symbol">%gs;
481 $highlighted_source =~ s%<span class="Type">%<span class="type">%gs;
484 unlink ($temp_source_file)
485 || die "Can't delete $temp_source_file: $!";
486 unlink ("$temp_source_file.html")
487 || die "Can't delete $temp_source_file.html: $!";
489 return &HighlightSourcePostprocess($type, $highlighted_source);
492 sub HighlightSourcePostprocess {
493 my ($type, $highlighted_source) = @_;
495 # chop of leading and trailing empty lines
496 $highlighted_source =~ s/^[\s\n]+//gs;
497 $highlighted_source =~ s/[\s\n]+$//gs;
499 # turn common urls in comments into links
500 $highlighted_source =~ s%<span class="url">(.*?)</span>%<span class="url"><a href="$1">$1</a></span>%gs;
502 # we do own line-numbering
504 my $line_count = () = $highlighted_source =~ /\n/gs;
505 for (my $i=1; $i < ($line_count+2); $i++) {
506 $source_lines.="$i\n";
508 $source_lines =~ s/\n\Z//;
512 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
515 <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
516 <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>