summaryrefslogtreecommitdiff
path: root/gtkdoc-fixxref.in
blob: 9831687982acaea60e2a490e8ace38a88b55fe8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!@PERL@ -w
# -*- cperl -*-
#
# gtk-doc - GTK DocBook documentation generator.
# Copyright (C) 1998  Damon Chaplin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

#############################################################################
# Script      : gtkdoc-fixxref
# Description : This fixes cross-references in the HTML documentation.
#############################################################################

use strict;
use bytes;
use Getopt::Long;

push @INC, '@PACKAGE_DATA_DIR@';
require "gtkdoc-common.pl";

# Options

# name of documentation module
my $MODULE;
my $MODULE_DIR;
my $HTML_DIR = "";
my @EXTRA_DIRS;
my $PRINT_VERSION;
my $PRINT_HELP;
my $SRC_LANG;

# This contains all the entities and their relative URLs.
my %Links;

# failing link targets we don't warn about even once
my %NoLinks = (
    'char' => 1,
    'double' => 1,
    'float' => 1,
    'int' => 1,
    'long' => 1,
    'main' => 1,
    'signed' => 1,
    'unsigned' => 1,
    'va-list' => 1,
    'void' => 1,
    'GBoxed' => 1,
    'GEnum' => 1,
    'GFlags' => 1,
    'GInterface' => 1
);


run() unless caller; # Run program unless loaded as a module


sub run {
    my %optctl = ('module' => \$MODULE,
                  'module-dir' => \$MODULE_DIR,
                  'html-dir' => \$HTML_DIR,
                  'extra-dir' => \@EXTRA_DIRS,
                  'version' => \$PRINT_VERSION,
                  'help' => \$PRINT_HELP,
                  'src-lang' => \$SRC_LANG);
    
    GetOptions(\%optctl, "module=s", "module-dir=s", "html-dir:s", "extra-dir=s@",
              "src-lang=s", "version", "help");
    
    if ($PRINT_VERSION) {
        print "@VERSION@\n";
        exit 0;
    }
    
    if ($PRINT_HELP) {
            print <<EOF;
gtkdoc-fixxref version @VERSION@ - fix cross references in html files

--module=MODULE_NAME    Name of the doc module being parsed
--module-dir=MODULE_DIR The directory which contains the generated HTML
--html-dir=HTML_DIR     The directory where gtk-doc generated documentation is
                        installed
--extra-dir=EXTRA_DIR   Directories to recursively scan for indices (index.sgml)
                        in addition to HTML_DIR
                        May be used more than once for multiple directories
--src-lang=SRC_LANG     Programing language used for syntax highlighting. The
                        available languages depend on the source source
                        highlighter you use.
--version               Print the version of this program
--help                  Print this help
EOF
        exit 0;
    }
    
    if (!$SRC_LANG) {
        $SRC_LANG="c"
    }
      
    my $path_prefix="";
    if ($HTML_DIR =~ m%(.*?)/share/gtk-doc/html%) {
        $path_prefix=$1;
        @TRACE@("Path prefix: $path_prefix");
    }
    
    if (!defined $MODULE_DIR) {
        $MODULE_DIR="$HTML_DIR/$MODULE";
    }
    
    my $dir;
    
    # We scan the directory containing GLib and any directories in GNOME2_PATH
    # first, but these will be overriden by any later scans.
    $dir = `pkg-config --variable=prefix glib-2.0`;
    $dir =~ s/\s+$//;
    $dir = $dir . "/share/gtk-doc/html";
    if (-d $dir) {
        # Some predefined link targets to get links into type hierarchies as these
        # have no targets. These are always absolute for now.
        $Links{'GBoxed'} = "$dir/gobject/gobject-Boxed-Types.html";
        $Links{'GEnum'} = "$dir/gobject/gobject-Enumeration-and-Flag-Types.html";
        $Links{'GFlags'} = "$dir/gobject/gobject-Enumeration-and-Flag-Types.html";
        $Links{'GInterface'} = "$dir/gobject/GTypeModule.html";
    
        if ($dir ne $HTML_DIR) {
            @TRACE@("Scanning GLib directory: $dir");
            if ($dir !~ m%^\Q$path_prefix\E/%) {
                &ScanIndices ($dir, 1);
            } else {
                &ScanIndices ($dir, 0);
            }
        }
    }
    
    if (defined ($ENV{"GNOME2_PATH"})) {
        foreach $dir (split (/:/, $ENV{"GNOME2_PATH"})) {
            $dir = $dir . "/share/gtk-doc/html";
            if (-d $dir && $dir ne $HTML_DIR) {
                @TRACE@("Scanning GNOME2_PATH directory: $dir");
                if ($dir !~ m%^\Q$path_prefix\E/%) {
                    &ScanIndices ($dir, 1);
                } else {
                    &ScanIndices ($dir, 0);
                }
            }
            # ubuntu started to compress this as index.sgml.gz :/
            # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
        }
    }
    
    @TRACE@("Scanning HTML_DIR directory: $HTML_DIR");
    &ScanIndices ($HTML_DIR, 0);
    @TRACE@("Scanning HTML_DIR directory: $MODULE_DIR");
    &ScanIndices ($MODULE_DIR, 0);
    
    # check all extra dirs, but skip already scanned dirs or subdirs of those
    foreach my $dir (@EXTRA_DIRS) {
        my $vdir;
        @TRACE@("Scanning EXTRA_DIR directory: $dir");
    
        # If the --extra-dir option is not relative and is not sharing the same
        # prefix as the target directory of the docs, we need to use absolute
        # directories for the links
        if ($dir !~m/^\.\./ &&  $dir !~ m%\Q$path_prefix\E/%) {
            &ScanIndices ($dir, 1);
        } else {
            &ScanIndices ($dir, 0);
        }
    }
    
    &ReadSections ();
    
    &FixCrossReferences ($MODULE_DIR);
}


sub ScanIndices {
    my ($scan_dir, $use_absolute_links) = @_;
    
    @TRACE@("Scanning source directory: $scan_dir absolute: $use_absolute_links");

    # This array holds any subdirectories found.
    my (@subdirs) = ();

    opendir (HTMLDIR, $scan_dir) || return;
    my $file;
    foreach $file (readdir (HTMLDIR)) {
        if ($file eq '.' || $file eq '..') {
            next;
        } elsif (-d "$scan_dir/$file") {
            push (@subdirs, $file);
        } elsif ($file eq "index.sgml") {
            &ScanIndex ("$scan_dir/$file", $use_absolute_links);
        }
        # ubuntu started to compress this as index.sgml.gz :/
        # https://bugs.launchpad.net/ubuntu/+source/gtk-doc/+bug/77138
    }
    closedir (HTMLDIR);

    # Now recursively scan the subdirectories.
    my $dir;
    foreach $dir (sort(@subdirs)) {
        &ScanIndices ("$scan_dir/$dir", $use_absolute_links);
    }
}


sub ScanIndex {
    my ($file, $use_absolute_links) = @_;

    # Determine the absolute directory, to be added to links in index.sgml
    # if we need to use an absolute link.
    # $file will be something like /opt/gnome/share/gtk-doc/html/gtk/index.sgml
    # We want the part up to 'html' since the links in index.sgml include
    # the rest.
    my $dir = "../";
    if ($use_absolute_links) {
        # For uninstalled index.sgml files we'd need to map the path to where it
        # will be installed to
        if ($file !~ /\.\//) {
          $file =~ /(.*\/)(.*?)\/index\.sgml/;
          $dir = $1;
        }
    }
    @TRACE@("Scanning index file=$file, absolute=$use_absolute_links, dir=$dir");

    open (INDEXFILE, $file)
        || die "Can't open $file: $!";
    while (<INDEXFILE>) {
        if (m/^<ANCHOR\s+id\s*=\s*"([^"]*)"\s+href\s*=\s*"([^"]*)"\s*>/) {
            @TRACE@("Found id: $1 href: $2");
            $Links{$1} = "$dir$2";
        }
    }
    close (INDEXFILE);
}


sub ReadSections {
    if (!defined($MODULE)) {
        return;
    }

    open (INPUT, "$MODULE-sections.txt")
            || die "Can't open $MODULE-sections.txt: $!";
    my $subsection = "";
    while (<INPUT>) {
        if (m/^#/) {
            next;

        } elsif (m/^<SECTION>/) {
            $subsection = "";
        } elsif (m/^<SUBSECTION\s*(.*)>/i) {
            $subsection = $1;
        } elsif (m/^<SUBSECTION>/) {
            next;
        } elsif (m/^<TITLE>(.*)<\/TITLE>/) {
            next;
        } elsif (m/^<FILE>(.*)<\/FILE>/) {
            next;
        } elsif (m/^<INCLUDE>(.*)<\/INCLUDE>/) {
            next;
        } elsif (m/^<\/SECTION>/) {
            next;
        } elsif (m/^(\S+)/) {
            my $symbol=$1;

            if ($subsection eq "Standard" || $subsection eq "Private") {
                $NoLinks{CreateValidSGMLID($symbol)} = 1;
            }
        }
    }
    close (INPUT);
}


sub FixCrossReferences {
    my ($scan_dir) = @_;

    opendir (HTMLDIR, $scan_dir)
        || die "Can't open HTML directory $scan_dir: $!";
    my $file;
    foreach $file (readdir (HTMLDIR)) {
        if ($file eq '.' || $file eq '..') {
            next;
        } elsif ($file =~ m/.html?$/) {
            &FixHTMLFile ("$scan_dir/$file");
        }
    }
    closedir (HTMLDIR);
}


sub FixHTMLFile {
    my ($file) = @_;
    @TRACE@("Fixing file: $file");

    open (HTMLFILE, $file)
        || die "Can't open $file: $!";
    undef $/;
    my $entire_file = <HTMLFILE>;
    close (HTMLFILE);

    if ("@HIGHLIGHT@" ne "") {
        # FIXME: ideally we'd pass a clue about the example language to the highligher
        # unfortunately the "language" attribute is not appearing in the html output
        # we could patch the customization to have <code class="xxx"> inside of <pre>
        if ("@HIGHLIGHT@" =~ m%/vim$%) {
            $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSourceVim($1,$2);%gse;
        }
        else {
            $entire_file =~ s%<div class=\"(example-contents|informalexample)\"><pre class=\"programlisting\">(.*?)</pre></div>%&HighlightSource($1,$2);%gse;
        }
        # this just broke existing GTKDOCLINK tags
        # &lt;GTKDOCLINK HREF=&quot;GST-PAD-SINK:CAPS&quot;&gt;GST_PAD_SINK&lt;/GTKDOCLINK&gt;
        $entire_file =~ s%\&lt;GTKDOCLINK\s+HREF=\&quot;(.*?)\&quot;\&gt;(.*?)\&lt;/GTKDOCLINK\&gt;%\<GTKDOCLINK\ HREF=\"$1\"\>$2\</GTKDOCLINK\>%gs;

        # from the highlighter we get all the functions marked up
        # now we could turn them into GTKDOCLINK items
        $entire_file =~ s%(<span class=\"function\">)(.*?)(</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
        # we could also try the first item in stuff marked up as 'normal'
        $entire_file =~ s%(<span class=\"normal\">\s*)(.+?)((\s+.+?)?\s*</span>)%&MakeGtkDocLink($1,$2,$3);%gse;
    }

    my @lines = split(/\n/, $entire_file);
    for (my $i=0; $i<$#lines; $i++) {
        $lines[$i] =~ s%<GTKDOCLINK\s+HREF="([^"]*)"\s*>(.*?)</GTKDOCLINK\s*>% &MakeXRef($file,$i+1,$1,$2); %ge;
        #if ($lines[$i] =~ m/GTKDOCLINK/) {
        #    print "make xref failed for line: ",$lines[$i], "\n";
        #}
    }
    $entire_file = join("\n",@lines);

    open (NEWFILE, ">$file.new")
        || die "Can't open $file: $!";
    print NEWFILE $entire_file;
    close (NEWFILE);

    unlink ($file)
        || die "Can't delete $file: $!";
    rename ("$file.new", $file)
        || die "Can't rename $file.new: $!";
}

sub MakeXRef {
    my ($file, $line, $id, $text) = @_;

    my $href = $Links{$id};

    # this is a workaround for some inconsistency we have with CreateValidSGMLID
    if (!$href && $id =~ m/:/) {
        my $tid = $id;
        $tid =~ s/:/--/g;
        $href = $Links{$tid};
    }
    # poor mans plural support
    if (!$href && $id =~ m/s$/) {
        my $tid = $id;
        $tid =~ s/s$//g;
        $href = $Links{$tid};
    }

    if ($href) {
        # if it is a link to same module, remove path to make it work
        # uninstalled
        if (defined($MODULE) && $href =~ m%^\.\./$MODULE/(.*)$%) {
            $href=$1;
            @TRACE@("  Fixing link to uninstalled doc: $id, $href, $text");
        } else {
            @TRACE@("  Fixing link: $id, $href, $text");
        }
        return "<a href=\"$href\">$text</a>";
    } else {
        my $warn = 1;
        @TRACE@("  no link for: $id, $text");

        # don't warn multiple times and also skip blacklisted (ctypes)
        $warn = 0 if exists $NoLinks{$id};
        # if it's a function, don't warn if it does not contain a "_"
        # (transformed to "-")
        # - gnome coding style would use '_'
        # - will avoid wrong warnings for ansi c functions
        $warn = 0 if ($text =~ m/ class=\"function\"/ && $id !~ m/-/);
        # if it's a 'return value', don't warn (implicitly created link)
        $warn = 0 if ($text =~ m/ class=\"returnvalue\"/);
        # if it's a 'type', don't warn if it starts with lowercase
        # - gnome coding style would use CamelCase
        $warn = 0 if ($text =~ m/ class=\"type\"/ && ($id =~ m/^[a-z]/));
        # don't warn for self links
        $warn = 0 if ($text eq $id);

        if ($warn == 1) {
          &LogWarning ($file, $line, "no link for: '$id' -> ($text).");
          $NoLinks{$id} = 1;
        }
        return $text;
    }
}


sub MakeGtkDocLink {
    my ($pre,$symbol,$post) = @_;

    my $id=CreateValidSGMLID($symbol);

    # these are implicitely created links in highlighed sources
    # we don't want warnings for those if the links cannot be resolved.
    $NoLinks{$id} = 1;

    #return "<span class=\"$type\"><GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK></span>";
    return "$pre<GTKDOCLINK HREF=\"$id\">$symbol</GTKDOCLINK>$post";
}


sub HighlightSource {
    my ($type, $source) = @_;

    # chop of leading and trailing empty lines
    $source =~ s/^\s*\n+//gs;
    $source =~ s/[\s\n]+$//gs;
    # cut common indent
    $source =~ m/^(\s*)/;
    $source =~ s/^$1//gms;
    # avoid double entity replacement
    $source =~ s/&lt;/</g;
    $source =~ s/&gt;/>/g;
    $source =~ s/&amp;/&/g;

    # write source to a temp file
    # FIXME: use .c for now to hint the language to the highlighter
    my $temp_source_file="$MODULE_DIR/_temp_src.$$.c";
    open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
    print NEWFILE $source;
    close (NEWFILE);

    @TRACE@(" running @HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file ");

    # format source
    my $highlighted_source=`@HIGHLIGHT@ @HIGHLIGHT_OPTIONS@$temp_source_file`;
    if ("@HIGHLIGHT@" =~ m%/source-highlight$%) {
        $highlighted_source =~ s%^<\!-- .*? -->%%gs;
        $highlighted_source =~ s%<pre><tt>(.*?)</tt></pre>%$1%gs;
    }
    elsif ("@HIGHLIGHT@" =~ m%/highlight$%) {
        # need to rewrite the stylesheet classes
        $highlighted_source =~ s%<span class="gtkdoc com">%<span class="comment">%gs;
        $highlighted_source =~ s%<span class="gtkdoc dir">%<span class="preproc">%gs;
        $highlighted_source =~ s%<span class="gtkdoc kwd">%<span class="function">%gs;
        $highlighted_source =~ s%<span class="gtkdoc kwa">%<span class="keyword">%gs;
        $highlighted_source =~ s%<span class="gtkdoc line">%<span class="linenum">%gs;
        $highlighted_source =~ s%<span class="gtkdoc num">%<span class="number">%gs;
        $highlighted_source =~ s%<span class="gtkdoc str">%<span class="string">%gs;
        $highlighted_source =~ s%<span class="gtkdoc sym">%<span class="symbol">%gs;
        # maybe also do
        # $highlighted_source =~ s%</span>(.+)<span%</span><span class="normal">$1</span><span%gs;
    }
    # remove temp file
    unlink ($temp_source_file)
        || die "Can't delete $temp_source_file: $!";

    return &HighlightSourcePostprocess($type, $highlighted_source);
}


sub HighlightSourceVim {
    my ($type, $source) = @_;

    # chop of leading and trailing empty lines
    $source =~ s/^\s*\n+//gs;
    $source =~ s/[\s\n]+$//gs;
    # cut common indent
    $source =~ m/^(\s*)/;
    $source =~ s/^$1//gms;
    # avoid double entity replacement
    $source =~ s/&lt;/</g;
    $source =~ s/&gt;/>/g;
    $source =~ s/&amp;/&/g;

    # write source to a temp file
    my $temp_source_file="$MODULE_DIR/_temp_src.$$.h";
    open (NEWFILE, ">$temp_source_file") || die "Can't open $temp_source_file: $!";
    print NEWFILE $source;
    close (NEWFILE);

    # format source
    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";

    my $highlighted_source;
    {
        local $/;
        open (NEWFILE, "<$temp_source_file.html");
        $highlighted_source = <NEWFILE>;
        close (NEWFILE);
    }
    $highlighted_source =~ s#.*<pre\b[^>]*>\n##s;
    $highlighted_source =~ s#</pre>.*##s;

    # need to rewrite the stylesheet classes
    # FIXME: Vim has somewhat different syntax groups
    $highlighted_source =~ s%<span class="Comment">%<span class="comment">%gs;
    $highlighted_source =~ s%<span class="PreProc">%<span class="preproc">%gs;
    $highlighted_source =~ s%<span class="Statement">%<span class="keyword">%gs;
    $highlighted_source =~ s%<span class="Identifier">%<span class="function">%gs;
    $highlighted_source =~ s%<span class="Constant">%<span class="number">%gs;
    $highlighted_source =~ s%<span class="Special">%<span class="symbol">%gs;
    $highlighted_source =~ s%<span class="Type">%<span class="type">%gs;

    # remove temp files
    unlink ($temp_source_file)
        || die "Can't delete $temp_source_file: $!";
    unlink ("$temp_source_file.html")
        || die "Can't delete $temp_source_file.html: $!";

    return &HighlightSourcePostprocess($type, $highlighted_source);
}


sub HighlightSourcePostprocess {
    my ($type, $highlighted_source) = @_;

    # chop of leading and trailing empty lines
    $highlighted_source =~ s/^[\s\n]+//gs;
    $highlighted_source =~ s/[\s\n]+$//gs;

    # turn common urls in comments into links
    $highlighted_source =~ s%<span class="url">(.*?)</span>%<span class="url"><a href="$1">$1</a></span>%gs;

    # we do own line-numbering
    my $source_lines="";
    my $line_count = () = $highlighted_source =~ /\n/gs;
    for (my $i=1; $i < ($line_count+2); $i++) {
        $source_lines.="$i\n";
    }
    $source_lines =~ s/\n\Z//;

    return <<END_OF_HTML
<div class="$type">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>$source_lines</pre></td>
        <td class="listing_code"><pre class="programlisting">$highlighted_source</pre></td>
      </tr>
    </tbody>
  </table>
</div>
END_OF_HTML
}