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