changeset 8717:28b8bd2f6e66

doc/interpreter/mk_doc_cache.m: improve performance
author John W. Eaton <jwe@octave.org>
date Tue, 10 Feb 2009 21:50:59 -0500
parents 80910b37d855
children c74c9692add7
files doc/ChangeLog doc/interpreter/Makefile.in doc/interpreter/mk_doc_cache.m scripts/help/makeinfo.m
diffstat 4 files changed, 81 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/doc/ChangeLog	Tue Feb 10 17:04:20 2009 -0500
+++ b/doc/ChangeLog	Tue Feb 10 21:50:59 2009 -0500
@@ -1,8 +1,8 @@
 2009-02-10  John W. Eaton  <jwe@octave.org>
 
 	* interpreter/Makefile.in (DOC): New target.
-	(mk_doc_cache.m): New file.
 	(DISTFILES): Add DOC nad mk_doc_cache.m to the list.
+	* mk_doc_cache.m: New file.
 
 2009-02-01  Soren Hauberg <hauberg@gmail.com>
 
--- a/doc/interpreter/Makefile.in	Tue Feb 10 17:04:20 2009 -0500
+++ b/doc/interpreter/Makefile.in	Tue Feb 10 21:50:59 2009 -0500
@@ -176,7 +176,7 @@
 DOCSTRING_FILES := $(TOPDIR)/src/DOCSTRINGS $(TOPDIR)/scripts/DOCSTRINGS
 
 DOC: $(DOCSTRING_FILES) mk_doc_cache.m
-	$(TOPDIR)/run-octave -f -q -H -p $(srcdir) $(srcdir)/mk_doc_cache.m DOC $(DOCSTRING_FILES)
+	$(TOPDIR)/run-octave -f -q -H $(srcdir)/mk_doc_cache.m DOC $(DOCSTRING_FILES) || rm -f DOC
 
 $(TEXINFO): $(DOCSTRING_FILES) munge-texi$(BUILD_EXEEXT)
 
--- a/doc/interpreter/mk_doc_cache.m	Tue Feb 10 17:04:20 2009 -0500
+++ b/doc/interpreter/mk_doc_cache.m	Tue Feb 10 21:50:59 2009 -0500
@@ -39,84 +39,103 @@
   str = str(j:end);
 endfunction
 
-doc_cache = [];
-
-for i = 1:numel (docstrings_files);
+## Read the contents of all the DOCSTRINGS files into TEXT.
 
+text = "";
+nfiles = numel (docstrings_files);
+text = cell (1, nfiles+1);
+for i = 1:nfiles
   file = docstrings_files{i};
-
   fid = fopen (file, "r");
-
   if (fid < 0)
     error ("unable to open %s for reading", file);
   else
-
-    printf ("processing %s\n", file);
-
-    text = fread (fid, Inf, "*char")';
+    tmp = fread (fid, Inf, "*char")';
+    delim_idx = find (tmp == doc_delim, 1);
+    text{i} = tmp(delim_idx:end);
+  endif
+endfor
+text = [text{:}, doc_delim];
 
-    delim_idx = find (text == doc_delim);
-    eof = numel (text);
-    idx = [delim_idx, eof];
+text = regexprep (text, "@seealso *{([^}]*)}", "See also: $1.");
+text = regexprep (text, "-\\*- texinfo -\\*-[ \t]*[\r\n]*", "");
 
-    n = numel (delim_idx);
-
-    tmp_doc_cache = cell (3, n);
-    k = 1;
+[fid, name, msg] = mkstemp ("octave_doc_XXXXXX", true);
 
-    for i = 1:n
-
-      tmp = text(idx(i)+1:idx(i+1)-1);
+if (fid < 0)
+  error ("%s: %s\n", name, msg);
+endif
 
-      [symbol, doc] = strtok (tmp, "\r\n");
-
-      doc = skip_newline (doc);
+fwrite (fid, text, "char");
 
-      ## Skip functions that start with __ as these shouldn't be
-      ## searched by lookfor.
-      if (numel (symbol) > 2 && regexp (symbol, "^__.+__$"))
-	continue;
-      endif
-  
-      printf ("  %s\n", symbol);
+fclose (fid);
+
+cmd = sprintf ("%s --no-headers --no-warn --force --no-validate %s",
+               makeinfo_program (), name);
+
+[status, formatted_text] = system (cmd);
 
-      if (strncmp (doc, "-*- texinfo -*-", 15))
-	doc = skip_newline (doc(16:end));
-      else
-	error ("doc string for %s is not in texinfo format", symbol);
-      endif
+## Did we get the help text?
+if (status != 0)
+  error ("makeinfo failed with exit status %d!", status);
+endif
 
-      [formatted_text, status] = makeinfo (doc, "plain text");
-    
-      ## Did we get the help text?
-      if (status != 0 || isempty (formatted_text))
-	error ("makeinfo failed for %s doc string", symbol);
-      endif
+if (isempty (formatted_text))
+  error ("makeinfo produced no output!");
+endif
 
-      ## Extract first line by searching for a period or a double
-      ## line-end.
+delim_idx = find (formatted_text == doc_delim);
+n = numel (delim_idx);
 
-      period_idx = find (formatted_text == ".", 1);
-
-      line_end_idx = strfind (formatted_text, "\n\n");
+cache = cell (3, n);
+k = 1;
 
-      max_first_sentence_len = 80;
+for i = 2:n
 
-      first_sentence = formatted_text (1:min ([period_idx(:); line_end_idx(:); max_first_sentence_len; numel(formatted_text)]));
+  block = formatted_text(delim_idx(i-1)+1:delim_idx(i)-1);
 
-      tmp_doc_cache{1,k} = symbol;
-      tmp_doc_cache{2,k} = formatted_text;
-      tmp_doc_cache{3,k} = first_sentence;
-      k++;
+  [symbol, doc] = strtok (block, "\r\n");
+
+  doc = skip_newline (doc);
 
-    endfor
-
-    tmp_doc_cache(:,k:end) = [];
-
+  ## Skip functions that start with __ as these shouldn't be
+  ## searched by lookfor.
+  if (numel (symbol) > 2 && regexp (symbol, "^__.+__$"))
+    continue;
   endif
 
-  doc_cache = [doc_cache, tmp_doc_cache];
+  if (isempty (doc))
+    continue;
+  endif
+
+  [s, e] = regexp (doc, "^ -- [^\r\n]*[\r\n]", "lineanchors");
+
+  if (isempty (s))
+    continue;
+  endif
+
+  start_of_first_sentence = e(end);
+
+  tmp = doc(start_of_first_sentence:end);
+
+  end_of_first_sentence = regexp (tmp, '(\.|[\r\n][\r\n])');
 
-  save ("-text", output_file, "doc_cache");
+  if (isempty (end_of_first_sentence))
+    end_of_first_sentence = numel (tmp);
+  else
+    end_of_first_sentence = end_of_first_sentence(1);
+  endif
+
+  first_sentence = tmp(1:end_of_first_sentence);
+  first_sentence = regexprep (first_sentence, "([\r\n]|  *)", " ");
+  first_sentence = regexprep (first_sentence, "^ *", "");
 
+  cache{1,k} = symbol;
+  cache{2,k} = doc;
+  cache{3,k} = first_sentence;
+  k++;
 endfor
+
+cache(:,k:end) = [];
+
+save ("-text", output_file, "cache");
--- a/scripts/help/makeinfo.m	Tue Feb 10 17:04:20 2009 -0500
+++ b/scripts/help/makeinfo.m	Tue Feb 10 21:50:59 2009 -0500
@@ -143,9 +143,9 @@
     [status, retval] = system (cmd);
    
   unwind_protect_cleanup
-    if (exist (name, "file"))
-      delete (name);
-    endif
+#    if (exist (name, "file"))
+#      delete (name);
+#    endif
   end_unwind_protect
 endfunction