comparison scripts/help/print_usage.m @ 8575:f134925a1cfa

m-file implementation of help system
author Soren Hauberg <soren@hauberg.org>
date Thu, 22 Jan 2009 18:22:52 -0500
parents
children 52956d669506
comparison
equal deleted inserted replaced
8574:83b8c739d626 8575:f134925a1cfa
1 ## Copyright (C) 2009 Søren Hauberg
2 ##
3 ## This program is free software; you can redistribute it and/or modify it
4 ## under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3 of the License, or (at
6 ## your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ## General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; see the file COPYING. If not, see
15 ## <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {} print_usage ()
19 ## @deftypefnx{Function File} {} print_usage (@var{name})
20 ## Print the usage message for a function. When called with no input arguments
21 ## the @code{print_usage} function displays the usage message of the currently
22 ## executing function.
23 ## @seealso{help}
24 ## @end deftypefn
25
26 function print_usage (name)
27 ## Handle input
28 if (nargin == 0)
29 ## Determine the name of the calling function
30 x = dbstack ();
31 if (numel (x) > 1)
32 name = x (2).name;
33 else
34 error ("print_usage: invalid function\n");
35 endif
36 elseif (!ischar (name))
37 error ("print_usage: input argument must be a string");
38 endif
39
40 ## Do the actual work
41 [text, format] = get_help_text (name);
42 max_len = 80;
43 switch (lower (format))
44 case "plain text"
45 [usage_string, status] = get_usage_plain_text (text, max_len);
46 case "texinfo"
47 [usage_string, status] = get_usage_texinfo (text, max_len);
48 case "html"
49 [usage_string, status] = get_usage_html (text, max_len);
50 case "not found"
51 error ("print_usage: `%s' not found\n", name);
52 otherwise
53 error ("print_usage: internal error: unsupported help text format: '%s'\n", format);
54 endswitch
55
56 ## Raise the final error
57 if (status != 0)
58 warning ("makeinfo: Texinfo formatting filter exited abnormally");
59 warning ("makeinfo: raw Texinfo source of help text follows...\n");
60 endif
61
62 error ("Invalid call to %s. Correct usage is:\n\n%s\n%s",
63 name, usage_string, __additional_help_message__ ());
64 endfunction
65
66 function [retval, status] = get_usage_plain_text (help_text, max_len)
67 ## Extract first line by searching for a double line-end.
68 line_end_idx = strfind (help_text, "\n\n");
69 retval = help_text (1:min ([line_end_idx , max_len, length(help_text)]));
70 status = 0;
71 endfunction
72
73 function [retval, status] = get_usage_texinfo (help_text, max_len)
74 ## Lines ending with "@\n" are continuation lines, so they should be
75 ## concatenated with the following line.
76 help_text = strrep (help_text, "@\n", " ");
77
78 ## Find, and keep, lines that start with @def or @end def. This should include things
79 ## such as @deftypefn, @deftypefnx, @defvar, etc. and their corresponding @end's
80 def_idx = strfind (help_text, "@def");
81 if (!isempty (def_idx))
82 buffer = "";
83 endl_idx = find (help_text == "\n");
84 for k = 1:length (def_idx)
85 endl = endl_idx (find (endl_idx > def_idx (k), 1));
86 if (isempty (endl))
87 buffer = strcat (buffer, help_text (def_idx (k):end), "\n");
88 else
89 buffer = strcat (buffer, help_text (def_idx (k):endl));
90 endif
91 endfor
92
93 end_def_idx = strfind (help_text, "@end def");
94 if (!isempty (end_def_idx))
95 buffer = strcat (buffer, help_text (end_def_idx:end));
96 endif
97 else
98 [retval, status] = get_usage_plain_text (help_text, max_len);
99 endif
100
101 ## Run makeinfo to generate plain text
102 [retval, status] = makeinfo (buffer, "plain text");
103 endfunction
104
105 function [retval, status] = get_usage_html (help_text, max_len)
106 ## Strip tags
107 [help_text, status] = strip_html_tags (help_text);
108
109 ## Extract first line with plain text method.
110 retval = get_usage_plain_text (help_text, max_len);
111 endfunction
112