diff scripts/general/grabcode.m @ 22793:d90bc9cdc638

grabcode.m: Overhaul code. * grabcode.m: Rewrite docstring. Don't error out if number of output arguments is greater than 1. Use single quotes to avoid having to escape double quotes. Use strncmpi to be case insensitive about file extension. Rewrite BIST tests.
author Rik <rik@octave.org>
date Thu, 17 Nov 2016 21:09:21 -0800
parents 3a2b891d0b33
children 12956e50e508
line wrap: on
line diff
--- a/scripts/general/grabcode.m	Thu Nov 17 16:13:25 2016 -0800
+++ b/scripts/general/grabcode.m	Thu Nov 17 21:09:21 2016 -0800
@@ -18,25 +18,25 @@
 
 ## -*- texinfo -*-
 ## @deftypefn  {} {} grabcode (@var{url})
-## @deftypefnx {} {@var{code_str} =} grabcode (@var{url})
+## @deftypefnx {} {} grabcode (@var{filename})
+## @deftypefnx {} {@var{code_str} =} grabcode (@dots{})
 ##
-## Grab by the @code{publish} function generated HTML reports from Octave
-## script files.
+## Grab the code from an HTML report previously created with the @code{publish}
+## function.
 ##
 ## The input parameter @var{url} must point to a local or remote HTML file
-## with extension @samp{.htm} or @samp{.html} which was generated by the
-## @code{publish} function.  With any other HTML file this will not work!
+## with extension @file{.htm} or @file{.html} which was generated by the
+## @code{publish} function.  @code{grabcode} will @strong{not} work with any
+## other HTML file.
 ##
-## If no return value is given, the grabbed code is saved to a temporary
-## file and opened in the default editor.
+## If no return value is requested the code is saved to a temporary file and
+## opened in the default editor.  NOTE: The temporary file must be saved under
+## a new or the code will be lost.
 ##
-## NOTE: You have to save the file at another location with arbitrary name,
-## otherwise any grabbed code will be lost!
-##
-## With a return value given, the grabbed code will be returned as string
+## If an output is requested the grabbed code will be returned as string
 ## @var{code_str}.
 ##
-## An example:
+## Example:
 ##
 ## @example
 ## @group
@@ -45,61 +45,64 @@
 ## @end group
 ## @end example
 ##
-## The example above publishes @samp{my_script.m} by default to
-## @samp{html/my_script.html}.  Afterwards this published Octave script
-## is grabbed to edit its content in a new temporary file.
+## The example above publishes @file{my_script.m} to the default location
+## @file{html/my_script.html}.  Next, the published Octave script is grabbed to
+## edit its content in a new temporary file.
 ##
 ## @seealso{publish}
 ## @end deftypefn
 
-function varargout = grabcode (url)
-  narginchk (1, 1);
-  nargoutchk (0, 1);
+function code_str = grabcode (url)
 
-  [~,~,ext] = fileparts (url);
-  if (! strncmp (ext, ".htm", 4))
-    error ("grabcode: URL should point to a published \".html\"-file");
+  if (nargin != 1)
+    print_usage ();
   endif
 
-  ## If url is a local file
+  [~,~, ext] = fileparts (url);
+  if (! strncmpi (ext, ".htm", 4))
+    error ('grabcode: URL must point to a published ".html" file');
+  endif
+
   if (exist (url) == 2)
+    ## URL is a local file
     oct_code = fileread (url);
-  ## Otherwise try to read a url
   else
+    ## Otherwise, try to read remote URL
     [oct_code, success, message] = urlread (url);
     if (! success)
-      error (["grabcode: ", message]);
+      error (["grabcode: " message]);
     endif
   endif
 
   ## Extract relevant part
   start_str = "##### SOURCE BEGIN #####";
   end_str = "##### SOURCE END #####";
-  oct_code = oct_code(strfind (oct_code, start_str) + length(start_str) + 1: ...
-    strfind (oct_code, end_str)-1);
+  start_idx = strfind (oct_code, start_str) + length (start_str) + 1;
+  end_idx = strfind (oct_code, end_str) - 1;
+  oct_code = oct_code(start_idx:end_idx);
 
-  ## Return Octave code string ...
   if (nargout == 1)
-    varargout{1} = oct_code;
-  ## ... or open temporary file in editor
+    code_str = oct_code;
   else
-    fname = [tempname(), ".m"];
+    ## Open temporary file in editor
+    fname = [tempname() ".m"];
     fid = fopen (fname, "w");
     if (fid < 0)
-      error ("grabcode: cannot open temporary file");
+      error ("grabcode: could not open temporary file");
     endif
     fprintf (fid, "%s", oct_code);
     fclose (fid);
     edit (fname);
     warndlg (["grabcode: Make sure to save the temporary file\n\n\t", ...
-      fname, "\n\nto a location of your choice. ", ...
-      "Otherwise all grabbed code will be lost!"]);
+              fname, "\n\nto a location of your choice. ", ...
+              "Otherwise all grabbed code will be lost!"]);
   endif
+
 endfunction
 
-## Bad function calls
 
+## Test input validation
 %!error grabcode ()
-%!error grabcode (1)
-%!error grabcode ("html/test_script.html", "pdf")
-%!error [str1, str2] = grabcode ("html/test_script.html")
\ No newline at end of file
+%!error grabcode (1,2)
+%!error <URL must point to a published> grabcode ("test_script.pdf")
+