Show More
@@ -1,87 +1,84 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-vaule 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 |
|
49 | """Is the unicode string valid in a Python 3 identifer.""" | |
50 | try: |
|
50 | # Some characters are not valid at the start of a name, but we still want to | |
51 | exec('a%s = 10' % i, {}, {}) |
|
51 | # include them. So prefix with 'a', which is valid at the start. | |
52 | except SyntaxError: |
|
52 | return ('a' + i).isidentifier() | |
53 | return False |
|
|||
54 | else: |
|
|||
55 | return True |
|
|||
56 |
|
53 | |||
57 | assert test_ident("Ξ±") |
|
54 | assert test_ident("Ξ±") | |
58 | assert not test_ident('β΄') |
|
55 | assert not test_ident('β΄') | |
59 |
|
56 | |||
60 | 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])] | |
61 |
|
58 | |||
62 |
|
59 | |||
63 | # Write the `latex_symbols.py` module in the cwd |
|
60 | # Write the `latex_symbols.py` module in the cwd | |
64 |
|
61 | |||
65 | s = """# encoding: utf-8 |
|
62 | s = """# encoding: utf-8 | |
66 |
|
63 | |||
67 | # DO NOT EDIT THIS FILE BY HAND. |
|
64 | # DO NOT EDIT THIS FILE BY HAND. | |
68 |
|
65 | |||
69 | # 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 | |
70 |
|
67 | |||
71 | # This file is autogenerated from the file: |
|
68 | # This file is autogenerated from the file: | |
72 | # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl |
|
69 | # 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 |
|
70 | # This original list is filtered to remove any unicode characters that are not valid | |
74 | # Python identifiers. |
|
71 | # Python identifiers. | |
75 |
|
72 | |||
76 | latex_symbols = {\n |
|
73 | latex_symbols = {\n | |
77 | """ |
|
74 | """ | |
78 | for line in valid_idents: |
|
75 | for line in valid_idents: | |
79 | s += ' "%s" : "%s",\n' % (line[0], line[1]) |
|
76 | s += ' "%s" : "%s",\n' % (line[0], line[1]) | |
80 | s += "}\n" |
|
77 | s += "}\n" | |
81 |
|
78 | |||
82 | fn = os.path.join('..','IPython','core','latex_symbols.py') |
|
79 | fn = os.path.join('..','IPython','core','latex_symbols.py') | |
83 | print("Writing the file: %s" % fn) |
|
80 | print("Writing the file: %s" % fn) | |
84 | with open(fn, 'w', encoding='utf-8') as f: |
|
81 | with open(fn, 'w', encoding='utf-8') as f: | |
85 | f.write(s) |
|
82 | f.write(s) | |
86 |
|
83 | |||
87 |
|
84 |
General Comments 0
You need to be logged in to leave comments.
Login now