Show More
@@ -0,0 +1,76 b'' | |||||
|
1 | # coding: utf-8 | |||
|
2 | ||||
|
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, | |||
|
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 | |||
|
7 | # Python 3 are included. | |||
|
8 | ||||
|
9 | # | |||
|
10 | # The original mapping of latex symbols to unicode comes from the `latex_symbols.jl` files from Julia. | |||
|
11 | ||||
|
12 | from __future__ import print_function | |||
|
13 | ||||
|
14 | # Import the Julia LaTeX symbols | |||
|
15 | print('Importing latex_symbols.js from Julia...') | |||
|
16 | import requests | |||
|
17 | url = 'https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl' | |||
|
18 | r = requests.get(url) | |||
|
19 | ||||
|
20 | ||||
|
21 | # Build a list of key, value pairs | |||
|
22 | print('Building a list of (latex, unicode) key-vaule pairs...') | |||
|
23 | lines = r.text.splitlines()[60:] | |||
|
24 | lines = [line for line in lines if '=>' in line] | |||
|
25 | lines = [line.replace('=>',':') for line in lines] | |||
|
26 | ||||
|
27 | def line_to_tuple(line): | |||
|
28 | """Convert a single line of the .jl file to a 2-tuple of strings like ("\\alpha", "α")""" | |||
|
29 | kv = line.split(',')[0].split(':') | |||
|
30 | # kv = tuple(line.strip(', ').split(':')) | |||
|
31 | k, v = kv[0].strip(' "'), kv[1].strip(' "') | |||
|
32 | # if not test_ident(v): | |||
|
33 | # print(line) | |||
|
34 | return k, v | |||
|
35 | ||||
|
36 | assert line_to_tuple(' "\\sqrt" : "\u221A",') == ('\\sqrt', '\u221A') | |||
|
37 | lines = [line_to_tuple(line) for line in lines] | |||
|
38 | ||||
|
39 | ||||
|
40 | # Filter out non-valid identifiers | |||
|
41 | print('Filtering out characters that are not valid Python 3 identifiers') | |||
|
42 | ||||
|
43 | def test_ident(i): | |||
|
44 | """Is the unicode string a valid Python 3 identifer.""" | |||
|
45 | try: | |||
|
46 | exec('a%s = 10' % i, {}, {}) | |||
|
47 | except SyntaxError: | |||
|
48 | return False | |||
|
49 | else: | |||
|
50 | return True | |||
|
51 | ||||
|
52 | assert test_ident("α") | |||
|
53 | assert not test_ident('‴') | |||
|
54 | ||||
|
55 | valid_idents = [line for line in lines if test_ident(line[1])] | |||
|
56 | ||||
|
57 | ||||
|
58 | # Write the `latex_symbols.py` module in the cwd | |||
|
59 | ||||
|
60 | s = """# encoding: utf-8 | |||
|
61 | ||||
|
62 | # This file is autogenerated from the file: | |||
|
63 | # https://raw.githubusercontent.com/JuliaLang/julia/master/base/latex_symbols.jl | |||
|
64 | # This original list is filtered to remove any unicode characters that are not valid | |||
|
65 | # Python identifiers. | |||
|
66 | ||||
|
67 | latex_symbols = {\n | |||
|
68 | """ | |||
|
69 | for line in valid_idents: | |||
|
70 | s += ' "%s" : "%s",\n' % (line[0], line[1]) | |||
|
71 | s += "}\n" | |||
|
72 | ||||
|
73 | with open('latex_symbols.py', 'w', encoding='utf-8') as f: | |||
|
74 | f.write(s) | |||
|
75 | ||||
|
76 |
General Comments 0
You need to be logged in to leave comments.
Login now