Show More
@@ -1,87 +1,87 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 | from pathlib import Path |
|
12 | from pathlib import Path | |
13 |
|
13 | |||
14 | # Import the Julia LaTeX symbols |
|
14 | # Import the Julia LaTeX symbols | |
15 | print('Importing latex_symbols.js from Julia...') |
|
15 | print('Importing latex_symbols.js from Julia...') | |
16 | import requests |
|
16 | import requests | |
17 | url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/stdlib/REPL/src/latex_symbols.jl' |
|
17 | url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/stdlib/REPL/src/latex_symbols.jl' | |
18 | r = requests.get(url) |
|
18 | r = requests.get(url) | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | # Build a list of key, value pairs |
|
21 | # Build a list of key, value pairs | |
22 | print('Building a list of (latex, unicode) key-value pairs...') |
|
22 | print('Building a list of (latex, unicode) key-value pairs...') | |
23 | lines = r.text.splitlines() |
|
23 | lines = r.text.splitlines() | |
24 |
|
24 | |||
25 | prefixes_line = lines.index('# "font" prefixes') |
|
25 | prefixes_line = lines.index('# "font" prefixes') | |
26 | symbols_line = lines.index('# manual additions:') |
|
26 | symbols_line = lines.index('# manual additions:') | |
27 |
|
27 | |||
28 | prefix_dict = {} |
|
28 | prefix_dict = {} | |
29 | for l in lines[prefixes_line + 1: symbols_line]: |
|
29 | for l in lines[prefixes_line + 1: symbols_line]: | |
30 | p = l.split() |
|
30 | p = l.split() | |
31 | if not p or p[1] == 'latex_symbols': continue |
|
31 | if not p or p[1] == 'latex_symbols': continue | |
32 | prefix_dict[p[1]] = p[3] |
|
32 | prefix_dict[p[1]] = p[3] | |
33 |
|
33 | |||
34 | idents = [] |
|
34 | idents = [] | |
35 | for l in lines[symbols_line:]: |
|
35 | for l in lines[symbols_line:]: | |
36 | if not '=>' in l: continue # if it's not a def, skip |
|
36 | if not '=>' in l: continue # if it's not a def, skip | |
37 | if '#' in l: l = l[:l.index('#')] # get rid of eol comments |
|
37 | if '#' in l: l = l[:l.index('#')] # get rid of eol comments | |
38 | x, y = l.strip().split('=>') |
|
38 | x, y = l.strip().split('=>') | |
39 | if '*' in x: # if a prefix is present substitute it with its value |
|
39 | if '*' in x: # if a prefix is present substitute it with its value | |
40 | p, x = x.split('*') |
|
40 | p, x = x.split('*') | |
41 | x = prefix_dict[p][:-1] + x[1:] |
|
41 | x = prefix_dict[p][:-1] + x[1:] | |
42 | x, y = x.split('"')[1], y.split('"')[1] # get the values in quotes |
|
42 | x, y = x.split('"')[1], y.split('"')[1] # get the values in quotes | |
43 | idents.append((x, y)) |
|
43 | idents.append((x, y)) | |
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 identifier.""" |
|
49 | """Is the unicode string valid in a Python 3 identifier.""" | |
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 idents if test_ident(line[1])] |
|
57 | valid_idents = [line for line in idents if test_ident(line[1])] | |
58 |
|
58 | |||
59 | # Write the `latex_symbols.py` module in the cwd |
|
59 | # Write the `latex_symbols.py` module in the cwd | |
60 |
|
60 | |||
61 | s = """# encoding: utf-8 |
|
61 | s = f"""# encoding: utf-8 | |
62 |
|
62 | |||
63 | # DO NOT EDIT THIS FILE BY HAND. |
|
63 | # DO NOT EDIT THIS FILE BY HAND. | |
64 |
|
64 | |||
65 | # To update this file, run the script /tools/gen_latex_symbols.py using Python 3 |
|
65 | # To update this file, run the script /tools/gen_latex_symbols.py using Python 3 | |
66 |
|
66 | |||
67 | # This file is autogenerated from the file: |
|
67 | # This file is autogenerated from the file: | |
68 | # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl |
|
68 | # {url} | |
69 | # This original list is filtered to remove any unicode characters that are not valid |
|
69 | # This original list is filtered to remove any unicode characters that are not valid | |
70 | # Python identifiers. |
|
70 | # Python identifiers. | |
71 |
|
71 | |||
72 | latex_symbols = {\n |
|
72 | latex_symbols = {{\n | |
73 | """ |
|
73 | """ | |
74 | for line in valid_idents: |
|
74 | for line in valid_idents: | |
75 | s += ' "%s" : "%s",\n' % (line[0], line[1]) |
|
75 | s += ' "%s" : "%s",\n' % (line[0], line[1]) | |
76 | s += "}\n" |
|
76 | s += "}\n" | |
77 |
|
77 | |||
78 | s += """ |
|
78 | s += """ | |
79 |
|
79 | |||
80 | reverse_latex_symbol = { v:k for k,v in latex_symbols.items()} |
|
80 | reverse_latex_symbol = { v:k for k,v in latex_symbols.items()} | |
81 | """ |
|
81 | """ | |
82 |
|
82 | |||
83 | fn = Path("..", "IPython", "core", "latex_symbols.py") |
|
83 | fn = Path("..", "IPython", "core", "latex_symbols.py") | |
84 | print("Writing the file: %s" % str(fn)) |
|
84 | print("Writing the file: %s" % str(fn)) | |
85 | fn.write_text(s, encoding="utf-8") |
|
85 | fn.write_text(s, encoding="utf-8") | |
86 |
|
86 | |||
87 |
|
87 |
General Comments 0
You need to be logged in to leave comments.
Login now