##// END OF EJS Templates
Fixed typo
Andrew Murray -
Show More
@@ -1,89 +1,89 b''
1 # coding: utf-8
1 # coding: utf-8
2
2
3 # This script autogenerates `IPython.core.latex_symbols.py`, which contains a
3 # This script autogenerates `IPython.core.latex_symbols.py`, which contains a
4 # single dict , named `latex_symbols`. The keys in this dict are latex symbols,
4 # single dict , named `latex_symbols`. The keys in this dict are latex symbols,
5 # such as `\\alpha` and the values in the dict are the unicode equivalents for
5 # such as `\\alpha` and the values in the dict are the unicode equivalents for
6 # those. Most importantly, only unicode symbols that are valid identifers in
6 # those. Most importantly, only unicode symbols that are valid identifers in
7 # Python 3 are included.
7 # Python 3 are included.
8
8
9 #
9 #
10 # The original mapping of latex symbols to unicode comes from the `latex_symbols.jl` files from Julia.
10 # The original mapping of latex symbols to unicode comes from the `latex_symbols.jl` files from Julia.
11
11
12 from __future__ import print_function
12 from __future__ import print_function
13 import os, sys
13 import os, sys
14
14
15 if not sys.version_info[0] == 3:
15 if not sys.version_info[0] == 3:
16 print("This script must be run with Python 3, exiting...")
16 print("This script must be run with Python 3, exiting...")
17 sys.exit(1)
17 sys.exit(1)
18
18
19 # Import the Julia LaTeX symbols
19 # Import the Julia LaTeX symbols
20 print('Importing latex_symbols.js from Julia...')
20 print('Importing latex_symbols.js from Julia...')
21 import requests
21 import requests
22 url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl'
22 url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl'
23 r = requests.get(url)
23 r = requests.get(url)
24
24
25
25
26 # Build a list of key, value pairs
26 # Build a list of key, value pairs
27 print('Building a list of (latex, unicode) key-vaule pairs...')
27 print('Building a list of (latex, unicode) key-value pairs...')
28 lines = r.text.splitlines()[60:]
28 lines = r.text.splitlines()[60:]
29 lines = [line for line in lines if '=>' in line]
29 lines = [line for line in lines if '=>' in line]
30 lines = [line.replace('=>',':') for line in lines]
30 lines = [line.replace('=>',':') for line in lines]
31
31
32 def line_to_tuple(line):
32 def line_to_tuple(line):
33 """Convert a single line of the .jl file to a 2-tuple of strings like ("\\alpha", "Ξ±")"""
33 """Convert a single line of the .jl file to a 2-tuple of strings like ("\\alpha", "Ξ±")"""
34 kv = line.split(',')[0].split(':')
34 kv = line.split(',')[0].split(':')
35 # kv = tuple(line.strip(', ').split(':'))
35 # kv = tuple(line.strip(', ').split(':'))
36 k, v = kv[0].strip(' "'), kv[1].strip(' "')
36 k, v = kv[0].strip(' "'), kv[1].strip(' "')
37 # if not test_ident(v):
37 # if not test_ident(v):
38 # print(line)
38 # print(line)
39 return k, v
39 return k, v
40
40
41 assert line_to_tuple(' "\\sqrt" : "\u221A",') == ('\\sqrt', '\u221A')
41 assert line_to_tuple(' "\\sqrt" : "\u221A",') == ('\\sqrt', '\u221A')
42 lines = [line_to_tuple(line) for line in lines]
42 lines = [line_to_tuple(line) for line in lines]
43
43
44
44
45 # Filter out non-valid identifiers
45 # Filter out non-valid identifiers
46 print('Filtering out characters that are not valid Python 3 identifiers')
46 print('Filtering out characters that are not valid Python 3 identifiers')
47
47
48 def test_ident(i):
48 def test_ident(i):
49 """Is the unicode string valid in a Python 3 identifer."""
49 """Is the unicode string valid in a Python 3 identifer."""
50 # Some characters are not valid at the start of a name, but we still want to
50 # Some characters are not valid at the start of a name, but we still want to
51 # include them. So prefix with 'a', which is valid at the start.
51 # include them. So prefix with 'a', which is valid at the start.
52 return ('a' + i).isidentifier()
52 return ('a' + i).isidentifier()
53
53
54 assert test_ident("Ξ±")
54 assert test_ident("Ξ±")
55 assert not test_ident('‴')
55 assert not test_ident('‴')
56
56
57 valid_idents = [line for line in lines if test_ident(line[1])]
57 valid_idents = [line for line in lines if test_ident(line[1])]
58
58
59
59
60 # Write the `latex_symbols.py` module in the cwd
60 # Write the `latex_symbols.py` module in the cwd
61
61
62 s = """# encoding: utf-8
62 s = """# encoding: utf-8
63
63
64 # DO NOT EDIT THIS FILE BY HAND.
64 # DO NOT EDIT THIS FILE BY HAND.
65
65
66 # To update this file, run the script /tools/gen_latex_symbols.py using Python 3
66 # To update this file, run the script /tools/gen_latex_symbols.py using Python 3
67
67
68 # This file is autogenerated from the file:
68 # This file is autogenerated from the file:
69 # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl
69 # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl
70 # This original list is filtered to remove any unicode characters that are not valid
70 # This original list is filtered to remove any unicode characters that are not valid
71 # Python identifiers.
71 # Python identifiers.
72
72
73 latex_symbols = {\n
73 latex_symbols = {\n
74 """
74 """
75 for line in valid_idents:
75 for line in valid_idents:
76 s += ' "%s" : "%s",\n' % (line[0], line[1])
76 s += ' "%s" : "%s",\n' % (line[0], line[1])
77 s += "}\n"
77 s += "}\n"
78
78
79 s += """
79 s += """
80
80
81 reverse_latex_symbol = { v:k for k,v in latex_symbols.items()}
81 reverse_latex_symbol = { v:k for k,v in latex_symbols.items()}
82 """
82 """
83
83
84 fn = os.path.join('..','IPython','core','latex_symbols.py')
84 fn = os.path.join('..','IPython','core','latex_symbols.py')
85 print("Writing the file: %s" % fn)
85 print("Writing the file: %s" % fn)
86 with open(fn, 'w', encoding='utf-8') as f:
86 with open(fn, 'w', encoding='utf-8') as f:
87 f.write(s)
87 f.write(s)
88
88
89
89
General Comments 0
You need to be logged in to leave comments. Login now