view scripts/general/grabcode.m @ 22113:b6f482e29afd

New functions publish.m and grabcode.m (patch #9048). * scripts/general/module.mk: Add entries for the new funtions. * scripts/general/grabcode.m: New function. * scripts/general/publish.m: New function. * scripts/general/private/__publish_html_output__.m: New function. * scripts/general/private/__publish_latex_output__.m: New function. * scripts/help/__unimplemented__.m: Remove entries publish and grabcode. * NEWS: Announce new functions. * doc/interpreter/func.txi: Add documentation for the new functions. * test/module.mk: New entry for test module publish. * test/publish/module.mk: New entries for publish tests. * test/publish/publish.tst: New test file, to run all test scripts on publish and grabcode. * test/publish/test_script.m: New test script. * test/publish/test_script_code_only.m: New test script. * test/publish/test_script_empty.m: New test script. * test/publish/test_script_example.m: New test script. * test/publish/test_script_head_only.m: New test script.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Fri, 15 Jul 2016 11:46:16 +0200
parents
children 9fc91bb2aec3
line wrap: on
line source

## Copyright (C) 2016 Kai T. Ohlhus <k.ohlhus@gmail.com>
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {} {} grabcode (@var{url})
## @deftypefnx {} {@var{code_str} =} grabcode (@var{url})
##
## Grab by the @code{publish} function generated HTML reports from Octave
## script files.
##
## 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!
##
## If no return value is given, the grabbed code is saved to a temporary
## file and opened in the default editor.
##
## 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
## @var{code_str}.
##
## An example:
##
## @example
## @group
## publish ("my_script.m");
## grabcode ("html/my_script.html");
## @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.
##
## @seealso{publish}
## @end deftypefn

function varargout = grabcode (url)
  narginchk (1, 1);
  nargoutchk (0, 1);

  [~,~,ext] = fileparts (url);
  if (! strncmp (ext, ".htm", 4))
    error ("grabcode: URL should point to a published \".html\"-file");
  endif

  ## If url is a local file
  if (exist (url) == 2)
    oct_code = fileread (url);
  ## Otherwise try to read a url
  else
    [oct_code, success, message] = urlread (url);
    if (! success)
      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);

  ## Return Octave code string ...
  if (nargout == 1)
    varargout{1} = oct_code;
  ## ... or open temporary file in editor
  else
    fname = [tempname(), ".m"];
    fid = fopen (fname, "w");
    if (fid < 0)
      error ("grabcode: cannot 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!"]);
  endif
endfunction

## Bad function calls

%!error grabcode ()
%!error grabcode (1)
%!error grabcode ("html/test_script.html", "pdf")
%!error [str1, str2] = grabcode ("html/test_script.html")