1 ## Copyright (C) 2009-2012 S�ren Hauberg
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} gen_doc_cache (@var{out_file}, @var{directory})
21 ## Generate documentation caches for all functions in a given directory.
23 ## A documentation cache is generated for all functions in @var{directory}.
25 ## resulting cache is saved in the file @var{out_file}.
26 ## The cache is used to speed up @code{lookfor}.
28 ## If no directory is given (or it is the empty matrix), a cache for builtin
29 ## operators, etc. is generated.
31 ## @seealso{lookfor, path}
34 function gen_doc_cache (out_file = "doc-cache", directory = [])
37 if (!ischar (out_file))
42 if (isempty (directory))
43 cache = gen_builtin_cache ();
44 elseif (ischar (directory))
45 cache = gen_doc_cache_in_dir (directory);
47 error ("gen_doc_cache: second input argument must be a string");
51 if (! isempty (cache))
52 save ("-text", out_file, "cache");
56 function [text, first_sentence, status] = handle_function (f, text, format)
58 ## Skip functions that start with __ as these shouldn't be searched by lookfor
59 if (length (f) > 2 && all (f (1:2) == "_"))
64 ## Take action depending on help text format
65 switch (lower (format))
69 [text, status] = __makeinfo__ (text, "plain text");
71 [text, status] = strip_html_tags (text);
76 ## Did we get the help text?
77 if (status != 0 || isempty (text))
78 warning ("gen_doc_cache: unusable help text found in file '%s'", f);
82 ## Get first sentence of help text
83 first_sentence = get_first_help_sentence (f);
86 function cache = create_cache (list)
90 for n = 1:length (list)
94 [text, format] = get_help_text (f);
96 [text, first_sentence, status] = handle_function (f, text, format);
98 ## Did we get the help text?
103 ## Store the help text
104 cache (1, end+1) = f;
105 cache (2, end) = text;
106 cache (3, end) = first_sentence;
110 function cache = gen_doc_cache_in_dir (directory)
111 ## If 'directory' is not in the current path, add it so we search it
114 idx = find (p == pathsep ());
116 for n = 1:length (idx)
117 f = p (prev_idx:idx (n)-1);
118 if (strcmp (f, directory))
122 prev_idx = idx (n) + 1;
129 ## Get list of functions in directory and create cache
130 list = __list_functions__ (directory);
131 cache = create_cache (list);
138 function cache = gen_builtin_cache ()
139 operators = __operators__ ();
140 keywords = __keywords__ ();
141 builtins = __builtins__ ();
142 list = {operators{:}, keywords{:}, builtins{:}};
144 cache = create_cache (list);
148 %% No true tests desirable for this function.
149 %% Test input validation
150 %!error gen_doc_cache (1)