File: munge-texi.pl

package info (click to toggle)
octave-optim 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,368 kB
  • sloc: cpp: 1,437; makefile: 185; perl: 169; xml: 29; sh: 3
file content (196 lines) | stat: -rwxr-xr-x 5,109 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -w
#
# Copyright (C) 2012-2014 Rik Wehbring
#
# This file is part of Octave.
#
# Octave 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 3 of the License, or (at
# your option) any later version.
#
# Octave 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 Octave; see the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>.

# copied from Octave and slightly modified by Olaf Till

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

# 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 This file is generated automatically by the packages munge-texi.pl.',"\n\n";

# parse ../DESCRIPTION to get package version
open (FH, "../DESCRIPTION") or die "unable to open file ../DESCRIPTION\n";
while (<FH>)
{
  # this represents the current logic of pkg
  if (/^Version\s*:\s*([^\s]+)/i)
  {
    $version = $1;
  }
}
close (FH);
defined ($version) or die "unable to find version string in ../DESCRIPTION\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*\@DOCSTRINGVERBATIM\((\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 '@'
    print '@anchor',"{XREF$func}\n";
    $docstring =~ s/^@[cC] .*?\n//;
    print '@verbatim',"\n";
    print $docstring,"\n";
    print '@end verbatim',"\n";

    next TXI_LINE;
  }
  if (/^\s*\@EXAMPLEFILE\((\S+)\)/)
  {
    $fname = "$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;
  }

  # replace @PACKAGEVERSION with version string
  s/\@PACKAGEVERSION/$version/o;

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


################################################################################
# Subroutines 
################################################################################
sub extract_docstring
{
  my ($docstring, $arg_list, $func_list, $repl, $rest_of_line);
  
  $cut = 0;

  DOC_LINE: while (defined ($_ = <DOCFH>) and ! /$doc_delim/o)
  {
    # drop text marked for cutting out
    if (/^\s*\@[cC]\s*BEGIN_CUT_TEXINFO\s*$/)
    {
      die "BEGIN_CUT_TEXINFO not matched by END_CUT_TEXINFO" if ($cut);
      $cut = 1;
      next DOC_LINE;
    }
    if (/^\s*\@[cC]\s*END_CUT_TEXINFO\s*$/)
    {
      die "END_CUT_TEXINFO without BEGIN_CUT_TEXINFO" if (! $cut);
      $cut = 0;
      next DOC_LINE;
    }
    if ($cut)
    {
      next DOC_LINE;
    }
    # expand any @seealso references
    if (m'^@seealso\s*{')
    {
      # Join multiple lines until full macro body found
      while (! /}/m) { $_ .= <DOCFH>; }

      ($arg_list, $rest_of_line) = m'^@seealso\s*{(.*)}(.*)?'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;
}