Back to index

texmacs  1.0.7.15
Classes | Functions | Variables
parsetex.cpp File Reference
#include "Tex/convert_tex.hpp"
#include "converter.hpp"

Go to the source code of this file.

Classes

struct  latex_parser

Functions

string string_arg (tree t)
static bool is_regular (tree t)
static bool is_tex_alpha (char c)
static bool is_tex_alpha (string s)
static string sharp_to_arg (string s, tree args)
static bool is_math_environment (tree t)
static bool is_text_argument (string cmd, int remaining_arity)
tree accented_to_Cork (tree t)
static bool japanese_tex (string &s)
static bool korean_tex (string &s)
static bool chinese_tex (string &s)
static bool taiwanese_tex (string &s)
tree parse_latex (string s, bool change)
tree parse_latex_document (string s, bool change)

Variables

bool textm_class_flag
hashmap< string, int > textm_recursion_level (0)
static char Cork_unaccented [128]
static char Cork_accent [128]

Function Documentation

Definition at line 876 of file parsetex.cpp.

                          {
  if (arity (t) == 0) return t;
  int i, n=N(t);
  tree r (t, n);
  for (i=0; i<n; i++) r[i]= accented_to_Cork (t[i]);
  if (is_compound (t[0])) return r;

  string s= t[0]->label;
  if ((N(s)==2) && (s[0]=='\\') && (n==2) &&
      is_atomic (r[1]) && (N(r[1]->label)<=2)) {
    string v= r[1]->label;
    if (N(v)==0) {
      if (s[1] == '`' ) {
       string ret_s (1);
       ret_s[0]= '\000';
       return ret_s;
      }
      if (s[1] == '\'') return "\001";
      if (s[1] == '^' ) return "\136";
      if (s[1] == '\"') return "\004"; // "
      if (s[1] == '~' ) return "\176";
      if (s[1] == '=' ) return "\026";
      if (s[1] == '.' ) return "\137";
      if (s[1] == 'u' ) return "\025";
      if (s[1] == 'v' ) return "\024";
      if (s[1] == 'H' ) return "\175";
      if (s[1] == 'c' ) return "\030";
    }
    else {
      char c1= v[0], c2= s[1];
      if (v == "\\i") c1= (char) 25;
      if ((N(v)==1) || (v=="\\i"))
       for (i=0; i<127; i++)
         if ((Cork_unaccented[i]==c1) && (Cork_accent[i]==c2))
           return tree (string ((char) (i+128)));
    }
  }
  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool chinese_tex ( string s) [static]

Definition at line 1054 of file parsetex.cpp.

                        {
  if (search_forwards ("\\kaishu", s) != -1)
    s= replace (s, "\\kaishu", "");
  if (search_forwards ("\\begin{CJK}{GBK}{kai}", s) != -1)
    s= replace (s, "\\begin{CJK}{GBK}{kai}", "");
  if (search_forwards ("\\begin{CJK*}{GBK}{kai}", s) != -1)
    s= replace (s, "\\begin{CJK*}{GBK}{kai}", "");
  if (search_forwards ("\\end{CJK}", s) != -1)
    s= replace (s, "\\end{CJK}", "");
  if (search_forwards ("\\end{CJK*}", s) != -1)
    s= replace (s, "\\end{CJK*}", "");
  if (search_forwards ("\\CJKindent", s) != -1)
    s= replace (s, "\\CJKindent", "");
  if (search_forwards ("\\CJKcaption{GBk}", s) != -1)
    s= replace (s, "\\CJKcaption{GBK}", "");
  if (search_forwards ("\\usepackage{CJK}", s) != -1) {
    s= replace (s, "\\usepackage{CJK}", "");
    s= convert (s, "cp936", "UTF-8");
    return true;
  }
  if (search_forwards ("\\documentclass{cctart}", s) != -1) {
    s= replace (s, "\\documentclass{cctart}", "\\documentclass{article}");
    s= convert (s, "cp936", "UTF-8");
    return true;
  }
  if (search_forwards ("\\documentclass[CJK]{cctart}", s) != -1) {
    s= replace (s, "\\documentclass[CJK]{cctart}", "\\documentclass{article}");
    s= convert (s, "cp936", "UTF-8");
    return true;
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool is_math_environment ( tree  t) [static]

Definition at line 455 of file parsetex.cpp.

                             {
  //cout << "t= " << t << "\n";
  tree b= t[N(t)-2];
  tree e= t[N(t)-1];
  if (!is_concat (b)) b= tree (CONCAT, b);
  if (!is_concat (e)) e= tree (CONCAT, e);
  int i, j;
  for (i=N(b)-1; i>=0; i--)
    if (is_tuple (b[i]) && N(b[i])>0 && is_atomic (b[i][0]))
      if (latex_type (b[i][0]->label) == "math-environment")
       break;
  for (j=0; j<N(e); j++)
    if (is_tuple (e[j]) && N(e[j])>0 && is_atomic (e[j][0]))
      if (latex_type (e[j][0]->label) == "math-environment")
       break;
  if (i >= 0 && j < N(e)) {
    string bs= b[i][0]->label;
    string es= e[j][0]->label;
    bool ok=
      starts (bs, "\\begin-") &&
      starts (es, "\\end-") &&
      bs (7, N(bs)) == es (5, N(es));
    //cout << t[1] << " -> " << ok << "\n";
    return ok;
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool is_regular ( tree  t) [static]

Definition at line 84 of file parsetex.cpp.

                    {
  if (!is_tuple (t)) return true;
  if (N(t) == 0 || !is_atomic (t[0])) return false;
  string s= t[0]->label;
  return !starts (s, "\\begin-") && !starts (s, "\\end-");
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool is_tex_alpha ( char  c) [static]

Definition at line 92 of file parsetex.cpp.

                      {
  return is_alpha (c) || c == '@';
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool is_tex_alpha ( string  s) [static]

Definition at line 97 of file parsetex.cpp.

                        {
  for (int i=0; i<N(s); i++)
    if (!is_alpha (s[i]) && s[i] != '@') return false;
  return true;
}

Here is the call graph for this function:

static bool is_text_argument ( string  cmd,
int  remaining_arity 
) [static]

Definition at line 484 of file parsetex.cpp.

                                                   {
  // FIXME: this test should be improved using DRD properties
  (void) remaining_arity;
  return cmd == "\\label" || cmd == "\\ref";
}

Here is the caller graph for this function:

static bool japanese_tex ( string s) [static]

Definition at line 1015 of file parsetex.cpp.

                         {
  if (search_forwards ("\\documentclass{jarticle}", s) != -1) {
    s= replace (s, "\\documentclass{jarticle}", "\\documentclass{article}");
    s= convert (s, "ISO-2022-JP", "UTF-8");
    return true;
  }
  if (search_forwards ("\\documentclass{jbook}", s) != -1) {
    s= replace (s, "\\documentclass{jbook}", "\\documentclass{book}");
    s= convert (s, "ISO-2022-JP", "UTF-8");
    return true;
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool korean_tex ( string s) [static]

Definition at line 1030 of file parsetex.cpp.

                       {
  if (search_forwards ("\\usepackage{hangul}", s) != -1 ||
      search_forwards ("\\usepackage{hfont}", s) != -1 ||
      search_forwards ("]{hangul}", s) != -1 ||
      search_forwards ("]{hfont}", s) != -1)
    {
      s= replace (s, "\\usepackage{hangul}", "");
      s= replace (s, "\\usepackage{hfont}", "");
      s= convert (s, "EUC-KR", "UTF-8");
      return true;
    }
  if (search_forwards ("\\usepackage{dhucs}", s) != -1 ||
      search_forwards ("\\usepackage{memhangul-ucs}", s) != -1 ||
      search_forwards ("]{dhucs}", s) != -1 ||
      search_forwards ("]{memhangul-ucs}", s) != -1)
    {
      s= replace (s, "\\usepackage{dhucs}", "");
      s= replace (s, "\\usepackage{memhangul-ucs}", "");
      return true;
    }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree parse_latex ( string  s,
bool  change 
)

Definition at line 1115 of file parsetex.cpp.

                                    {
  s= dos_to_better (s);
  string lan= "";
  if (japanese_tex (s)) lan= "japanese";
  else if (korean_tex (s)) lan= "korean";
  else if (taiwanese_tex (s)) lan= "taiwanese";
  else if (chinese_tex (s)) lan= "chinese";
  bool unicode= (lan == "chinese" || lan == "japanese" ||
               lan == "korean" || lan == "taiwanese");
  latex_parser ltx (unicode);
  tree r= accented_to_Cork (ltx.parse (s, change));
  if (lan == "") return r;
  return compound ("!language", r, lan);
}

Here is the call graph for this function:

Here is the caller graph for this function:

tree parse_latex_document ( string  s,
bool  change 
)

Definition at line 1131 of file parsetex.cpp.

                                             {
  return compound ("!file", parse_latex (s, change));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static string sharp_to_arg ( string  s,
tree  args 
) [static]

Definition at line 426 of file parsetex.cpp.

                                   {
  int i;
  string r;
  for (i=0; i<N(s); i++)
    if ((s[i]=='#') && ((i+1)<N(s)) && (s[i+1]>='1') && (s[i+1]<='9')) {
      int nr= ((int) s[++i]) - ((int) '0');
      if (N(args)>nr) r << string_arg (args[nr]);
    }
    else r << s[i];
  return r;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 355 of file fromtex.cpp.

                    {
  if (is_atomic (t)) return t->label;
  else if (is_concat (t)) {
    string r;
    int i, n= N(t);
    for (i=0; i<n; i++)
      r << string_arg (t[i]);
    return r;
  }
  else if (is_func (t, RSUB, 1))
    return "_" * string_arg (t[0]);
  else if (is_func (t, RSUP, 1))
    return "^" * string_arg (t[0]);
  else if (is_func (t, APPLY, 1) && t[0] == "nbsp")
    return " ";
  else {
    //cout << "t= " << t << "\n";
    return "";
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool taiwanese_tex ( string s) [static]

Definition at line 1088 of file parsetex.cpp.

                          {
  if (search_forwards ("\\usepackage{CJKvert,type1cm}", s) != -1)
    s= replace (s, "\\usepackage{CJKvert,type1cm}", "");
  if (search_forwards ("\\begin{CJK}{Bg5}{aming}", s) != -1)
    s= replace (s, "\\begin{CJK}{Bg5}{aming}", "");
  if (search_forwards ("\\begin{CJK}{Bg5}{kai}", s) != -1)
    s= replace (s, "\\begin{CJK}{Bg5}{kai}", "");
  if (search_forwards ("\\end{CJK}", s) != -1)
    s= replace (s, "\\end{CJK}", "");
  if (search_forwards ("\\CJKcaption{Bg5}", s) != -1)
    s= replace (s, "\\CJKcaption{Bg5}", "");
  if (search_forwards ("\\CJKindent", s) != -1)
    s= replace (s, "\\CJKindent", "");
  if (search_forwards ("\\usepackage{CJK}", s) != -1) {
    s= replace (s, "\\usepackage{CJK}", "");
    s= convert (s, "cp950", "UTF-8");
    return true;
  }
  if (search_forwards ("\\usepackage{CJK*}", s) != -1) {
    s= replace (s, "\\usepackage{CJK*}", "");
    s= convert (s, "cp950", "UTF-8");
    return true;
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char Cork_accent[128] [static]
Initial value:
 {
  'u' , 'k' , '\'', 'v' , 'v' , 'v' , 'k' , 'u' ,
  '\'', 'v' , ' ' , '\'', 'v' , ' ' , 'H' , '\'',
  'v' , '\'', 'v' , 'c' , 'v' , 'c' , 'H' , 'r' ,
  '\"', '\'', 'v' , '.' , ' ' , '.' , '=' , ' ' , 
  'u' , 'k' , '\'', 'v' , 'v' , 'v' , 'k' , 'u' ,
  '\'', 'v' , ' ' , '\'', 'v' , ' ' , 'H' , '\'',
  'v' , '\'', 'v' , 'c' , 'v' , 'c' , 'H' , 'r' ,
  '\"', '\'', 'v' , '.' , ' ' , ' ' , ' ' , ' ' , 
  '`' , '\'', '^' , '~' , '\"', ' ' , ' ' , 'c' , 
  '`' , '\'', '^' , '\"', '`' , '\'', '^' , '\"', 
  '=' , '~' , '`' , '\'', '^' , '~' , '\"', ' ' , 
  ' ' , '`' , '\'', '^' , '\"', '\'', ' ' , ' ' , 
  '`' , '\'', '^' , '~' , '\"', ' ' , ' ' , 'c' , 
  '`' , '\'', '^' , '\"', '`' , '\'', '^' , '\"', 
  '=' , '~' , '`' , '\'', '^' , '~' , '\"', ' ' , 
  ' ' , '`' , '\'', '^' , '\"', '\'', ' ' , ' '   
}

Definition at line 856 of file parsetex.cpp.

char Cork_unaccented[128] [static]
Initial value:
 {
  'A', 'A', 'C', 'C', 'D', 'E', 'E', 'G',
  'L', 'L', ' ', 'N', 'N', ' ', 'O', 'R',
  'R', 'S', 'S', 'S', 'T', 'T', 'U', 'U',
  'Y', 'Z', 'Z', 'Z', ' ', 'I', 'd', ' ',
  'a', 'a', 'c', 'c', 'd', 'e', 'e', 'g',
  'l', 'l', ' ', 'n', 'n', ' ', 'o', 'r',
  'r', 's', 's', 's', 't', 't', 'u', 'u',
  'y', 'z', 'z', 'z', ' ', ' ', ' ', ' ',
  'A', 'A', 'A', 'A', 'A', 'A', ' ', 'C',
  'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I',
  'D', 'N', 'O', 'O', 'O', 'O', 'O', ' ',
  ' ', 'U', 'U', 'U', 'U', 'Y', ' ', ' ',
  'a', 'a', 'a', 'a', 'a', 'a', ' ', 'c',
  'e', 'e', 'e', 'e', 25 , 25 , 25 , 25 ,
  'd', 'n', 'o', 'o', 'o', 'o', 'o', ' ',
  ' ', 'u', 'u', 'u', 'u', 'y', ' ', ' '
}

Definition at line 837 of file parsetex.cpp.

Definition at line 19 of file fromtex.cpp.