]> git.lyx.org Git - lyx.git/blob - development/tools/generate_symbols_list.py
Fix indentation for 'python -tt'
[lyx.git] / development / tools / generate_symbols_list.py
1 #!/usr/bin/python
2 import sys,string,re,os,os.path
3
4 def get_code(code, font):
5     if font != "dontknowwhichfontusesthisstrangeencoding":
6         return code
7     if code < 10:
8         return code+161
9     elif code < 32:
10         return code+163
11     else:
12         return code
13
14 font_names = {}
15 symbols = {}
16 xsymbols = {}
17
18 ignore_list = ["not", "braceld", "bracerd", "bracelu", "braceru",
19                "lmoustache", "rmoustache", "lgroup", "rgroup", "bracevert"]
20
21 def process(file):
22     fh = open(file)
23     lines = fh.readlines()
24     fh.close()
25     package, ext = os.path.splitext(os.path.basename(file))
26     if ext != ".sty":
27         package = ''
28
29     n = len(lines)
30     for i in xrange(n):
31         line = lines[i]
32         mo =  re.match(r'\s*%.*', line)
33         if mo != None:
34             continue
35         next_line = ""
36         if i+1 < n:
37             next_line = lines[i+1]
38
39         # some entries are spread over two lines so we join the next line
40         # to the current one, (if current line contains a comment, we remove it)
41         line = string.split(line,'%')[0]+next_line
42
43         mo =  re.match(r'.*\\DeclareSymbolFont\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}.*', line)
44         if mo != None:
45             font_names[mo.group(1)] = mo.group(3)
46
47         mo =  re.match(r'.*\\DeclareMath(Symbol|Delimiter)\s*\{?\\(\w*?)\}?\s*\{?\\(.*?)\}?\s*\{(.*?)\}\s*\{([\'"]?)(.*?)\}.*', line)
48         code = -1
49         try:
50             if mo != None:
51                 symbol = mo.group(2)
52                 type = mo.group(3)
53                 font = mo.group(4)
54                 if mo.group(5) == '':
55                     code = int(mo.group(6))
56                 elif mo.group(5) == '"':
57                     code = int(mo.group(6), 16)
58                 else:
59                     code = int(mo.group(6), 8)
60             else:
61                 mo = re.match(r'.*\\edef\\(\w*?)\{.*?\{\\hexnumber@\\sym(.*?)\}(.*?)\}', line)
62                 if mo != None:
63                     symbol = mo.group(1)
64                     type = "mathord"
65                     font = mo.group(2)
66                     code = int(mo.group(3), 16)
67         except ValueError:
68                 code = -1
69
70         if mo != None and symbol not in ignore_list:
71             mo2 = re.match(r'\s*\\def\\(.*?)\{', next_line)
72             if mo2 != None and symbol == mo2.group(1)+"op":
73                 sys.stderr.write("%s -> %s\n" % (symbol, mo2.group(1)))
74                 symbol = mo2.group(1)
75
76             if font_names.has_key(font):
77                 font = font_names[font]
78
79             code = get_code(code, font)
80             if code < 0:
81                 continue
82
83             xcode = 0
84             if xsymbols.has_key(symbol):
85                 xcode = xsymbols[symbol]
86                 del xsymbols[symbol]
87
88             if symbols.has_key(symbol):
89                 sys.stderr.write(symbol+ " exists\n")
90                 if code != symbols[symbol]:
91                     sys.stderr.write("code is not equal!!!\n")
92             else:
93                 symbols[symbol] = code
94                 if package == '':
95                     print "%-18s %-4s %3d %3d %-6s" % (symbol,font,code,xcode,type)
96                 else:
97                     print "%-18s %-4s %3d %3d %-9s x  %s" % (symbol,font,code,xcode,type,package)
98
99
100 path = os.path.split(sys.argv[0])[0]
101 fh = open(os.path.join(path, "x-font"))
102 lines = fh.readlines()
103 fh.close()
104 for line in lines:
105     x = string.split(line)
106     symbol = x[0]
107     code = string.atoi(x[1],16)
108     xsymbols[symbol] = code
109
110 for file in sys.argv[1:]:
111     print "# Generated from " + os.path.basename(file) + "\n"
112     process(file)
113     print
114
115 exceptions = [
116     ("neq", "x", 0, 185, "mathrel"),
117     ("textdegree", "x", 0, 176, "mathord"),
118     ("cong", "x", 0, 64, "mathrel"),
119     ("surd", "x", 0, 214, "mathord")
120 ]
121
122 if xsymbols.has_key("leq"):
123     sys.exit(0)
124
125 for x in exceptions:
126     print "%-18s %-4s %3d %3d %-6s" % x
127     if xsymbols.has_key(x[0]):
128         del xsymbols[x[0]]
129
130 print """
131 lyxbar             cmsy 161   0 mathord
132 lyxeq              cmr   61   0 mathord
133 lyxdabar           msa   57   0 mathord
134 lyxright           msa   75   0 mathord
135 lyxleft            msa   76   0 mathord
136 """
137
138 for symbol in xsymbols.keys():
139     sys.stderr.write(symbol+"\n")