Subversion Repositories wimsdev

Rev

Rev 5996 | Rev 13310 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     itex2MML  1.3.3
  3.     renamed to "wims_mathml"
  4.     Slightly modified for WIMS mathml usage 05/2012
  5.     J.M. Evers, B. Perrin-Riou
  6.    
  7. */
  8.  
  9. #include <cstdio>
  10. #include <string>
  11. #include "wims_mathml.h"
  12.  
  13. // these C headers are needed to compile on OpenSuSE Linux (and others?) to use atoi/strlen
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <sys/time.h>
  17.  
  18.  
  19. int use_javascript;
  20. char wims_texfontsize[64]; // is set by cmd-line argument "--tex-size" via wims
  21. char wims_mathml_id[64]; // is set by cmd-line argument "--mathml-id" via wims
  22.  
  23. const int set_javascript(){
  24.     if(use_javascript == 1){
  25.         return 1;
  26.     }
  27.     else
  28.     {
  29.         return 0;
  30.     }
  31. }
  32. const char * read_mathml_id(){
  33.     return wims_mathml_id; // wims_mathml.y will use this in first <mstyle id="read_mathml_id()" ...
  34.     // the whole mathml-string will get this "id" ; does not interfere (?) with "maction" tag, which is on "sub math level"
  35. }
  36.  
  37. const char * read_fontsize(){ // declared in wims_mathml.h
  38.     // in not given : mathsize="110%"
  39.     if(strlen(wims_texfontsize) == 0){snprintf(wims_texfontsize,sizeof(wims_texfontsize),"%s","110%");}
  40.     return wims_texfontsize; // wims_mathml.y will use this in first <mstyle mathsize="read_fontsize()" ...
  41. }
  42. // count number of substring occurences
  43. int count_substrings( const std::string & str , const std::string & obj ) {
  44.     int n = 0;
  45.     std::string ::size_type position = 0;
  46.     while( (position = obj.find( str, position )) != std::string::npos ){
  47.         n++;
  48.         position += str.size();
  49.     }
  50.     return n;
  51. }
  52.  
  53. int main (int argc, char ** argv)
  54. {
  55.         bool bPrintItex = false;
  56.         bool bRawFilter = false;
  57.         bool bInline    = false;
  58.         bool bDisplay   = false;
  59.         bool bTexString = false;
  60.         bool bStop = false;
  61.         bool bForbidMarkup = false;
  62.         // wims
  63.         int MAX_MML_SIZE = 8192;
  64.         int i_texstring = 0;
  65.         int insize=0;
  66.         using namespace std;
  67.         std::string itex;
  68.         for (int arg = 1; arg < argc; arg++)
  69.                 {
  70.                         std::string args = argv[arg];
  71.  
  72.                         if (args == "--version" || args == "-v")
  73.                                 {
  74.                                         fputs("wims_mathml version " ITEX2MML_VERSION "\n"
  75.                                               "See http://golem.ph.utexas.edu/~distler/blog/wims_mathml.html for more information.\n", stdout);
  76.                                         bStop = true;
  77.                                         break;
  78.                                 }
  79.  
  80.                         if (args == "--help" || args == "-h"){
  81.                             fputs ("usage: wims_mathml [OPTIONS]\n"
  82.                             "\n"
  83.                             "wims_mathml filters an input text stream (e.g., an XHTML web page) converting itex expressions\n"
  84.                             "to MathML. Inline itex expressions are delimited either side by single dollar symbols ($):\n"
  85.                             "\n"
  86.                             "\t<p>The parameters $\\alpha$ and $\\beta$ in the function $f(x)$ are defined below.</p>\n"
  87.                             "\n"
  88.                             "For normal display of equations, etc., itex expressions can be delimited with double dollar\n"
  89.                             "symbols ($$) either side or by \\[ to the left and \\] to the right:\n"
  90.                             "\n"
  91.                             "\t<p class=\"equation\">\\[\n"
  92.                             "\t\tf(x) = \\alpha x + \\frac{\\beta}{1+|x|}\n"
  93.                             "\t\\]</p>\n"
  94.                             "\n"
  95.                             "wims_mathml Options:\n"
  96.                             "\n"
  97.                             "  --raw-filter    filter input stream, converting equations as found to MathML [stops on error]\n"
  98.                             "  --inline        converts a single itex equation, without any $ symbols, to inline MathML\n"
  99.                             "  --display       converts a single itex equation, without any $ symbols, to display-mode MathML\n"
  100.                             "  --forbid-markup forbid markup (more precisely, the '<' and '>' characters) in itex equations\n"
  101.                             "  --print-itex    used in conjuction with --inline or --display: prints the itex string\n"
  102.                             "\n"
  103.                             "WIMS USAGE:"
  104.                             "  --mathml-id      unique random id for every mathml string\n"
  105.                             "  --tex-size       base fontsize of the mathml string\n"
  106.                             "  --max-mml-size   wims maximum stringlength  \n"
  107.                             "  --tex-string     the actual math string\n"
  108.                             "\n"
  109.                             "For further information, see http://golem.ph.utexas.edu/~distler/blog/wims_mathml.html\n", stdout);
  110.                             bStop = true;
  111.                             break;
  112.                         }
  113.                         if (args == "--print-itex"){
  114.                             bPrintItex = true;
  115.                             bRawFilter = false;
  116.                             continue;
  117.                         }
  118.                         if (args == "--forbid-markup"){
  119.                             bRawFilter = false;
  120.                             bForbidMarkup = true;
  121.                             continue;
  122.                         }
  123.                         if (args == "--inline"){
  124.                             bRawFilter = false;
  125.                             bInline    = true;
  126.                             bDisplay   = false;
  127.                             continue;
  128.                         }
  129.                         if (args == "--display"){
  130.                             bRawFilter = false;
  131.                             bInline    = false;
  132.                             bDisplay   = true;
  133.                             continue;
  134.                         }
  135.                         if (args == "--raw-filter"){
  136.                             bRawFilter = true;
  137.                             bPrintItex = false;
  138.                             bInline    = false;
  139.                             bDisplay   = false;
  140.                             continue;
  141.                         }
  142.                         // wims        
  143.                         if (args == "--use-zoom"){
  144.                             use_javascript = atoi( argv[arg+1] );
  145.                             continue;
  146.                         }
  147.                         if (args == "--tex-size"){
  148.                             snprintf(wims_texfontsize,sizeof(wims_texfontsize),"%s",argv[arg+1]);
  149.                             continue;
  150.                         }
  151.                         if (args == "--max-mml-size"){
  152.                             MAX_MML_SIZE = atoi( argv[arg+1]);
  153.                             continue;
  154.                         }
  155.                         if (args == "--tex-string"){
  156.                             bPrintItex = false;
  157.                             bRawFilter = false;
  158.                             bTexString = true;
  159.                             bDisplay = false;
  160.                             i_texstring = arg+1;
  161.                             break;
  162.                         }
  163.                 }
  164.         if (bStop) return 0;
  165.         /* every mathml-string will have unique id */
  166.         struct timeval tv;
  167.         struct timezone tz;
  168.         struct tm *tm;
  169.         gettimeofday(&tv, &tz);
  170.         tm=localtime(( const time_t * ) &tv.tv_sec);
  171.         snprintf(wims_mathml_id,sizeof(wims_mathml_id),"wims_mathml%ld",long(tv.tv_usec));
  172.  
  173.         if(bTexString){ // WIMS modification: reads a 'latex string' from commandline, if arg="--tex-string"
  174.             char *input;
  175.             int dollarcnt = 0;
  176.             input = argv[i_texstring];
  177.             while(input != NULL){
  178.                  for(int i = 0; i <  strlen(input); i++){
  179.                     switch(input[i]){
  180.                         case '<':itex+= "\\lt ";break; // < \lt
  181.                         case '>':itex+= "\\gt ";break; // < \gt
  182.                         case '$':dollarcnt++;break; // ignore $ we put them directly in itex
  183.                         default:itex+=(char) input[i];
  184.                     }
  185.                 }
  186.                 i_texstring++;// probably never more than 1 ...
  187.                 input = argv[i_texstring];
  188.             }
  189.             // common error to forget \right.
  190.             // this must be done before adding $ around itex
  191.             int left =  count_substrings("\\left\\" ,itex) + count_substrings("\\left[" ,itex) + count_substrings("\\left(" ,itex) + count_substrings("\\left{" ,itex) + count_substrings("\\left." ,itex) + count_substrings("\\left \\" ,itex) + count_substrings("\\left [" ,itex) + count_substrings("\\left (" ,itex) + count_substrings("\\left {" ,itex) + count_substrings("\\left ." ,itex) ;
  192.             int right = count_substrings("\\right\\",itex) + count_substrings("\\right]",itex) + count_substrings("\\right)",itex) + count_substrings("\\right}",itex) + count_substrings("\\right.",itex) + count_substrings("\\right \\",itex) + count_substrings("\\right ]",itex) + count_substrings("\\right )",itex) + count_substrings("\\right }",itex) + count_substrings("\\right .",itex) ;
  193.             if( left != right){
  194.                 if( left > right){
  195.                     for(int i = 0 ; i<left-right;i++){
  196.                         itex+=" \\right.";
  197.                     }
  198.                 }
  199.                 else
  200.                 {
  201.                     for(int i = 0 ; i<right-left;i++){
  202.                         itex="\\left. "+itex;
  203.                     }
  204.                 }
  205.             }
  206.  
  207.             // finish the math mode with appropriate $-signs
  208.             if( dollarcnt > 2 ){
  209.                 bDisplay = true; // use display mode
  210.                 itex="$$" + itex;
  211.                 itex+="$$";
  212.             }
  213.             else
  214.             {
  215.                 bInline = true; // use inline mode
  216.                     itex="$" + itex;
  217.                     itex+="$";
  218.             }
  219.         }
  220.         else // standard syntax echo  "\frac{1}{2}" | wims_mathml --inline > test.xml
  221.         { // nothing changed here...
  222.             #define BUFSIZE 1024
  223.             char buffer[BUFSIZE];
  224.             if (bInline)  itex += "$";
  225.             if (bDisplay) itex += "$$";
  226.             while (fgets (buffer, BUFSIZE, stdin)) itex += buffer;
  227.             if (bInline)  itex += "$";
  228.             if (bDisplay) itex += "$$";
  229.         }
  230.  
  231.         if (bPrintItex)
  232.                 {
  233.                         fputs (itex.c_str (), stdout);
  234.                         fputs ("\n", stdout);
  235.                         fflush (stdout);
  236.                 }
  237.  
  238.         if (!bInline && !bDisplay)
  239.                 {
  240.                         if (bRawFilter)
  241.                                 wims_mathml_filter (itex.c_str(), itex.size());
  242.                         else
  243.                                 if (bForbidMarkup)
  244.                                         wims_mathml_strict_html_filter (itex.c_str(), itex.size());
  245.                                 else
  246.                                         wims_mathml_html_filter (itex.c_str(), itex.size());
  247.                         return 0;
  248.                 }
  249.         char * mathml = wims_mathml_parse (itex.c_str(), itex.size() );
  250.  
  251.         if (mathml)
  252.                 {
  253.                     if(bTexString){ // wims usage
  254.                         if(strlen(mathml) > (MAX_MML_SIZE)){
  255.                             fputs("ERROR",stdout);
  256.                         }
  257.                         else
  258.                         {
  259.                             fputs (mathml, stdout);
  260.                         }
  261.                     }
  262.                     else
  263.                     {
  264.                         fputs (mathml, stdout);
  265.                     }
  266.                     wims_mathml_free_string (mathml);
  267.                     mathml = 0;
  268.                 }
  269.         else
  270.                 {
  271.                     if(bTexString){ // wims usage
  272.                         fprintf(stdout,"ERROR");
  273.                     }
  274.                     else
  275.                     {
  276.                         fputs ("wims_mathml: itex parser failed to generate MathML from itex!\n", stderr);
  277.                     }
  278.                 }
  279.         return 0;
  280. }
  281.