<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">## $Id: Rdoc.pm,v 1.3 1998/04/16 15:39:47 bates Exp $

## R documentation objects created from a file.

## Copyright (C) 1998 Douglas M. Bates &lt;bates@stat.wisc.edu&gt;

## This file 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, or (at your option)
## any later version.

## This file 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.

## A copy of the GNU General Public License is available via WWW at
## http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
## writing to the Free Software Foundation, Inc., 59 Temple Place, 
## Suite 330, Boston, MA  02111-1307  USA

## Send any bug reports to bates@stat.wisc.edu

package Rdoc;

=head1 NAME

 Rdoc - class for contents of a documentation file for R

=head1 SYNOPSIS

  use Rdoc;
  $rd = Rdoc-&gt;new( "foo.Rd" );

  @keys = keys( %{ $rd } );     # returns an array of the sections in the file
  @ordered = @{ $rd-&gt;{ '.order' } }; # sections in order seen
  $title = $rd-&gt;{ 'title' };         # returns the title section
  @aliases = @{ $rd-&gt;{ 'alias' } };  # aliases are an array
  @keywords = @{ $rd-&gt;{'keyword'}};  # as are keywords
  %sections = %{ $rd-&gt;{'section'}};  # sections are a hash

=head1 DESCRIPTION

The Rdoc class takes a file of R documentation in .Rd format, strips
the comments, and parses the rest of the file into separate sections.
The object itself is a hash.  The different sections are entries in
the hash.

=cut

use strict;
use Carp;
use FileHandle;
use Text::DelimMatch;
use File::Basename;

sub new {
    my ($class, $file) = @_;
    $class = ref($class) || $class;
    my $fh = new FileHandle "&lt; $file" or croak "open($file): $!\n";
    my @lines;
    my $escapedNewline = 0;
    my $lastEscNL = 0;
    while (&lt;$fh&gt;) {
	next if /^\s*\%/o;	# eliminate complete comment lines
	chomp;			# strip the newline
	s/\\\%/\007/og;		# substitute escaped `%' characters 
	$escapedNewline = s/\%$//o;
	s/\s*\%.*$//o;		# strip comment endings
	s/\007/%/og;		# re-insert escaped `%' characters w/o the escape
	s/^\s+//o;		# strip leading white space
	if ( $lastEscNL ) {
	  $lines[ $#lines ] .= $_;
	} else {
	  push( @lines, $_ );
	}
	$lastEscNL = $escapedNewline;
    }
    $fh-&gt;close;

    my $mc = new Text::DelimMatch "\\{", "\\}", '', "\\", '';
    my ($prefix, $match) = $mc-&gt;match( join( "\n", @lines ) );
    my $self = {};
    $self-&gt;{ '.basename' } = basename( $file, ".Rd" );
    $self-&gt;{ 'section' } = {};
    $self-&gt;{ 'alias' } = [];
    my $seenalias = 0;
    $self-&gt;{ 'keyword' } = [];
    my $seenkeyword = 0;
    $self-&gt;{ '.order' } = [];
    while ( $match ) {
      croak "Malformed R documentation file $file\n"
	unless ($prefix =~ /\s*\\(\w+)\s*$/o);
      my $key = $1;
      $match =~ s/\s*\}$//o;	# strip trailing white space and delimiter
      $match =~ s/^\{\s*//o;	# strip leading white space and delimiter
      if ($key =~ /section/i) { # swallow the section
	my $secname = $match;
	($prefix, $match) = $mc-&gt;match();
	$match =~ s/\s*\}$//o;
	$match =~ s/^\{\s*//o;
	($self-&gt;{ 'section' })-&gt;{ $secname } = $match;
	push( @{ $self-&gt;{'.order'} }, 'section:' . $secname );
      } elsif ($key =~ /alias/io) { # add to the alias array
	push( @{ $self-&gt;{'alias'} }, $match );
	push( @{ $self-&gt;{'.order'} }, 'alias' ) unless $seenalias++;
      } elsif ($key =~ /keyword/io) { # add to the keyword array
	push( @{ $self-&gt;{'keyword'} }, $match );
	push( @{ $self-&gt;{'.order'} }, 'keyword' ) unless $seenkeyword++;
      } else {
	$self-&gt;{ $key } = $match;
	push( @{ $self-&gt;{'.order'} }, $key );
      }
      ($prefix, $match ) = $mc-&gt;match();
    }
    delete $self-&gt;{'keyword'} unless $seenkeyword;
    delete $self-&gt;{'alias'} unless $seenalias;
    delete $self-&gt;{'section'} unless scalar( keys( %{ $self-&gt;{'section'} } ) ) &gt; 0;
    bless $self, $class;
}

sub arguments {
    my $self = shift;
    my @args;
    my $mc = new Text::DelimMatch "\\{", "\\}", '', "\\", '';
    my ($prefix, $match) = $mc-&gt;match( $self-&gt;{'arguments'} );
    while ( $match ) {
	croak "Malformed R documentation file.\n"
	    unless ($prefix =~ /\s*\\item\s*$/o);
			      # strip leading and trailing white space
	$match =~ s/(^\{\s*)|(\s*\}$)//og;
	push @args, $match;
	($prefix, $match) = $mc-&gt;match();
	$match =~ s/(^\{\s*)|(\s*\}$)//og; 
	push @args, $match;
	($prefix, $match) = $mc-&gt;match();
    }
    return( \@args );
}

sub transform {		# transform internal tags to output forms
  my $str = shift;
  return $str unless $str =~ /[^\\]\{/o;
  my $trTpt = shift;
  my $out;
  my $mc = new Text::DelimMatch "\\{", "\\}", '', "\\", '';
  my ( $prefix, $match, $remainder ) = $mc-&gt;match( $str );
  while ( $prefix || $match ) {
    $match =~ s/(^\{)|(\}$)//og;
    if ( $prefix =~ s/\\(\w+)\s*$//o ) {
      if ( $trTpt-&gt;{ $1 } ) {
	my @subs = @{ $trTpt-&gt;{ $1 } };
	$out .= $prefix . $subs[0] . &amp;transform( $match, $trTpt ) . $subs[1];
      } else {
	carp "unknown tag $1 seen\n";
	$out .= $prefix . &amp;transform( $match, $trTpt );
      }
    } else {
      $out .= $prefix . "{" . &amp;transform( $match, $trTpt ) . "}";
    }
    last unless $remainder =~ /[^\\]\{/o;
    ( $prefix, $match, $remainder ) = $mc-&gt;match( );
  }
  $out .= $remainder if $remainder;
  return $out;
}

sub SdPrint {			# print in S-PLUS .d format
  my $SdSectRef = {		# table for mapping sections
		   "name"        =&gt; [ ".FN  ", "\n" ],
		   "title"       =&gt; [ ".TL  ", "\n" ],
		   "usage"       =&gt; [ ".CS\n", "\n" ],
		   "alias"       =&gt; [ ], # do nothing
		   "arguments"   =&gt; [ ], # should cause an error
		   "description" =&gt; [ ".DT\n", "\n" ],
		   "value"       =&gt; [ ".RT\n", "\n" ],
		   "references"  =&gt; [ ".SH REFERENCES\n", "\n" ],
		   "note"        =&gt; [ ".SH NOTE\n", "\n" ],
		   "author"      =&gt; [ ],
		   "seealso"     =&gt; [ ".SA\n", "\n" ],
		   "examples"    =&gt; [ ".EX\n", "\n" ],
		   "keyword"     =&gt; [ ]
		  };

  my $SdInternal = {		# Table for mapping internal tags
		  "link" =&gt; [  "",  "" ], # "\link{foo}" becomes "foo"
		  "code" =&gt; [ "`", "'" ], # "\code{foo}" becomes "`foo'"
		  "eqn"  =&gt; [  "",  "" ], # "\eqn{x}" becomes "x"
		 };
  my $self = shift;
  my $file = $self-&gt;{ '.basename' } . ".d";
  my $fh = new FileHandle "&gt; $file" or croak "open($file): $!\n";
  
  for ( "name", "title", "keyword" ) { # check for required sections
    croak "File $file does not have a \\$_ section" unless $self-&gt;{$_};
  }
  my @args;
  for ( @{ $self-&gt;{ '.order' } } ) {
    next if /alias/o;
    next if /author/o;
    if ( /arguments/o ) {
      @args = @{ $self-&gt;arguments() };
      while ( $#args &gt;= 0 ) {
	print $fh ".AG ", shift( @args ), "\n";
	print $fh &amp;transform( shift( @args ), $SdInternal ), "\n";
      }
      next;
    }
    if ( /keyword/o ) {
      @args = @{ $self-&gt;{'keyword'} };
      while ( $#args &gt;= 0 ) {
	print $fh ".KW ", shift( @args ), "\n";
      }
      next;
    }
    if ( $SdSectRef-&gt;{$_} ) {
      print $fh $SdSectRef-&gt;{$_}[0],
      &amp;transform( $self-&gt;{$_}, $SdInternal ) , $SdSectRef-&gt;{$_}[1];
    } else {
      print "unknown section name $_\n";
    }
  }
  print $fh ".WR\n";
  $fh-&gt;close;
}

1;
</pre></body></html>