annotate scripts/miscellaneous/grabcode.m @ 24491:d8fb16ab0992

maint: move non-numeric m-files from general/ to miscellaneous/ dir. * scripts/miscellaneous/fieldnames.m, scripts/miscellaneous/grabcode.m, scripts/miscellaneous/inputParser.m, scripts/miscellaneous/isdir.m, scripts/miscellaneous/loadobj.m, scripts/miscellaneous/methods.m, scripts/miscellaneous/nargchk.m, scripts/miscellaneous/narginchk.m, scripts/miscellaneous/nargoutchk.m, scripts/miscellaneous/nthargout.m, scripts/miscellaneous/private/__publish_html_output__.m, scripts/miscellaneous/private/__publish_latex_output__.m, scripts/miscellaneous/publish.m, scripts/miscellaneous/saveobj.m, scripts/miscellaneous/validateattributes.m: Moved from scripts/general dir. * scripts/general/module.mk, scripts/miscellaneous/module.mk: Update build system.
author Rik <rik@octave.org>
date Thu, 28 Dec 2017 16:14:37 -0800
parents scripts/general/grabcode.m@184ab9034ad5
children 194eb4bd202b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23219
3ac9f9ecfae5 maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents: 22299
diff changeset
1 ## Copyright (C) 2016-2017 Kai T. Ohlhus <k.ohlhus@gmail.com>
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
2 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
3 ## 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
4 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
8 ## (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
9 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
10 ## 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
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22299
diff changeset
13 ## 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
14 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
15 ## 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
16 ## along with Octave; see the file COPYING. If not, see
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
18
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
22299
9fc91bb2aec3 doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents: 22113
diff changeset
20 ## @deftypefn {} {} grabcode (@var{url})
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
21 ## @deftypefnx {} {} grabcode (@var{filename})
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
22 ## @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
23 ##
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
24 ## 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
25 ##
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
26 ## 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
27 ## 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
28 ## 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
29 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
30 ## 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
31 ## 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
32 ## 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
33 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
34 ## 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
35 ## @var{code_str}.
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 ## Example:
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
38 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
39 ## @example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
40 ## @group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
41 ## publish ("my_script.m");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
42 ## 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
43 ## @end group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
44 ## @end example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
45 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
46 ## 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
47 ## @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
48 ## 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
49 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
50 ## @seealso{publish}
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
51 ## @end deftypefn
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 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
54
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
55 if (nargin != 1)
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
56 print_usage ();
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
57 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
58
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
59 if (exist (url) == 2)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
60 ## 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
61 oct_code = fileread (url);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
62 else
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
63 ## 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
64 [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
65 if (! success)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
66 error (["grabcode: " message]);
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
67 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
68 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
69
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
70 ## Extract relevant part
23244
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
71 oct_code = regexp (oct_code, ...
b506b43f999e Permit several file formats in grabcode.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 23220
diff changeset
72 '##### 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
73 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
74
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
75 if (nargout == 1)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
76 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
77 else
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
78 ## Open temporary file in editor
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
79 fname = [tempname() ".m"];
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
80 fid = fopen (fname, "w");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
81 if (fid < 0)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
82 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
83 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
84 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
85 fclose (fid);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
86 edit (fname);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
87 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
88 fname, "\n\nto a location of your choice. ", ...
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
89 "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
90 endif
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
91
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
92 endfunction
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
93
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
94
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
95 ## Test input validation
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
96 %!error grabcode ()
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
97 %!error grabcode (1,2)