[hmath-commits] org.hmath.server/WEB-INF/src/org/hartmath/server/filter CachedWikipediaParser.java,1
Status: Pre-Alpha
Brought to you by:
jsurfer
From: Klaus H. <js...@us...> - 2004-05-11 20:55:25
|
Update of /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/filter In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5933/WEB-INF/src/org/hartmath/server/filter Modified Files: CachedWikipediaParser.java Log Message: Improved wikipedi syntax Improved code:none macro for special characters <>"' Index: CachedWikipediaParser.java =================================================================== RCS file: /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/filter/CachedWikipediaParser.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** CachedWikipediaParser.java 2 May 2004 17:34:10 -0000 1.3 --- CachedWikipediaParser.java 11 May 2004 20:55:10 -0000 1.4 *************** *** 18,24 **** --- 18,27 ---- package org.hartmath.server.filter; + import java.io.File; import java.io.IOException; + import java.io.StringWriter; import java.io.Writer; import java.util.ArrayList; + import java.util.Date; import java.util.Map; import java.util.NoSuchElementException; *************** *** 28,31 **** --- 31,36 ---- import javax.servlet.http.HttpServletRequest; + import net.sourceforge.jeuclid.gif.MathMLToGIFConverter; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; *************** *** 53,56 **** --- 58,62 ---- import org.snipsnap.snip.Snip; import org.snipsnap.snip.SnipLink; + import org.snipsnap.snip.attachment.Attachment; /** *************** *** 65,68 **** --- 71,76 ---- private FilterContext fContext; private RenderEngine fWikiEngine; + // TODO check, if this counter is correct in recursions: + private int fImageCounter; /** * The current snip *************** *** 122,126 **** private int fWhiteStartPosition = 0; private TeXParser fTeXParser; ! /** * --- 130,134 ---- private int fWhiteStartPosition = 0; private TeXParser fTeXParser; ! private TeXParser fTeXImageParser; /** * *************** *** 153,157 **** fRecursionLevel = recursionLevel; fTokenStack = new Stack(); ! fTeXParser = new TeXParser(""); } /** --- 161,167 ---- fRecursionLevel = recursionLevel; fTokenStack = new Stack(); ! fTeXParser = new TeXParser("", "m:"); ! fTeXImageParser = new TeXParser("", ""); ! fImageCounter = 1; } /** *************** *** 184,188 **** } /** ! * copy the content in the resulting buffer and escape special html characters (< > " & ') */ private void copyWhite(boolean whiteStart, final int whiteStartPosition, final int diff) { --- 194,198 ---- } /** ! * copy the content in the resulting buffer and escape special html characters (< > " & ') */ private void copyWhite(boolean whiteStart, final int whiteStartPosition, final int diff) { *************** *** 252,256 **** /** ! * copy the text in the resulting buffer and escape special html characters (< > " & ') */ private void copyWhite(String text) { --- 262,266 ---- /** ! * copy the text in the resulting buffer and escape special html characters (< > " & ') */ private void copyWhite(String text) { *************** *** 311,316 **** /** ! * Copy the text in the resulting buffer and escape special html characters (< > " & ') ! * Additionally every newline will be replaced by <br/> */ private void copyWhiteNewLine(String text) { --- 321,326 ---- /** ! * Copy the text in the resulting buffer and escape special html characters (< > " & ') Additionally every ! * newline will be replaced by <br/> */ private void copyWhiteNewLine(String text) { *************** *** 1054,1057 **** --- 1064,1087 ---- try { switch (fStringSource.charAt(fCurrentPosition)) { + case '!' : // <!-- html comment --> + String htmlCommentString = fStringSource.substring(fCurrentPosition - 1, fCurrentPosition + 3); + + if (htmlCommentString.equals("<!--")) { + fCurrentPosition += 3; + if (readUntilString("-->")) { + String htmlCommentContent = new String(fSource, htmlStartPosition + 3, fCurrentPosition - htmlStartPosition - 6); + if (htmlCommentContent != null) { + copyWhite(fWhiteStart, fWhiteStartPosition, fCurrentPosition - htmlStartPosition + 1); + fWhiteStart = false; + // insert html comment for visual checks only: + /* fResultBuffer.append("<!--"); + copyWhite(htmlCommentContent); + fResultBuffer.append("-->"); + */ + continue; + } + } + } + break; case 'm' : // math String mathString = fStringSource.substring(fCurrentPosition - 1, fCurrentPosition + 5); *************** *** 1270,1287 **** mathStrings[0] = mathBuffer.toString(); mathBuffer.setLength(0); if (inlineExpression) { ! // css inline ! mathBuffer.append("<m>"); ! fTeXParser.initialize(mathContent); ! fTeXParser.parse2CSS(mathBuffer, true); ! mathBuffer.append("</m>"); } else { ! // css block mode ! mathBuffer.append("<e>"); ! fTeXParser.initialize(mathContent); ! fTeXParser.parse2CSS(mathBuffer, true); ! mathBuffer.append("</e>"); } - mathStrings[1] = mathBuffer.toString(); mathBuffer.setLength(0); CachedWikipediaFilter.copyWhite(mathBuffer, mathContent); --- 1300,1372 ---- mathStrings[0] = mathBuffer.toString(); mathBuffer.setLength(0); + // if (inlineExpression) { + // // css inline + // mathBuffer.append("<m>"); + // fTeXParser.initialize(mathContent); + // fTeXParser.parse2CSS(mathBuffer, true); + // mathBuffer.append("</m>"); + // } else { + // // css block mode + // mathBuffer.append("<e>"); + // fTeXParser.initialize(mathContent); + // fTeXParser.parse2CSS(mathBuffer, true); + // mathBuffer.append("</e>"); + // } + + String encodedMathContent = Encoder.escape(mathContent); + StringBuffer mathmlBuffer = new StringBuffer(); + String shortImageName = "____tex_" + (fImageCounter++); + String longImageName = shortImageName + ".gif"; + mathmlBuffer.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"); if (inlineExpression) { ! mathmlBuffer.append("<math><mstyle mathcolor=\"Black\" displaystyle=\"true\">"); ! fTeXImageParser.initialize(mathContent); ! fTeXImageParser.parse2MML(mathmlBuffer); ! mathmlBuffer.append("</mstyle></math>"); } else { ! mathmlBuffer.append("<math><mstyle mathcolor=\"Black\" displaystyle=\"true\">"); ! fTeXImageParser.initialize(mathContent); ! fTeXImageParser.parse2MML(mathmlBuffer); ! mathmlBuffer.append("</mstyle></math>"); ! } ! ! String snipname = fSnip.getName(); ! String SnipSnapSpacePath = Application.get().getConfiguration().getFilePath().getAbsolutePath(); ! // Remove old image ! fSnip.getAttachments().removeAttachment(longImageName + ".gif"); ! ! String directoryName = SnipSnapSpacePath + "/" + snipname; ! File directory = new File(directoryName); ! if (!directory.exists()) { ! directory.mkdirs(); ! } ! String gifFilename = directoryName + "/" + longImageName; ! // File file = new File(); ! // Get the number of bytes in the file ! // long length = file.length(); ! MathMLToGIFConverter conv = new MathMLToGIFConverter(); ! // String test = ! // "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><math ! // mode=\"display\"><mrow><munderover><mo>∫</mo><mn>1</mn><mi>x</mi></munderover><mfrac><mi>dt</mi><mi>t</mi></mfrac></mrow></math>"; ! try { ! File imageFile = conv.convert(mathmlBuffer.toString(), gifFilename); ! if (imageFile != null) { ! Attachment attachment = new Attachment(longImageName, "image/gif", imageFile.length(), new Date(), snipname + "/" + longImageName, true); ! // fSnip.getAttachments().addAttachment(longImageName, "image/gif", imageFile.length(), snipname + "/" + longImageName, true); ! fSnip.getAttachments().addAttachment(attachment); ! StringWriter writer = new StringWriter(); ! ! SnipLink.appendImage(writer, fSnip, shortImageName, encodedMathContent, "gif", null); ! ! mathBuffer = writer.getBuffer(); ! mathStrings[1] = mathBuffer.toString(); ! } else { ! mathStrings[1] = encodedMathContent; ! } ! } catch (IOException e) { ! // TODO Auto-generated catch block ! e.printStackTrace(); ! mathStrings[1] = encodedMathContent; } mathBuffer.setLength(0); CachedWikipediaFilter.copyWhite(mathBuffer, mathContent); |