annotate scripts/miscellaneous/grabcode.m @ 28891:de5f2f9a64ff

maint: Use same coding style when checking for a minimum of 1 input. * disp.m, __get_properties__.m, disp.m, isplaying.m, pause.m, resume.m, stop.m, __get_properties__.m, disp.m, isrecording.m, pause.m, resume.m, stop.m, mu2lin.m, acosd.m, acot.m, acotd.m, acoth.m, acsc.m, acscd.m, acsch.m, asec.m, asecd.m, asech.m, asind.m, atand.m, cosd.m, cot.m, cotd.m, coth.m, csc.m, cscd.m, csch.m, sec.m, secd.m, sech.m, sind.m, tand.m, cell2mat.m, deg2rad.m, fliplr.m, flipud.m, int2str.m, nextpow2.m, num2str.m, rad2deg.m, subsindex.m, rotx.m, roty.m, rotz.m, voronoin.m, __unimplemented__.m, frame2im.m, hsv2rgb.m, iscolormap.m, __imfinfo__.m, rgb2gray.m, rgb2hsv.m, fileread.m, is_valid_file_id.m, usejava.m, argnames.m, char.m, formula.m, vectorize.m, isdir.m, vectorize.m, condeig.m, duplication_matrix.m, expm.m, isdiag.m, istril.m, istriu.m, normest.m, planerot.m, trace.m, vech.m, fieldnames.m, fileparts.m, grabcode.m, isfile.m, isfolder.m, mustBeFinite.m, mustBeInteger.m, mustBeNegative.m, mustBeNonNan.m, mustBeNonempty.m, mustBeNonnegative.m, mustBeNonpositive.m, mustBeNonsparse.m, mustBeNonzero.m, mustBeNumeric.m, mustBeNumericOrLogical.m, mustBePositive.m, mustBeReal.m, namedargs2cell.m, open.m, run.m, swapbytes.m, pareto.m, allchild.m, colstyle.m, hdl2struct.m, isaxes.m, isfigure.m, ishandle.m, compan.m, poly.m, polyreduce.m, ppjumps.m, roots.m, unmkpp.m, profexplore.m, profile.m, bartlett.m, fftshift.m, hurst.m, ifftshift.m, sinc.m, spencer.m, yulewalker.m, colperm.m, nonzeros.m, spconvert.m, spones.m, sprandsym.m, cosint.m, expint.m, factor.m, factorial.m, isprime.m, perms.m, primes.m, reallog.m, realsqrt.m, sinint.m, gallery.m, hadamard.m, hilb.m, invhilb.m, magic.m, wilkinson.m, center.m, iqr.m, meansq.m, median.m, runlength.m, bin2dec.m, blanks.m, deblank.m, hex2dec.m, isletter.m, isstring.m, str2num.m, strtrim.m, asctime.m, ctime.m: Replace "if (nargin != 1)" formulations with "if (nargin < 1)" to match the rest of Octave.
author Rik <rik@octave.org>
date Sat, 10 Oct 2020 16:32:20 -0700
parents d8318c12d903
children 7854d5752dd2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## Copyright (C) 2016-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
7 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
8 ## This file is part of Octave.
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
13 ## (at your option) any later version.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
14 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
18 ## GNU General Public License for more details.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
19 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24491
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
25
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
22299
9fc91bb2aec3 doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents: 22113
diff changeset
27 ## @deftypefn {} {} grabcode (@var{url})
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
28 ## @deftypefnx {} {} grabcode (@var{filename})
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
29 ## @deftypefnx {} {@var{code_str} =} grabcode (@dots{})
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
30 ##
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
31 ## Grab the code from a report created by the @code{publish} function.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
32 ##
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
33 ## The grabbed code inside the published report must be enclosed by the
23246
184ab9034ad5 * grabcode.m: Fix typo in doc string.
John W. Eaton <jwe@octave.org>
parents: 23244
diff changeset
34 ## strings @samp{##### SOURCE BEGIN #####} and @samp{##### SOURCE END #####}.
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
35 ## The @code{publish} function creates this format automatically.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
36 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
37 ## If no return value is requested the code is saved to a temporary file and
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
38 ## opened in the default editor. NOTE: The temporary file must be saved under
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
39 ## a new or the code will be lost.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
40 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
41 ## If an output is requested the grabbed code will be returned as string
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
42 ## @var{code_str}.
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
43 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
44 ## Example:
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
45 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
46 ## @example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
47 ## @group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
48 ## publish ("my_script.m");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
49 ## grabcode ("html/my_script.html");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
50 ## @end group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
51 ## @end example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
52 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
53 ## The example above publishes @file{my_script.m} to the default location
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
54 ## @file{html/my_script.html}. Next, the published Octave script is grabbed to
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
55 ## edit its content in a new temporary file.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
56 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
57 ## @seealso{publish}
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
58 ## @end deftypefn
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
59
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
60 function code_str = grabcode (url)
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
61
28891
de5f2f9a64ff maint: Use same coding style when checking for a minimum of 1 input.
Rik <rik@octave.org>
parents: 28886
diff changeset
62 if (nargin < 1)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
63 print_usage ();
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
64 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
65
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
66 if (exist (url) == 2)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
67 ## URL is a local file
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
68 oct_code = fileread (url);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
69 else
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
70 ## Otherwise, try to read remote URL
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
71 [oct_code, success, message] = urlread (url);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
72 if (! success)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
73 error (["grabcode: " message]);
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
74 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
75 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
76
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
77 ## Extract relevant part
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
78 oct_code = regexp (oct_code, ...
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
79 '##### SOURCE BEGIN #####\n(.*)##### SOURCE END #####', "once", "tokens");
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
80 oct_code = oct_code{1};
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
81
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
82 if (nargout == 1)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
83 code_str = oct_code;
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
84 else
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
85 ## Open temporary file in editor
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
86 fname = [tempname() ".m"];
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
87 fid = fopen (fname, "w");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
88 if (fid < 0)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
89 error ("grabcode: could not open temporary file");
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
90 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
91 fprintf (fid, "%s", oct_code);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
92 fclose (fid);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
93 edit (fname);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
94 warndlg (["grabcode: Make sure to save the temporary file\n\n\t", ...
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
95 fname, "\n\nto a location of your choice. ", ...
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
96 "Otherwise all grabbed code will be lost!"]);
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
97 endif
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
98
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
99 endfunction
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
100
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
101
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
102 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 27923
diff changeset
103 %!error <Invalid call> grabcode ()