annotate scripts/general/grabcode.m @ 23220:092078913d54

maint: Merge stable to default.
author John W. Eaton <jwe@octave.org>
date Wed, 22 Feb 2017 12:58:07 -0500
parents 12956e50e508 3ac9f9ecfae5
children b506b43f999e
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 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
24 ## Grab the code from an HTML report previously created with the @code{publish}
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
25 ## function.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
26 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
27 ## The input parameter @var{url} must point to a local or remote HTML file
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
28 ## with extension @file{.htm} or @file{.html} which was generated by the
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
29 ## @code{publish} function. @code{grabcode} will @strong{not} work with any
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
30 ## other HTML file.
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
31 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
32 ## 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
33 ## 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
34 ## 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
35 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
36 ## 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
37 ## @var{code_str}.
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
38 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
39 ## Example:
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
40 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
41 ## @example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
42 ## @group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
43 ## publish ("my_script.m");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
44 ## 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
45 ## @end group
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
46 ## @end example
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
47 ##
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
48 ## 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
49 ## @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
50 ## 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
51 ##
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
52 ## @seealso{publish}
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
53 ## @end deftypefn
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 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
56
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
57 if (nargin != 1)
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
58 print_usage ();
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
59 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
60
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
61 [~,~, ext] = fileparts (url);
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
62 if (! strncmpi (ext, ".htm", 4))
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
63 error ('grabcode: URL must point to a published ".html" file');
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
64 endif
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
65
22113
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
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
78 start_str = "##### SOURCE BEGIN #####";
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
79 end_str = "##### SOURCE END #####";
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
80 start_idx = strfind (oct_code, start_str) + length (start_str) + 1;
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
81 end_idx = strfind (oct_code, end_str) - 1;
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
82 oct_code = oct_code(start_idx:end_idx);
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
83
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
84 if (nargout == 1)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
85 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
86 else
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
87 ## Open temporary file in editor
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
88 fname = [tempname() ".m"];
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
89 fid = fopen (fname, "w");
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
90 if (fid < 0)
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
91 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
92 endif
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
93 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
94 fclose (fid);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
95 edit (fname);
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
96 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
97 fname, "\n\nto a location of your choice. ", ...
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
98 "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
99 endif
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
100
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
101 endfunction
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
102
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
103
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
104 ## Test input validation
22113
b6f482e29afd New functions publish.m and grabcode.m (patch #9048).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff changeset
105 %!error grabcode ()
22793
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
106 %!error grabcode (1,2)
d90bc9cdc638 grabcode.m: Overhaul code.
Rik <rik@octave.org>
parents: 22755
diff changeset
107 %!error <URL must point to a published> grabcode ("test_script.pdf")