changeset 23244:b506b43f999e

Permit several file formats in grabcode. * scripts/general/grabcode: Don't restrict input to '.htm' files for Matlab compatibility. Update docstring. Improve code readability with regexp. Remove no longer suitable test case.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Fri, 03 Mar 2017 16:06:39 +0100
parents 756c7a550542
children aaf5ac82979d
files scripts/general/grabcode.m
diffstat 1 files changed, 7 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/grabcode.m	Tue Feb 28 16:54:08 2017 -0800
+++ b/scripts/general/grabcode.m	Fri Mar 03 16:06:39 2017 +0100
@@ -21,13 +21,11 @@
 ## @deftypefnx {} {} grabcode (@var{filename})
 ## @deftypefnx {} {@var{code_str} =} grabcode (@dots{})
 ##
-## Grab the code from an HTML report previously created with the @code{publish}
-## function.
+## Grab the code from a report created by the @code{publish} function.
 ##
-## The input parameter @var{url} must point to a local or remote HTML file
-## 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.
+## The grabbed code inside the published report must be enclosed by the
+## strings @samp{##### SOURCE BEGIN #####} and @samp {##### SOURCE END #####}.
+## The @code{publish} function creates this format automatically.
 ##
 ## 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
@@ -58,11 +56,6 @@
     print_usage ();
   endif
 
-  [~,~, 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);
@@ -75,11 +68,9 @@
   endif
 
   ## Extract relevant part
-  start_str = "##### SOURCE BEGIN #####";
-  end_str = "##### SOURCE END #####";
-  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);
+  oct_code = regexp (oct_code, ...
+    '##### SOURCE BEGIN #####\n(.*)##### SOURCE END #####', "once", "tokens");
+  oct_code = oct_code{1};
 
   if (nargout == 1)
     code_str = oct_code;
@@ -104,4 +95,3 @@
 ## Test input validation
 %!error grabcode ()
 %!error grabcode (1,2)
-%!error <URL must point to a published> grabcode ("test_script.pdf")