The R Project SVN R

Rev

Rev 43267 | Rev 47442 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 43267 Rev 46994
Line 18... Line 18...
18
# http://www.r-project.org/Licenses/
18
# http://www.r-project.org/Licenses/
19
 
19
 
20
use Getopt::Long;
20
use Getopt::Long;
21
use R::Utils;
21
use R::Utils;
22
 
22
 
23
my $revision = ' $Rev: 43267 $ ';
23
my $revision = ' $Rev: 46994 $ ';
24
my $version;
24
my $version;
25
my $name;
25
my $name;
26
 
26
 
27
$revision =~ / ([\d\.]*) /;
27
$revision =~ / ([\d\.]*) /;
28
$version = $1;
28
$version = $1;
Line 37... Line 37...
37
Options:
37
Options:
38
  -h, --help		print short help message and exit
38
  -h, --help		print short help message and exit
39
  -v, --version		print Sd2Rd version info and exit
39
  -v, --version		print Sd2Rd version info and exit
40
  -n			render examples non-executable by wrapping them
40
  -n			render examples non-executable by wrapping them
41
			into a \\dontrun{} environment
41
			into a \\dontrun{} environment
42
  -x			(S3 docs) interpret all single-quoted names 
42
  -x			(S3 docs) interpret all single-quoted names
43
                        as code names
43
			as code names
44
 
44
 
45
Email bug reports to <r-bugs\@r-project.org>.
45
Email bug reports to <r-bugs\@r-project.org>.
46
END
46
END
47
  exit 0; 
47
  exit 0;
48
}
48
}
49
 
49
 
50
@knownoptions = ("v|version", "h|help", "n", "x");
50
@knownoptions = ("v|version", "h|help", "n", "x");
51
GetOptions(@knownoptions) || &usage();
51
GetOptions(@knownoptions) || &usage();
52
&R_version($name, $version) if $opt_v;
52
&R_version($name, $version) if $opt_v;
Line 55... Line 55...
55
@lines = <>;
55
@lines = <>;
56
## peek at the first line of the file, then dispatch to
56
## peek at the first line of the file, then dispatch to
57
## S3-style (nroff) or S4-style (SGML spec).
57
## S3-style (nroff) or S4-style (SGML spec).
58
if(@lines[0] =~ /^<!doctype/) { doS4(); } else { doS3(); }
58
if(@lines[0] =~ /^<!doctype/) { doS4(); } else { doS3(); }
59
exit 0;
59
exit 0;
60
 
60
 
61
sub doS3 {
61
sub doS3 {
62
  $braceLevel = 0;
62
  $braceLevel = 0;
63
  $inReferences = 0;
63
  $inReferences = 0;
64
  $inVerbatim = 0;
64
  $inVerbatim = 0;
65
  $inSeeAlso = 0;
65
  $inSeeAlso = 0;
Line 75... Line 75...
75
    chomp;
75
    chomp;
76
    &substitute unless /^\./;
76
    &substitute unless /^\./;
77
    my @word = split;
77
    my @word = split;
78
 
78
 
79
    if (/^\s*$/) { &output("\n"); }
79
    if (/^\s*$/) { &output("\n"); }
80
    if (/^[^.]/) { 
80
    if (/^[^.]/) {
81
      if ($underlineNext) {
81
      if ($underlineNext) {
82
	$_ = "\\emph{" . $_ ."}";
82
	$_ = "\\emph{" . $_ ."}";
83
	$underlineNext = 0;
83
	$underlineNext = 0;
84
      }
84
      }
85
      &output($_);
85
      &output($_);
Line 131... Line 131...
131
    if (/^\.FN/) {
131
    if (/^\.FN/) {
132
      unless($fun) { $fun = $word[1]; }
132
      unless($fun) { $fun = $word[1]; }
133
      push(@aliases, $word[1]);
133
      push(@aliases, $word[1]);
134
    }
134
    }
135
    if (/^\.(IP|PP)/) { &output("\n"); }
135
    if (/^\.(IP|PP)/) { &output("\n"); }
136
    if (/^\.KW/) { 
136
    if (/^\.KW/) {
137
      if ($braceLevel > 0) {
137
      if ($braceLevel > 0) {
138
	&section(0, "");
138
	&section(0, "");
139
	$braceLevel = 0;
139
	$braceLevel = 0;
140
      }
140
      }
141
      if ($word[1] =~ /sysdata/) {
141
      if ($word[1] =~ /sysdata/) {
Line 144... Line 144...
144
	&output("\\keyword\{$word[1]\}");
144
	&output("\\keyword\{$word[1]\}");
145
      }
145
      }
146
    }
146
    }
147
    if (/^\.RC/) {
147
    if (/^\.RC/) {
148
      if ($needVal) {
148
      if ($needVal) {
149
	$needVal = 0;	    
149
	$needVal = 0;
150
	&section(0, "\\value\{\n$output\n");
150
	&section(0, "\\value\{\n$output\n");
151
	$doprint = 1;
151
	$doprint = 1;
152
      }
152
      }
153
      &section(1, "\\item\{" . join(" ", @word[1..$#word]) . "\}\{");
153
      &section(1, "\\item\{" . join(" ", @word[1..$#word]) . "\}\{");
154
    }
154
    }
Line 278... Line 278...
278
  } elsif (!$inVerbatim) {
278
  } elsif (!$inVerbatim) {
279
    if(s/\`([^\']*)$/\\code\{$1/) {
279
    if(s/\`([^\']*)$/\\code\{$1/) {
280
      $inCode = 1;
280
      $inCode = 1;
281
    }
281
    }
282
    s/\`([^\']*)\'$/\\code{$1}/g;
282
    s/\`([^\']*)\'$/\\code{$1}/g;
283
    s/\`([^\']*)\'([^\'])/\\code{$1}$2/g;  
283
    s/\`([^\']*)\'([^\'])/\\code{$1}$2/g;
284
  }
284
  }
285
  if ($inReferences) {
285
  if ($inReferences) {
286
    s/([0-9])-([0-9])/$1--$2/g;
286
    s/([0-9])-([0-9])/$1--$2/g;
287
  }
287
  }
288
}
288
}
Line 293... Line 293...
293
  print "\}" x $n, "\n" if ($n > 0);
293
  print "\}" x $n, "\n" if ($n > 0);
294
  if ($needVal) {
294
  if ($needVal) {
295
    print("\\value\{\n$output\n\}\n");
295
    print("\\value\{\n$output\n\}\n");
296
    $needVal = 0;
296
    $needVal = 0;
297
  }
297
  }
298
  print("$text\n") if $text;    
298
  print("$text\n") if $text;
299
  $braceLevel = $level + 1;
299
  $braceLevel = $level + 1;
300
  $inReferences = 0;
300
  $inReferences = 0;
301
  $inVerbatim = 0;
301
  $inVerbatim = 0;
302
  $inSeeAlso = 0;
302
  $inSeeAlso = 0;
303
  $doprint = 1;    
303
  $doprint = 1;
304
}
304
}
305
 
305
 
306
sub paragraph {
306
sub paragraph {
307
  my($name) = @_;
307
  my($name) = @_;
308
  &output("\n\\bold\{$name.\} ");
308
  &output("\n\\bold\{$name.\} ");
309
}
309
}
310
    
310
 
311
sub output {
311
sub output {
312
  my($text) = @_;
312
  my($text) = @_;
313
  if ($doprint) {
313
  if ($doprint) {
314
    print("$text\n");
314
    print("$text\n");
315
  } elsif ($output) {
315
  } elsif ($output) {
Line 367... Line 367...
367
{
367
{
368
    my $text = $_[0];
368
    my $text = $_[0];
369
    my $body;
369
    my $body;
370
    my $rest;
370
    my $rest;
371
 
371
 
372
    die "not at beginning of a group in |$text|" unless 
372
    die "not at beginning of a group in |$text|" unless
373
	$text =~ /^\s*<s-(.+?)>/s;
373
	$text =~ /^\s*<s-(.+?)>/s;
374
    my $type = $1;
374
    my $type = $1;
375
    my $tt = $type;
375
    my $tt = $type;
376
    $tt =~ s/([a-zA-Z-]+).*/$1/;
376
    $tt =~ s/([a-zA-Z-]+).*/$1/;
377
    if($text =~ /^\s*<s-\Q$type\E>(.*?)<\/s-$tt>(.*)/s) {
377
    if($text =~ /^\s*<s-\Q$type\E>(.*?)<\/s-$tt>(.*)/s) {
378
	$body = $1;
378
	$body = $1;
379
	$rest = $2;
379
	$rest = $2;
380
    } else { 
380
    } else {
381
	warn "no match for 's-$tt'";
381
	warn "no match for 's-$tt'";
382
	$text =~ /^\s*<s-\Q$type\E>\s*(.*)/s;
382
	$text =~ /^\s*<s-\Q$type\E>\s*(.*)/s;
383
	$body = $1;
383
	$body = $1;
384
	$rest = "";
384
	$rest = "";
385
    }
385
    }
Line 403... Line 403...
403
 
403
 
404
    } elsif ($type eq "title") {
404
    } elsif ($type eq "title") {
405
	print "\\title{\n  $text\n}\n";
405
	print "\\title{\n  $text\n}\n";
406
 
406
 
407
    } elsif ($type eq "description") {
407
    } elsif ($type eq "description") {
408
	print "\\description{\n", sub4($text), "\n}\n";	
408
	print "\\description{\n", sub4($text), "\n}\n";
409
 
409
 
410
    } elsif ($type eq "usage") {
410
    } elsif ($type eq "usage") {
411
	## new-style usage is not catered for here: no examples seen
411
	## new-style usage is not catered for here: no examples seen
412
	## it will be passed through verbatim.
412
	## it will be passed through verbatim.
413
	if ($text =~ /^\s*<s-old-style-usage>\s*(.*?)\s*<\/s-old-style-usage>/s) {
413
	if ($text =~ /^\s*<s-old-style-usage>\s*(.*?)\s*<\/s-old-style-usage>/s) {
414
	    $text = $1;
414
	    $text = $1;
415
	}
415
	}
416
	print "\\usage{\n$text\n}\n";	
416
	print "\\usage{\n$text\n}\n";
417
 
417
 
418
    } elsif ($type eq "args" || $type eq "args-optional" 
418
    } elsif ($type eq "args" || $type eq "args-optional"
419
	     || $type eq "args-required" ) {
419
	     || $type eq "args-required" ) {
420
	if(!$InArgs) {
420
	if(!$InArgs) {
421
	    print "\\arguments{\n";
421
	    print "\\arguments{\n";
422
	    $InArgs = 1;
422
	    $InArgs = 1;
423
	}
423
	}
Line 450... Line 450...
450
	    }
450
	    }
451
	}
451
	}
452
	print "}\n";
452
	print "}\n";
453
 
453
 
454
    } elsif ($type eq "details") {
454
    } elsif ($type eq "details") {
455
	print "\\details{\n", sub4($text), "\n}\n";	
455
	print "\\details{\n", sub4($text), "\n}\n";
456
 
456
 
457
    } elsif ($type eq "see") {
457
    } elsif ($type eq "see") {
458
 	print "\\seealso{\n", makelinks($text), "\n}\n";	
458
	print "\\seealso{\n", makelinks($text), "\n}\n";
459
 
459
 
460
    } elsif ($type eq "examples") {
460
    } elsif ($type eq "examples") {
461
	print "\\examples{\n";
461
	print "\\examples{\n";
462
	print "\\dontrun{\n" if $opt_n;
462
	print "\\dontrun{\n" if $opt_n;
463
	process_sub_groups($text, "example");
463
	process_sub_groups($text, "example");
464
	print "}\n" if $opt_n;
464
	print "}\n" if $opt_n;
465
	print "}\n";
465
	print "}\n";
466
 
466
 
467
    } elsif ($type eq "note" || $type eq "notes") {
467
    } elsif ($type eq "note" || $type eq "notes") {
468
	print "\\note{\n", sub4($text), "\n}\n";	
468
	print "\\note{\n", sub4($text), "\n}\n";
469
 
469
 
470
    } elsif ($type eq "bugs") {
470
    } elsif ($type eq "bugs") {
471
 	print "\\section{Bugs}{\n", sub4($text), "\n}\n";	
471
	print "\\section{Bugs}{\n", sub4($text), "\n}\n";
472
 
472
 
473
    } elsif ($type eq "references") {
473
    } elsif ($type eq "references") {
474
	print "\\references{\n", inref(sub4($text)), "\n}\n";	
474
	print "\\references{\n", inref(sub4($text)), "\n}\n";
475
 
475
 
476
    } elsif ($type =~ /^section\s+name\s*=\s*(.*)/) {
476
    } elsif ($type =~ /^section\s+name\s*=\s*(.*)/) {
477
	my $name = $1;
477
	my $name = $1;
478
	$name =~ s/\s*\"(.*?)\"/$1/o;
478
	$name =~ s/\s*\"(.*?)\"/$1/o;
479
	$name =~ s/^\s*//o;
479
	$name =~ s/^\s*//o;
480
	if ($name =~ /^reference$/io) {
480
	if ($name =~ /^reference$/io) {
481
	    print "\\references{\n", inref(sub4($text)), "\n}\n";	
481
	    print "\\references{\n", inref(sub4($text)), "\n}\n";
482
	} elsif ($name =~ /^source$/io) {
482
	} elsif ($name =~ /^source$/io) {
483
	    print "\\source{\n", inref(sub4($text)), "\n}\n";	
483
	    print "\\source{\n", inref(sub4($text)), "\n}\n";
484
	} elsif ($name =~ /^note$/io) {
484
	} elsif ($name =~ /^note$/io) {
485
	    print "\\note{\n", sub4($text), "\n}\n";	
485
	    print "\\note{\n", sub4($text), "\n}\n";
486
	} elsif ($name =~ /^summary$/io) {
486
	} elsif ($name =~ /^summary$/io) {
487
	    print "\\description{\n", sub4($text), "\n}\n";	
487
	    print "\\description{\n", sub4($text), "\n}\n";
488
	} elsif ($name =~ /^data description$/io) {
488
	} elsif ($name =~ /^data description$/io) {
489
	    print "\\usage{\ndata($fun)\n}\n";
489
	    print "\\usage{\ndata($fun)\n}\n";
490
	    print "\\format{\n", sub4($text), "\n}\n";	
490
	    print "\\format{\n", sub4($text), "\n}\n";
491
	} else {
491
	} else {
492
	    $name = lc($name);
492
	    $name = lc($name);
493
	    $name =~ s/^([a-z])/\U$1/;
493
	    $name =~ s/^([a-z])/\U$1/;
494
	    print "\\section{$name}{\n", sub4($text), "\n}\n";	
494
	    print "\\section{$name}{\n", sub4($text), "\n}\n";
495
	}
495
	}
496
 
496
 
497
    } elsif ($type eq "docclass") {
497
    } elsif ($type eq "docclass") {
498
	print "% docclass is $text\n";
498
	print "% docclass is $text\n";
499
 
499
 
500
    } elsif ($type eq "warnings") {
500
    } elsif ($type eq "warnings") {
501
	process_sub_groups($text, "warning");
501
	process_sub_groups($text, "warning");
502
 
502
 
503
    } elsif ($type eq "warning") {
503
    } elsif ($type eq "warning") {
504
	print "\\section{Warning}{\n", sub4($text), "\n}\n";	
504
	print "\\section{Warning}{\n", sub4($text), "\n}\n";
505
 
505
 
506
    } elsif ($type eq "background") {
506
    } elsif ($type eq "background") {
507
	print "\\section{Background}{\n", sub4($text), "\n}\n";	
507
	print "\\section{Background}{\n", sub4($text), "\n}\n";
508
 
508
 
509
    } elsif ($type eq "side-effects") {
509
    } elsif ($type eq "side-effects") {
510
	print "\\section{Side Effects}{\n", sub4($text), "\n}\n";	
510
	print "\\section{Side Effects}{\n", sub4($text), "\n}\n";
511
 
511
 
512
    } elsif ($type eq "author") {
512
    } elsif ($type eq "author") {
513
	print "\\author{\n", sub4($text), "\n}\n";	
513
	print "\\author{\n", sub4($text), "\n}\n";
514
 
514
 
515
    } elsif ($type eq "keywords") {
515
    } elsif ($type eq "keywords") {
516
	process_sub_groups($text, "keyword");
516
	process_sub_groups($text, "keyword");
517
 
517
 
518
    } else {
518
    } else {
Line 520... Line 520...
520
	print "%type:\n%$text\n";
520
	print "%type:\n%$text\n";
521
    }
521
    }
522
}
522
}
523
 
523
 
524
sub process_sub_groups {
524
sub process_sub_groups {
525
  
525
 
526
    my $text = $_[0];
526
    my $text = $_[0];
527
    my $topic = $_[1];
527
    my $topic = $_[1];
528
 
528
 
529
    while(length($text) > 0) {
529
    while(length($text) > 0) {
530
	($type, $body, $text) = get_group($text);
530
	($type, $body, $text) = get_group($text);
Line 532... Line 532...
532
	process_sub_group($type, $body);
532
	process_sub_group($type, $body);
533
    }
533
    }
534
}
534
}
535
 
535
 
536
sub process_sub_group {
536
sub process_sub_group {
537
  
537
 
538
    my $topic = $_[0];
538
    my $topic = $_[0];
539
    my $text = $_[1];
539
    my $text = $_[1];
540
 
540
 
541
    $text =~ s/^\n*//;
541
    $text =~ s/^\n*//;
542
    $text =~ s/\n*$//;
542
    $text =~ s/\n*$//;
Line 547... Line 547...
547
	    $nalias = 1;
547
	    $nalias = 1;
548
	    $fun = $text;
548
	    $fun = $text;
549
	}
549
	}
550
	print "\\alias{$text}\n";
550
	print "\\alias{$text}\n";
551
    } elsif ($type eq "warning") {
551
    } elsif ($type eq "warning") {
552
	print "\\section{Warning}{\n", sub4($text), "\n}\n";	
552
	print "\\section{Warning}{\n", sub4($text), "\n}\n";
553
    } elsif ($type eq "keyword") {
553
    } elsif ($type eq "keyword") {
554
	print "\\keyword{$text}\n";
554
	print "\\keyword{$text}\n";
555
    } elsif ($type =~ /^example/) {
555
    } elsif ($type =~ /^example/) {
556
	if ($nexample++ > 0) { print "\n"; }
556
	if ($nexample++ > 0) { print "\n"; }
557
	print verbsub($text), "\n";
557
	print verbsub($text), "\n";
Line 559... Line 559...
559
	$type =~ /^arg\s+name\s*=\s*(.*)/;
559
	$type =~ /^arg\s+name\s*=\s*(.*)/;
560
	my $name = $1;
560
	my $name = $1;
561
	$name =~ s/\s*\"(.*?)\"/$1/o;
561
	$name =~ s/\s*\"(.*?)\"/$1/o;
562
	$name =~ s/\s*(\w*)\s*/$1/o;
562
	$name =~ s/\s*(\w*)\s*/$1/o;
563
	$name =~ s/\.\.\./\\dots/;
563
	$name =~ s/\.\.\./\\dots/;
564
	print "\\item{$name}{\n", sub4($text), "\n}\n";	
564
	print "\\item{$name}{\n", sub4($text), "\n}\n";
565
    } else {
565
    } else {
566
	warn "unknown SGML entity '$type'";
566
	warn "unknown SGML entity '$type'";
567
	print "%type:\n%$text\n";
567
	print "%type:\n%$text\n";
568
    }
568
    }
569
}
569
}