view doc/interpreter/munge-texi.pl @ 19033:c8240a60dd01

Add penny.mat to build system. * examples/data/penny.mat: New example data file. * examples/data/Makefile.am: Automake file for new directory of example data files. * examples/Makefile.am: Change to be just a pass-through to the subdirectories code/ and data/. * examples/@FIRfilter/FIRfilter.m, examples/@FIRfilter/FIRfilter_aggregation.m, examples/@FIRfilter/display.m, examples/@FIRfilter/module.mk, examples/@FIRfilter/subsasgn.m, examples/@FIRfilter/subsref.m, examples/@polynomial/display.m, examples/@polynomial/double.m, examples/@polynomial/end.m, examples/@polynomial/get.m, examples/@polynomial/module.mk, examples/@polynomial/mtimes.m, examples/@polynomial/numel.m, examples/@polynomial/plot.m, examples/@polynomial/polynomial.m, examples/@polynomial/polynomial_superiorto.m, examples/@polynomial/polyval.m, examples/@polynomial/roots.m, examples/@polynomial/set.m, examples/@polynomial/subsasgn.m, examples/@polynomial/subsref.m, examples/COPYING, examples/addtwomatrices.cc, examples/celldemo.cc, examples/embedded.cc, examples/fortrandemo.cc, examples/fortransub.f, examples/funcdemo.cc, examples/globaldemo.cc, examples/helloworld.cc, examples/make_int.cc, examples/mex_demo.c, examples/mycell.c, examples/myfeval.c, examples/myfevalf.f, examples/myfunc.c, examples/myhello.c, examples/mypow2.c, examples/myprop.c, examples/myset.c, examples/mysparse.c, examples/mystring.c, examples/mystruct.c, examples/oct_demo.cc, examples/oregonator.cc, examples/oregonator.m, examples/paramdemo.cc, examples/standalone.cc, examples/standalonebuiltin.cc, examples/stringdemo.cc, examples/structdemo.cc, examples/unwinddemo.cc: Move current example files to examples/code/ directory. * configure.ac: Add Makefiles in examples/code and examples/data directories. * build-aux/common.mk: Define octdatadir variable. * doc/interpreter/Makefile.am: Change location of External Code Interface examples to examples/code directory. * doc/interpreter/munge-texi.pl: Expand EXAMPLEFILE macro to examples/code directory.
author Rik <rik@octave.org>
date Tue, 19 Aug 2014 14:32:44 -0700
parents ee1d19174316
children 0e1f5a750d00
line wrap: on
line source

#!/usr/bin/perl -w

# Validate program call
die "usage: munge-texi TOP-SRCDIR DOCSTRING-FILE < file" if (@ARGV < 2);

$top_srcdir = shift (@ARGV);

# Constant patterns
$doc_delim = qr/^\x{1d}/;
$tex_delim = qr/\Q-*- texinfo -*-\E/;
$comment_line = qr/^\s*(?:$|#)/;
# Pre-declare hash size for efficiency
keys(%help_text) = 1800;

################################################################################
# Load DOCSTRINGS into memory while expanding @seealso references
foreach $DOCSTRING_file (@ARGV)
{
  open (DOCFH, $DOCSTRING_file) or die "Unable to open $DOCSTRING_file\n";

  # Skip comments
  while (defined ($_ = <DOCFH>) and /$comment_line/o) {;}

  # Validate DOCSTRING file format
  die "invalid doc file format\n" if (! /$doc_delim/o);
  
  do 
  {
    s/\s*$//;   # strip EOL character(s)
    $symbol = substr ($_,1);
    $docstring = extract_docstring ();
    if ($help_text{$symbol})
    {
      warn "ignoring duplicate entry for $symbol\n";
    }
    else
    {
      $help_text{$symbol} = $docstring;
    }

  } while (! eof);

}

################################################################################
# Process .txi to .texi by expanding @DOCSTRING, @EXAMPLEFILE macros

# Add warning header
print '@c DO NOT EDIT!  Generated automatically by munge-texi.pl.',"\n\n";

TXI_LINE: while (<STDIN>)
{
  if (/^\s*\@DOCSTRING\((\S+)\)/)
  {
    $func = $1;
    $docstring = $help_text{$func};
    if (! $docstring)
    {
      warn "no docstring entry for $func\n";
      next TXI_LINE;
    }

    $func =~ s/^@/@@/;   # Texinfo uses @@ to produce '@'
    $docstring =~ s/^$tex_delim$/\@anchor{XREF$func}/m;
    print $docstring,"\n";

    next TXI_LINE;
  }
  if (/^\s*\@EXAMPLEFILE\((\S+)\)/)
  {
    $fname = "$top_srcdir/examples/code/$1";
    print '@verbatim',"\n";
    open (EXAMPFH, $fname) or die "unable to open example file $fname\n";
    while (<EXAMPFH>) 
    { 
      print $_;
      print "\n" if (eof and substr ($_, -1) ne "\n");
    }
    close (EXAMPFH);
    print '@end verbatim',"\n\n";

    next TXI_LINE;
  }

  # pass ordinary lines straight through to output
  print $_;
}


################################################################################
# Subroutines 
################################################################################
sub extract_docstring
{
  my ($docstring, $arg_list, $func_list, $repl, $rest_of_line);
  
  while (defined ($_ = <DOCFH>) and ! /$doc_delim/o)
  {
    # expand any @seealso references
    if (m'^@seealso{')
    {
      # Join multiple lines until full macro body found
      while (! /}/m) { $_ .= <DOCFH>; }

      ($arg_list, $rest_of_line) = m'^@seealso{(.*)}(.*)?'s;
     
      $func_list = $arg_list;
      $func_list =~ s/\s+//gs;
      $repl = "";
      foreach $func (split (/,/, $func_list))
      {
        $func =~ s/^@/@@/;   # Texinfo uses @@ to produce '@'
        $repl .= "\@ref{XREF$func,,$func}, ";
      }
      substr($repl,-2) = "";   # Remove last ', ' 
      $_ = "\@seealso{$repl}$rest_of_line";
    }

    $docstring .= $_;
  }

  return $docstring;
}