Show More
@@ -1,92 +1,92 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 identifiers in |
|
6 | # those. Most importantly, only unicode symbols that are valid identifiers 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 | import os, sys |
|
12 | import os, sys | |
13 |
|
13 | |||
14 |
if |
|
14 | if sys.version_info[0] < 3: | |
15 | print("This script must be run with Python 3, exiting...") |
|
15 | print("This script must be run with Python 3, exiting...") | |
16 | sys.exit(1) |
|
16 | sys.exit(1) | |
17 |
|
17 | |||
18 | # Import the Julia LaTeX symbols |
|
18 | # Import the Julia LaTeX symbols | |
19 | print('Importing latex_symbols.js from Julia...') |
|
19 | print('Importing latex_symbols.js from Julia...') | |
20 | import requests |
|
20 | import requests | |
21 | url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/stdlib/REPL/src/latex_symbols.jl' |
|
21 | url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/stdlib/REPL/src/latex_symbols.jl' | |
22 | r = requests.get(url) |
|
22 | r = requests.get(url) | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | # Build a list of key, value pairs |
|
25 | # Build a list of key, value pairs | |
26 | print('Building a list of (latex, unicode) key-value pairs...') |
|
26 | print('Building a list of (latex, unicode) key-value pairs...') | |
27 | lines = r.text.splitlines() |
|
27 | lines = r.text.splitlines() | |
28 |
|
28 | |||
29 | prefixes_line = lines.index('# "font" prefixes') |
|
29 | prefixes_line = lines.index('# "font" prefixes') | |
30 | symbols_line = lines.index('# manual additions:') |
|
30 | symbols_line = lines.index('# manual additions:') | |
31 |
|
31 | |||
32 | prefix_dict = {} |
|
32 | prefix_dict = {} | |
33 | for l in lines[prefixes_line + 1: symbols_line]: |
|
33 | for l in lines[prefixes_line + 1: symbols_line]: | |
34 | p = l.split() |
|
34 | p = l.split() | |
35 | if not p or p[1] == 'latex_symbols': continue |
|
35 | if not p or p[1] == 'latex_symbols': continue | |
36 | prefix_dict[p[1]] = p[3] |
|
36 | prefix_dict[p[1]] = p[3] | |
37 |
|
37 | |||
38 | idents = [] |
|
38 | idents = [] | |
39 | for l in lines[symbols_line:]: |
|
39 | for l in lines[symbols_line:]: | |
40 | if not '=>' in l: continue #Β if it's not a def, skip |
|
40 | if not '=>' in l: continue #Β if it's not a def, skip | |
41 | if '#' in l: l = l[:l.index('#')] #Β get rid of eol comments |
|
41 | if '#' in l: l = l[:l.index('#')] #Β get rid of eol comments | |
42 | x, y = l.strip().split('=>') |
|
42 | x, y = l.strip().split('=>') | |
43 | if '*' in x: #Β if a prefix is present substitute it with its value |
|
43 | if '*' in x: #Β if a prefix is present substitute it with its value | |
44 | p, x = x.split('*') |
|
44 | p, x = x.split('*') | |
45 | x = prefix_dict[p][:-1] + x[1:] |
|
45 | x = prefix_dict[p][:-1] + x[1:] | |
46 | x, y = x.split('"')[1], y.split('"')[1] #Β get the values in quotes |
|
46 | x, y = x.split('"')[1], y.split('"')[1] #Β get the values in quotes | |
47 | idents.append((x, y)) |
|
47 | idents.append((x, y)) | |
48 |
|
48 | |||
49 | # Filter out non-valid identifiers |
|
49 | # Filter out non-valid identifiers | |
50 | print('Filtering out characters that are not valid Python 3 identifiers') |
|
50 | print('Filtering out characters that are not valid Python 3 identifiers') | |
51 |
|
51 | |||
52 | def test_ident(i): |
|
52 | def test_ident(i): | |
53 | """Is the unicode string valid in a Python 3 identifier.""" |
|
53 | """Is the unicode string valid in a Python 3 identifier.""" | |
54 | # Some characters are not valid at the start of a name, but we still want to |
|
54 | # Some characters are not valid at the start of a name, but we still want to | |
55 | # include them. So prefix with 'a', which is valid at the start. |
|
55 | # include them. So prefix with 'a', which is valid at the start. | |
56 | return ('a' + i).isidentifier() |
|
56 | return ('a' + i).isidentifier() | |
57 |
|
57 | |||
58 | assert test_ident("Ξ±") |
|
58 | assert test_ident("Ξ±") | |
59 | assert not test_ident('β΄') |
|
59 | assert not test_ident('β΄') | |
60 |
|
60 | |||
61 | valid_idents = [line for line in idents if test_ident(line[1])] |
|
61 | valid_idents = [line for line in idents if test_ident(line[1])] | |
62 |
|
62 | |||
63 | # Write the `latex_symbols.py` module in the cwd |
|
63 | # Write the `latex_symbols.py` module in the cwd | |
64 |
|
64 | |||
65 | s = """# encoding: utf-8 |
|
65 | s = """# encoding: utf-8 | |
66 |
|
66 | |||
67 | # DO NOT EDIT THIS FILE BY HAND. |
|
67 | # DO NOT EDIT THIS FILE BY HAND. | |
68 |
|
68 | |||
69 | # To update this file, run the script /tools/gen_latex_symbols.py using Python 3 |
|
69 | # To update this file, run the script /tools/gen_latex_symbols.py using Python 3 | |
70 |
|
70 | |||
71 | # This file is autogenerated from the file: |
|
71 | # This file is autogenerated from the file: | |
72 | # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl |
|
72 | # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl | |
73 | # This original list is filtered to remove any unicode characters that are not valid |
|
73 | # This original list is filtered to remove any unicode characters that are not valid | |
74 | # Python identifiers. |
|
74 | # Python identifiers. | |
75 |
|
75 | |||
76 | latex_symbols = {\n |
|
76 | latex_symbols = {\n | |
77 | """ |
|
77 | """ | |
78 | for line in valid_idents: |
|
78 | for line in valid_idents: | |
79 | s += ' "%s" : "%s",\n' % (line[0], line[1]) |
|
79 | s += ' "%s" : "%s",\n' % (line[0], line[1]) | |
80 | s += "}\n" |
|
80 | s += "}\n" | |
81 |
|
81 | |||
82 | s += """ |
|
82 | s += """ | |
83 |
|
83 | |||
84 | reverse_latex_symbol = { v:k for k,v in latex_symbols.items()} |
|
84 | reverse_latex_symbol = { v:k for k,v in latex_symbols.items()} | |
85 | """ |
|
85 | """ | |
86 |
|
86 | |||
87 | fn = os.path.join('..','IPython','core','latex_symbols.py') |
|
87 | fn = os.path.join('..','IPython','core','latex_symbols.py') | |
88 | print("Writing the file: %s" % fn) |
|
88 | print("Writing the file: %s" % fn) | |
89 | with open(fn, 'w', encoding='utf-8') as f: |
|
89 | with open(fn, 'w', encoding='utf-8') as f: | |
90 | f.write(s) |
|
90 | f.write(s) | |
91 |
|
91 | |||
92 |
|
92 |
General Comments 0
You need to be logged in to leave comments.
Login now