]> Creatis software - CreaPhase.git/blob - octave_packages/m/help/gen_doc_cache.m
update packages
[CreaPhase.git] / octave_packages / m / help / gen_doc_cache.m
1 ## Copyright (C) 2009-2012 S�ren Hauberg
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} gen_doc_cache (@var{out_file}, @var{directory})
21 ## Generate documentation caches for all functions in a given directory.
22 ##
23 ## A documentation cache is generated for all functions in @var{directory}.
24 ## The
25 ## resulting cache is saved in the file @var{out_file}.
26 ## The cache is used to speed up @code{lookfor}.
27 ##
28 ## If no directory is given (or it is the empty matrix), a cache for builtin
29 ## operators, etc. is generated.
30 ##
31 ## @seealso{lookfor, path}
32 ## @end deftypefn
33
34 function gen_doc_cache (out_file = "doc-cache", directory = [])
35
36   ## Check input
37   if (!ischar (out_file))
38     print_usage ();
39   endif
40
41   ## Generate cache
42   if (isempty (directory))
43     cache = gen_builtin_cache ();
44   elseif (ischar (directory))
45     cache = gen_doc_cache_in_dir (directory);
46   else
47     error ("gen_doc_cache: second input argument must be a string");
48   endif
49
50   ## Save cache
51   if (! isempty (cache))
52     save ("-text", out_file, "cache");
53   endif
54 endfunction
55
56 function [text, first_sentence, status] = handle_function (f, text, format)
57   first_sentence = "";
58   ## Skip functions that start with __ as these shouldn't be searched by lookfor
59   if (length (f) > 2 && all (f (1:2) == "_"))
60     status = 1;
61     return;
62   endif
63
64   ## Take action depending on help text format
65   switch (lower (format))
66     case "plain text"
67       status = 0;
68     case "texinfo"
69       [text, status] = __makeinfo__ (text, "plain text");
70     case "html"
71       [text, status] = strip_html_tags (text);
72     otherwise
73       status = 1;
74   endswitch
75
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);
79     return;
80   endif
81
82   ## Get first sentence of help text
83   first_sentence = get_first_help_sentence (f);
84 endfunction
85
86 function cache = create_cache (list)
87   cache = {};
88
89   ## For each function:
90   for n = 1:length (list)
91     f = list {n};
92
93     ## Get help text
94     [text, format] = get_help_text (f);
95
96     [text, first_sentence, status] = handle_function (f, text, format);
97
98     ## Did we get the help text?
99     if (status != 0)
100       continue;
101     endif
102
103     ## Store the help text
104     cache (1, end+1) = f;
105     cache (2, end) = text;
106     cache (3, end) = first_sentence;
107   endfor
108 endfunction
109
110 function cache = gen_doc_cache_in_dir (directory)
111   ## If 'directory' is not in the current path, add it so we search it
112   dir_in_path = false;
113   p = path ();
114   idx = find (p == pathsep ());
115   prev_idx = 1;
116   for n = 1:length (idx)
117     f = p (prev_idx:idx (n)-1);
118     if (strcmp (f, directory))
119       dir_in_path = true;
120       break;
121     endif
122     prev_idx = idx (n) + 1;
123   endfor
124
125   if (!dir_in_path)
126     addpath (directory);
127   endif
128
129   ## Get list of functions in directory and create cache
130   list = __list_functions__ (directory);
131   cache = create_cache (list);
132
133   if (!dir_in_path)
134     rmpath (directory);
135   endif
136 endfunction
137
138 function cache = gen_builtin_cache ()
139   operators = __operators__ ();
140   keywords = __keywords__ ();
141   builtins = __builtins__ ();
142   list = {operators{:}, keywords{:}, builtins{:}};
143
144   cache = create_cache (list);
145 endfunction
146
147
148 %% No true tests desirable for this function.
149 %% Test input validation
150 %!error gen_doc_cache (1)
151