On Tue, Jan 06, 2004 at 05:35:31PM +0000, Angus Leeming wrote:
> > Isn't this script only called when instant preview is turned on? If
> > so, Preview: Tightpage will always exist.

> Nope.

I do not quite understand when this script will be called. Anyway, I 
have enhanced the error handling of my script. It now
1. exits if there is no snippet or no incorrect preview.
2. exits if the number of snippets in LOG file does not match that of 
   the tex file. Useful if latex exits on error or can not handle all 
   previews.
3. tests if the perl script exits normally.

The updated patch is attached. I am now eager to see how it performs on 
other platforms.


-- 
Bo Peng
Index: lib/scripts/lyxpreview2bitmap.sh
===================================================================
RCS file: /cvs/lyx/lyx-devel/lib/scripts/lyxpreview2bitmap.sh,v
retrieving revision 1.15
diff -u -r1.15 lyxpreview2bitmap.sh
--- lib/scripts/lyxpreview2bitmap.sh    2003/09/03 17:40:56     1.15
+++ lib/scripts/lyxpreview2bitmap.sh    2004/01/06 23:15:25
@@ -128,7 +128,72 @@
 cd ${DIR}
 ${LATEX} ${TEXFILE} ||
 {
-       BAIL_OUT "Failed: ${LATEX} ${TEXFILE}"
+  INCORRECTFORMULAMESSAGE="Formula does not compile"
+# if latex fails, try to locate the incorrect formulas and
+# replace them with \fbox{ $INCORRECTFORMULAMESSAGE }
+# The first snippet, if it is incorrect, is identified in $LOGFILE by
+#   ! + error
+# before line
+#   Preview: Tightpage
+# Other incorrect snippets are identified by
+#   Preview Snippet snippet-number numbers
+#   [snippet-number]
+#   ! and then error messages for the next snippet.
+# NOTE: the "Preview: Snippet .." line is actually a summary line at the end of
+# each preview and the ! line afterwards is the error message for the next snippet.
+# Therefore, the obtained snippet-number should +1.
+perl --  <<EOF
+undef \$/;     # reading the whole file at a time.
+open(LOG, "${LOGFILE}") || die "Can not open log file";
+\$_ = <LOG>;   
+close(LOG);
+
+# count the number of snippets, exit if no snippet is found.
[EMAIL PROTECTED] = split(/^Preview:\s+Snippet\s+\d+/m,\$_);
+\$numSnippets = @Snippets - 1;
+
+# identify incorrect previews
[EMAIL PROTECTED] = ();
+# test if the first snippet is incorrect
+push( @badsnippets, 1) if (/^!.*Preview:\s+Tightpage/gcms);
+# find other incorrect formula
+while(/^Preview:\s+Snippet\s+(\d+)\s+\d+\s+\d+\s+\d+\s*\[\d+\]\s*!/gcm){
+  push( @badsnippets, \$1+1);
+}
+die "No incorrect formula is found" if ( \$numSnippets == 0 || @badsnippets == 0);
+
+# replace incorrect formulas by a message box.
+open(TEX, "${TEXFILE}") || die "Can not open tex file.";
+\$_ = <TEX>;    # get everything.
+close(TEX);
[EMAIL PROTECTED] = split(/^\\\\begin{preview}/m, \$_);  # separate previews
+# check to see if number of preview match
+die "Number of previews does not match that in the LOG file"
+  unless ( \$numSnippets == @PREVIEWS - 1 );
+foreach \$snip (@badsnippets){
+  \$PREVIEWS[\$snip] = "\n\\\\fbox{$INCORRECTFORMULAMESSAGE}\n\\\\end{preview}\n\n";
+  if( \$snip+1 == @PREVIEWS ){  # if the last one, add \end{document}
+    \$PREVIEWS[\$snip] .= "\n\\\\end{document}\n";
+  }
+}
+# join all the pieces together and write back to $TEXFILE
+\$_ = join('\begin{preview}', @PREVIEWS);
+open(TEX, ">${TEXFILE}") || die "Can not write to tex file.";
+print TEX \$_;
+close(TEX);
+EOF
+
+  if [ "$?" -ne 0 ]
+  then
+    BAIL_OUT "Failed to repair the latex file"
+  fi
+
+  # compile the repaired latex file 
+  ${LATEX} ${TEXFILE} ||
+  {
+     BAIL_OUT "Failed: ${LATEX} ${TEXFILE} with incorrect formula replaced"
+  }
+  echo "Found incorrect formula"
 }
 
 # Parse ${LOGFILE} to obtain bounding box info to output to

Reply via email to